I'm working with this library, which (very reasonably because it is translating from another language's idioms) makes heavy use of operator redefinition.
Usage begins with from parsec import *
which in general, I try to avoid, and rather keep my namespace. Something more like import parsec as p
then using the sub functions and every operator, explicitly.
This works for some functions, like p.many1()
or p.spaces()
or p.regex()
.
However, this also leaves me trying to import name-spaced operators, and that looks even less pythonic.
For example, the bitwise >>
and <<
are redefined. Here is a real-world usage.
Not only does trying to use a namespace to call these operators also look unpythonic, it's not even clear to me how to do it: p.>>
? As you can see, this is a syntax error:
>>> import parsec as p
>>> p.>> p.many()
File "<stdin>", line 1
p.>>
^
SyntaxError: invalid syntax
This leaves me deciding I want to import the operators implicitly, that is, to call >>
without a namespace, but to import the non-operators inside a p
namespace.
edit update
To be clear, my question is:
How can I import the functions like many1() with a namespace as p.many1(), and import the operator functions like "+" ">>" "<<" nakedly, without a namespace?
end update
While I can do
import parsec as p
To get the non-operator functions, it's not clear to me how to say:
from parsec import >>
As the following attempts at explicitly importing operators all fail:
>>> from parsec import >>
File "<stdin>", line 1
from parsec import >>
^
SyntaxError: invalid syntax
>>> from parsec import (>>)
File "<stdin>", line 1
from parsec import (>>)
^
SyntaxError: invalid syntax
>>> from parsec import ">>"
File "<stdin>", line 1
from parsec import ">>"
^
SyntaxError: invalid syntax
>>> from parsec import __lshift__, __rshift__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name '__lshift__' from 'parsec' ()
How can I import these?