Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernpython #18

Merged
merged 3 commits into from
Sep 20, 2023
Merged

Modernpython #18

merged 3 commits into from
Sep 20, 2023

Conversation

guillaume-alliander
Copy link
Collaborator

@guillaume-alliander guillaume-alliander commented Aug 2, 2023

Adding Python 3.9+ langpack, with pydantic dataclasses.

Those classes cannot be used directly with cimpy, but they do simplify quite a lot of coding. The class tree crawling is built into Base, profiles to which an attribute belongs are added to the attribute definition itself, json dump/parsing is built in.

Sonarcloud complains about code smells - those are logical, but fixing those would cause discrepancy with the style of the rest of the code, so I leave them as is for now. If that's not the best option, let me know.

Signed-off-by: Guillaume Roger <[email protected]>
Signed-off-by: Guillaume Roger <[email protected]>
Signed-off-by: Guillaume Roger <[email protected]>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@chicco785
Copy link
Collaborator

@guillaume-alliander interesting work!

I am working on something similar (albeit I am not using dataclass but BaseModel), and found out a few issues with recursive models across multiple files, which lead me to generate a single file:

zaphiro-technologies#5

did you test the generated code?

happy to help :)

# the default value is either None or [] depending on the multiplicity. See also write_python_files
# The default will be copied as-is, hence the possibility to have default or default_factory.
if result in ["M:1", "M:0..1", "M:1..1", ""]:
return ("Optional[str]", "default=None")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If think I understand why you don't have recursion errors: you are not using other dataclassess as attribute type.

Albeit this avoids any recursion and make validation faster, it does not exploit the full power of pydantic, and makes difficult to navigate the data.

@guillaume-alliander
Copy link
Collaborator Author

@chicco785

I based this a lot on the current python serialization. As the recursion aspect was not there, it's not present in my classes :)

You are correct that I do not use other dataclasses as attributes. It would be nice indeed. I am currently heavily using the generated classes, and adding dataclasses as possible attributes could have very interesting aspects. My final goal would be to load the graph in rdflib (to export to xml, json-ld, ttl... for free), and that could help.

@chicco785
Copy link
Collaborator

chicco785 commented Aug 3, 2023

@chicco785

I based this a lot on the current python serialization. As the recursion aspect was not there, it's not present in my classes :)

to keep simple and the parse easy, it may make completely sense! it is just more complex to navigate a linked object (you will have to search it by id through the full list of objects)

You are correct that I do not use other dataclasses as attributes. It would be nice indeed. I am currently heavily using the generated classes, and adding dataclasses as possible attributes could have very interesting aspects. My final goal would be to load the graph in rdflib (to export to xml, json-ld, ttl... for free), and that could help.

of course we want also to be able to import and export to different format, plus ideally make easy to manipulate / filter the objects via APIs (that's why we have been looking into pydantic and we tested the recursion support), beyond that, for us, also storage in a database (so adding also SQL Alchemy support) is an interesting feature.

Maybe we can have a short call, exchange ideas, and see if there is a chance for a collaboration?

@guillaume-alliander
Copy link
Collaborator Author

Maybe we can have a short call, exchange ideas, and see if there is a chance for a collaboration?

Sure, that sounds great! To avoid sharing publicly email addresses, I found you on LinkedIn and will send a message here.

@guillaume-alliander
Copy link
Collaborator Author

@iripiri Would it be possible to get a review for this PR?

More updates here will come, but this PR is a nicely self-contained.

@iripiri
Copy link
Contributor

iripiri commented Sep 20, 2023

Sorry for the delay, the changes look good to me.
Thanks for your efforts!

Copy link
Contributor

@iripiri iripiri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iripiri iripiri merged commit 47a8eda into master Sep 20, 2023
@iripiri iripiri deleted the modernpython branch September 20, 2023 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants