{ "info": { "author": "\u00c1lvaro Justen", "author_email": "alvarojusten@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "Intended Audience :: Science/Research", "License :: OSI Approved :: GNU General Public License (GPL)", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Templater\n=========\n\nIntroduction\n------------\n\nGiven some strings (or files), this library extracts a common template between\nthem (method ``learn``) -- some people call it \"reverse templating\". Having\nyour template created, you can parse other strings/files using it - the\n``parse`` method will return only what changes in this file (the \"blanks\"). It\ndoes something like the opposite of what template libraries (such as\n`Jinja `_) do. But for now, it only can identify\nfixed variables (it can't create ``for`` and ``if`` blocks, for example).\n\nIf you have the template and the \"blanks\" you can also fill the blanks with\nthe method ``join`` - it'll return a string with the template filled. There are\nsome other features:\n\n- If you don't want/need to ``Templater`` (the main class) create the template\n for you, you can pass a pre-processed template (created manually or created\n before using ``learn`` and saved somewhere).\n- You can split the learning and parsing process, since the learning process\n generally is executed one time and takes a lot of time compared to parsing\n process. To turn this process handy, ``Templater`` has the methods ``dump``,\n ``save``, ``load`` and ``open``, so you can learn and save a template\n definition for later loading and parsing how many times you want (you can\n also load, learn more and save).\n\n`templater `_ is simple to use, easy to\nlearn and does the hard work for you (for example: part of the learning\nalgorithm is implemented in C for performance). Do you have 5 minutes? So learn\nwith the `Examples`_.\n\n\nInstallation\n------------\n\n`templater is available at PyPI `_, so\ninstalling it is as simple as executing::\n\n pip install templater\n\nOr you can download the latest version and install it using ``setup.py``::\n\n git clone https://turicas@github.com/turicas/templater.git\n cd templater\n python setup.py build install\n\n\nTerminology\n-----------\n\nThere are some definitions/concepts we should explicit here:\n\n- **Template**: the whole object (instance of ``Templater``).\n- **Document**: a string or file that have some kind of pattern. You'll use\n documents to make a template object learn and recognize these patterns, so\n later you can use the template object to parse a document and get only the\n information that is not \"static\".\n- **Blocks**: the fixed parts of a template. Can change (in number and size)\n when ``learn`` is run.\n- **Blanks**: also called holes or variables, blanks are the parts in a\n template that changes between documents with the same template.\n- **Template definition**: the information stored in a template that defines it\n (it is a Python list with a very simple grammar that describes how the\n template is composed).\n- **Markers**: when you want to save a template, something should be put\n between blocks to \"mark\" the blanks (so the template definition can be\n reconstructed later).\n- **Named marker**: a marker plus a header is called a named marker. They are\n handy and more legible since you can access the \"blanks\" by names instead of\n indexes.\n\nDoubts? Don't worry, see the `Examples`_ and you'll get it.\n\n\nExamples\n--------\n\nAll you need to know is below (and in the ``examples`` directory)::\n\n >>> from templater import Templater\n >>> documents_to_learn = [' spam and eggs ', ' ham and spam ',\n ' white and black '] # list of documents\n >>> template = Templater()\n >>> for document in documents_to_learn:\n ... template.learn(document)\n ...\n\n >>> print 'Template created:', template._template # template definition\n Template created: [None, ' ', None, ' and ', None, ' ', None]\n\n >>> document_to_parse = ' yellow and blue '\n >>> print 'Parsing other document:', template.parse(document_to_parse)\n Parsing other document: ['', 'yellow', 'blue', '']\n\n >>> print 'Filling the blanks:', template.join(['', 'red', 'orange', ''])\n Filling the blanks: red and orange \n\nYou can pass pre-processed templates as a list (blanks are ``None``, blocks are\nstrings)::\n\n >>> t2 = Templater(template=[None, 'Music: ', None, ', Band: ', None])\n >>> print t2.join(['', 'Welcome to the Jungle', 'Guns and Roses'])\n Music: Welcome to the Jungle, Band: Guns and Roses\n\n...or you can pass a string with markers, then ``Templater`` will create the\nlist for you::\n\n >>> t3 = Templater(template='language=#,cool=#', marker='#')\n >>> print t3.join(['', 'Python', 'YES', ''])\n language=Python,cool=YES\n\nSaving and opening templates is easy::\n\n >>> template.save('my-first-template.html', marker='|||')\n >>> # and some time later...\n >>> loaded_template = Templater.open('my-first-template.html', marker='|||')\n >>> print loaded_template.parse(' Romeo and Juliet ')\n ['', 'Romeo', 'Juliet', '']\n\nThe difference between ``save`` and ``dump`` is that ``save`` stores the\ntemplate string, filling the blanks with a marker and ``dump`` saves the whole\n``Templater`` object with ``cPickle``. The pairs are:\n\n- ``save`` and ``open`` (raw template string filled with marker)\n- ``load`` and ``dump`` (whole object)\n\n**Note**: ``save`` always add a ``\\n`` to the end of file; ``load``\ndeletes trailing ``\\r\\n`` or ``\\n`` in the end of file (if any).\n\n**Note-2**: when passing a pre-processed template (using ``Templater``\ninitializer or ``Templater.open``) make sure it **starts and ends** with a\nmarker.\n\nIf you are getting a lot of blanks you can configure the learning process: just\nadjust ``min_block_size`` - it's the minimum number of characters permitted to\ncreate a new block in template::\n\n >>> str_1 = 'my favorite color is blue'\n >>> str_2 = 'my favorite color is violet'\n >>> t = Templater() # default min_block_size = 1\n >>> t.learn(str_1)\n >>> t.learn(str_2)\n >>> print t._template\n [None, 'my favorite color is ', None, 'l', None, 'e', None]\n\nWe don't want that ``'l'`` and ``'e'`` there, right? So::\n\n >>> t = Templater(min_block_size=2)\n >>> t.learn(str_1)\n >>> t.learn(str_2)\n >>> print t._template\n [None, 'my favorite color is ', None]\n\n\nYou can also add \"headers\" to your template - the headers will be the name of\nyour markers, so you'll have a template with named markers and ``parse`` will\nreturn a ``dict`` instead of ``list``. It's more legible than using list\nindices, let's see::\n\n >>> import re\n >>> # Let's create a regexp that cases with '{{var}}' (it'll be our marker)\n >>> regexp_marker = re.compile(r'{{([a-zA-Z0-9_-]*)}}')\n >>> template = Templater('{{first-var}}{{second-var}}{{third-var}}',\n marker=regexp_marker)\n >>> # The template knows the name of each marker just using the regexp provided\n >>> # Passing marker as regexp to specify named markers also work for Templater.open\n\n >>> print template.parse('This is a test.')\n {'second-var': ' is ', 'third-var': ' a test.', 'first-var': 'This '}\n\n >>> # To save the template with named markers we need to provide a Python string.\n >>> # Templater will call .format() of this string for each marker with its name\n >>> template.save('template-with-named-markers.html', marker='--{}--')\n >>> # Will save '--first-var----second-var----third-var--\\n'\n\nAnd if you have a template without headers, just add to it with ``add_headers``\nmethod::\n\n >>> t = Templater('++++', marker='+')\n >>> t.parse('helloworld')\n ['', 'hello', 'world', '']\n\n >>> t.add_headers(['before', 'first-column', 'second-column', 'after'])\n >>> t.parse('helloworld')\n {'after': '', 'before': '', 'first-column': 'hello', 'second-column': 'world'}\n\n**Note**: named markers have a problem: you can't run ``learn`` if you use them.\n\n\nNotes\n-----\n\nI really want to know if you are using this project and what is your impression\nabout it. If you have new ideas of features, discovered bugs or just want to\nsay \"thank you, I'm using it!\", please contact me at\n`alvarojusten at gmail `_.\n\nIf you want to code some stuff,\njust `fork it on GitHub `_ and create a\npull request. Some technical notes for you:\n\n- This project uses `Test-Driven Development\n `_.\n\n - The tests are run using Python 2.7.2 on Ubuntu 11.10 amd64.\n- You can see the changes between versions in\n `CHANGELOG.rst `_.\n- This project uses `semantic versioning `_ (thanks,\n `Tom Preston-Werner `_).\n\n\n\nAuthor\n------\n\nThis software is developed by\n`\u00c1lvaro Justen aka Turicas `_.\n\nMany thanks to `Adrian Holovaty `_ - he created\n`templatemaker `_, the project which\n``templater`` was inspired in/forked from - and to\n`Escola de Matem\u00e1tica Aplicada (Funda\u00e7\u00e3o Get\u00falio Vargas) `_\nwhich gives me interesting problems to solve. :-)\n\n\nLicense\n-------\n\n`GPL version 2 `_", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/turicas/templater/", "keywords": "template,reversed template,template making,wrapper induction", "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "templater", "package_url": "https://pypi.org/project/templater/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/templater/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/turicas/templater/" }, "release_url": "https://pypi.org/project/templater/0.4.0/", "requires_dist": null, "requires_python": null, "summary": "Extract template (a pattern) from strings and parse otherstrings with this pattern.", "version": "0.4.0" }, "last_serial": 751265, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "9cda4c0f102d842403aee6b5e57a8de7", "sha256": "cc3b578a1ce320633825d7ee61c8716b7c909526b73b6f7344da15860d7b7bea" }, "downloads": -1, "filename": "templater-0.1.0.tar.gz", "has_sig": false, "md5_digest": "9cda4c0f102d842403aee6b5e57a8de7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11844, "upload_time": "2012-04-20T19:47:45", "url": "https://files.pythonhosted.org/packages/48/d8/99cdce9449b96e623b2cf70d4a19faacb1c580972e6225461961c338cfe2/templater-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "90c0f370686884a10e751e3ec96c7569", "sha256": "9b60fd5450f60419a0a5d0eefca7ae66a5404126858482f8e271962de9adf25f" }, "downloads": -1, "filename": "templater-0.1.1.tar.gz", "has_sig": false, "md5_digest": "90c0f370686884a10e751e3ec96c7569", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11826, "upload_time": "2012-04-20T20:06:05", "url": "https://files.pythonhosted.org/packages/5b/50/6669e564e9be673c241c00380ef9665f4f987179bef2ffd427ec0a0a74b1/templater-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "15087c282d0b4cc283550c5fbb031445", "sha256": "e189f61e64aa1d76a97e710edd862c4baafd34f915b702a382a4ea600589ef59" }, "downloads": -1, "filename": "templater-0.2.0.tar.gz", "has_sig": false, "md5_digest": "15087c282d0b4cc283550c5fbb031445", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11867, "upload_time": "2012-04-20T23:29:37", "url": "https://files.pythonhosted.org/packages/23/53/97cc7b1ca6ca3f8079b14fbe6c27c14ae644ea0a04bde2f5264f24ddad6b/templater-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "8ecc8730d2d34c070be0ca53396e6813", "sha256": "aea902a560fcda6ca9b02d29fe64fc539d39b4e8526c4780b9a46bd09d176b10" }, "downloads": -1, "filename": "templater-0.2.1.tar.gz", "has_sig": false, "md5_digest": "8ecc8730d2d34c070be0ca53396e6813", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12286, "upload_time": "2012-04-20T23:34:11", "url": "https://files.pythonhosted.org/packages/99/ec/84d6f42af8eaa06fd1b495e35acab2d38fd1a142bfb4cdc21291a4c963b2/templater-0.2.1.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "85cb6702bfad9e2176ac68c2be87b841", "sha256": "50af7612320ac1dbb8763f0aca836e84660c92c04a4f92adbee5d68ba1ff47a4" }, "downloads": -1, "filename": "templater-0.3.0.tar.gz", "has_sig": false, "md5_digest": "85cb6702bfad9e2176ac68c2be87b841", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12769, "upload_time": "2012-04-21T01:10:04", "url": "https://files.pythonhosted.org/packages/5d/8c/0dc1ad84da4ea5883cde17f27874d917654591c4b269c617eab2ab393e9b/templater-0.3.0.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "e5f38dc64238c7fbee19c85d94c5c51e", "sha256": "e6b5f7ce722211db11ee81ffe424e60aafee0304a1a92f74e30aa6776cf5576c" }, "downloads": -1, "filename": "templater-0.4.0.tar.gz", "has_sig": false, "md5_digest": "e5f38dc64238c7fbee19c85d94c5c51e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16477, "upload_time": "2012-04-24T20:08:14", "url": "https://files.pythonhosted.org/packages/a6/f0/83cc9312784d0d3949eb9dbde104c49cefc26f057dc0d5008e21783709a7/templater-0.4.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e5f38dc64238c7fbee19c85d94c5c51e", "sha256": "e6b5f7ce722211db11ee81ffe424e60aafee0304a1a92f74e30aa6776cf5576c" }, "downloads": -1, "filename": "templater-0.4.0.tar.gz", "has_sig": false, "md5_digest": "e5f38dc64238c7fbee19c85d94c5c51e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16477, "upload_time": "2012-04-24T20:08:14", "url": "https://files.pythonhosted.org/packages/a6/f0/83cc9312784d0d3949eb9dbde104c49cefc26f057dc0d5008e21783709a7/templater-0.4.0.tar.gz" } ] }