Hi @izzylys,
Over the last weeks I have been testing pyspeckle 2.0. I have a server running locally to send and receive some objects. This week I tried to set up some very basic classes that inherit from the Base object. As a structural engineer, I start of course with making a SpeckleBeam class, which inherits from the SpeckleLine class, see below.
Part of the problem is that I’m not exactly sure on how to subclass Base, which in turn inherits from the Pydantic BaseModel. To me it seems that the use of this Pydantic class brings quite some limitations in making the subclasses (this is at least my first impression, but might be my own lack of experience with it ). I tried to formulate some clear questions:
- Is it possible to create a read-only attribute/getter? I tried the “const” parameter of Field to achieve this, but that didn’t work so far. Currently that means that I can change the “speckle_type” attribute of the objects to anything at anytime, which is not desirable. My intention is to override the “speckle_type” attribute from Base in each class to have a constant value for each object, e.g. “SpeckleBeam”. In pyspeckle 1.0, we could simply use the property decorator to define a getter, and reference that in a dataclass field, as shown here:
Trying to do that now raises an error when it’s trying to initiate the SpeckleLine class, as it cannot deepcopy a MappingProxy object within the field.
- How should I properly nest an object in another one? Below you can see what I did currently, the SpeckleLine is nested in the baseLine attribute of the SpeckleBeam. Afterwards I serialize both items and commit them using their hashes.
Subsequently I try to retrieve these two items back from my stream. This actually succeeds using the hashes. However, both are Base objects which are more or less empty/default, they do not contain any of the attributes I assigned to them. Am I doing something wrong in the serialization?
- Then another remarkable thing when I retrieve my two objects back. For some reason it expects that there are “speckleType” attributes instead of “speckle_type”. It seems that at some point it converts the “speckle_type” attributes to camelCase.
Those are the main issues I’m currently experiencing as an early tester. Would be nice to hear your thoughts on (subclassing) the Pydantic Base and the serialization as shown above. Looking forward to your answer! I’m also very interested in the experiences of others using pyspeckle 2.0