草庐IT

nested-properties

全部标签

python - 谷歌应用引擎 : how can I programmatically access the properties of my Model class?

我有一个模型类:classPerson(db.Model):first_name=db.StringProperty(required=True)last_name=db.StringProperty(required=True)我在p中有一个此类的实例,字符串s包含值'first_name'。我想做类似的事情:printp[s]和p[s]=new_value两者都会导致TypeError。有人知道我怎样才能实现我想要的吗? 最佳答案 如果模型类足够智能,它应该能够识别执行此操作的标准Python方法。尝试:getattr(p,s)

Python 类变量或@property

我正在编写一个python类来存储数据,然后另一个类将创建该类的一个实例来打印不同的变量。一些类变量需要大量格式化,这可能需要多行代码才能使其处于“最终状态”。仅使用这种结构从类外部访问变量是否是一种不好的做法?classData():def__init__(self):self.data="data"还是使用@property方法访问变量更好?classData:@propertydefdata(self):return"data" 最佳答案 小心,如果你这样做:classData:@propertydefdata(self):r

python - celery 的困难 : function object has no property 'delay'

我最近一直在忙于软件开发,并取得了一些成功,使celery屈服于我的意志。我已经成功地使用它发送电子邮件,并且刚刚尝试使用几乎完全相同的代码(在重新启动所有进程等之后)通过Twilio发送短信。但是我不断遇到以下问题:File"/Users/Rob/Dropbox/Python/secTrial/views.py",line115,insend_smssend_sms.delay(recipients,form.text.data)AttributeError:'function'objecthasnoattribute'delay'我的代码如下:@celery.taskdefsend

python - Flask-Login 引发 TypeError : 'bool' object is not callable when trying to override is_active property

我想修改Flask-Login中的is_active,这样用户就不会一直处于事件状态。默认值始终返回True,但我将其更改为返回banned列的值。根据文档,is_active应该是一个属性。但是,内部Flask-Login代码引发:TypeError:'bool'objectisnotcallable尝试使用is_active时。如何正确使用is_active来停用某些用户?classUser(UserMixin,db.Model):id=db.Column(db.Integer,primary_key=True)banned=db.Column(db.Boolean,default

python - @property 在这种情况下的用处

给定以下类:classBasicRNNCell(RNNCell):"""ThemostbasicRNNcell."""def__init__(self,num_units,input_size=None):self._num_units=num_unitsself._input_size=num_unitsifinput_sizeisNoneelseinput_size@propertydefinput_size(self):returnself._input_size@propertydefoutput_size(self):returnself._num_units@propert

python - 何时使用 "property"内置 : auxiliary functions and generators

我最近发现了Python的propertybuilt-in,它将类方法的getter和setter伪装成类的属性。我现在很想以我非常确定不合适的方式使用它。如果类A有一个属性_x,您希望限制其允许值,那么使用property关键字显然是正确的做法;即,它将取代可能用C++编写的getX()和setX()构造。但是还有什么地方适合将函数设为属性呢?例如,如果您有classVertex(object):def__init__(self):self.x=0.0self.y=1.0classPolygon(object):def__init__(self,list_of_vertices):s

python - 高级 Python 正则表达式 : how to evaluate and extract nested lists and numbers from a multiline string?

我试图将元素与多行字符串分开:lines='''c0c1c2c3c4c5010100.5[1.5,2][[10,10.4],[c,10,eee]][[a,bg],[5.5,ddd,edd]]100.5120200.5[2.5,2][[20,20.4],[d,20,eee]][[a,bg],[7.5,udd,edd]]200.5'''我的目标是得到一个列表lst这样:#firstvalueisindexlst[0]=['c0','c1','c2','c3','c4','c5']lst[1]=[0,10,100.5,[1.5,2],[[10,10.4],['c',10,'eee']],[[

python - Django 休息框架 : Serialize data from nested json fields to plain object

我想将非平面结构序列化为一个平面对象。这是我收到的API调用的示例(不幸的是我无法控制它):{"webhookEvent":"jira:issue_updated","user":{"id":2434,"name":"Ben",},"issue":{"id":"33062","key":"jira-project-key-111","fields":{"summary":"Theweekahead",},"changelog":{"id":"219580","items":[{"field":"status","fieldtype":"jira","from":"10127","fro

python - 子类化 matplotlib 文本 : manipulate properties of child artist

我正在研究一个用于线对象内联标签的类的实现。为此,我制作了Text类的子类,作为Line2D对象的属性。我的代码previouspost可能有点冗长,所以我在这里隔离了问题:frommatplotlib.textimportTextfrommatplotlibimportpyplotaspltimportnumpyasnpclassLineText(Text):def__init__(self,line,*args,**kwargs):x_pos=line.get_xdata().mean()y_pos=line.get_ydata().mean()Text.__init__(self

python - 在 Python 中使用 Property 的 deleter 装饰器

我正在玩弄Python中的属性,我想知道这个@propertyName.deleter装饰器是如何工作的。我可能遗漏了一些东西,我无法通过谷歌找到明确的答案。我想要实现的是,当调用此删除器行为时,我可以触发其他操作(例如:使用我的3d应用程序SDK)。目前只有一个简单的print()似乎没有被触发。当我使用del(instance.property)删除属性时,deleter是否被触发?否则,我该如何实现?classM():def__init__(self):self._m=None@propertydefmmm(self):returnself._m@mmm.setterdefmmm