Git Product home page Git Product logo

autoprop's People

Contributors

actions-user avatar kalekundert avatar mgorny avatar

Stargazers

 avatar

Watchers

 avatar  avatar

autoprop's Issues

Incompatible with Latest Autoprop Version

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.

Doesn't work with class methods

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>

Be more conservative about overriding base class properties

When using autoprop on subclasses:

  • Superclass properties defined by autoprop` will be overridden.
  • Superclass properties not defined by autoprop will be left.

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:

  • Find all getters/setters (in current class, not parent classes)
  • For each getter/setter, check if other relevant methods exist in parent classes (e.g. using getattr)
  • Create properties.

Improve caching performance

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.