Changes in Struct to allow SQL-like query interface
This commit is contained in:
parent
870f776fa5
commit
ab46727c56
@ -159,7 +159,8 @@ def parse(string):
|
|||||||
current += c
|
current += c
|
||||||
elif stack and stack[-1] in ["'", '"', '(']: # in quote or args
|
elif stack and stack[-1] in ["'", '"', '(']: # in quote or args
|
||||||
current += c
|
current += c
|
||||||
elif c == ".":
|
elif c in [".", "[", "]"]:
|
||||||
|
if current:
|
||||||
retval.append(current)
|
retval.append(current)
|
||||||
current = ""
|
current = ""
|
||||||
else:
|
else:
|
||||||
@ -383,12 +384,67 @@ class Struct(object):
|
|||||||
else:
|
else:
|
||||||
self.transaction = None
|
self.transaction = None
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct == other.struct
|
||||||
|
else:
|
||||||
|
return self.struct == other
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct < other.struct
|
||||||
|
else:
|
||||||
|
return self.struct < other
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct > other.struct
|
||||||
|
else:
|
||||||
|
return self.struct > other
|
||||||
|
|
||||||
|
def __le__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct <= other.struct
|
||||||
|
else:
|
||||||
|
return self.struct <= other
|
||||||
|
|
||||||
|
def __ge__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct >= other.struct
|
||||||
|
else:
|
||||||
|
return self.struct >= other
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, Struct):
|
||||||
|
return self.struct != other.struct
|
||||||
|
else:
|
||||||
|
return self.struct != other
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.struct)
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
"""
|
||||||
|
Called when getattr fails. Lookup attr in struct; returns Struct
|
||||||
|
if more struct. This is used only in eval for where clause.
|
||||||
|
self.setitem_from_path(path, v) should be used to set value of
|
||||||
|
item.
|
||||||
|
"""
|
||||||
|
# for where eval:
|
||||||
|
if attr in self.struct:
|
||||||
|
return Struct(self.getitem(attr), self.db)
|
||||||
|
else:
|
||||||
|
raise AttributeError("no such attribute '%s'" % attr)
|
||||||
|
|
||||||
def __getitem__(self, path):
|
def __getitem__(self, path):
|
||||||
"""
|
"""
|
||||||
Given a path to a struct part, return the part, or None.
|
Given a path to a struct part, return the part, or None.
|
||||||
|
|
||||||
>>> Struct(struct)["primary_name.surname_list.0.surname"]
|
>>> Struct(struct)["primary_name.surname_list.0.surname"]
|
||||||
"""
|
"""
|
||||||
|
# For where eval:
|
||||||
|
if isinstance(path, int):
|
||||||
|
return Struct(self.struct[path], self.db)
|
||||||
# Work way down to last part:
|
# Work way down to last part:
|
||||||
return self.getitem_from_path(parse(path))
|
return self.getitem_from_path(parse(path))
|
||||||
|
|
||||||
@ -438,7 +494,7 @@ class Struct(object):
|
|||||||
elif hasattr(struct, item):
|
elif hasattr(struct, item):
|
||||||
return getattr(struct, item)
|
return getattr(struct, item)
|
||||||
elif item.startswith("("):
|
elif item.startswith("("):
|
||||||
args = eval(item[1:-1] + ",")
|
args = eval(item[1:-1] + ",") # tuple of args
|
||||||
return struct(*args)
|
return struct(*args)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
Loading…
Reference in New Issue
Block a user