Finished first data structure and basic io

This commit is contained in:
Maximilian Giller 2025-09-14 18:20:46 +02:00
parent 539e54902a
commit 02a140ee67
4 changed files with 151 additions and 5 deletions

View file

@ -5,5 +5,6 @@ description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"dataclasses-json>=0.6.7",
"phue>=1.1",
]

View file

@ -1,19 +1,21 @@
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from enum import Enum
from decimal import Decimal
@dataclass_json
@dataclass
class SpaceVector:
"""A 3-dimensional coordinate structure with clear orientation."""
lr: float
lr: float = 0
"""Left-to-Right."""
tb: float
tb: float = 0
"""Top-to-Bottom."""
fb: float
fb: float = 0
"""Front-to-Behind."""
@ -24,6 +26,7 @@ class Capabilities(Enum):
COLOR = "color"
@dataclass_json
@dataclass
class Fixture:
"""Definition of a fixture that generates light, its capabilities and positioning."""
@ -50,6 +53,7 @@ class Fixture:
"""In range [0,1], how exposed is the light source of the fixture. 0: no exposure. 1: full exposure."""
@dataclass_json
@dataclass
class Space:
"""A space containing fixtures."""
@ -66,7 +70,11 @@ class Space:
center: SpaceVector
"""A reference point in the space from which the audience perspective might be assumed."""
version: str = "1"
"""Metatag to track version of datastructure."""
@dataclass_json
@dataclass
class Keyframe:
"""State description for a specified point in time."""
@ -81,6 +89,7 @@ class Keyframe:
"""Targeted state value reached by reaching the keyframe."""
@dataclass_json
@dataclass
class Track:
"""Contains a sequence of keyframes for specified fixtures."""
@ -98,6 +107,7 @@ class Track:
"""Sequence of keyframes describing actions"""
@dataclass_json
@dataclass
class Sequence:
"""A sequence of light events described over fixtures."""
@ -108,9 +118,16 @@ class Sequence:
name: str
"""Human readable name."""
tracks: list[Track]
"""List of tracks associated with sequence."""
version: str = "1"
"""Metatag to track version of datastructure."""
@dataclass_json
@dataclass
class Event:
class Flickr:
"""A collection of sequences and spaces."""
spaces: list[Space]
@ -118,3 +135,61 @@ class Event:
sequences: list[Sequence]
"""A collection of sequences."""
version: str = "1"
"""Metatag to track version of datastructure."""
def save(self, path: str) -> int:
"""Write flickr data to file."""
with open(path, "x", encoding="UTF-8") as fp:
return fp.write(self.to_json())
@staticmethod
def load(path: str) -> "Flickr":
"""Write flickr data to file."""
with open(path, "r", encoding="UTF-8") as fp:
json = "\n".join(fp.readlines())
return Flickr.schema().loads(json)
f = Flickr(
[
Space(
"iosf",
"Living Room",
[
Fixture(
"98iwd",
"Cool Light",
Capabilities.BRIGHTNESS,
SpaceVector(),
SpaceVector(),
SpaceVector(),
0.5,
)
],
SpaceVector(),
)
],
[
Sequence(
"094rj",
"Thunder",
[
Track(
"89idf",
"Ceiling flicker",
["98iwd"],
[Keyframe(Decimal.from_float(0), "bri", 1)],
)
],
)
],
)
f.save("test.json")
a = Flickr.load("test.json")
print(f.to_json())

1
src/test.json Normal file
View file

@ -0,0 +1 @@
{"spaces": [{"id": "iosf", "name": "Living Room", "fixtures": [{"id": "98iwd", "name": "Cool Light", "capabilities": "brightness", "position": {"lr": 0, "tb": 0, "fb": 0}, "target": {"lr": 0, "tb": 0, "fb": 0}, "spread": {"lr": 0, "tb": 0, "fb": 0}, "exposure": 0.5}], "center": {"lr": 0, "tb": 0, "fb": 0}, "version": "1"}], "sequences": [{"id": "094rj", "name": "Thunder", "tracks": [{"id": "89idf", "name": "Ceiling flicker", "fixture_ids": ["98iwd"], "keyframes": [{"time": "0", "parameter": "bri", "value": 1}]}], "version": "1"}], "version": "1"}

71
uv.lock
View file

