Parser and Composer #181
Replies: 6 comments 1 reply
-
I'll describe the composer in it's own post, this seemed long enough |
Beta Was this translation helpful? Give feedback.
-
@edger-asiimwe @kallyas @ushergodwin I think I should self assign myself the task of writing the parser and composer while you guys focus on finalizing with the telegram objects. There's still a lot of work to be done there. |
Beta Was this translation helpful? Give feedback.
-
That's Okay, Incase we are done before you finish up, we can as well as write some |
Beta Was this translation helpful? Give feedback.
-
@kallyas you should help out by adding |
Beta Was this translation helpful? Give feedback.
-
if I understood your point correctly, you mean this class Poll:
__slots__ = ('foo', 'barz')
def __init__(self, foo: User = None) -> None
self.foo = foo
self.barz: SomeType = None |
Beta Was this translation helpful? Give feedback.
-
@kallyas yup, that's it |
Beta Was this translation helpful? Give feedback.
-
So now that we got the necessary objects laid out, we need to work on the system that inter-converts between these objects and the form in which these objects shall be provided to us. So generally our requests and responses shall revolve around deeply nested JSON objects. the JSON objects shall be converted to dictionaries which shall be our mode of converting between JSON and python types.
So basically what we need to do is write a parser that shall receive a request in form of a dictionary and then build out a tree structure using the objects we have. Then we shall also have to build a composer which shall take in the internally generated tree of objects and convert it to a dictionary which shall later be converted into JSON to send back as a response.
I have created a file called parser inside the telegram module. It will have two main classes, that is, Parser and Composer. The work of the Parser class is to house methods used to convert a dictionary into an object tree based on our telegram objects, and the Composer contains methods to convert the object tree back to a dictionary. All the methods of these classes will be private to them, except the parse method for the Parser class and compose method for the Composer class.
Each individual method in the Parser class, except the
parse
method, shall be responsible for parsing all objects in a given module. The method responsible for parsing objects in a given module shall have a name such as _parse_module-name for example_parse_chat
or_parse_proximityalerttriggered
. The methods take in a key and a dictionary describing the object to parse and then they construct the object from the given dictionary keys and values. The key tells the parse method which object to create among the many objects it might be able to parseIf a module contains more than one object, The _parse method should be able to parse all the objects in that module, and should be able to identify the particular object that needs to be parsed from the dictionary provided as the argument. For example, the
poll.py
object containsPoll
,PollOption
andPollAnswer
, so the parse method_parse_poll
needs to identify from the dictionary provided if it needs to create thePoll
object orPollOption
orPollAnswer
and needs to return the created object.the public
parse
method shall be the starting point of the parse and shall return the complete tree. usually it shall take in theupdate
object since this is the one provided for every update, so by default this shall parse the update object and then start the chain parsing mechanismChain parsing.
We shall use chain parsing to build the tree. This means that if the first method is parsing a dictionary and discovers the presence of another object, it shall pass that portion of the dictionary on to the specialized method for parsing that object. And if the other method discovers another object that it cannot parse, it passes it on to the method that is responsible for parsing that object. In that way, we create a chain of methods to parse the object till it's complete. For example
This is not a real object to parse but it just demonstrates the chain parsing mechanism.
corner cases
write now, object instantiation needs non-optional arguments as a must in order to create the object instance. This will complicate the parsing process by requiring the loop to retrieve the non-optional args first before creating the object instance. It's best to remove this restriction by adding
None
as the default value of the required args in order to enable recursive building of the objects and adding properties dynamically.The other corner case is the
from_
attribute in some classes, which corresponds to thefrom
property of telegram objects. This is becausefrom
is a keyword in python and we can't use it for variables, so care must be taken when parsing an object that contains thefrom
attribute, this includes the message object.Beta Was this translation helpful? Give feedback.
All reactions