Skip to content

Instantly share code, notes, and snippets.

@Hiroshiba
Last active May 20, 2021 21:54
Show Gist options
  • 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
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python383jvsc74a57bd09913ec6fc3da10649d6f7ccef1eec6a40d235c354cf07903ac5a1fe9ad50de3b",
"display_name": "Python 3.8.3 64-bit ('.venv': venv)"
},
"metadata": {
"interpreter": {
"hash": "9913ec6fc3da10649d6f7ccef1eec6a40d235c354cf07903ac5a1fe9ad50de3b"
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"cat << EOF > data.txt\n",
"MainModel\n",
"now_recording_uniq str \"\"\n",
"now_playing_uniq str \"\"\n",
"EOF"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"name = None\n",
"properties = []\n",
"types = []\n",
"defaults = []\n",
"for i, line in enumerate(Path(\"data.txt\").read_text().strip().splitlines()):\n",
" if i == 0:\n",
" name = line.strip()\n",
" else:\n",
" p, t, d = line.split()\n",
" properties.append(p)\n",
" types.append(t)\n",
" defaults.append(d)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"output = f'''\\\n",
"from PySide2.QtCore import Property, QObject, Signal\n",
"\n",
"\n",
"class {name}(QObject):\n",
" def __init__(self):\n",
" super().__init__(parent=None)\n",
"'''\n",
"\n",
"for p, d in zip(properties, defaults):\n",
" output += f'''\\\n",
" self.{p} = {d}\n",
"'''\n",
"\n",
"for p, t, d in zip(properties, types, defaults):\n",
" c = re.sub(\"_(.)\", lambda m:m.group(1).upper(), p)\n",
" output += f'''\n",
" def get_{p}(self):\n",
" return self.{p}\n",
"\n",
" def set_{p}(self, value):\n",
" self.{p} = value\n",
" self.changed_{p}.emit()\n",
"\n",
" @Signal\n",
" def changed_{p}(self):\n",
" pass\n",
"\n",
" {c} = Property(\n",
" {t},\n",
" fget=get_{p},\n",
" fset=set_{p},\n",
" notify=changed_{p},\n",
" )\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"from PySide2.QtCore import Property, QObject, Signal\n\n\nclass MainModel(QObject):\n def __init__(self):\n super().__init__(parent=None)\n self.now_recording_uniq = \"\"\n self.now_playing_uniq = \"\"\n\n def get_now_recording_uniq(self):\n return self.now_recording_uniq\n\n def set_now_recording_uniq(self, value):\n self.now_recording_uniq = value\n self.changed_now_recording_uniq.emit()\n\n @Signal\n def changed_now_recording_uniq(self):\n pass\n\n nowRecordingUniq = Property(\n str,\n fget=get_now_recording_uniq,\n fset=set_now_recording_uniq,\n notify=changed_now_recording_uniq,\n )\n\n def get_now_playing_uniq(self):\n return self.now_playing_uniq\n\n def set_now_playing_uniq(self, value):\n self.now_playing_uniq = value\n self.changed_now_playing_uniq.emit()\n\n @Signal\n def changed_now_playing_uniq(self):\n pass\n\n nowPlayingUniq = Property(\n str,\n fget=get_now_playing_uniq,\n fset=set_now_playing_uniq,\n notify=changed_now_playing_uniq,\n )\n\n"
]
}
],
"source": [
"print(output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
]
}
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