Skip to content

Instantly share code, notes, and snippets.

@Hiroshiba
Last active May 20, 2021 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hiroshiba/b47c8e5fa9a67007dfc87e8c74ce5489 to your computer and use it in GitHub Desktop.
Save Hiroshiba/b47c8e5fa9a67007dfc87e8c74ce5489 to your computer and use it in GitHub Desktop.
PySide2 Property Generator
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import re
from pathlib import Path
data = """\
MainModel
now_recording_uniq str ""
now_playing_uniq str ""
"""
name = None
properties = []
types = []
defaults = []
for i, line in enumerate(Path("data.txt").read_text().strip().splitlines()):
if i == 0:
name = line.strip()
else:
p, t, d = line.split()
properties.append(p)
types.append(t)
defaults.append(d)
output = f"""\
from PySide2.QtCore import Property, QObject, Signal
class {name}(QObject):
def __init__(self):
super().__init__(parent=None)
"""
for p, d in zip(properties, defaults):
output += f"""\
self.{p} = {d}
"""
for p, t, d in zip(properties, types, defaults):
c = re.sub("_(.)", lambda m: m.group(1).upper(), p)
output += f"""
def get_{p}(self):
return self.{p}
def set_{p}(self, value):
self.{p} = value
self.changed_{p}.emit()
@Signal
def changed_{p}(self):
pass
{c} = Property(
{t},
fget=get_{p},
fset=set_{p},
notify=changed_{p},
)
"""
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment