Skip to content Skip to sidebar Skip to footer

A Dsl In Python For Tabular Analysis And Processing?

I am carrying around the idea to create a restricted form of a language for tabular analysis. Thequestion is if there is something like the following already around or if there are

Solution 1:

littletable is a module that I wrote to do some of this tabular analysis of a list of like items. littletable does not use SQL for selects and queries, but fields can be indexed, and tables can do joins, pivots, and queries. Tables can be treated just like Python lists. Probably the biggest philosophical point about littletable is that every join, query, etc. returns a new Table, so that a complex expression can be built up from intermediate joins and queries. Here are some samples of manipulating your data with littletable:

attrs = "Id,First,Last,Department,Hourly_Wage".split(',') 
data = [ (1, 'Greg','Foo','HR',100), 
          (2, 'Judy','Bar','EE',51), 
          (3, 'Jake','Bar','HR',75), 
          (4, 'Lila','Bax','HR',49), 
          (5, 'Norm','Fob','EE',49) ] 

from littletable import Table, DataObject

instructors = Table()
instructors.insert_many(
    DataObject(**dict(zip(attrs,d))) 
        for d in data)

# can add index before or after items are added to table
instructors.create_index("Id", unique=True)
instructors.create_index("Department")

# unique keys are enforcedtry:
    instructors.insert(DataObject(Id=4, First="Bob", Last="Fob"))
except KeyError as e:
    print e

# keys checked for uniqueness when creating unique indextry:
    instructors.create_index("Last", unique=True)
except KeyError as e:
    print e

# Uniquely indexed access gives a single recordprint"%(First)s %(Last)s" % instructors.by.Id[3]

# Non-uniquely indexed access gives a new Tableprint'\n'.join("%(Department)s %(First)s %(Last)s" % inst 
                    for inst in instructors.by.Department["HR"])

# Table can still be accessed like a Python listprint"%(First)s %(Last)s" % instructors[-1]
print'\n'.join("%(Department)s %(First)s %(Last)s" % inst 
                    for inst in instructors)

# use pivot for multi-dimensional grouping
instructors.addfield("wage_bracket", lambda d:d.Hourly_Wage/10*10)
instructors.create_index("wage_bracket")
instructors.pivot("Department wage_bracket").dump()
instructors.pivot("Department wage_bracket").dump_counts()

import sys
instructors.csv_export(sys.stdout)

prints:

("duplicate unique key value '4' for index Id", {'Last': 'Fob', 'Id': 4, 'First': 'Bob'})
'duplicate key value Bar'
Jake Bar
HR Greg Foo
HR Jake Bar
HR Lila Bax
Norm Fob
HR Greg Foo
EE Judy Bar
HR Jake Bar
HR Lila Bax
EE Norm Fob
Pivot: Department,wage_bracket
  Department:EE
    Department:EE/wage_bracket:40
      {'Last': 'Fob', 'Hourly_Wage': 49, 'Department': 'EE', 'wage_bracket': 40, 'Id': 5, 'First': 'Norm'}
    Department:EE/wage_bracket:50
      {'Last': 'Bar', 'Hourly_Wage': 51, 'Department': 'EE', 'wage_bracket': 50, 'Id': 2, 'First': 'Judy'}
  Department:HR
    Department:HR/wage_bracket:40
      {'Last': 'Bax', 'Hourly_Wage': 49, 'Department': 'HR', 'wage_bracket': 40, 'Id': 4, 'First': 'Lila'}
    Department:HR/wage_bracket:70
      {'Last': 'Bar', 'Hourly_Wage': 75, 'Department': 'HR', 'wage_bracket': 70, 'Id': 3, 'First': 'Jake'}
    Department:HR/wage_bracket:100
      {'Last': 'Foo', 'Hourly_Wage': 100, 'Department': 'HR', 'wage_bracket': 100, 'Id': 1, 'First': 'Greg'}
Pivot: Department,wage_bracket
              40         50         70        100      Total
EE             1          1          0          0          2
HR             1          0          1          1          3
Total          2          1          1          1          5
Last,Hourly_Wage,Department,wage_bracket,Id,First
Foo,100,HR,100,1,Greg
Bar,51,EE,50,2,Judy
Bar,75,HR,70,3,Jake
Bax,49,HR,40,4,Lila
Fob,49,EE,40,5,Norm

Post a Comment for "A Dsl In Python For Tabular Analysis And Processing?"