From 754bec4b5547f1b1fd244d7bb88df43bc0f5c7ee Mon Sep 17 00:00:00 2001 From: adrienmalin <41926238+adrienmalin@users.noreply.github.com> Date: Wed, 22 Aug 2018 18:38:46 +0200 Subject: [PATCH] Class allowing to use snake cased properties on qt --- source/pythonize.py | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 source/pythonize.py diff --git a/source/pythonize.py b/source/pythonize.py new file mode 100644 index 0000000..d9f2cae --- /dev/null +++ b/source/pythonize.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +from inflection import camelize + + +class Pythonic: + def __getattribute__(self, name): + # Don't touch magic methods + if "__" in name: + return object.__getattribute__(self, name) + + # If class has a setter named "set" + CamelCased name, + # assume attribute has a getter named camelCased name + try: + object.__getattribute__(self, "set" + camelize(name)) # setter + getter = object.__getattribute__(self, camelize(name, False)) + except AttributeError: + try: + # Try camelCased attribute + return object.__getattribute__(self, camelize(name, False)) + except AttributeError: + # Else return attribute + return object.__getattribute__(self, name) + else: + return getter() + + def __setattr__(self, name, value): + # Use setter if exists + try: + setter = object.__getattribute__(self, "set" + camelize(name)) + except AttributeError: + # Try setting camelCased attribute + try: + object.__setattr__(self, camelize(name, False)) + except AttributeError: + # Else set attribute + object.__setattr__(self, name) + else: + setter(value) + + +if __name__ == "__main__": + # Test + + from PyQt5 import QtWidgets, QtCore + + + class Point(QtCore.QPoint, Pythonic): + pass + + + p = Point(0, 0) + p.x = 9 + p.y = 3 + assert p == Point(9, 3) + + + + class Window(QtWidgets.QWidget, Pythonic): + def __init__(self): + super().__init__() + self.window_title = "I'm pythonic!" + self.geometry = QtCore.QRect(300, 300, 400, 300) + self.layout = HBoxLayout() + self.layout << PushButton() + + + class HBoxLayout(QtWidgets.QHBoxLayout, Pythonic): + def __lshift__(self, widget): + self.add_widget(widget) + + + class PushButton(QtWidgets.QPushButton, Pythonic): + def __init__(self): + super().__init__() + self.text = "Click me!" + + + app = QtWidgets.QApplication([]) + win = Window() + win.show() + app.exec_() + \ No newline at end of file