kalekundert / autoprop Goto Github PK
View Code? Open in Web Editor NEWInfer properties from accessor methods
License: MIT License
Infer properties from accessor methods
License: MIT License
Given this test script:
import glooey
class TilePickerButton(glooey.Widget):
custom_alignment = 'center'
custom_propagate_mouse_events = True
def __init__(self, cls, on_mouse_press):
super().__init__()
self.cls = cls
vbox = glooey.VBox()
self.image = glooey.Image(cls.image)
self.label = glooey.Label(cls.__name__)
vbox.pack(self.image)
vbox.add(self.label)
import pdb
#pdb.set_trace()
vbox.padding = 6
self._attach_child(vbox)
self.on_mouse_press_func = on_mouse_press
def on_mouse_press(self, x, y, button, modifiers):
super().on_mouse_press(x, y, button, modifiers)
print("pressed")
return self.on_mouse_press_func()
class FakeSprite:
image = "fake.png"
cls = FakeSprite
button = TilePickerButton(cls, lambda cls=cls: setattr(self, "active_content_cls", cls))
This code works using autoprop 0.0.3, but fails with the latest version, 3.0.0. The error:
File "test_glooey.py", line 33, in <module>
button = TilePickerButton(cls, lambda cls=cls: setattr(self, "active_content_cls", cls))
File "test_glooey.py", line 19, in __init__
vbox.padding = 6
AttributeError: can't set attribute
This is happening because autoprop (at least as of 3.0.0) specifically validates that the setter accepts exactly one argument other than self and must be passed positionally. In older versions, it wasn't so strict (or at least, this example works when i run it using autoprop version 0.0.3) , but I checked and the documentation has always at least claimed this to be a requirement.
This code demonstrates the bug:
import autoprop
@autoprop
class Foo:
@classmethod
def get_bar(cls):
return "bar"
Foo.bar
This is the output:
<autoprop.property object at 0x7f12201eaac0>
When using autoprop
on subclasses:
The motivation, if I remember it right, is just to be sure of getting the right behavior. For example, if a subclass overwrites a getter but not a setter, we need to include the getter and the setter in the property.
The problem with overridding all superclass properties is that it confuses Sphinx/autoclasstoc
. So I think it'd be better to be more judicious about deciding which properties to override. Here is some pseudocode:
getattr
)Attribute access with caching is โ15x slower than with properties directly:
import autoprop
class MyObj1:
def __init__(self, x):
self.x = x
class MyObj2:
def __init__(self, x):
self._x = x
@property
def x(self):
return self._x
@autoprop
class MyObj3:
def __init__(self, x):
self._x = x
def get_x(self):
return self._x
@autoprop.immutable
class MyObj4:
def __init__(self, x):
self._x = x
def get_x(self):
return self._x
o1 = MyObj1(1)
o2 = MyObj2(1)
o3 = MyObj3(1)
o4 = MyObj4(1)
from timeit import timeit
debug(
timeit('o1.x', globals=globals()),
timeit('o2.x', globals=globals()),
timeit('o3.x', globals=globals()),
timeit('o4.x', globals=globals()),
)
$ py performance.py
performance.py:44 <module>
timeit('o1.x', globals=globals()): 0.05056732299271971 (float)
timeit('o2.x', globals=globals()): 0.12571916298475116 (float)
timeit('o3.x', globals=globals()): 0.12429857498500496 (float)
timeit('o4.x', globals=globals()): 1.8535652839927934 (float)
Some overhead is unavoidable, but I think I can do better than this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.