Skip to content

Commit d95f26e

Browse files
README fixes
Co-authored-by: Kurt McKee <contactme@kurtmckee.org>
1 parent 983f4e0 commit d95f26e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Diff for: README.rst

+13-13
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ The ``^`` operator is often mistaken for a exponent operator, not the bitwise
157157
operation that it is in python, so if you want ``3 ^ 2`` to equal ``9``, you can
158158
replace the operator like this:
159159

160-
.. code-block:: python
160+
.. code-block:: pycon
161161
162162
>>> import ast
163163
>>> from simpleeval import safe_power
@@ -200,15 +200,15 @@ If Expressions
200200

201201
You can use python style ``if x then y else z`` type expressions:
202202

203-
.. code-block:: python
203+
.. code-block:: pycon
204204
205205
>>> simple_eval("'equal' if x == y else 'not equal'",
206206
names={"x": 1, "y": 2})
207207
'not equal'
208208
209209
which, of course, can be nested:
210210

211-
.. code-block:: python
211+
.. code-block:: pycon
212212
213213
>>> simple_eval("'a' if 1 == 2 else 'b' if 2 == 3 else 'c'")
214214
'c'
@@ -219,15 +219,15 @@ Functions
219219

220220
You can define functions which you'd like the expresssions to have access to:
221221

222-
.. code-block:: python
222+
.. code-block:: pycon
223223
224224
>>> simple_eval("double(21)", functions={"double": lambda x:x*2})
225225
42
226226
227227
You can define "real" functions to pass in rather than lambdas, of course too,
228228
and even re-name them so that expressions can be shorter
229229

230-
.. code-block:: python
230+
.. code-block:: pycon
231231
232232
>>> def double(x):
233233
return x * 2
@@ -252,7 +252,7 @@ are provided in the ``DEFAULT_FUNCTIONS`` dict:
252252
If you want to provide a list of functions, but want to keep these as well,
253253
then you can do a normal python ``.copy()`` & ``.update``:
254254

255-
.. code-block:: python
255+
.. code-block:: pycon
256256
257257
>>> my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
258258
>>> my_functions.update(
@@ -267,15 +267,15 @@ Names
267267
Sometimes it's useful to have variables available, which in python terminology
268268
are called 'names'.
269269

270-
.. code-block:: python
270+
.. code-block:: pycon
271271
272272
>>> simple_eval("a + b", names={"a": 11, "b": 100})
273273
111
274274
275275
You can also hand the handling of names over to a function, if you prefer:
276276

277277

278-
.. code-block:: python
278+
.. code-block:: pycon
279279
280280
>>> def name_handler(node):
281281
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
288288

289289
In general, when it attempts to find a variable by name, if it cannot find one,
290290
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``
292292
exception. Eg:
293293

294-
.. code-block:: python
294+
.. code-block:: pycon
295295
296296
>>> def name_handler(node):
297297
... if node.id[0] == 'a':
298298
... return 21
299299
... raise NameNotDefined(node.id[0], "Not found")
300300
...
301301
... simple_eval('a + a', names=name_handler, functions={"b": 100})
302-
302+
303303
42
304304
305305
>>> 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
321321
time (which should be a bit quicker, and certainly more convenient for some use
322322
cases):
323323

324-
.. code-block:: python
324+
.. code-block:: pycon
325325
326326
>>> s = SimpleEval()
327327
@@ -400,7 +400,7 @@ comprehensions.
400400
Since the primary intention of this library is short expressions - an extra 'sweetener' is
401401
enabled by default. You can access a dict (or similar's) keys using the .attr syntax:
402402

403-
.. code-block:: python
403+
.. code-block:: pycon
404404
405405
>>> simple_eval("foo.bar", names={"foo": {"bar": 42}})
406406
42

0 commit comments

Comments
 (0)