drmfinlay / pyjsgf

JSpeech Grammar Format (JSGF) compiler, matcher and parser package for Python.
MIT License
53 stars 22 forks source link

Split up the monstrosity that is jsgf/expansions.py #27

Closed drmfinlay closed 4 years ago

drmfinlay commented 5 years ago

As the title says, this project's jsgf/expansions.py file really needs to be split up into multiple files. It is nearly 2000 lines long! This makes it difficult to find where to make changes and I can imagine it is quite intimidating for anyone wanting to contribute.

The main barrier to achieving this is that there is far too much type checking in that file, making it difficult to extract any classes without running into import cycles. I will be adjusting the various methods and functions to use duck typing instead. For instance, using getattr() to check for the referenced_rule property instead of checking if an object is a NamedRuleRef. I believe this is also quicker and more Pythonic.

I should be able to do this and keep it backwards compatible by changing jsgf.expansions into a directory instead and import classes from the new modules in jsgf/expansions/__init__.py. So something like the following:

from .base import Expansion, ChildList
from .functions import (map_expansion, filter_expansion, flat_map_expansion, TraversalOrder,
                        save_current_matches, restore_current_matches)
from .single_child import SingleChildExpansion, OptionalGrouping, Repeat, KleeneStar
from .multi_child import VariableChildExpansion, AlternativeSet, RequiredGrouping, Sequence
from .references import NamedRuleRef, NullRef, VoidRef, RuleRef
from .other import JointTreeContext, Literal
drmfinlay commented 4 years ago

I'm closing this issue now because, on reflection, I think it's a big waste of time. There are more pressing issues for me to work on.