{ "info": { "author": "Jakub Ska\u0142ecki", "author_email": "jakub.skalecki@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "=====================\nPython Business Logic\n=====================\n\n.. image:: https://badge.fury.io/py/python-business-logic.svg\n :target: https://badge.fury.io/py/python-business-logic\n\n.. image:: https://travis-ci.org/Valian/python-business-logic.svg?branch=master\n :target: https://travis-ci.org/Valian/python-business-logic\n\n.. image:: https://codecov.io/gh/Valian/python-business-logic/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/Valian/python-business-logic\n\n.. image:: https://readthedocs.org/projects/python-business-logic/badge/?version=latest\n :target: http://python-business-logic.readthedocs.io\n\n\nTraditionally, most web applications are written using MVC pattern. Python Business Logic helps you to add *Business Layer*, also called *Application Layer*, that is dependent only on models and composed of simple functions. Code written this way is extremely easy to read, test, and use in different scenarios. Package has no dependencies and can be used in any web framework, like Django, Flask, Bottle and others.\n\nDocumentation\n-------------\n\nThe full documentation is at https://python-business-logic.readthedocs.io. Still under development. \n\nInstallation\n------------\n\nInstall Python Business Logic::\n\n pip install python-business-logic\n\n\nGetting Started\n---------------\n\nCore elements of library are validators, functions created to ensure that business logic is correct:\n\n.. code:: python\n\n >>> from business_logic.core import validator\n\n >>> @validator\n ... def can_watch_movie(user, movie):\n ... # some example business logic, it can be as complex as you want\n ... return user.is_parent or user.age >= movie.age_restriction\n\n\n\nWith validators you can decorate actions performed that will be checked against that validator:\n\n.. code:: python\n\n >>> from business_logic.core import validated_by\n\n >>> @validated_by(can_watch_movie)\n ... def watch_movie(user, movie):\n ... print(\"'{}' is watching movie '{}'\".format(user.name, movie.name))\n\n\n\nAs you can see, arguments to validator must match those passed to function.\nNow every call to :code:`watch_movie` will require that validator :code:`can_watch_movie` passes:\n\n.. code:: python\n\n >>> import collections\n >>> User = collections.namedtuple('User', ['name', 'age', 'is_parent'])\n >>> Movie = collections.namedtuple('Movie', ['name', 'age_restriction'])\n >>> alice = User('Alice', 32, True)\n >>> bob = User('Bob', 6, False)\n >>> cartoon = Movie('Tom&Jerry', 0)\n >>> horror = Movie('Scream', 18)\n\n >>> watch_movie(bob, cartoon)\n 'Bob' is watching movie 'Tom&Jerry'\n \n >>> watch_movie(alice, horror)\n 'Alice' is watching movie 'Scream'\n \n >>> watch_movie(bob, horror) # doctest: +IGNORE_EXCEPTION_DETAIL\n Traceback (most recent call last):\n File \"business_logic/core.py\", line 48, in wrapper\n raise ServiceException(\"Validation failed!\")\n business_logic.exceptions.LogicException: Validation failed!\n\n\nYou can skip validation using :code:`validate=False`:\n\n.. code:: python\n\n >>> watch_movie(user=bob, movie=horror, validate=False)\n 'Bob' is watching movie 'Scream'\n\n\n\nAlso, if we just want to know if action is permitted, just let's run:\n\n.. code:: python\n\n >>> validation = can_watch_movie(bob, horror, raise_exception=False)\n >>> validation\n \n \n >>> bool(validation)\n False\n \n >>> validation.error # it's an actual exception\n LogicException('Validation failed!', error_code=None)\n\n\n\nChaining validators is really easy and readable:\n\n.. code:: python\n\n >>> @validator\n ... def is_old_enough(user, movie):\n ... return user.age >= movie.age_restriction\n\n >>> @validator\n ... def can_watch_movie(user, movie):\n ... is_old_enough(user, movie)\n ... # we don't have to return anything, @validator makes use of exceptions\n\n >>> can_watch_movie(bob, horror) # doctest: +IGNORE_EXCEPTION_DETAIL\n Traceback (most recent call last):\n File \"business_logic/core.py\", line 48, in wrapper\n raise LogicException(\"Validation failed!\")\n business_logic.exceptions.LogicException: Validation failed!\n\n\n\nOk, but we're still missing something. We don't know why exactly validation failed,\nall we have is a generic \"Validation failed!\" message. How to fix that? It's easy, let's\nmake our own errors!\n\n.. code:: python\n\n >>> from business_logic import LogicErrors, LogicException\n >>> class AgeRestrictionErrors(LogicErrors):\n ... CANT_WATCH_MOVIE_TOO_YOUNG = LogicException(\"User is too young to watch this\")\n\n >>> @validator\n ... def is_old_enough(user, movie):\n ... if user.age < movie.age_restriction:\n ... raise AgeRestrictionErrors.CANT_WATCH_MOVIE_TOO_YOUNG\n\n >>> is_old_enough(bob, horror) # doctest: +IGNORE_EXCEPTION_DETAIL\n Traceback (most recent call last):\n business_logic.exceptions.LogicException: User is too young to watch this\n\n >>> # we can also obtain exception details like this\n >>> result = is_old_enough(bob, horror, raise_exception=False)\n >>> bool(result)\n False\n \n >>> result.error\n LogicException('User is too young to watch this', error_code='CANT_WATCH_MOVIE_TOO_YOUNG')\n \n >>> result.error_code == 'CANT_WATCH_MOVIE_TOO_YOUNG'\n True\n \n >>> # result.errors is shortcut to registry with all errors\n >>> result.error == result.errors['CANT_WATCH_MOVIE_TOO_YOUNG']\n True\n\n\n\nTesting is really easy:\n\n.. code:: python\n\n >>> def test_user_cant_watch_movie_if_under_age_restriction():\n ... bob = User('Bob', 6, False)\n ... horror = Movie('Scream', 18)\n ... result = is_old_enough(bob, horror, raise_exception=False)\n ... # There are two ways to check if expected exceptions was raised\n ... assert result.error_code == 'CANT_WATCH_MOVIE_TOO_YOUNG'\n ... assert result.error == AgeRestrictionErrors.CANT_WATCH_MOVIE_TOO_YOUNG\n\n >>> test_user_cant_watch_movie_if_under_age_restriction()\n\n\nAlso, if you need to display parametrizable error messages, just use `.format` method\n\n.. code:: python\n >>>\n >>> exc = LogicException('User {user} is way too young!', error_code='TOO_YOUNG')\n >>> formatted_exc = exc.format(user='Bob')\n >>> assert str(formatted_exc) == 'User Bob is way too young!'\n >>> assert exc.error_code == formatted_exc.error_code\n >>> assert exc == formatted_exc\n\nUsage\n-----\n\nWhen using this package, you should write all your business logic as simple functions, using only\ninputs and Database Layer (eg. `Django ORM or SQLAlchemy`). This way, you can easily test your\nlogic and use it in any way you like. Convention that I follow is to put all functions inside `logic.py` file or `logic` submodule.\n\nIn **views** and **API** calls: Your role is to prepare all required data for business function (from forms, user session etc), call function\nand present results to user. Middleware catching LogicException and, for example, displaying message to user in a generic way\ncan improve readability a lot, because no exception handling need to be done in view.\n\nAs **management commands**: In Django you can create custom `management command`, that allows you to use cli to perform custom logic.\nPython Business Logic functions works very well for that use case!\n\nFrom **external code**: Just import your function and use it. Since there shouldn't be any framework-related\ninputs other than Database Models, usage is really simple. In reality, your business functions form *business API* of your application.\n\nExamples\n--------\n\nFor examples how to use this library, look into directory *examples*. Currently there is only one called *Football match*. Most important file there is :code:`logic.py`.\n\n\nRunning Tests\n-------------\n\nDoes the code actually work?\n\n::\n\n $ pip install -r requirements_test.txt\n $ tox\n\n\n\n\nHistory\n-------\n\n0.2.0 (2017-10-22)\n++++++++++++++++++\n\n* Support for Python 3.7\n* Parametrizable errors\n\n0.1.1 (2017-10-22)\n++++++++++++++++++\n\n* Fixed encoding in python 2.7.\n\n0.1.0 (2017-07-16)\n++++++++++++++++++\n\n* First release on PyPI.\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Valian/python-business-logic", "keywords": "python-business-logic", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "python-business-logic", "package_url": "https://pypi.org/project/python-business-logic/", "platform": "", "project_url": "https://pypi.org/project/python-business-logic/", "project_urls": { "Homepage": "https://github.com/Valian/python-business-logic" }, "release_url": "https://pypi.org/project/python-business-logic/0.2.0/", "requires_dist": null, "requires_python": "", "summary": "Python package that makes creating complicated business logic easy", "version": "0.2.0" }, "last_serial": 4277378, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "f7fe0b103d2efc33e79dd3825f5e5179", "sha256": "b27960b27b4c3ea5b35da2fdb8e5c02724a8aaa9d4c119ea322a200cfb3f2930" }, "downloads": -1, "filename": "python_business_logic-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "f7fe0b103d2efc33e79dd3825f5e5179", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 9734, "upload_time": "2017-09-06T22:30:11", "url": "https://files.pythonhosted.org/packages/9d/f4/9f93e8cb696ad43a54354c8c0790e400393f1d26fc87b02cbe930cad10f7/python_business_logic-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "32c592baa0bd43f9717d647334132d7f", "sha256": "f231f30ad0933978582c5a15b668597472586e51e89a5d9153651bd1f54c20e7" }, "downloads": -1, "filename": "python-business-logic-0.1.0.tar.gz", "has_sig": false, "md5_digest": "32c592baa0bd43f9717d647334132d7f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8563, "upload_time": "2017-09-06T22:30:09", "url": "https://files.pythonhosted.org/packages/cb/07/0316ab3f73f4d58f28695e56cecc01318fe10c70911b978259e049072382/python-business-logic-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "a11da841ba3b3041676227e51a90f0dc", "sha256": "44c4c5d02cd2183ec47a56968c2bd83673b8776de362e232c9b0c44ae5fa82c0" }, "downloads": -1, "filename": "python-business-logic-0.1.1.tar.gz", "has_sig": false, "md5_digest": "a11da841ba3b3041676227e51a90f0dc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8973, "upload_time": "2017-10-24T07:47:10", "url": "https://files.pythonhosted.org/packages/2f/e9/9df84b38b02239c3c86f1ae7a915d7c6f124a1adbdde1f86f24424222815/python-business-logic-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "eabac8e80a513c4805bf11a60cb65471", "sha256": "d6b76ab56de979c96cfa91b771d54be6f3982a854e9e2df52db97330ee755bc1" }, "downloads": -1, "filename": "python_business_logic-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "eabac8e80a513c4805bf11a60cb65471", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 11282, "upload_time": "2018-09-16T20:28:54", "url": "https://files.pythonhosted.org/packages/29/3c/e9f00491b77436c5135c79029491cd6791888042374da57927cc5cc7b97e/python_business_logic-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e64a1e7d5158f670e90b8b1b222f52d6", "sha256": "224304ce47fdc8151cb65e86ed164a9f310addd06a1a1eeaea3484e34011583e" }, "downloads": -1, "filename": "python-business-logic-0.2.0.tar.gz", "has_sig": false, "md5_digest": "e64a1e7d5158f670e90b8b1b222f52d6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9673, "upload_time": "2018-09-16T20:28:52", "url": "https://files.pythonhosted.org/packages/e0/1e/50dfd868855f7d652edae5a855540163b5b422c0f4382b57604b278333f6/python-business-logic-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "eabac8e80a513c4805bf11a60cb65471", "sha256": "d6b76ab56de979c96cfa91b771d54be6f3982a854e9e2df52db97330ee755bc1" }, "downloads": -1, "filename": "python_business_logic-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "eabac8e80a513c4805bf11a60cb65471", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 11282, "upload_time": "2018-09-16T20:28:54", "url": "https://files.pythonhosted.org/packages/29/3c/e9f00491b77436c5135c79029491cd6791888042374da57927cc5cc7b97e/python_business_logic-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e64a1e7d5158f670e90b8b1b222f52d6", "sha256": "224304ce47fdc8151cb65e86ed164a9f310addd06a1a1eeaea3484e34011583e" }, "downloads": -1, "filename": "python-business-logic-0.2.0.tar.gz", "has_sig": false, "md5_digest": "e64a1e7d5158f670e90b8b1b222f52d6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9673, "upload_time": "2018-09-16T20:28:52", "url": "https://files.pythonhosted.org/packages/e0/1e/50dfd868855f7d652edae5a855540163b5b422c0f4382b57604b278333f6/python-business-logic-0.2.0.tar.gz" } ] }