{ "info": { "author": "Omar Elawady", "author_email": "omarelawady1998@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# Codra\n\nCodra is a template engine written in python to render a string given its template and the data to fill this template\n## Installation\n\n`pip install codra`\n\n## Example\n\n```python\nfrom codra import Template\ntemplate = r\"\"\"\nHello my name is {{ name }}. I am {{ age }} years old.\nThis is the activities I do:\n{{ for i, activity in enumerate(activities) }}\n {{ i }}. {{ activity }}\n{{ endfor}}\nThose are the type of numbers less than 20\n{{ for i in range(20) }}\n {{ if is_prime(i) }}\n {{i}} is prime.\n {{ endif }}\n {{ if not is_prime(i) }}\n {{i}} is not prime.\n {{ endif}}\n{{ endfor}}\n\"\"\"\ndef is_prime(n):\n if n < 2:\n return False\n elif n == 2:\n return False\n else:\n for i in range(2, n):\n if n % i == 0:\n return False\n return True\n\nprint(Template(template).render(name = \"Alice\", age = 20, activities = [\"coding\", \"playing\", \"eating\"], is_prime = is_prime, enumerate = enumerate))\n```\n\n## Format\n\nThe template consists mainly of data and executable sections which may be interleaved with each other. The executable sections is surrounded by double curly braces. `{{` can be escaped by preceding it with backslash. \n\nThe executable sections (from now on called constructs) is divided into three types:\n\n\t1. Expressions.\n\t2. If conditions.\n\t3. For loops.\n\nThe constructs are eventually replaced by text after rendering. Since expressions might contain identifiers and function calls, they're supplied in kwargs to the render function with the names that appear iniside the template.\n\n## Language constructs\n\nIn this section, the different language constructs is covered in more details.\n\n### Expressions\n\nExpressions in codra can be considered as a subset of the expressions in python. It's worth noting that the semantic analysis is based on that of python. The expressions is parsed and computed using the corresponding construct in python. That might explain a lot of the behaviour of the templare engine.\n\n#### Literals\n\nString literals are surrounded by either single quotation marks, or double quotation marks.\n\n_Example_:\n\n`'hello world'`\n\nNumeric literals is any consecutive digits (only Integers is supported for now). Despite the fact that only integers is supported as literals, the result of any expression can be either integer of float.\n\n`1984`\n\nBoolean literals is planned to be added with the case sensitive keywords True and False.\n\n#### Arithmetic operators\n\nThe operators `+`, `-`, `/`, `*` and `%` have the same semantic meaning as they do in python. Which means that `+` can be used also to concatanate strings.\n\n_Example_:\n\n`1 + 2 * 6 / 4 % 5` evaluates to 4\n\n#### Comparison operators\n\nYou can use `==`, `!=` to check for equality and `<`, `<=`, `>` and '>=` to compare different expressions\n\n`1 < 4` is True\n\n#### Logical operators\n\n`and`, `or and `not` can be used to combine the result of comparison expressions.\n\n_Example_:\n\n`1 == 2 or not 3 == 4` evaluates to `True`\n\n#### Access operators\n\nAccess operators are used to fetch the value from an object or a dictionary. \n\n`.` is used to access class members of an object.\n\n`[]` is used to get the value associated with a key in a dict.\n\n_Example_:\n\n`person.favorite_food['morning']`\n\n#### Function calls\n\nThe built in functions `len` and `range` is supplied by default. To use another functions they must be supplied in the arguments of render. To call a function use its name along with a list of possibly empty parameters separated by `,` and enclosed in parentheses.\n\n_Example_:\n\n`Template(\"{{ is_odd(1) }}\").render(is_odd = lambda x : x % 2 == 1)` evaluates to `\"True\"`\n\n### If conditions\n\nIf conditions are used to print the enclosing data only if the expression evaluates to True:\n\nThe Syntax of if condition is:\n\n```python\n{{ if _condition_ }}\n_body_\n{{ endif }}\n```\nThe body of the if can be any valid template (data, constructs or both). The if construct evaluates to its body if the condition is True, Otherwise, it's replaced by an empty string.\n\n_Example_:\n\n```python\nTemplate(\"\"\"\nThis is a condition to greet Alice only.\n{{ if name == \"Alice\" }}\nHello {{ name }}.\n{{ endif }}\n\"\"\").render(name = \"Alice\")\n```\n\nthis evaluates to:\n\n```python\n\"\"\"\nThis is condition to greet Alice only.\n\nHello Alice.\n\n\"\"\"\n```\n\n### For loops\n\nLoops can be used to iterate over any iterable python object (e.g. lists).\n\nLoops can have one or more variables. It's translated by assigning the variable(s) to each item in the given iterable and evaluating the body of the loop. Then the result of each iteration is combined into a single unit.\n\n_Examples_:\n\n```python\nTemplate(\"\"\"\n{{ for i in range(len(lst)) }}\n{{i + 1}}. {{ lst[i] }}\n{{ endfor }}\n\"\"\").render(lst = ['a', 'b', 'c'])\n```\nwhich is equivalent to:\n\n```python\nTemplate(\"\"\"\n{{ for i, e in enumerate(lst) }}\n{{i + 1}}. {{ e }}\n{{ endfor }}\n\"\"\").render(lst = ['a', 'b', 'c'], enumerate = enumerate)\n```\n\nevaluates to:\n\n```python\n\"\"\"\n\n1. a\n\n2. b\n\n3. c\n\n\"\"\"\n```\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/OmarElawady/codra", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "codra", "package_url": "https://pypi.org/project/codra/", "platform": "", "project_url": "https://pypi.org/project/codra/", "project_urls": { "Homepage": "https://github.com/OmarElawady/codra" }, "release_url": "https://pypi.org/project/codra/0.0.3/", "requires_dist": [ "ply" ], "requires_python": "", "summary": "A python template engine", "version": "0.0.3" }, "last_serial": 5540091, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "07119f192a89f1173a67dffc096b91ab", "sha256": "a4e9a3eb9e88319842a3b2c1b0343592dadefe212bbf8314de43327f0a95cb69" }, "downloads": -1, "filename": "codra-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "07119f192a89f1173a67dffc096b91ab", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7886, "upload_time": "2019-07-16T09:34:16", "url": "https://files.pythonhosted.org/packages/83/6d/0a4634b51164502c83ca09fca0ac03dabe0c38a36bff326fc22287ba2b28/codra-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "12e9a9bdf6ffc71234642cf207d29dc9", "sha256": "61453f3f3af6bb01a33efc04159bd8f030ceed92aed23625bf40ae7e8f8e67af" }, "downloads": -1, "filename": "codra-0.0.1.tar.gz", "has_sig": false, "md5_digest": "12e9a9bdf6ffc71234642cf207d29dc9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7106, "upload_time": "2019-07-16T09:34:19", "url": "https://files.pythonhosted.org/packages/1a/04/7212451ea6a1c79ffa93679e10a74da60e682248de88721835c4e9860b90/codra-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "00c4ab59a33b008d433e591326112ff0", "sha256": "bdee77a6abe3f5f9651b69142fc19486d3888f768249620328a100e608e4089a" }, "downloads": -1, "filename": "codra-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "00c4ab59a33b008d433e591326112ff0", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7930, "upload_time": "2019-07-16T10:01:04", "url": "https://files.pythonhosted.org/packages/bb/80/a384ec1ce5ea50e0200617073d00f3a60f525abf0d9e60245efb356fbeb6/codra-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4b144cf7a2ebda479c5112c088737f29", "sha256": "818968055ca853462ca883e2f942b6560f86609d0239fe52aed2bba78d0a6a43" }, "downloads": -1, "filename": "codra-0.0.2.tar.gz", "has_sig": false, "md5_digest": "4b144cf7a2ebda479c5112c088737f29", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7214, "upload_time": "2019-07-16T10:01:06", "url": "https://files.pythonhosted.org/packages/fc/dd/69049adbf058d0496ff9ed8a2fc20e4347657bc3bf692ac3d445091c3ce1/codra-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "20987b976c73b1d20c61b5214a436902", "sha256": "44e127453da76fd9536d48dd3d865e16bd230e25717654cedd2e645619234484" }, "downloads": -1, "filename": "codra-0.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "20987b976c73b1d20c61b5214a436902", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10245, "upload_time": "2019-07-16T11:44:07", "url": "https://files.pythonhosted.org/packages/d7/bb/d00f2d912197b320ea1393025cb5860fbf347bc4b652d8c15967602f0f9f/codra-0.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "745b6bac4fc9a6d8f43c0088da75d15e", "sha256": "74685b2336dbfaf702783a7dfc13e97b16ac8f70a79f3e6ed70c26c6150cb9be" }, "downloads": -1, "filename": "codra-0.0.3.tar.gz", "has_sig": false, "md5_digest": "745b6bac4fc9a6d8f43c0088da75d15e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9350, "upload_time": "2019-07-16T11:44:09", "url": "https://files.pythonhosted.org/packages/7b/3a/912a2d33049f09a5a1085914aa4df825b8ff4b7a9288d10173b4a1218919/codra-0.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "20987b976c73b1d20c61b5214a436902", "sha256": "44e127453da76fd9536d48dd3d865e16bd230e25717654cedd2e645619234484" }, "downloads": -1, "filename": "codra-0.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "20987b976c73b1d20c61b5214a436902", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 10245, "upload_time": "2019-07-16T11:44:07", "url": "https://files.pythonhosted.org/packages/d7/bb/d00f2d912197b320ea1393025cb5860fbf347bc4b652d8c15967602f0f9f/codra-0.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "745b6bac4fc9a6d8f43c0088da75d15e", "sha256": "74685b2336dbfaf702783a7dfc13e97b16ac8f70a79f3e6ed70c26c6150cb9be" }, "downloads": -1, "filename": "codra-0.0.3.tar.gz", "has_sig": false, "md5_digest": "745b6bac4fc9a6d8f43c0088da75d15e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9350, "upload_time": "2019-07-16T11:44:09", "url": "https://files.pythonhosted.org/packages/7b/3a/912a2d33049f09a5a1085914aa4df825b8ff4b7a9288d10173b4a1218919/codra-0.0.3.tar.gz" } ] }