getattr

Can I dynamically choose the method applied on a pandas Resampler object?

点点圈 提交于 2021-01-29 17:33:12
问题 I am trying to create a function which resamples time series data in pandas . I would like to have the option to specify the type of aggregation that occurs depending on what type of data I am sending through (i.e. for some data, taking the sum of each bin is appropriate, while for others, taking the mean is needed, etc.). For example data like these: import pandas as pd import numpy as np dr = pd.date_range('01-01-2020', '01-03-2020', freq='1H') df = pd.DataFrame(np.random.rand(len(dr)),

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

删除回忆录丶 提交于 2020-12-25 03:50:52
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

余生长醉 提交于 2020-12-25 03:49:50
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Can the usage of `setattr` (and `getattr`) be considered as bad practice?

半世苍凉 提交于 2020-12-25 03:47:37
问题 setattr and getattr kind of got into my style of programing (mainly scientific stuff, my knowledge about python is self told). Considering that exec and eval inherit a potential danger since in some cases they might lead to security issues, I was wondering if for setattr the same argument is considered to be valid. (About getattr I found this question which contains some info - although the argumentation is not very convincing.) From what I know, setattr can be used without worrying to much,

Python - getattr and concatenation

笑着哭i 提交于 2020-02-14 00:58:34
问题 So in playing around with getattr in my code I discovered the following: myVariable = foo.A.bar works...but something like this: B = "A" myVariable = getattr(foo, B + ".bar") returns an error that foo does not contain an attribute A.bar. Where am I going wrong? Thanks! 回答1: Because there is no attribute A.bar on foo . Attribute bar is a part of the object pointed to by A , which is an attribute of foo . You need either getattr(foo.A, "bar") or getattr(getattr(foo, 'A'), 'bar') The generic

How to use __getattr__ to delegate methods to attribute?

Deadly 提交于 2020-01-15 06:27:49
问题 I have the following class: class MyInt: def __init__(self, v): if type(v) != int: raise ValueError('value must be an int') self.v = v def __getattr__(self, attr): return getattr(self.v, attr) i = MyInt(0) print(i + 1) I get the error: TypeError: unsupported operand type(s) for +: 'MyInt' and 'int' Shouldn't i.__add__(1) be called? And shouldn't __getattr__ be called when no such method is found in the MyInt class? 回答1: __getattr__ cannot be used to generate other magic methods. You'll need

Why does the “name” parameter to __setattr__ include the class, but __getattr__ doesn't?

巧了我就是萌 提交于 2020-01-04 09:03:30
问题 The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value): print name def __getattr__(self, name): print name raise AttributeError(name) x = MyClass() x.test() x.__y Outputs: _MyClass__x __y Traceback (most recent call last): ... AttributeError: __y The documentation is utterly unhelpful stating the "name" is the "name of the attribute", yet for some reason it's different depending on whether you are setting it or getting it. What I want to know is:

Get attributes for class and instance in python

ぐ巨炮叔叔 提交于 2020-01-04 03:12:23
问题 In python work next code: class MyClass(object): field = 1 >>> MyClass.field 1 >>> MyClass().field 1 When I want return value for custom fields I use next code: class MyClass(object): def __getattr__(self, name): if name.startswith('fake'): return name raise AttributeError("%r object has no attribute %r" % (type(self).__name__, name)) >>> MyClass().fake fake But: >>> MyClass.fake Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: class MyClass has no

setattr and getattr with methods

百般思念 提交于 2020-01-01 10:27:47
问题 I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): self.refClass.action1() def action2(self): self.refClass.action2() def action3(self): self.refClass.action3() This is the refClass: class RefClass(): def __init__(self): self.myClass = MyClass(self) def action1(self): #Stuff to execute action1 def action2(self): #Stuff to execute action2 def action3(self

What is the difference between type.__getattribute__ and object.__getattribute__?

守給你的承諾、 提交于 2020-01-01 05:55:04
问题 Given: In [37]: class A: ....: f = 1 ....: In [38]: class B(A): ....: pass ....: In [39]: getattr(B, 'f') Out[39]: 1 Okay, that either calls super or crawls the mro? In [40]: getattr(A, 'f') Out[40]: 1 This is expected. In [41]: object.__getattribute__(A, 'f') Out[41]: 1 In [42]: object.__getattribute__(B, 'f') --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-42-de76df798d1d> in <module>() ----> 1