Game Utils v1.0
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# UV properties
|
||||||
|
.venv/
|
||||||
|
.python-version
|
||||||
2
__init__.py
Normal file
2
__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
__name__ = 'NLP_MCP'
|
||||||
|
__all__ = ['Game']
|
||||||
17
dice.py
Normal file
17
dice.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import random as rd
|
||||||
|
from . import serializable
|
||||||
|
|
||||||
|
class Dice(serializable.Serializable):
|
||||||
|
def __init__(self, seed=42):
|
||||||
|
self.seed = seed
|
||||||
|
rd.seed(self.seed)
|
||||||
|
|
||||||
|
def roll(self, num_faces=20):
|
||||||
|
return rd.randrange(start=1, stop=num_faces+1, step=1)
|
||||||
|
|
||||||
|
def head_or_tails():
|
||||||
|
result = rd.randint(0,1)
|
||||||
|
if result: # true
|
||||||
|
return "head" # face
|
||||||
|
else:
|
||||||
|
return "tails" # pile
|
||||||
6
main.py
Normal file
6
main.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
def main():
|
||||||
|
print("Hello from mcp-nlp!")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
7
pyproject.toml
Normal file
7
pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[project]
|
||||||
|
name = "mcp-nlp"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Add your description here"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.11"
|
||||||
|
dependencies = []
|
||||||
64
serializable.py
Normal file
64
serializable.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import json
|
||||||
|
from typing import Any, Dict, List, Type, TypeVar
|
||||||
|
|
||||||
|
T = TypeVar('T', bound='Serializable')
|
||||||
|
|
||||||
|
class Serializable:
|
||||||
|
@classmethod
|
||||||
|
def from_dict(cls: Type[T], data: Dict[str, Any]) -> T:
|
||||||
|
"""Creates an instance of the class from a dictionary."""
|
||||||
|
instance = cls.__new__(cls)
|
||||||
|
instance.deserialize_dict(data)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
def serialize(self, file) -> str:
|
||||||
|
"""Serializes the object and all nested Serializable objects to JSON."""
|
||||||
|
def serialize_value(value: Any) -> Any:
|
||||||
|
if isinstance(value, Serializable):
|
||||||
|
return value.serialize_dict()
|
||||||
|
elif isinstance(value, list):
|
||||||
|
return [serialize_value(v) for v in value]
|
||||||
|
elif isinstance(value, dict):
|
||||||
|
return {k: serialize_value(v) for k, v in value.items()}
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
|
||||||
|
attrs = {k: serialize_value(v) for k, v in self.__dict__.items() if not k.startswith('_')}
|
||||||
|
return json.dumps(attrs, file, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
def serialize_dict(self) -> Dict[str, Any]:
|
||||||
|
"""Serializes the object to a dictionary (for nested serialization)."""
|
||||||
|
def serialize_value(value: Any) -> Any:
|
||||||
|
if isinstance(value, Serializable):
|
||||||
|
return value.serialize_dict()
|
||||||
|
elif isinstance(value, list):
|
||||||
|
return [serialize_value(v) for v in value]
|
||||||
|
elif isinstance(value, dict):
|
||||||
|
return {k: serialize_value(v) for k, v in value.items()}
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
|
||||||
|
return {k: serialize_value(v) for k, v in self.__dict__.items() if not k.startswith('_')}
|
||||||
|
|
||||||
|
def deserialize(self, json_str: str):
|
||||||
|
"""Deserializes JSON and recursively reconstructs Serializable objects."""
|
||||||
|
data = json.loads(json_str)
|
||||||
|
self.deserialize_dict(data)
|
||||||
|
|
||||||
|
def deserialize_dict(self, data: Dict[str, Any]):
|
||||||
|
"""Deserializes a dictionary and recursively reconstructs Serializable objects."""
|
||||||
|
for key, value in data.items():
|
||||||
|
if isinstance(value, dict) and 'py/class' in value:
|
||||||
|
# Handle nested Serializable objects
|
||||||
|
class_name = value['py/class']
|
||||||
|
class_ = globals()[class_name]
|
||||||
|
setattr(self, key, class_.from_dict(value))
|
||||||
|
elif isinstance(value, list):
|
||||||
|
# Handle lists of Serializable objects
|
||||||
|
setattr(self, key, [
|
||||||
|
item if not isinstance(item, dict) or 'py/class' not in item
|
||||||
|
else globals()[item['py/class']].from_dict(item)
|
||||||
|
for item in value
|
||||||
|
])
|
||||||
|
else:
|
||||||
|
setattr(self, key, value)
|
||||||
Reference in New Issue
Block a user