Skip to content

Commit ce7b3d8

Browse files
committed
WIP-STASH
1 parent 0a09117 commit ce7b3d8

File tree

12 files changed

+1035
-48
lines changed

12 files changed

+1035
-48
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/******************************************************************************
2+
* SofaPython3 plugin *
3+
* (c) 2021 CNRS, University of Lille, INRIA *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify it *
6+
* under the terms of the GNU Lesser General Public License as published by *
7+
* the Free Software Foundation; either version 2.1 of the License, or (at *
8+
* your option) any later version. *
9+
* *
10+
* This program is distributed in the hope that it will be useful, but WITHOUT *
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
13+
* for more details. *
14+
* *
15+
* You should have received a copy of the GNU Lesser General Public License *
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17+
*******************************************************************************
18+
* Contact information: [email protected] *
19+
******************************************************************************/
20+
21+
#include <SofaPython3/config.h>
22+
23+
#include <regex>
24+
#include <iostream>
25+
26+
#include "PyBindHelper.h"
27+
28+
namespace sofapython3
29+
{
30+
31+
//void PythonMethodDescription::add_entry(const std::string& signature)
32+
//{
33+
// signatures.push_back(signature);
34+
// docstrings.push_back("");
35+
//}
36+
37+
void PythonMethodDescription::parse_docstring(const std::string& name, std::string docstring)
38+
{
39+
std::smatch m;
40+
std::string last;
41+
std::regex e;
42+
43+
if(signatures.size()==0)
44+
return;
45+
46+
// easy path. Take the signature, then consider the remaining as a docstring
47+
if(signatures.size()==1)
48+
{
49+
e = std::regex("("+name+".*)");
50+
if (std::regex_search (docstring, m,e))
51+
{
52+
for (auto x:m)
53+
last = x;
54+
if(signatures[0].size()==0)
55+
signatures[0] = last;
56+
docstrings[0] = m.suffix().str();
57+
}
58+
return;
59+
}
60+
61+
int idx = 0;
62+
e = std::regex("(1\\. )("+name+".*)");
63+
while (std::regex_search (docstring, m,e))
64+
{
65+
for (auto x:m)
66+
last = x;
67+
68+
if(signatures[idx].size()==0)
69+
signatures[idx] = last;
70+
71+
if(idx>0)
72+
docstrings[idx-1] = m.prefix().str();
73+
idx++;
74+
docstring = m.suffix().str();
75+
76+
auto pex = "("+std::to_string(idx+1)+"\\. )("+name+".*)";
77+
e = std::regex(pex);
78+
}
79+
if(!docstring.empty())
80+
docstrings[idx-1] = docstring;
81+
}
82+
83+
const std::string PythonMethodDescription::build_docstring(const std::string& name) const
84+
{
85+
std::stringstream tmp;
86+
if(signatures.size() == 1)
87+
{
88+
tmp << signatures[0];
89+
tmp << docstrings[0];
90+
return tmp.str();
91+
}
92+
else
93+
{
94+
tmp << name <<"(*args, **kwargs)\n";
95+
tmp << "Overloaded function.\n\n";
96+
}
97+
for(unsigned int i=0;i<docstrings.size();i++)
98+
{
99+
tmp << (i+1) << ". " << signatures[i] ;
100+
tmp << docstrings[i] ;
101+
}
102+
return tmp.str();
103+
}
104+
105+
106+
}

bindings/Sofa/package/TypeHints.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
from typing import TypeVar, Generic, Optional as __optional__
3+
import numpy.typing
4+
from Sofa.Core import Node, Object, LinkPath
5+
import numpy
6+
from numpy.typing import ArrayLike
7+
8+
T = TypeVar("T", bound=object)
9+
10+
# This is a generic type 'T' implemented without PEP 695 (as it needs python 3.12)
11+
class Data(Generic[T]):
12+
linkpath: LinkPath
13+
value: T
14+
15+
Optional = __optional__
16+
SofaArray = numpy.ndarray | list
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import typing
2+
3+
class Prefab(object):
4+
def __init_subclass__(cls) -> None:
5+
super().__init_subclass__()
6+
7+
if not hasattr(cls, "__orig_bases__"):
8+
cls.templates = {}
9+
return
10+
11+
12+
args = typing.get_args(cls.__orig_bases__[1])
13+
print("= ==== > ", cls, typing.get_type_hints(cls))
14+
15+
print(" ----------->", typing.get_type_hints(cls))
16+
cls.args = typing.get_type_hints(cls)
17+
cls.templates = None
18+
19+
def get_templates(self):
20+
values = []
21+
if not hasattr(self, "__orig_class__"):
22+
if not hasattr(self, "__orig_bases__"):
23+
return
24+
values += [ variabletype for variabletype in typing.get_args(self.__orig_bases__[-1]) ]
25+
26+
if hasattr(self, "__orig_class__"):
27+
values += typing.get_args(self.__orig_class__)
28+
29+
names = self.__class__.args
30+
print("VALUES.... ", names, values )
31+
if self.__class__.templates is None:
32+
self.__class__.templates = {}
33+
for name, variable in names.items():
34+
value = variable
35+
self.__class__.templates[name] = (value.__default__, type(value))
36+
return self.templates
37+
38+
39+
X = typing.TypeVar("X", default=int)
40+
B = typing.TypeVar("B", default=float)
41+
42+
class AClass(Prefab, typing.Generic[X,B]):
43+
i : X
44+
b : B
45+
pass
46+
47+
class Rigid3:
48+
pass
49+
class Vec3:
50+
pass
51+
52+
Template = typing.TypeVar("Template", default=Rigid3)
53+
Template2 = typing.TypeVar("Template2", default=Vec3)
54+
55+
class MechanicalObject(Prefab, typing.Generic[Template, Template2]):
56+
template : Template
57+
template2 : Template2
58+
59+
positions : list[int]
60+
61+
BClass = AClass[MechanicalObject[Rigid3, Vec3], int]
62+
63+
print(type( AClass ))
64+
65+
print(dir( BClass ))
66+
print(dir(type( BClass )))
67+
68+
print(AClass.__annotations__)
69+
70+
b = BClass()
71+
print("get_origin(BClass)", typing.get_origin(BClass))
72+
print("get_args(BClass)", typing.get_args(BClass))
73+
74+
print("get_origin(self.)", typing.get_origin(b))
75+
print("get_args(self)", typing.get_args(b))
76+
77+
print("get_origin(self.__class__)", typing.get_origin(b.__class__))
78+
print("get_args(self.__class__)", typing.get_args(b.__class__))
79+
80+
print("b ", b)
81+
print("b type", type(b))
82+
print("b ", type(b.__class__))
83+
84+
print(b.__annotations__)
85+
print("getTemplate", MechanicalObject().get_templates())

0 commit comments

Comments
 (0)