##// END OF EJS Templates
support for unicode identifiers...
support for unicode identifiers This rewrites some of the regular expressions that are used to match Python identifiers, so that they are unicode compatible. In Python 3, identifiers can contain unicode characters as long as the first character is not numeric. Examples for the changes: • inputtransformer: ``` In [1]: π = 3.14 In [2]: π.is_integer? Object `is_integer` not found. ``` ---------- • namespace: ``` π.is_integ*? ``` or ``` In [1]: %psearch π.is_integ Python identifiers can only contain ascii characters. ``` ---------- • prefilter: ``` %autocall 1 φ = float get_ipython().prefilter("φ 3") # should be 'φ(3)', but returns 'φ 3' ``` ---------- • completerlib: If there is a file e.g. named `π.py` in the current directory, then ``` import IPython IPython.core.completerlib.module_list('.') # should contain module 'π' ```

File last commit:

r13392:bace1f46
r25595:d9c0e690
Show More
strdispatch.py
68 lines | 1.8 KiB | text/x-python | PythonLexer
"""String dispatch class to match regexps and dispatch commands.
"""
# Stdlib imports
import re
# Our own modules
from IPython.core.hooks import CommandChainDispatcher
# Code begins
class StrDispatch(object):
"""Dispatch (lookup) a set of strings / regexps for match.
Example:
>>> dis = StrDispatch()
>>> dis.add_s('hei',34, priority = 4)
>>> dis.add_s('hei',123, priority = 2)
>>> dis.add_re('h.i', 686)
>>> print(list(dis.flat_matches('hei')))
[123, 34, 686]
"""
def __init__(self):
self.strs = {}
self.regexs = {}
def add_s(self, s, obj, priority= 0 ):
""" Adds a target 'string' for dispatching """
chain = self.strs.get(s, CommandChainDispatcher())
chain.add(obj,priority)
self.strs[s] = chain
def add_re(self, regex, obj, priority= 0 ):
""" Adds a target regexp for dispatching """
chain = self.regexs.get(regex, CommandChainDispatcher())
chain.add(obj,priority)
self.regexs[regex] = chain
def dispatch(self, key):
""" Get a seq of Commandchain objects that match key """
if key in self.strs:
yield self.strs[key]
for r, obj in self.regexs.items():
if re.match(r, key):
yield obj
else:
#print "nomatch",key # dbg
pass
def __repr__(self):
return "<Strdispatch %s, %s>" % (self.strs, self.regexs)
def s_matches(self, key):
if key not in self.strs:
return
for el in self.strs[key]:
yield el[1]
def flat_matches(self, key):
""" Yield all 'value' targets, without priority """
for val in self.dispatch(key):
for el in val:
yield el[1] # only value, no priority
return