Beispiel #1
0
def paginate_characters_table(csv_path, page):
    characters_table, headers, total_characters = get_characters_table(
        csv_path)
    data = etl.data(
        etl.rowslice(characters_table, 10 * (page - 1), 10 + 10 * (page - 1)))

    return headers, data, total_characters
Beispiel #2
0
def load_more(filepath, page, total_items):
    length = 10
    offset = (page - 1) * length

    table = etl.fromcsv(filepath)
    rows = etl.rowslice(table, offset, offset + length)
    next_page = page + 1 if offset + length < total_items else None
    return etl.header(table), list(rows.dicts()), next_page
Beispiel #3
0
def _display(tbl, sliceargs, **kwargs):
    try:
        from IPython.core.display import display_html
    except ImportError as e:
        raise UnsatisfiedDependency(e, dep_message)
    if sliceargs is not None:
        tbl = rowslice(tbl, *sliceargs)
    html = repr_html(tbl, **kwargs)
    display_html(html, raw=True)
Beispiel #4
0
def create_config(csvfile,config_name):
    '''
        Creates a configuration file from a CSV file
    '''
    print csvfile
    var = ''
    try: 
        open(config_name+".ini")
        var = raw_input("This file already exists. Do you wish to continue? (Yes/No) ")
    except:
        pass
        

    if var == 'Yes':
        cfgfile = open(config_name+".ini", "w")
        examplefile = open(config_name+".example", "w")
    else:
        print "goodbye"
        sys.exit()
        

    c = fromcsv(csvfile)
    columns = header(c)
    it = iterdata(c)
    print it.next()
    examplefile.write(str(see(rowslice(c,2,3))))
    examplefile.close()


    # add the settings to the structure of the file, and lets write it out...
    Config = ConfigParser.ConfigParser()
    # dont' change  names to lower case
    Config.optionxform = str
    Config.add_section('FieldTypes')
    Config.add_section('FieldMap')
    for name in columns:
        #Config.set('FieldTypes',c)
        #print name
        new = name
        new = new.split("(", 1)[0].strip()
        # Connect words with underscore
        new = new.replace("/","_")
        new = new.replace(" ","_")
        new = new.replace("-","_")
        new = new.lower()
        # try to guess some of the names
        if "amount" in name: 
            print name
            Config.set('FieldMap',name, new + " FLOAT")
        else:
            print name
            Config.set('FieldMap',name, new + " VARCHAR(10)")
        
    Config.write(cfgfile)
    cfgfile.close()
Beispiel #5
0
    def chunk(self, rows):
        """
        Divides a Parsons table into smaller tables of a specified row count. If the table
        cannot be divided evenly, then the final table will only include the remainder.

        `Args:`
            rows: int
                The number of rows of each new Parsons table
        `Returns:`
            List of Parsons tables
        """

        from parsons.etl import Table
        return [Table(petl.rowslice(self.table, i, i+rows)) for i in range(0, self.num_rows, rows)]
Beispiel #6
0
    def __getitem__(self, index):

        if isinstance(index, int):

            return self.row_data(index)

        elif isinstance(index, str):

            return self.column_data(index)

        elif isinstance(index, slice):
            tblslice = petl.rowslice(self.table, index.start, index.stop,
                                     index.step)
            return [row for row in tblslice]

        else:

            raise TypeError('You must pass a string or an index as a value.')
Beispiel #7
0
table3 = addfield(table1, 'baz', expr('{bar} * 2'))
look(table3)
    

# rowslice

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
          ['d', 7],
          ['f', 42]]

from petl import rowslice, look
look(table1)
table2 = rowslice(table1, 2)
look(table2)
table3 = rowslice(table1, 1, 4)
look(table3)
table4 = rowslice(table1, 0, 5, 2)
look(table4)


# head

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
          ['d', 7],
          ['f', 42],
Beispiel #8
0
table3 = addfield(table1, 'baz', expr('{bar} * 2'))
look(table3)
    

# rowslice

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
          ['d', 7],
          ['f', 42]]

from petl import rowslice, look
look(table1)
table2 = rowslice(table1, 2)
look(table2)
table3 = rowslice(table1, 1, 4)
look(table3)
table4 = rowslice(table1, 0, 5, 2)
look(table4)


# head

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
          ['d', 7],
          ['f', 42],
Beispiel #9
0
        mins, maxs = etl.limits(fact, 'sales')
        print( "Minimum Sales:",mins)
        print("Maximum Sales:",maxs)
        
        #OLAP ---> PIVOT
        table1 = etl.pivot(product, 'category', 'subcategory','quantity', sum)
        print("PIVOT:")
        print(table1)
        
        
        #OLAP OPERATIONS ---> ROLL UP
        table2 = etl.aggregate(customer, 'state', len)
        table3 = etl.aggregate(customer, 'city', len) 
        print("ROLL UP:")
        print(table2)
        print(table3)
        
        #OLAP OPERATIONS ---> SLICING
        print("SLICING:")
        table4= etl.rowslice(table3,3)
        print(table4)
        
        
except Error as e :
    print ("Error while connecting to MySQL", e)
finally:
#closing database connection.
    if(connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Beispiel #10
0
# calculating the value
table2 = etl.addfield(table1, 'baz', lambda rec: rec['bar'] * 2)
table2


# rowslice()
############

import petl as etl
table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
          ['d', 7],
          ['f', 42]]
table2 = etl.rowslice(table1, 2)
table2
table3 = etl.rowslice(table1, 1, 4)
table3
table4 = etl.rowslice(table1, 0, 5, 2)
table4


# head()
########

import petl as etl
table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 5],
# coding:utf8

import petl as etl

table1 = [('foo', 'bar', 'baz'), ('apple', 1, 2.5), ('orange', 3, 4.5),
          ('pears', 5, 6.5), ('bananer', 7, 8.5), ('cat', 9, 10.5)]
# head 4
table_head = etl.head(table1, 4)
print(table_head)

# tail 4
table_tail = etl.tail(table1, 4)
print(table_tail)

# rowslice
rowsliceTb = etl.rowslice(table1, 2)
print(rowsliceTb)

rowsliceTb_2_4 = etl.rowslice(table1, 2, 4)
print(rowsliceTb_2_4)

# 从1开始,2作为第一个,步长为2,
rowsliceTb_1_2_5 = etl.rowslice(table1, 1, 5, 2)
print(rowsliceTb_1_2_5)

# cut
cutTb = etl.cut(table1, 'foo', 'bar')
print(cutTb)

# index starts from 0
cutTb_0_2 = etl.cut(table1, 0, 2)