diff options
author | ptmcg <ptmcg@austin.rr.com> | 2018-10-27 14:22:56 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2018-10-27 14:22:56 -0500 |
commit | 04112e0654cc4c183aba876fc957b1f56ad56430 (patch) | |
tree | e08605028731b8ba0888b86ed716b2b03196a1b8 | |
parent | 39d8f4f4911b59e1dbfb15cc5793c8b3a449c29f (diff) | |
download | pyparsing-git-04112e0654cc4c183aba876fc957b1f56ad56430.tar.gz |
Add Py2 unicodeisms
-rw-r--r-- | pyparsing.py | 13 | ||||
-rw-r--r-- | unitTests.py | 4 |
2 files changed, 11 insertions, 6 deletions
diff --git a/pyparsing.py b/pyparsing.py index 0914788..61261e6 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -91,7 +91,11 @@ import pprint import traceback
import types
from datetime import datetime
-from itertools import filterfalse
+try:
+ # Python 3
+ from itertools import filterfalse
+except ImportError:
+ from itertools import ifilterfalse as filterfalse
try:
from _thread import RLock
@@ -144,6 +148,7 @@ if PY_3: _MAX_INT = sys.maxsize
basestring = str
unichr = chr
+ unicode = str
_ustr = str
# build list of single arg builtins, that can be used as parse actions
@@ -5838,15 +5843,15 @@ class _unicode_set: @_lazyclassproperty
def printables(cls):
- return ''.join(filterfalse(str.isspace, (chr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
+ return ''.join(filterfalse(unicode.isspace, (unichr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
@_lazyclassproperty
def alphas(cls):
- return ''.join(filter(str.isalpha, (chr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
+ return ''.join(filter(unicode.isalpha, (unichr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
@_lazyclassproperty
def nums(cls):
- return ''.join(filter(str.isdigit, (chr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
+ return ''.join(filter(unicode.isdigit, (unichr(c) for r in cls._ranges for c in range(r[0], r[-1] + 1))))
@_lazyclassproperty
def alphanums(cls):
diff --git a/unitTests.py b/unitTests.py index cc4db6f..a32f6dc 100644 --- a/unitTests.py +++ b/unitTests.py @@ -3628,10 +3628,10 @@ class UnicodeTests(ParseTestCase): greet = pp.Word(alphas) + ',' + pp.Word(alphas) + '!'
# input string
- hello = "Καλημέρα, κόσμε!"
+ hello = u"Καλημέρα, κόσμε!"
result = greet.parseString(hello)
print_(result)
- self.assertTrue(result.asList() == ['Καλημέρα', ',', 'κόσμε', '!'],
+ self.assertTrue(result.asList() == [u'Καλημέρα', ',', u'κόσμε', '!'],
"Failed to parse Greek 'Hello, World!' using pyparsing_unicode.Greek.alphas")
class MiscellaneousParserTests(ParseTestCase):
|