This week, an assignment on the creation of valid regular expressions was due. Truthfully, the assignment was very easy. The hardest part of the assignment required understanding of the syntax that was used.
The coding for the assignment required a master class, and 4 sub-classes calling to its __init__ method, custom __repr__ method, and custom __eq__ method. Although this was the first time I've ever done this, the process made sense. I had to call the master class's __init__ method from inside the __init__ method of the subclass. Therefore, I saved code and made my process clearer.
The coding for the __repr__ and __eq__ methods were probably the hardest part, but that's not saying much. The __repr__ code required recursion for regexes with and without children, an the __eq__method made sure that the regexes were equal in every way. If they were equal, True would be output. The representation for each regex also looked very clean:
a = leaf_regex(0)
b = leaf_regex(1)
c = bar_regex([a,b])
c
regex('|', [regex('0'), regex('1')])
All in all, the assignment was very straightforward.
No comments:
Post a Comment