Exemplo n.º 1
0
    def testHtmlOutputFormated(self):
        t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
        t.add_row(['value 1', 'value2', 'value3'])
        t.add_row(['value 4', 'value5', 'value6'])
        t.add_row(['value 7', 'value8', 'value9'])
        result = t.get_html_string(format=True)
        assert result.strip() == """
<table frame="box" rules="cols">
    <tr>
        <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
        <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
        <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
    </tr>
    <tr>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 1</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value2</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value3</td>
    </tr>
    <tr>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 4</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value5</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value6</td>
    </tr>
    <tr>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 7</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value8</td>
        <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value9</td>
    </tr>
</table>
""".strip()
Exemplo n.º 2
0
def from_db_cursor(cursor, **kwargs):
    if cursor.description:
        table = PrettyTable(**kwargs)
        table.field_names = [col[0] for col in cursor.description]
        for row in cursor.fetchall():
            table.add_row(row)
        return table
Exemplo n.º 3
0
    def testHtmlOutput(self):
        t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
        t.add_row(['value 1', 'value2', 'value3'])
        t.add_row(['value 4', 'value5', 'value6'])
        t.add_row(['value 7', 'value8', 'value9'])
        result = t.get_html_string()
        assert result.strip() == """
<table>
    <tr>
        <th>Field 1</th>
        <th>Field 2</th>
        <th>Field 3</th>
    </tr>
    <tr>
        <td>value 1</td>
        <td>value2</td>
        <td>value3</td>
    </tr>
    <tr>
        <td>value 4</td>
        <td>value5</td>
        <td>value6</td>
    </tr>
    <tr>
        <td>value 7</td>
        <td>value8</td>
        <td>value9</td>
    </tr>
</table>
""".strip()
Exemplo n.º 4
0
def from_csv(fp, field_names=None, **kwargs):
    fmtparams = {}
    for param in [
            "delimiter", "doublequote", "escapechar", "lineterminator",
            "quotechar", "quoting", "skipinitialspace", "strict"
    ]:
        if param in kwargs:
            fmtparams[param] = kwargs.pop(param)
    if fmtparams:
        reader = csv.reader(fp, **fmtparams)
    else:
        dialect = csv.Sniffer().sniff(fp.read(1024))
        fp.seek(0)
        reader = csv.reader(fp, dialect)

    table = PrettyTable(**kwargs)
    if field_names:
        table.field_names = field_names
    else:
        table.field_names = [x.strip() for x in next(reader)]

    for row in reader:
        table.add_row([x.strip() for x in row])

    return table
Exemplo n.º 5
0
 def testSortSlice(self):
     """Make sure sorting and slicing interact in the expected way"""
     x = PrettyTable(["Foo"])
     for i in range(20, 0, -1):
         x.add_row([i])
     newstyle = x.get_string(sortby="Foo", end=10)
     assert "10" in newstyle
     assert "20" not in newstyle
     oldstyle = x.get_string(sortby="Foo", end=10, oldsortslice=True)
     assert "10" not in oldstyle
     assert "20" in oldstyle
Exemplo n.º 6
0
def stddetails():
    stdid = int(input('Enter The Student Id: '))
    mycursor.execute("select * from students where STD_ID='{}'".format(stdid))
    results = mycursor.fetchall()
    t = PrettyTable([
        'STD_ID', 'STD_NAME', 'STD_GRADE', 'STD_AGE', 'STD_NUMBER',
        'STD_ADDRESS'
    ])
    for STD_ID, STD_NAME, STD_GRADE, STD_AGE, STD_NUMBER, STD_ADDRESS in results:
        t.add_row(
            [STD_ID, STD_NAME, STD_GRADE, STD_AGE, STD_NUMBER, STD_ADDRESS])
    print(t)
