4
4
This module defines a Boolean algebra over the set {TRUE, FALSE} with boolean
5
5
variables called Symbols and the boolean functions AND, OR, NOT.
6
6
7
- Some basic logic comparison are supported: Two expressions can be compared for
8
- equivalence or containment. Furthermore you can simplify an expression and
9
- obtain its normal form.
7
+ Some basic logic comparison is supported: two expressions can be
8
+ compared for equivalence or containment. Furthermore you can simplify
9
+ an expression and obtain its normal form.
10
10
11
- You can create expressions in Python using familiar boolean operators or parse
12
- expressions from strings. The parsing`easy to extend with your own tokenizer.
13
- You can also subclass some classes to customize how expressions behave and are
14
- presented.
11
+ You can create expressions in Python using familiar boolean operators
12
+ or parse expressions from strings. The parsing can be extended with
13
+ your own tokenizer. You can also customize how expressions behave and
14
+ how they are presented.
15
15
16
16
For extensive documentation look either into the docs directory or view it
17
17
online, at https://booleanpy.readthedocs.org/en/latest/.
57
57
}
58
58
59
59
60
- # parsing errors code and messages
60
+ # parsing error code and messages
61
61
PARSE_UNKNOWN_TOKEN = 1
62
62
PARSE_UNBALANCED_CLOSING_PARENS = 2
63
63
PARSE_INVALID_EXPRESSION = 3
@@ -149,11 +149,9 @@ def __init__(self, TRUE_class=None, FALSE_class=None, Symbol_class=None,
149
149
150
150
def _wrap_type (self , base_class ):
151
151
"""
152
- Return a new type wrapping the base class using the base class name as
153
- wrapped type name.
152
+ Wrap the base class using its name as the name of the new type
154
153
"""
155
- wrapped_type = type (base_class .__name__ , (base_class ,), {})
156
- return wrapped_type
154
+ return type (base_class .__name__ , (base_class ,), {})
157
155
158
156
def _cross_refs (self , objects ):
159
157
"""
@@ -215,9 +213,9 @@ def is_sym(_t):
215
213
return _t == TOKEN_SYMBOL or isinstance (_t , Symbol )
216
214
217
215
prev = None
218
- for tok in tokenized :
219
- if TRACE_PARSE : print ( ' \n processing token:' , repr ( tok ))
220
- token , tokstr , position = tok
216
+ for token , tokstr , position in tokenized :
217
+ if TRACE_PARSE :
218
+ print ( ' \n processing token:' , repr ( token ), repr ( tokstr ), repr ( position ))
221
219
222
220
if prev :
223
221
prev_token , _ , _ = prev
@@ -226,11 +224,13 @@ def is_sym(_t):
226
224
227
225
if token == TOKEN_SYMBOL :
228
226
ast .append (self .Symbol (tokstr ))
229
- if TRACE_PARSE : print (' ast: token == TOKEN_SYMBOL: append new symbol' , repr (ast ))
227
+ if TRACE_PARSE :
228
+ print (' ast: token == TOKEN_SYMBOL: append new symbol' , repr (ast ))
230
229
231
230
elif isinstance (token , Symbol ):
232
231
ast .append (token )
233
- if TRACE_PARSE : print (' ast: isinstance(token, Symbol): append existing symbol' , repr (ast ))
232
+ if TRACE_PARSE :
233
+ print (' ast: isinstance(token, Symbol): append existing symbol' , repr (ast ))
234
234
235
235
elif token == TOKEN_TRUE :
236
236
ast .append (self .TRUE )
@@ -288,7 +288,7 @@ def is_sym(_t):
288
288
else :
289
289
raise ParseError (token , tokstr , position , PARSE_UNKNOWN_TOKEN )
290
290
291
- prev = tok
291
+ prev = ( token , tokstr , position )
292
292
293
293
try :
294
294
while True :
@@ -363,7 +363,7 @@ def tokenize(self, expr):
363
363
unicode string.
364
364
365
365
This 3-tuple contains (token, token string, position):
366
- - token: either a Symbol instance or one of TOKEN_* token types..
366
+ - token: either a Symbol instance or one of TOKEN_* token types.
367
367
- token string: the original token unicode string.
368
368
- position: some simple object describing the starting position of the
369
369
original token string in the `expr` string. It can be an int for a
@@ -420,8 +420,8 @@ def tokenize(self, expr):
420
420
'false' : TOKEN_FALSE , '0' : TOKEN_FALSE , 'none' : TOKEN_FALSE
421
421
}
422
422
423
- length = len (expr )
424
- position = 0
423
+ position , length = 0 , len (expr )
424
+
425
425
while position < length :
426
426
tok = expr [position ]
427
427
0 commit comments