@@ -157,7 +157,7 @@ The ``^`` operator is often mistaken for a exponent operator, not the bitwise
157
157
operation that it is in python, so if you want ``3 ^ 2 `` to equal ``9 ``, you can
158
158
replace the operator like this:
159
159
160
- .. code-block :: python
160
+ .. code-block :: pycon
161
161
162
162
>>> import ast
163
163
>>> from simpleeval import safe_power
@@ -200,15 +200,15 @@ If Expressions
200
200
201
201
You can use python style ``if x then y else z `` type expressions:
202
202
203
- .. code-block :: python
203
+ .. code-block :: pycon
204
204
205
205
>>> simple_eval("'equal' if x == y else 'not equal'",
206
206
names={"x": 1, "y": 2})
207
207
'not equal'
208
208
209
209
which, of course, can be nested:
210
210
211
- .. code-block :: python
211
+ .. code-block :: pycon
212
212
213
213
>>> simple_eval("'a' if 1 == 2 else 'b' if 2 == 3 else 'c'")
214
214
'c'
@@ -219,15 +219,15 @@ Functions
219
219
220
220
You can define functions which you'd like the expresssions to have access to:
221
221
222
- .. code-block :: python
222
+ .. code-block :: pycon
223
223
224
224
>>> simple_eval("double(21)", functions={"double": lambda x:x*2})
225
225
42
226
226
227
227
You can define "real" functions to pass in rather than lambdas, of course too,
228
228
and even re-name them so that expressions can be shorter
229
229
230
- .. code-block :: python
230
+ .. code-block :: pycon
231
231
232
232
>>> def double(x):
233
233
return x * 2
@@ -252,7 +252,7 @@ are provided in the ``DEFAULT_FUNCTIONS`` dict:
252
252
If you want to provide a list of functions, but want to keep these as well,
253
253
then you can do a normal python ``.copy() `` & ``.update ``:
254
254
255
- .. code-block :: python
255
+ .. code-block :: pycon
256
256
257
257
>>> my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
258
258
>>> my_functions.update(
@@ -267,15 +267,15 @@ Names
267
267
Sometimes it's useful to have variables available, which in python terminology
268
268
are called 'names'.
269
269
270
- .. code-block :: python
270
+ .. code-block :: pycon
271
271
272
272
>>> simple_eval("a + b", names={"a": 11, "b": 100})
273
273
111
274
274
275
275
You can also hand the handling of names over to a function, if you prefer:
276
276
277
277
278
- .. code-block :: python
278
+ .. code-block :: pycon
279
279
280
280
>>> def name_handler(node):
281
281
return ord(node.id[0].lower(a))-96
@@ -288,18 +288,18 @@ from a database or file, looking up spreadsheet cells, say, or doing some kind o
288
288
289
289
In general, when it attempts to find a variable by name, if it cannot find one,
290
290
then it will look in the ``functions `` for a function of that name. If you want your name handler
291
- function to return a "I can't find that name!", then it should raise a ``simpleeval.NameNotDefined ``
291
+ function to return an "I can't find that name!", then it should raise a ``simpleeval.NameNotDefined ``
292
292
exception. Eg:
293
293
294
- .. code-block :: python
294
+ .. code-block :: pycon
295
295
296
296
>>> def name_handler(node):
297
297
... if node.id[0] == 'a':
298
298
... return 21
299
299
... raise NameNotDefined(node.id[0], "Not found")
300
300
...
301
301
... simple_eval('a + a', names=name_handler, functions={"b": 100})
302
-
302
+
303
303
42
304
304
305
305
>>> simple_eval('a + b', names=name_handler, functions={'b': 100})
@@ -321,7 +321,7 @@ evaluations, you can create a SimpleEval object, and pass it expressions each
321
321
time (which should be a bit quicker, and certainly more convenient for some use
322
322
cases):
323
323
324
- .. code-block :: python
324
+ .. code-block :: pycon
325
325
326
326
>>> s = SimpleEval()
327
327
@@ -400,7 +400,7 @@ comprehensions.
400
400
Since the primary intention of this library is short expressions - an extra 'sweetener' is
401
401
enabled by default. You can access a dict (or similar's) keys using the .attr syntax:
402
402
403
- .. code-block :: python
403
+ .. code-block :: pycon
404
404
405
405
>>> simple_eval("foo.bar", names={"foo": {"bar": 42}})
406
406
42
0 commit comments