utils
ParamToken §
first_nonequal_idx
cached
§
first_nonequal_idx(left, right)
Find first string index where left and right strings do not match
In [1]: first_nonequal_idx("", "californian")
Out[1]: 0
In [2]: first_nonequal_idx("aba", "abc")
Out[2]: 2
Note, if the strings match, the first-non-equal index will be equal to the length of the string:
In [3]: first_nonequal_idx("aba", "aba")
Out[3]: 3
parse_dynamic §
parse_dynamic(path)
This method discerns the dynamic elements in a string.
There are some assumptions involved
- This will be used in URI paths, so regexes will be for typically allowed URI characters
- A dynamic element always begins with the char
{
and ends with the char}
- A colon separates a parameter name from a regex, which are optional.
- It is possible to include nested braces as part of a regex, but they must be matched:
- This is fine:
{name:[a-zA-Z]{10}}
- This (a valid regex) will raise an exception:
{code:[}{}]}
- We may later construct a named pattern out of this, so any parenthesis in the pattern will likely break.
Returns a generator that yields in turn, static strings and DynamicParseNodes:
test = "/company/{name:[a-z][A-Z]{10}[0-9]*}/bla/bla/{dept}" list(parse_dynamic(test)) ['/company/',
, '/bla/bla/', ]