Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pocs/PAH/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Python Annotation Hacking
=========================

In several PEP and discussion it was envisioned that the python syntax with type annotation would
serve as a sofa scene description langage. So here is the corresponding POC.

This POC is related with:
- PEP #12
- ...

We would like to see if we can implement most of the PEP > 12 features on top of a syntax like the following

```python
class VisualModel(Sofa.Core.Prefab):
state : MechanicalObject

enable : Data[bool]
"""bla bla"""

root.add(VisualModel(enable=True))

root.add(VisualModel, enable=True)

v = VisualModel()
v.enable = True
root.add(v)
```

85 changes: 85 additions & 0 deletions pocs/PAH/src/xp1/xp-annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import typing

class Prefab(object):
def __init_subclass__(cls) -> None:
super().__init_subclass__()

if not hasattr(cls, "__orig_bases__"):
cls.templates = {}
return


args = typing.get_args(cls.__orig_bases__[1])
print("= ==== > ", cls, typing.get_type_hints(cls))

print(" ----------->", typing.get_type_hints(cls))
cls.args = typing.get_type_hints(cls)
cls.templates = None

def get_templates(self):
values = []
if not hasattr(self, "__orig_class__"):
if not hasattr(self, "__orig_bases__"):
return
values += [ variabletype for variabletype in typing.get_args(self.__orig_bases__[-1]) ]

if hasattr(self, "__orig_class__"):
values += typing.get_args(self.__orig_class__)

names = self.__class__.args
print("VALUES.... ", names, values )
if self.__class__.templates is None:
self.__class__.templates = {}
for name, variable in names.items():
value = variable
self.__class__.templates[name] = (value.__default__, type(value))
return self.templates


X = typing.TypeVar("X", default=int)
B = typing.TypeVar("B", default=float)

class AClass(Prefab, typing.Generic[X,B]):
i : X
b : B
pass

class Rigid3:
pass
class Vec3:
pass

Template = typing.TypeVar("Template", default=Rigid3)
Template2 = typing.TypeVar("Template2", default=Vec3)

class MechanicalObject(Prefab, typing.Generic[Template, Template2]):
template : Template
template2 : Template2

positions : list[int]

BClass = AClass[MechanicalObject[Rigid3, Vec3], int]

print(type( AClass ))

print(dir( BClass ))
print(dir(type( BClass )))

print(AClass.__annotations__)

b = BClass()
print("get_origin(BClass)", typing.get_origin(BClass))
print("get_args(BClass)", typing.get_args(BClass))

print("get_origin(self.)", typing.get_origin(b))
print("get_args(self)", typing.get_args(b))

print("get_origin(self.__class__)", typing.get_origin(b.__class__))
print("get_args(self.__class__)", typing.get_args(b.__class__))

print("b ", b)
print("b type", type(b))
print("b ", type(b.__class__))

print(b.__annotations__)
print("getTemplate", MechanicalObject().get_templates())
Loading