@ -2,19 +2,88 @@ version = 1
revision = 3
requires-python = ">=3.12"
[[package]]
name = "dataclasses-json"
version = "0.6.7"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "marshmallow" },
{ name = "typing-inspect" },
]
sdist = { url = "https://files.pythonhosted.org/packages/64/a4/f71d9cf3a5ac257c993b5ca3f93df5f7fb395c725e7f1e6479d2514173c3/dataclasses_json-0.6.7.tar.gz", hash = "sha256:b6b3e528266ea45b9535223bc53ca645f5208833c29229e847b3f26a1cc55fc0", size = 32227, upload-time = "2024-06-09T16:20:19.103Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/c3/be/d0d44e092656fe7a06b55e6103cbce807cdbdee17884a5367c68c9860853/dataclasses_json-0.6.7-py3-none-any.whl", hash = "sha256:0dbf33f26c8d5305befd61b39d2b3414e8a407bedc2834dea9b8d642666fb40a", size = 28686, upload-time = "2024-06-09T16:20:16.715Z" },
]
[[package]]
name = "flickr"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "dataclasses-json" },
{ name = "phue" },
]
[package.metadata]
requires-dist = [{ name = "phue", specifier = ">=1.1" }]
requires-dist = [
{ name = "dataclasses-json", specifier = ">=0.6.7" },
{ name = "phue", specifier = ">=1.1" },
]
[[package]]
name = "marshmallow"
version = "3.26.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "packaging" },
]
sdist = { url = "https://files.pythonhosted.org/packages/ab/5e/5e53d26b42ab75491cda89b871dab9e97c840bf12c63ec58a1919710cd06/marshmallow-3.26.1.tar.gz", hash = "sha256:e6d8affb6cb61d39d26402096dc0aee12d5a26d490a121f118d2e81dc0719dc6", size = 221825, upload-time = "2025-02-03T15:32:25.093Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/34/75/51952c7b2d3873b44a0028b1bd26a25078c18f92f256608e8d1dc61b39fd/marshmallow-3.26.1-py3-none-any.whl", hash = "sha256:3350409f20a70a7e4e11a27661187b77cdcaeb20abca41c1454fe33636bea09c", size = 50878, upload-time = "2025-02-03T15:32:22.295Z" },
]
[[package]]
name = "mypy-extensions"
version = "1.1.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/a2/6e/371856a3fb9d31ca8dac321cda606860fa4548858c0cc45d9d1d4ca2628b/mypy_extensions-1.1.0.tar.gz", hash = "sha256:52e68efc3284861e772bbcd66823fde5ae21fd2fdb51c62a211403730b916558", size = 6343, upload-time = "2025-04-22T14:54:24.164Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505", size = 4963, upload-time = "2025-04-22T14:54:22.983Z" },
]
[[package]]
name = "packaging"
version = "25.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727, upload-time = "2025-04-19T11:48:59.673Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },
]
[[package]]
name = "phue"
version = "1.1"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/bd/b9/456c439ded199e67a2630c798e9974e517a11c7628918ceee7a19343c762/phue-1.1.tar.gz", hash = "sha256:61facc991a2ead1d727df8513d06ce089033489b6dca09be093db24f7cdb8874", size = 10130, upload-time = "2019-01-27T23:06:30.762Z" }
[[package]]
name = "typing-extensions"
version = "4.15.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/72/94/1a15dd82efb362ac84269196e94cf00f187f7ed21c242792a923cdb1c61f/typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466", size = 109391, upload-time = "2025-08-25T13:49:26.313Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548", size = 44614, upload-time = "2025-08-25T13:49:24.86Z" },
]
[[package]]
name = "typing-inspect"
version = "0.9.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "mypy-extensions" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/dc/74/1789779d91f1961fa9438e9a8710cdae6bd138c80d7303996933d117264a/typing_inspect-0.9.0.tar.gz", hash = "sha256:b23fc42ff6f6ef6954e4852c1fb512cdd18dbea03134f91f856a95ccc9461f78", size = 13825, upload-time = "2023-05-24T20:25:47.612Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/65/f3/107a22063bf27bdccf2024833d3445f4eea42b2e598abfbd46f6a63b6cb0/typing_inspect-0.9.0-py3-none-any.whl", hash = "sha256:9ee6fc59062311ef8547596ab6b955e1b8aa46242d854bfc78f4f6b0eff35f9f", size = 8827, upload-time = "2023-05-24T20:25:45.287Z" },
]