Allow calling lists to select and filter
This commit is contained in:
parent
37ba4122ba
commit
946a213fbc
@ -438,26 +438,34 @@ class Struct(object):
|
|||||||
|
|
||||||
If no args are given, all are provided.
|
If no args are given, all are provided.
|
||||||
"""
|
"""
|
||||||
selected = self.struct # better be a list
|
selected = self.struct # better be dicts
|
||||||
for key in kwargs:
|
# First, find elements of the list that match any given
|
||||||
for item in selected[:]:
|
# selection criteria:
|
||||||
part = self.getitem_from_path(key.split("__"), item)
|
selected = self.struct # assume dicts
|
||||||
if part[0] == kwargs[key]:
|
# assume True
|
||||||
# ok, keep as selected
|
to_delete = []
|
||||||
pass
|
for key in kwargs: # value="Social Security Number"
|
||||||
else:
|
parts = self.getitem_from_path(key.split("__")) # returns all
|
||||||
# remove
|
# This will return a list; we keep the ones that match
|
||||||
selected.remove(item)
|
for p in range(len(parts)):
|
||||||
|
# if it matches, keep it:
|
||||||
|
if parts[p] != kwargs[key]:
|
||||||
|
to_delete.append(p)
|
||||||
|
# delete from highest to lowest, to use pop:
|
||||||
|
for p in reversed(to_delete):
|
||||||
|
selected.pop(p)
|
||||||
# now select which parts to show:
|
# now select which parts to show:
|
||||||
if args:
|
if args: # just some of the parts, ["type.string", ...]
|
||||||
results = []
|
results = []
|
||||||
if len(args) == 1:
|
for select in selected: # dict in dicts
|
||||||
for item in selected:
|
parts = []
|
||||||
results.append(getattr(Struct(item, self.db), args[0]))
|
for item in args: # ["type.string"]
|
||||||
else:
|
items = item.split(".") # "type.string"
|
||||||
for item in selected:
|
values = Struct(select, self.db).getitem_from_path(items)
|
||||||
results.append(tuple([getattr(Struct(item, self.db), field) for field in args]))
|
if values:
|
||||||
else:
|
parts.append((item, values))
|
||||||
|
results.append(parts) # return [["type.string", "Social Security Number"], ...]
|
||||||
|
else: # return all
|
||||||
results = selected
|
results = selected
|
||||||
# return them
|
# return them
|
||||||
return results
|
return results
|
||||||
@ -523,7 +531,7 @@ class Struct(object):
|
|||||||
else:
|
else:
|
||||||
return self.handle_join(self.struct[item])
|
return self.handle_join(self.struct[item])
|
||||||
|
|
||||||
def getitem_from_path(self, items, item):
|
def getitem_from_path(self, items):
|
||||||
"""
|
"""
|
||||||
path is a list
|
path is a list
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user