Skip to content

Commit

Permalink
Merge pull request #144 from danthedeckie/1.0.0
Browse files Browse the repository at this point in the history
1.0.0 - Time for a new updated release, and call this iteration stable.
  • Loading branch information
danthedeckie authored Oct 5, 2024
2 parents 2a12b58 + 0fe45bb commit f046223
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 105 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Codecov
name: CI
on: [push]
jobs:
lint:
Expand All @@ -18,7 +18,14 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python: ['2.7', '3.7', '3.8', '3.9', '3.10', '3.11', '3.12-dev', 'pypy-3.8']
python:
- '3.9'
- '3.10'
- '3.11'
- '3.12'
- '3.13'
- 'pypy-3.9'
- 'pypy-3.10'
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python }}
Expand All @@ -29,13 +36,16 @@ jobs:
uses: actions/setup-python@master
with:
python-version: ${{ matrix.python }}
allow-prereleases: true

- name: Generate Report
run: |
pip install coverage
coverage run -m test_simpleeval
coverage xml
- name: Upload to codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v4.0.1
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
simpleeval - Copyright (c) 2013-2017 Daniel Fairhead
simpleeval - Copyright (c) 2013-2024 Daniel Fairhead

(MIT Licence)

Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
python test_simpleeval.py
python -Werror test_simpleeval.py

autotest:
find . -name \*.py -not -path .\/.v\* | entr make test
Expand All @@ -22,11 +22,10 @@ coverage:
coverage run test_simpleeval.py

lint:
black --check --diff simpleeval.py test_simpleeval.py
isort --check-only --diff simpleeval.py test_simpleeval.py
pylint simpleeval.py test_simpleeval.py
ruff check simpleeval.py test_simpleeval.py
ruff format --check simpleeval.py test_simpleeval.py
mypy simpleeval.py test_simpleeval.py

format:
black simpleeval.py test_simpleeval.py
isort simpleeval.py test_simpleeval.py
ruff check --fix-only simpleeval.py test_simpleeval.py
ruff format simpleeval.py test_simpleeval.py
75 changes: 52 additions & 23 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
simpleeval (Simple Eval)
========================

.. image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=gh-actions-build
.. |build-status| image:: https://github.com/danthedeckie/simpleeval/actions/workflows/ci.yml/badge.svg?branch=gh-actions-build
:target: https://github.com/danthedeckie/simpleeval/actions/
:alt: Build Status

.. image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/master/graph/badge.svg?token=isRnN1yrca
.. |code-coverage| image:: https://codecov.io/gh/danthedeckie/simpleeval/branch/master/graph/badge.svg?token=isRnN1yrca
:target: https://codecov.io/gh/danthedeckie/simpleeval
:alt: Code Coverage
:alt: Code Coverage Status

.. image:: https://badge.fury.io/py/simpleeval.svg
.. |pypi-version| image:: https://badge.fury.io/py/simpleeval.svg
:target: https://badge.fury.io/py/simpleeval
:alt: PyPI Version

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
.. |python-versions| image:: https://img.shields.io/badge/python-3.9_%7C_3.10_%7C_3.11_%7C_3.12_%7C_3.13_%7C_PyPy3.9_%7C_PyPy3.10-blue
:alt: Static Badge

.. image:: https://img.shields.io/badge/linting-pylint-yellowgreen
:target: https://github.com/PyCQA/pylint
.. |pypi-monthly-downloads| image:: https://img.shields.io/pypi/dm/SimpleEval
:alt: PyPI - Downloads

.. |formatting-with-ruff| image:: https://img.shields.io/badge/-ruff-black?logo=lightning&logoColor=%2300ff00&link=https%3A%2F%2Fdocs.astral.sh%2Fruff%2F
:alt: Static Badge

|build-status| |code-coverage| |pypi-version| |python-versions| |pypi-monthly-downloads| |formatting-with-ruff|


A single file library for easily adding evaluatable expressions into
Expand Down Expand Up @@ -157,7 +162,7 @@ The ``^`` operator is often mistaken for a exponent operator, not the bitwise
operation that it is in python, so if you want ``3 ^ 2`` to equal ``9``, you can
replace the operator like this:

.. code-block:: python
.. code-block:: pycon
>>> import ast
>>> from simpleeval import safe_power
Expand Down Expand Up @@ -200,15 +205,15 @@ If Expressions

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

.. code-block:: python
.. code-block:: pycon
>>> simple_eval("'equal' if x == y else 'not equal'",
names={"x": 1, "y": 2})
'not equal'
which, of course, can be nested:

.. code-block:: python
.. code-block:: pycon
>>> simple_eval("'a' if 1 == 2 else 'b' if 2 == 3 else 'c'")
'c'
Expand All @@ -219,15 +224,15 @@ Functions

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

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

