Python:循环 NDB 实体模型
Python: Loop over NDB entity model
我有一个 class/NDB 实体模型定义如下:
class CashModel(ndb.Model):
"Models a Cash record"
# Name, Balance, Interest Rate
accountName = ndb.StringProperty()
interestRate = ndb.FloatProperty()
balance = ndb.FloatProperty()
accountType = ndb.StringProperty()
futureView = ndb.JsonProperty()
time_stored = ndb.DateTimeProperty(auto_now_add=True)
将值添加到 class
后,打印出来的结果如下所示:
CashModel(accountName=u'Bank', accountType=u'cash', balance=1000.0, futureView=None, interestRate=5.0, time_stored=datetime.datetime(2015, 7, 7, 18, 33, 3, 925601))
如何遍历 key/value 对 class,以便输出类似于:
accountName, Bank
accountType, cash
balance, 1000.0
futureView, None
interestRate, 5.0
time_stored, datetime.datetime(2015, 7, 7, 18, 33, 3, 925601)
查看了 S.O 上的其他答案,但 none 似乎很合适。使用内置方法的简单解决方案将是最好的。 TIA
向您的 class 添加一个 __iter__()
方法,该方法从 self.__dict__.items()
.
产生
>>> class Test:
def __init__(self):
self.a = 1
self.b = 2
def __iter__(self): yield from self.__dict__.items()
>>>[t for t in Test()]
[('b', 2), ('a', 1)]
您还可以覆盖 class 的 __repr__
和 __str__
方法:
In [7]: class Foo(object):
def __init__(self):
self.bar = "A"
self.baz = "B"
self.bug = "C"
def __repr__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
def __str__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
...:
In [8]: print Foo()
baz=B
bar=A
bug=C
In [9]: Foo()
Out[9]:
baz=B
bar=A
bug=C
这里有一个 __str__
方法可以满足您的需求:
In [39]: class Foo(object):
def __init__(self):
self.bar = "A"
self.baz = "B"
self.bug = "C"
def __repr__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
def __str__(self):
return self.__class__.__name__+'('+';'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])+')'
....:
In [40]: print Foo()
Foo(baz=B;bar=A;bug=C)
您可以使用 to_dict(include=None, exclude=None)
实例方法将您的模型对象转换为字典,然后使用 iteritems()
迭代 key/value 对
to_dict(include=None, exclude=None)
Returns 包含模型 属性 值的字典。
我有一个 class/NDB 实体模型定义如下:
class CashModel(ndb.Model):
"Models a Cash record"
# Name, Balance, Interest Rate
accountName = ndb.StringProperty()
interestRate = ndb.FloatProperty()
balance = ndb.FloatProperty()
accountType = ndb.StringProperty()
futureView = ndb.JsonProperty()
time_stored = ndb.DateTimeProperty(auto_now_add=True)
将值添加到 class
后,打印出来的结果如下所示:
CashModel(accountName=u'Bank', accountType=u'cash', balance=1000.0, futureView=None, interestRate=5.0, time_stored=datetime.datetime(2015, 7, 7, 18, 33, 3, 925601))
如何遍历 key/value 对 class,以便输出类似于:
accountName, Bank
accountType, cash
balance, 1000.0
futureView, None
interestRate, 5.0
time_stored, datetime.datetime(2015, 7, 7, 18, 33, 3, 925601)
查看了 S.O 上的其他答案,但 none 似乎很合适。使用内置方法的简单解决方案将是最好的。 TIA
向您的 class 添加一个 __iter__()
方法,该方法从 self.__dict__.items()
.
>>> class Test:
def __init__(self):
self.a = 1
self.b = 2
def __iter__(self): yield from self.__dict__.items()
>>>[t for t in Test()]
[('b', 2), ('a', 1)]
您还可以覆盖 class 的 __repr__
和 __str__
方法:
In [7]: class Foo(object):
def __init__(self):
self.bar = "A"
self.baz = "B"
self.bug = "C"
def __repr__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
def __str__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
...:
In [8]: print Foo()
baz=B
bar=A
bug=C
In [9]: Foo()
Out[9]:
baz=B
bar=A
bug=C
这里有一个 __str__
方法可以满足您的需求:
In [39]: class Foo(object):
def __init__(self):
self.bar = "A"
self.baz = "B"
self.bug = "C"
def __repr__(self):
return '\n'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])
def __str__(self):
return self.__class__.__name__+'('+';'.join(['{}={}'.format(k,v) for (k, v) in self.__dict__.items()])+')'
....:
In [40]: print Foo()
Foo(baz=B;bar=A;bug=C)
您可以使用 to_dict(include=None, exclude=None)
实例方法将您的模型对象转换为字典,然后使用 iteritems()
迭代 key/value 对
to_dict(include=None, exclude=None)
Returns 包含模型 属性 值的字典。