3 ms·
I also think the naming is really bad and can be improved. The central problem is that you're using action words for a descriptive API. You should use something
by Asooka 9y ago
I also think the naming is really bad and can be improved. The central problem is that you're using action words for a descriptive API. You should use something like "Repeating" instead of "Times". Also, the parameters to Times are distinctly un-pythonic. I would probably make it provide this API:
def Repeating(pattern, *args, min=None, max=None, exact=None):...
Repeating(P) => P*
Repeating(P, a, b) and
Repeating(P, min=a, max=b) => P{a, b}
Repeating(P, n) and
Repeating(P, exact=n) => P{n, n}
Repeating(P, min=n) => P{n,}
Repeating(P, max=n) => P{0, n}
"Exclude" and "Include" suffer from the aforementioned problem of being procedural words for a declarative syntax. And they're easy to confuse with one another, having only two differing letters. And they don't make it clear that they match exactly one character. I would use "AnyBut" and "OneOf".
Why the split between Group and NamedGroup? Just add a name parameter to Group!
Seq as mentioned by parent is also problematic. Use Range or maybe Between or FromTo. To me FromTo flows the best, but Range is also ok.
I'm tempted to advocate adding operator overloading, so | would make a pattern that matches any one of two alternatives, * would be used for specifying repeats, etc. So you could write
Digit = FromTo('0', '9')
CapitalLetter = FromTo('A', 'Z')
POCode = CapitalLetter * 2 + Digit * 3
SmallLetter = FromTo('a', 'z')
Letter = CapitalLetter or SmallLetter
Name = CapitalLetter + Letter * (0, Inf)
FirstName = Group(name='first', Name)
LastName = Group(name='last', Name)
FullName = FirstName + ' ' + LastName
However, implemented badly that can hurt readability much more than having to write functions everywhere.
- masklinn 9y ago> I'm tempted to advocate adding operator overloading, so | would make a pattern that matches any one of two alternatives, * would be used for specifying repeats, etc. So you could write For repeat I think using indexing would make more sense though it might look somewhat unusual: Pattern[:] would be * Pattern[1:] would be + aka {1,} Pattern[5] would be {5} Pattern[m:n] would be {m,n} It also uses a single operator for all repetition operations which is nice.
- deleted 9y ago[deleted]
- iogf 9y agoWhen i thought of Include and Exclude i had in mind it as a shorthand for IncludeFromTo and ExcludeFromTo. About the Repeating/Repeat it could be just an idiomatic way of writing Include which stands for just one char thats fetched from a given sequence.