コード例 #1
0
def test_tocsv_pipe():

    t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'),
         ('banana', 'London', '42'), ('orange', 'Paris', '31'),
         ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')]

    f1 = NamedTemporaryFile(delete=False)
    f2 = NamedTemporaryFile(delete=False)
    p = tocsv(f1.name)
    p.pipe(tocsv(f2.name))
    p.push(t)

    ieq(t, fromcsv(f1.name))
    ieq(t, fromcsv(f2.name))
コード例 #2
0
ファイル: test_push.py プロジェクト: greeness/petl
def test_tocsv_pipe():

    t = [('fruit', 'city', 'sales'),
         ('orange', 'London', '12'),
         ('banana', 'London', '42'),
         ('orange', 'Paris', '31'),
         ('banana', 'Amsterdam', '74'),
         ('kiwi', 'Berlin', '55')]

    f1 = NamedTemporaryFile(delete=False)
    f2 = NamedTemporaryFile(delete=False)
    p = tocsv(f1.name)
    p.pipe(tocsv(f2.name))
    p.push(t)

    iassertequal(t, fromcsv(f1.name))
    iassertequal(t, fromcsv(f2.name))
コード例 #3
0
def test_tocsv_pipe():

    t = [
        ("fruit", "city", "sales"),
        ("orange", "London", "12"),
        ("banana", "London", "42"),
        ("orange", "Paris", "31"),
        ("banana", "Amsterdam", "74"),
        ("kiwi", "Berlin", "55"),
    ]

    f1 = NamedTemporaryFile(delete=False)
    f2 = NamedTemporaryFile(delete=False)
    p = tocsv(f1.name)
    p.pipe(tocsv(f2.name))
    p.push(t)

    ieq(t, fromcsv(f1.name))
    ieq(t, fromcsv(f2.name))
