{ "info": { "author": "Jeremies P\u00e9rez Morata", "author_email": "jeremiespm@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development", "Topic :: System :: Distributed Computing" ], "description": "# basic-agent\nMinimal & Simple Agent Engine for Python\n\n[![Build Status](https://travis-ci.org/jepemo/basic-agent.svg?branch=master)](https://travis-ci.org/jepemo/basic-agent)\n\n- [Getting started](#getting-started)\n- [Features](#features)\n- [Documentation](#documentation)\n - [Agent creation](#agent-creation)\n - [Message passing](#message-passing)\n - [Pattern matching](#pattern-matching)\n\n\n## Getting started\n\n### Installation\n\n```bash\npip install bagent\n```\n\n## Example\n\n```python\nfrom bagent import *\n\nasync def slave(ctx):\n async with ctx.get_message() as m:\n if m.is_str():\n print(m.msg)\n\nasync def master(ctx):\n slave_pid = await ctx.start(slave)\n await ctx.send(slave_pid, \"Hello World\")\n\nwith get_agent_context() as ctx:\n ctx.start(master)\n```\n\n## Features\n- **asyncio** integration\n- Simple message passing model\n- Simple-pattern matching\n\n## Documentation\n\n### Agent creation\nYou can create agents like python3 async functions.\n\nFirst of all you need to create the root context, and start the first level processes inside. This processes can be started in any order.\nFor example:\n\n```python\nfrom bagent import *\n\nasync def agent1(ctx, param1):\n print(param1)\n\nwith get_agent_context() as ctx:\n for i in range(0, 4):\n ctx.start(agent1, i)\n\n# Result:\n# 0\n# 3\n# 1\n# 2\n```\n\nAn agent is defined like an *async function*. The only requirement is that the first parameter is the *agent context*. This context is used to start another agents, send messages to them and use the pattern matching.\n\nAnd of course, you can use async functions inside an agent:\n\n```python\nfrom bagent import *\n\nasync slave(ctx):\n await asyncio.sleep(1)\n\nasync master(ctx):\n await ctx.start(slave)\n\nwith get_agent_context() as ctx:\n ctx.start(master)\n```\n\n### Message passing\n\nWhen you start an agent it's PID is returned, so you can use it to send it messages. You can only send messages inside an agent, not in the root context.\n\n```python\nasync def slave(ctx):\n (sender, msg) = await ctx.recv()\n await ctx.send(sender, \"PONG\")\n\nasync def master(ctx):\n pid = ctx.start(slave)\n await pid.send(pid, \"PING\")\n (sender, msg) = await ctx.recv()\n print(msg)\n```\n\nThere are two ways to receive a message. With the simple recv and with the *message context*. You can see recv case in the above example.\n\nFor the *message context* you need the agent context:\n\n```python\nasync def slave(ctx):\n async with ctx.get_message() as m:\n print(\"Message {0} received from {1}\".format(m.msg, m.sender))\n```\n\n\n### Pattern matching\n\nWhen you receive a message with the *message context* you can use it to facilitate the message processing. This provides several pattern-matching utilities.\n\nThe most simple are the type checkers:\n - is_str : Check if the message is a string\n - is_float : Check if the message is a float\n - is_int : Check if the message is an integer\n - is_type(type T) : Check if the message is from type T\n - is_re(re string) : Check if the message matches with the regular expresion string.\n\nFor example:\n```python\n\nasync def slave(ctx):\n async with get_message() as m:\n if m.is_int():\n print(\"It's an integer\")\n elif m.is_float():\n print(\"It's a float\")\n\nasync def master(ctx):\n pid = await ctx.start(slave)\n await ctx.send(pid, 1.2)\n```\n\nOne tipical pattern is to have al infinite loop inside the agent and finish it with one type of message:\n\n```python\nasync def slave(ctx):\n while True:\n async with get_message() as m:\n if m.is_re(\"EXIT\"):\n break\n else:\n print(m.msg)\n\nasync def master(ctx):\n pid = await ctx.start(slave)\n await ctx.send(pid, 1)\n await ctx.send(pid, 2)\n await ctx.send(pid, 3)\n await ctx.send(pid, \"EXIT\")\n\n# Exit:\n# 1\n# 2\n# 3\n```\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jepemo/basic-agent", "keywords": "distributedsystems", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "bagent", "package_url": "https://pypi.org/project/bagent/", "platform": "", "project_url": "https://pypi.org/project/bagent/", "project_urls": { "Homepage": "https://github.com/jepemo/basic-agent" }, "release_url": "https://pypi.org/project/bagent/0.5.1/", "requires_dist": null, "requires_python": "", "summary": "Minimal & Simple Agent Engine for Python", "version": "0.5.1" }, "last_serial": 3807447, "releases": { "0.5": [ { "comment_text": "", "digests": { "md5": "65a7f8216f815a7a75302ebf95fa0f32", "sha256": "08cd1ea6a6d4b74cccf5edf46631517dbc9dbb99f086f7841658acb7a346ebd5" }, "downloads": -1, "filename": "bagent-0.5.tar.gz", "has_sig": false, "md5_digest": "65a7f8216f815a7a75302ebf95fa0f32", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5700, "upload_time": "2018-04-25T15:57:29", "url": "https://files.pythonhosted.org/packages/97/6a/4834b3a4e7c1eae52caf93b1a7ae6251709a297621b92634fea3a3d09e22/bagent-0.5.tar.gz" } ], "0.5.1": [ { "comment_text": "", "digests": { "md5": "102d7eb00a96023bcf5b62c638f781b1", "sha256": "51bdf636cf123d420d2029b0b3efeeeeed62395402f545083d5133fd96955132" }, "downloads": -1, "filename": "bagent-0.5.1.tar.gz", "has_sig": false, "md5_digest": "102d7eb00a96023bcf5b62c638f781b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5687, "upload_time": "2018-04-25T16:19:34", "url": "https://files.pythonhosted.org/packages/57/4a/be0c3be34ec375c2e3244b1833930373d13f6372b42961d671bddb9af930/bagent-0.5.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "102d7eb00a96023bcf5b62c638f781b1", "sha256": "51bdf636cf123d420d2029b0b3efeeeeed62395402f545083d5133fd96955132" }, "downloads": -1, "filename": "bagent-0.5.1.tar.gz", "has_sig": false, "md5_digest": "102d7eb00a96023bcf5b62c638f781b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5687, "upload_time": "2018-04-25T16:19:34", "url": "https://files.pythonhosted.org/packages/57/4a/be0c3be34ec375c2e3244b1833930373d13f6372b42961d671bddb9af930/bagent-0.5.1.tar.gz" } ] }