def one_record(self, sql): try: if isinstance(sql, str): sql = execute(sql, self) return next(iter(etl.namedtuples(sql))) except SQLError as e: print(e)
def namedtuples(self, sql): try: if isinstance(sql, str): sql = execute(sql, self) return etl.namedtuples(sql) except SQLError as e: print(e)
def test_namedtuples_unevenrows(): table = (('foo', 'bar'), ('a', 1, True), ('b', )) actual = namedtuples(table) it = iter(actual) o = it.next() eq_('a', o.foo) eq_(1, o.bar) o = it.next() eq_('b', o.foo) eq_(None, o.bar)
def test_namedtuples(): table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = namedtuples(table) it = iter(actual) o = it.next() eq_('a', o.foo) eq_(1, o.bar) o = it.next() eq_('b', o.foo) eq_(2, o.bar)
def test_namedtuples_unevenrows(): table = (('foo', 'bar'), ('a', 1, True), ('b',)) actual = namedtuples(table) it = iter(actual) o = it.next() eq_('a', o.foo) eq_(1, o.bar) o = it.next() eq_('b', o.foo) eq_(None, o.bar)
def test_namedtuples_unevenrows(): table = (("foo", "bar"), ("a", 1, True), ("b",)) actual = namedtuples(table) it = iter(actual) o = it.next() eq_("a", o.foo) eq_(1, o.bar) o = it.next() eq_("b", o.foo) eq_(None, o.bar)
def test_namedtuples(): table = (("foo", "bar"), ("a", 1), ("b", 2)) actual = namedtuples(table) it = iter(actual) o = it.next() eq_("a", o.foo) eq_(1, o.bar) o = it.next() eq_("b", o.foo) eq_(2, o.bar)
# dicts() ######### import petl as etl table = [['foo', 'bar'], ['a', 1], ['b', 2]] d = etl.dicts(table) d list(d) # namedtuples() ############### import petl as etl table = [['foo', 'bar'], ['a', 1], ['b', 2]] d = etl.namedtuples(table) d list(d) # records() ############### import petl as etl table = [['foo', 'bar'], ['a', 1], ['b', 2]] d = etl.records(table) d list(d) # rowgroupby() ##############
locationxmlfile = './datafiles/Vic_Locations.xml' mergedcsvfile = './datafiles/practice_locations.csv' # xmlfields is a dictionary to be used as xmlfields = {'Town_name': 'Town', 'Latitude': 'Lat', 'Longitude': 'Lon'} # type: Dict[str, str] xmlparent = 'Town_location' initialrow = ['Practice_Name', 'Latitude', 'Longitude', 'Town', 'State', 'Post_Code'] # tables in memory created from xml and csv files csvtable = petl.fromcsv(healthcsvfile) xmltable = petl.fromxml(locationxmlfile, xmlparent, xmlfields) # Find the row in xmltable matching town from csv lktbl = petl.lookupone(xmltable, 'Town_name') # type: Union[Dict[Any, Union[tuple[Any], Tuple[Any]]], Any] nmdtbl = petl.namedtuples(csvtable) finaltabl = [initialrow] for lin in nmdtbl: tabl = lktbl[lin.Town] latitude = tabl[0] longitude = tabl[1] insertline = (str(lin.Practice_Name) + ',' + latitude + ',' + longitude + ',' + str( lin.Town) + ',' + str(lin.State) + ',' + str(lin.Postcode)).split(',') print insertline finaltabl.extend([insertline]) petl.tocsv(finaltabl, mergedcsvfile)
import petl as etl table = [["foo", "bar"], ["a", 1], ["b", 2]] d = etl.dicts(table) d list(d) # namedtuples() ############### import petl as etl table = [["foo", "bar"], ["a", 1], ["b", 2]] d = etl.namedtuples(table) d list(d) # records() ############### import petl as etl table = [["foo", "bar"], ["a", 1], ["b", 2]] d = etl.records(table) d list(d)
def aggregate_execute(c, header, aggregates, **kwargs): rows = list(iter(etl.namedtuples(c()))) data = [f(rows) for f in aggregates] return etl.wrap([header, data])