コード例 #4
0
ファイル: test_push.py プロジェクト: greeness/petl
def test_tocsv():

    t = [('fruit', 'city', 'sales'), ('orange', 'London', '12'),
         ('banana', 'London', '42'), ('orange', 'Paris', '31'),
         ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')]

    f = NamedTemporaryFile(delete=False)
    p = tocsv(f.name)
    p.push(t)

    iassertequal(t, fromcsv(f.name))
コード例 #5
0
ファイル: test_push.py プロジェクト: greeness/petl
def test_partition():

    t = [('fruit', 'city', 'sales'),
         ('orange', 'London', 12),
         ('banana', 'London', 42),
         ('orange', 'Paris', 31),
         ('banana', 'Amsterdam', 74),
         ('kiwi', 'Berlin', 55)]

    p = partition('fruit')
    p.pipe('orange', tocsv('oranges.csv'))
    p.pipe('banana', tocsv('bananas.csv'))
    p.push(t)

    oranges_expected = [('fruit', 'city', 'sales'),
                        ('orange', 'London', '12'),
                        ('orange', 'Paris', '31')]

    bananas_expected = [('fruit', 'city', 'sales'),
                        ('banana', 'London', '42'),
                        ('banana', 'Amsterdam', '74')]

    oranges_actual = fromcsv('oranges.csv')
    bananas_actual = fromcsv('bananas.csv')
    iassertequal(oranges_expected, oranges_actual)
    iassertequal(bananas_expected, bananas_actual)

    # alternative syntax

    p = partition('fruit')
    p | ('orange', tocsv('oranges.csv'))
    p | ('banana', tocsv('bananas.csv'))
    p.push(t)
    iassertequal(oranges_expected, oranges_actual)
    iassertequal(bananas_expected, bananas_actual)
    
    # test with callable discriminator

    p = partition(lambda row: row['sales'] > 40)
    p | (True, tocsv('high.csv'))
    p | (False, tocsv('low.csv'))
    p.push(t)

    high_expected = [('fruit', 'city', 'sales'),
                     ('banana', 'London', '42'),
                     ('banana', 'Amsterdam', '74'),
                     ('kiwi', 'Berlin', '55')]

    low_expected = [('fruit', 'city', 'sales'),
                    ('orange', 'London', '12'),
                    ('orange', 'Paris', '31')]

    high_actual = fromcsv('high.csv')
    low_actual = fromcsv('low.csv')
    iassertequal(high_expected, high_actual)
    iassertequal(low_expected, low_actual)
コード例 #6
0
def test_partition():

    t = [
        ("fruit", "city", "sales"),
        ("orange", "London", 12),
        ("banana", "London", 42),
        ("orange", "Paris", 31),
        ("banana", "Amsterdam", 74),
        ("kiwi", "Berlin", 55),
    ]

    p = partition("fruit")
    p.pipe("orange", tocsv("oranges.csv"))
    p.pipe("banana", tocsv("bananas.csv"))
    p.push(t)

    oranges_expected = [("fruit", "city", "sales"), ("orange", "London", "12"), ("orange", "Paris", "31")]

    bananas_expected = [("fruit", "city", "sales"), ("banana", "London", "42"), ("banana", "Amsterdam", "74")]

    oranges_actual = fromcsv("oranges.csv")
    bananas_actual = fromcsv("bananas.csv")
    ieq(oranges_expected, oranges_actual)
    ieq(bananas_expected, bananas_actual)

    # alternative syntax

    p = partition("fruit")
    p | ("orange", tocsv("oranges.csv"))
    p | ("banana", tocsv("bananas.csv"))
    p.push(t)
    ieq(oranges_expected, oranges_actual)
    ieq(bananas_expected, bananas_actual)

    # test with callable discriminator

    p = partition(lambda row: row["sales"] > 40)
    p | (True, tocsv("high.csv"))
    p | (False, tocsv("low.csv"))
    p.push(t)

    high_expected = [
        ("fruit", "city", "sales"),
        ("banana", "London", "42"),
        ("banana", "Amsterdam", "74"),
        ("kiwi", "Berlin", "55"),
    ]

    low_expected = [("fruit", "city", "sales"), ("orange", "London", "12"), ("orange", "Paris", "31")]

    high_actual = fromcsv("high.csv")
    low_actual = fromcsv("low.csv")
    ieq(high_expected, high_actual)
    ieq(low_expected, low_actual)
コード例 #7
0
def test_partition():

    t = [('fruit', 'city', 'sales'), ('orange', 'London', 12),
         ('banana', 'London', 42), ('orange', 'Paris', 31),
         ('banana', 'Amsterdam', 74), ('kiwi', 'Berlin', 55)]

    p = partition('fruit')
    p.pipe('orange', tocsv('tmp/oranges.csv'))
    p.pipe('banana', tocsv('tmp/bananas.csv'))
    p.push(t)

    oranges_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'),
                        ('orange', 'Paris', '31')]

    bananas_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'),
                        ('banana', 'Amsterdam', '74')]

    oranges_actual = fromcsv('tmp/oranges.csv')
    bananas_actual = fromcsv('tmp/bananas.csv')
    ieq(oranges_expected, oranges_actual)
    ieq(bananas_expected, bananas_actual)

    # alternative syntax

    p = partition('fruit')
    p | ('orange', tocsv('tmp/oranges.csv'))
    p | ('banana', tocsv('tmp/bananas.csv'))
    p.push(t)
    ieq(oranges_expected, oranges_actual)
    ieq(bananas_expected, bananas_actual)

    # test with callable discriminator

    p = partition(lambda row: row['sales'] > 40)
    p | (True, tocsv('tmp/high.csv'))
    p | (False, tocsv('tmp/low.csv'))
    p.push(t)

    high_expected = [('fruit', 'city', 'sales'), ('banana', 'London', '42'),
                     ('banana', 'Amsterdam', '74'), ('kiwi', 'Berlin', '55')]

    low_expected = [('fruit', 'city', 'sales'), ('orange', 'London', '12'),
                    ('orange', 'Paris', '31')]

    high_actual = fromcsv('tmp/high.csv')
    low_actual = fromcsv('tmp/low.csv')
    ieq(high_expected, high_actual)
    ieq(low_expected, low_actual)