On 05/06/2024 13:19, Juraj Linkeš wrote:
+        return cls.compose(partial(int, base=int_base), cls.find(pattern))
+
+    """============ END PARSER FUNCTIONS ============"""
+
+    @classmethod
+    def parse(cls, text: str) -> Self:

Would converting this into __init__(self, text: str) work? Sounds like we could just use "for field in fields(self)" and then setattr() to populate the variables.

I am not in favour of overriding the constructor, as I see the parsing ability as an extension to the class.

Well, the class should primarily do the parsing (based on its name), so everything else is an extension in my mind >> Nonetheless, this would make sense if we think that this would be used
exclusively for objects that need to be parsed in order to be constructed. I purposefully left the flexibility open, but if we don't think it'll ever be needed, then it's not a game changer to me.

Everything about the class indicates (or outright says) that parsing should be the only purpose. I can't really imagine what else we could add (in this state, an instance before calling parse() would be just the text with fields containing function in the metadata needed for parsing - what else is there to do with this data?). Can you elaborate if you can think of something?

And again, that said, it doesn't matter much, but I like the constructor as it really doesn't look like the class could (or should) do anything else than parsing text.

I think there is some confusion going on. The class is any data object like TestPmdPort. TextParser is an implementation of a parsing functionality, a Protocol/interface with a pre-implemented functionality, which can be added as an extension. The only way this would make sense is if these data objects are actually "text representation to object adapters". If we are exclusively interpreting them as such, then yes, it would totally make sense to bring a custom constructor. But if we will need to re-use the objects for other reasons aside from parsing, then we may have to revert to the current implementation.

Reply via email to