草庐IT

dynamic-properties

全部标签

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

javascript - Selenium / python : extract text from a dynamically-loading webpage after every scroll

我正在使用Selenium/python自动向下滚动社交媒体网站并抓取帖子。我目前正在滚动一定次数后一次“点击”提取所有文本(下面的代码),但我想在每次滚动后只提取新加载的文本。例如,如果页面最初包含文本“A、B、C”,然后在第一次滚动后显示“D、E、F”,我想存储“A、B、C”,然后滚动,然后存储“D、E、F”等。我想提取的具体元素是帖子的日期和消息文本,可以使用css选择器'.message-date'和'获得。message-body',分别(例如,dates=driver.find_elements_by_css_selector('.message-date'))。谁能建议如

python : creating dynamic functions

我有问题,我想创建动态函数,它会根据从数据库中检索到的值进行一些计算,我很清楚我的内部计算,但对如何创建动态类有疑问:我的结构是这样的:classxyz:defProject():start=2011-01-03defPhase1():effort='2d'defPhase2():effort='3d'defPhase3():effort='4d'现在想动态生成所有PhaseX()函数,所以任何人都可以建议我如何使用Python代码实现这样的事情等待正面回复问候谢谢你 最佳答案 与closures.defmakefunc(val):

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

python - Cython 编译错误 : dynamic module does not define module export function

我正在用Cython构建一个包。我使用以下作为setup.py的结构:fromdistutils.coreimportsetupfromdistutils.extensionimportExtensionfromCython.Buildimportcythonizeimportnumpyimportscipyextensions=[Extension("xxxxx",["xxxx/xxxxx.pyx"],include_dirs=[numpy.get_include(),"."]),Extension("nnls",["xxxxx/xxxxx.pyx"],include_dirs=[n

python - 通过@property 订购 Django 查询集

这个问题在这里已经有了答案:SortingaDjangoQuerySetbyaproperty(notafield)oftheModel(1个回答)关闭8年前。我正在尝试对由我在模型中定义的属性设置的查询进行排序,但不确定执行此操作的最佳方法。这是属性:@propertydefname(self):ifself.custom_name:returnself.custom_nameelse:returnself.module_object.name本质上,我想做一个:things=Thing.objects.all().order_by('-name')但当然在渲染时会出现CaughtF

Python @property vs 方法性能——使用哪一个?

我写了一些使用对象属性的代码:classFoo:def__init__(self):self.bar="baz"myFoo=Foo()print(myFoo.bar)现在我想做一些花哨的计算来返回bar。我可以使用@property使方法充当属性bar,或者我可以重构我的代码以使用myFoo.bar()。我应该返回并为所有bar访问添加括号还是使用@property?假设我的代码库现在很小,但由于熵,它会增加。 最佳答案 如果它在逻辑上是对象的属性/属性,我会说将其保留为属性。如果它可能会被参数化,我的意思是您可能想要调用myFoo