.. code-block:: python
.. code-block:: pycon
>>> def double(x):
return x * 2
Expand All @@ -246,13 +251,13 @@ are provided in the ``DEFAULT_FUNCTIONS`` dict:
+----------------+--------------------------------------------------+
| ``float(x)`` | Convert ``x`` to a ``float``. |
+----------------+--------------------------------------------------+
| ``str(x)`` | Convert ``x`` to a ``str`` (``unicode`` in py2) |
| ``str(x)`` | Convert ``x`` to a ``str`` |
+----------------+--------------------------------------------------+

If you want to provide a list of functions, but want to keep these as well,
then you can do a normal python ``.copy()`` & ``.update``:

.. code-block:: python
.. code-block:: pycon
>>> my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
>>> my_functions.update(
Expand All @@ -267,15 +272,15 @@ Names
Sometimes it's useful to have variables available, which in python terminology
are called 'names'.

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


.. code-block:: python
.. code-block:: pycon
>>> def name_handler(node):
return ord(node.id[0].lower(a))-96
Expand All @@ -284,9 +289,34 @@ You can also hand the handling of names over to a function, if you prefer:
3
That was a bit of a silly example, but you could use this for pulling values
from a database or file, say, or doing some kind of caching system.
from a database or file, looking up spreadsheet cells, say, or doing some kind of caching system.

The two default names that are provided are ``True`` and ``False``. So if you want to provide your own names, but want ``True`` and ``False`` to keep working, either provide them yourself, or ``.copy()`` and ``.update`` the ``DEFAULT_NAMES``. (See functions example above).
In general, when it attempts to find a variable by name, if it cannot find one,
then it will look in the ``functions`` for a function of that name. If you want your name handler
function to return an "I can't find that name!", then it should raise a ``simpleeval.NameNotDefined``
exception. Eg:

.. code-block:: pycon
>>> def name_handler(node):
... if node.id[0] == 'a':
... return 21
... raise NameNotDefined(node.id[0], "Not found")
...
... simple_eval('a + a', names=name_handler, functions={"b": 100})
42
>>> simple_eval('a + b', names=name_handler, functions={'b': 100})
121
(Note: in that example, putting a number directly into the ``functions`` dict was done just to
show the fall-back to functions. Normally only put actual callables in there.)


The two default names that are provided are ``True`` and ``False``. So if you want to provide
your own names, but want ``True`` and ``False`` to keep working, either provide them yourself,
or ``.copy()`` and ``.update`` the ``DEFAULT_NAMES``. (See functions example above).

Creating an Evaluator Class
---------------------------
Expand All @@ -296,7 +326,7 @@ evaluations, you can create a SimpleEval object, and pass it expressions each
time (which should be a bit quicker, and certainly more convenient for some use
cases):

.. code-block:: python
.. code-block:: pycon
>>> s = SimpleEval()
Expand Down Expand Up @@ -375,7 +405,7 @@ comprehensions.
Since the primary intention of this library is short expressions - an extra 'sweetener' is
enabled by default. You can access a dict (or similar's) keys using the .attr syntax:

.. code-block:: python
.. code-block:: pycon
>>> simple_eval("foo.bar", names={"foo": {"bar": 42}})
42
Expand Down Expand Up @@ -405,8 +435,7 @@ and then use ``EvalNoMethods`` instead of the ``SimpleEval`` class.
Other...
--------

The library supports python 3 - but should be mostly compatible (and tested before 0.9.11)
with python 2.7 as well.
The library supports Python 3.9 and higher.

Object attributes that start with ``_`` or ``func_`` are disallowed by default.
If you really need that (BE CAREFUL!), then modify the module global
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ build-backend = "setuptools.build_meta"
line-length = 99
target-version = ['py310']

[tool.ruff]
line-length = 99

[tool.isort]
combine_as_imports = true
float_to_top = true
Expand Down
6 changes: 2 additions & 4 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
black==23.1.0
isort==5.10.1
pylint==2.12.2
mypy==0.931
ruff==0.6.9
mypy==1.11.2
9 changes: 4 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
[metadata]
name = simpleeval
version = 0.9.13
version = 1.0.0
author = Daniel Fairhead
author_email = danthedeckie@gmail.com
description = A simple, safe single expression evaluator library.
keywords = eval, simple, expression, parse, ast
url = https://github.com/danthedeckie/simpleeval
license = MIT
long_description = file: README.rst
long_description_content_type = text/x-rst
classifiers =
Development Status :: 4 - Beta
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Topic :: Software Development :: Libraries :: Python Modules
Programming Language :: Python

[options]
py_modules = simpleeval

[bdist_wheel]
universal=1
python_requires = >=3.9

[pycodestyle]
max_line_length = 99
Loading

0 comments on commit f046223

Please sign in to comment.