def test_lookupone(): """Test the lookupone function.""" t1 = (("foo", "bar"), ("a", 1), ("b", 2), ("b", 3)) # lookup one column on another under strict mode try: lookupone(t1, "foo", "bar", strict=True) except DuplicateKeyError: pass # expected else: assert False, "expected error" # lookup one column on another under, not strict actual = lookupone(t1, "foo", "bar", strict=False) expect = {"a": 1, "b": 2} # first value wins eq_(expect, actual) # test default value - tuple of whole row actual = lookupone(t1, "foo", strict=False) # no value selector expect = {"a": ("a", 1), "b": ("b", 2)} # first wins eq_(expect, actual) t2 = (("foo", "bar", "baz"), ("a", 1, True), ("b", 2, False), ("b", 3, True), ("b", 3, False)) # test value selection actual = lookupone(t2, "foo", ("bar", "baz"), strict=False) expect = {"a": (1, True), "b": (2, False)} eq_(expect, actual) # test compound key actual = lookupone(t2, ("foo", "bar"), "baz", strict=False) expect = {("a", 1): True, ("b", 2): False, ("b", 3): True} # first wins eq_(expect, actual)
def test_lookupone(): """Test the lookupone function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) # lookup one column on another under strict mode try: lookupone(t1, 'foo', 'bar', strict=True) except DuplicateKeyError: pass # expected else: assert False, 'expected error' # lookup one column on another under, not strict actual = lookupone(t1, 'foo', 'bar', strict=False) expect = {'a': 1, 'b': 2} # first value wins eq_(expect, actual) # test default value - tuple of whole row actual = lookupone(t1, 'foo', strict=False) # no value selector expect = {'a': ('a', 1), 'b': ('b', 2)} # first wins eq_(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test value selection actual = lookupone(t2, 'foo', ('bar', 'baz'), strict=False) expect = {'a': (1, True), 'b': (2, False)} eq_(expect, actual) # test compound key actual = lookupone(t2, ('foo', 'bar'), 'baz', strict=False) expect = {('a', 1): True, ('b', 2): False, ('b', 3): True} # first wins eq_(expect, actual)
def test_lookupone(): t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) # lookup one column on another under strict mode try: lookupone(t1, 'foo', 'bar', strict=True) except DuplicateKeyError: pass # expected else: assert False, 'expected error' # lookup one column on another under, not strict actual = lookupone(t1, 'foo', 'bar', strict=False) expect = {'a': 1, 'b': 2} # first value wins eq_(expect, actual) # test default value - tuple of whole row actual = lookupone(t1, 'foo', strict=False) # no value selector expect = {'a': ('a', 1), 'b': ('b', 2)} # first wins eq_(expect, actual) # test default value - key only actual = lookupone(cut(t1, 'foo'), 'foo') expect = {'a': ('a',), 'b': ('b',)} eq_(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test value selection actual = lookupone(t2, 'foo', ('bar', 'baz'), strict=False) expect = {'a': (1, True), 'b': (2, False)} eq_(expect, actual) # test compound key actual = lookupone(t2, ('foo', 'bar'), 'baz', strict=False) expect = {('a', 1): True, ('b', 2): False, ('b', 3): True} # first wins eq_(expect, actual)
def test_lookupone(): """Test the lookupone function.""" t1 = (('foo', 'bar'), ('a', 1), ('b', 2), ('b', 3)) # lookup one column on another under strict mode try: lookupone(t1, 'foo', 'bar') except DuplicateKeyError: pass # expected else: assert False, 'expected error' # lookup one column on another under, not strict actual = lookupone(t1, 'foo', 'bar', strict=False) expect = {'a': 1, 'b': 3} # last value wins assertequal(expect, actual) # test default value - tuple of whole row actual = lookupone(t1, 'foo', strict=False) # no value selector expect = {'a': ('a', 1), 'b': ('b', 3)} # last wins assertequal(expect, actual) t2 = (('foo', 'bar', 'baz'), ('a', 1, True), ('b', 2, False), ('b', 3, True), ('b', 3, False)) # test value selection actual = lookupone(t2, 'foo', ('bar', 'baz'), strict=False) expect = {'a': (1, True), 'b': (3, False)} assertequal(expect, actual) # test compound key actual = lookupone(t2, ('foo', 'bar'), 'baz', strict=False) expect = {('a', 1): True, ('b', 2): False, ('b', 3): False} # last wins assertequal(expect, actual)
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)
lkp = shelve.open('example.dat', flag='r') lkp['a'] lkp['b'] # lookupone() ############# import petl as etl table1 = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]] # if the specified key is not unique and strict=False (default), # the first value wins lkp = etl.lookupone(table1, 'foo', 'bar') lkp['a'] lkp['b'] # if the specified key is not unique and strict=True, will raise # DuplicateKeyError try: lkp = etl.lookupone(table1, 'foo', strict=True) except etl.errors.DuplicateKeyError as e: print(e) # compound keys are supported table2 = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, False], ['b', 3, True], ['b', 3, False]]
import shelve lkp = shelve.open('example.dat', flag='n') lkp = etl.lookup(table1, 'foo', 'bar', lkp) lkp.close() lkp = shelve.open('example.dat', flag='r') lkp['a'] lkp['b'] # lookupone() ############# import petl as etl table1 = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]] # if the specified key is not unique and strict=False (default), # the first value wins lkp = etl.lookupone(table1, 'foo', 'bar') lkp['a'] lkp['b'] # if the specified key is not unique and strict=True, will raise # DuplicateKeyError try: lkp = etl.lookupone(table1, 'foo', strict=True) except etl.errors.DuplicateKeyError as e: print(e) # compound keys are supported table2 = [['foo', 'bar', 'baz'], ['a', 1, True], ['b', 2, False], ['b', 3, True], ['b', 3, False]] lkp = etl.lookupone(table2, ('foo', 'bar'), 'baz') lkp[('a', 1)] lkp[('b', 2)]
# xmlfields = {'Town_name': 'Town', 'Latitude': 'Lat', 'Longitude': 'Lon'} # xml parent tag to start from xmlparent = 'Town_location' # # Create a list to be used as final merged table finaltabl_initialrow = ['Practice_Name', 'Latitude', 'Longitude', 'Town', 'State', 'Post_Code'] finaltabl = [finaltabl_initialrow] # # Tables in memory created from xml and csv files # csvtable = petl.fromcsv(healthcsvfile) xmltable = petl.fromxml(locationxmlfile, xmlparent, xmlfields) # # Create a dictionary with key as town name and values as latitude, longitude and town name lkupdictionary = petl.lookupone(xmltable, 'Town_name') # # Create named tuple instances accessible by attribute name nmdtpl = petl.namedtuples(csvtable) # # # Return the tuple from Dictionary using Town as key for instance in nmdtpl: towntuple = lkupdictionary[instance.Town] latitude = towntuple[0] longitude = towntuple[1] # Create a list of values from comma delimited string
from bottle import run, route, request, response import petl csvdata = petl.fromcsv('./datafiles/practice_locations.csv') lkupdictionary = petl.lookupone(csvdata, 'Post_Code') def getjson(pcode): valueslist = list(lkupdictionary[str(pcode)]) keyslist = [ 'Practice_Name', 'Latitude', 'Longitude', 'Town', 'State', 'Post_Code' ] json_dictionary = {} for i in range(len(keyslist)): json_dictionary[keyslist[i]] = valueslist[i] return json_dictionary @route('/getlocation') def jsonreturn(): response.headers['Access-Control-Allow-Origin'] = '*' response.headers[ 'Access-Control-Allow-Methods'] = 'PUT, GET, POST, DELETE, OPTIONS' response.headers[ 'Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' return getjson(request.query.postcode)