Exemplo n.º 7
0
    def generate_table(self, rows):
        """
        Generates from a list of rows a PrettyTable object.
        """
        table = PrettyTable(**self.kwargs)
        for row in self.rows:
            if len(row[0]) < self.max_row_width:
                appends = self.max_row_width - len(row[0])
                for i in range(1, appends):
                    row[0].append("-")

            if row[1]:
                self.make_fields_unique(row[0])
                table.field_names = row[0]
            else:
                table.add_row(row[0])
        return table
Exemplo n.º 8
0
    def testHtmlBreakLine(self):
        t = PrettyTable(['Field 1', 'Field 2'])
        t.add_row(['value 1', 'value2\nsecond line'])
        t.add_row(['value 3', 'value4'])
        result = t.get_html_string(hrules=ALL)
        assert result.strip() == """
<table>
    <tr>
        <th>Field 1</th>
        <th>Field 2</th>
    </tr>
    <tr>
        <td>value 1</td>
        <td>value2<br>second line</td>
    </tr>
    <tr>
        <td>value 3</td>
        <td>value4</td>
    </tr>
</table>
""".strip()
Exemplo n.º 9
0
class FloatFormatTests(unittest.TestCase):

    def setUp(self):
        self.x = PrettyTable(["Constant", "Value"])
        self.x.add_row(["Pi", pi]) 
        self.x.add_row(["e", e]) 
        self.x.add_row(["sqrt(2)", sqrt(2)]) 

    def testNoDecimals(self):
        self.x.float_format = ".0f"
        self.x.caching = False
        assert "." not in self.x.get_string()

    def testRoundTo5DP(self):
        self.x.float_format = ".5f"
        string = self.x.get_string()
        assert "3.14159" in string
        assert "3.141592" not in string
        assert "2.71828" in string 
        assert "2.718281" not in string 
        assert "2.718282" not in string 
        assert "1.41421" in string
        assert "1.414213" not in string

    def testPadWith2Zeroes(self):
        self.x.float_format = "06.2f"
        string = self.x.get_string()
        assert "003.14" in string
        assert "002.72" in string
        assert "001.41" in string
Exemplo n.º 10
0
class CityDataTest(unittest.TestCase):

    """Just build the Australian capital city data example table."""

    def setUp(self):

        self.x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
        self.x.add_row(["Adelaide",1295, 1158259, 600.5])
        self.x.add_row(["Brisbane",5905, 1857594, 1146.4])
        self.x.add_row(["Darwin", 112, 120900, 1714.7])
        self.x.add_row(["Hobart", 1357, 205556, 619.5])
        self.x.add_row(["Sydney", 2058, 4336374, 1214.8])
        self.x.add_row(["Melbourne", 1566, 3806092, 646.9])
        self.x.add_row(["Perth", 5386, 1554769, 869.4])
Exemplo n.º 11
0
class PrintJapanestTest(unittest.TestCase):

    def setUp(self):

        self.x = PrettyTable(["Kanji", "Hiragana", "English"])
        self.x.add_row(["神戸", "こうべ", "Kobe"])
        self.x.add_row(["京都", "きょうと", "Kyoto"])
        self.x.add_row(["長崎", "ながさき", "Nagasaki"])
        self.x.add_row(["名古屋", "なごや", "Nagoya"])
        self.x.add_row(["大阪", "おおさか", "Osaka"])
        self.x.add_row(["札幌", "さっぽろ", "Sapporo"])
        self.x.add_row(["東京", "とうきょう", "Tokyo"])
        self.x.add_row(["横浜", "よこはま", "Yokohama"])

    def testPrint(self):
        print()
        print(self.x)
Exemplo n.º 12
0
    def testAsciiBreakLine(self):
        t = PrettyTable(['Field 1', 'Field 2'])
        t.add_row(['value 1', 'value2\nsecond line'])
        t.add_row(['value 3', 'value4'])
        result = t.get_string(hrules=ALL)
        assert result.strip() == """
+---------+-------------+
| Field 1 |   Field 2   |
+---------+-------------+
| value 1 |    value2   |
|         | second line |
+---------+-------------+
| value 3 |    value4   |
+---------+-------------+
""".strip()

        t = PrettyTable(['Field 1', 'Field 2'])
        t.add_row(['value 1', 'value2\nsecond line'])
        t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
        result = t.get_string(hrules=ALL)
        assert result.strip() == """
+------------+-------------+
|  Field 1   |   Field 2   |
+------------+-------------+
|  value 1   |    value2   |
|            | second line |
+------------+-------------+
|  value 3   |    value4   |
|            |             |
| other line |             |
|            |    value5   |
+------------+-------------+
""".strip()

        t = PrettyTable(['Field 1', 'Field 2'])
        t.add_row(['value 1', 'value2\nsecond line'])
        t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
        result = t.get_string()
        assert result.strip() == """
+------------+-------------+
|  Field 1   |   Field 2   |
+------------+-------------+
|  value 1   |    value2   |
|            | second line |
|  value 3   |    value4   |
|            |             |
| other line |             |
|            |    value5   |
+------------+-------------+
""".strip()
Exemplo n.º 13
0
class BuildEquivelanceTest(unittest.TestCase):
    """Make sure that building a table row-by-row and column-by-column yield the same results"""

    def setUp(self):

        # Row by row...
        self.row = PrettyTable()
        self.row.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
        self.row.add_row(["Adelaide",1295, 1158259, 600.5])
        self.row.add_row(["Brisbane",5905, 1857594, 1146.4])
        self.row.add_row(["Darwin", 112, 120900, 1714.7])
        self.row.add_row(["Hobart", 1357, 205556, 619.5])
        self.row.add_row(["Sydney", 2058, 4336374, 1214.8])
        self.row.add_row(["Melbourne", 1566, 3806092, 646.9])
        self.row.add_row(["Perth", 5386, 1554769, 869.4])

        # Column by column...
        self.col = PrettyTable()
        self.col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
        self.col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
        self.col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
        self.col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])

        # A mix of both!
        self.mix = PrettyTable()
        self.mix.field_names = ["City name", "Area"]
        self.mix.add_row(["Adelaide",1295])
        self.mix.add_row(["Brisbane",5905])
        self.mix.add_row(["Darwin", 112])
        self.mix.add_row(["Hobart", 1357])
        self.mix.add_row(["Sydney", 2058])
        self.mix.add_row(["Melbourne", 1566])
        self.mix.add_row(["Perth", 5386])
        self.mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
        self.mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])

    def testRowColEquivalenceASCII(self):

        self.assertEqual(self.row.get_string(), self.col.get_string())

    def testRowMixEquivalenceASCII(self):

        self.assertEqual(self.row.get_string(), self.mix.get_string())

    def testRowColEquivalenceHTML(self):

        self.assertEqual(self.row.get_html_string(), self.col.get_html_string())

    def testRowMixEquivalenceHTML(self):

        self.assertEqual(self.row.get_html_string(), self.mix.get_html_string())
Exemplo n.º 14
0
    media = round(
        calculo.calcularMedia(grupo_variaveis["qtd_msg%d" % idx],
                              grupo_variaveis["qtd_plv%d" % idx]), 2)
    grupo_variaveis["qtd_media%d" % idx] = media

##criação da tabela de output 1
zc = PrettyTable()
zc.field_names = [
    "Nome", "Qtd Mensagens", "Qtd Palavras", "Média", "Qtd imagens"
]

for idx, nome in enumerate(dict_nomes, 1):
    zc.add_row([
        nome,
        format(grupo_variaveis["qtd_msg%d" % idx], ",d"),
        format(grupo_variaveis["qtd_plv%d" % idx], ",d"),
        grupo_variaveis["qtd_media%d" % idx],
        format(grupo_variaveis["qtd_img%d" % idx], ",d")
    ])

print(zc)

##criação da tabela de output 2
listas = [[] for i in dict_nomes]

zx = PrettyTable()
zx.field_names = [
    "Msg por Hora", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
    "23"
]