Python Schema Generator Documentation Release 1.0.0 Peter Demin June 26, 2016
Contents 1 Mutant - Python code generator 3 1.1 Project Status............................................... 3 1.2 Design.................................................. 3 1.3 Credits.................................................. 3 2 Installation 5 3 Tutorial 7 4 Succinct syntax 9 5 Usage 11 6 Contributing 13 6.1 Types of Contributions.......................................... 13 6.2 Get Started!................................................ 14 6.3 Pull Request Guidelines......................................... 14 6.4 Tips.................................................... 15 7 Credits 17 7.1 Development Lead............................................ 17 7.2 Contributors............................................... 17 8 History 19 8.1 0.1.0 (2016-1-22)............................................. 19 9 Indices and tables 21 i
ii
Python Schema Generator Documentation, Release 1.0.0 Contents: Contents 1
Python Schema Generator Documentation, Release 1.0.0 2 Contents
CHAPTER 1 Mutant - Python code generator Define your data once and auto generate all representations. Mutant takes YAML formatted schema definition and generates django models files, cerberus validation rules and so on (currently there is nothing more ;). 1.1 Project Status I started this project to aid development of Django-based RESTfull APIs. I found myself defining the same data schema several times - once for database, once for serialization framework, once for cerberus validator, once for Solr and so on. Every time I made a change to a schema I had to visit all this places often forgetting about one or two. I thought what if I define my data schema once and then will automatically derive all representation from it. I liked the idea, but the project finished before I had a chance to apply mutant to it. So now I don t have enough motivation for contributing much time to this project, but maybe some day I will, or someone else will. 1.2 Design Mutant s design is inspired by Flask. One creates an app, registers parsers, renderers and extensions on it. Then run the generator pipeline and converts input schema definition to one of available representations. tests directory has some examples of current mutant abilities. Free software: ISC license 1.3 Credits This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template. 3
Python Schema Generator Documentation, Release 1.0.0 4 Chapter 1. Mutant - Python code generator
CHAPTER 2 Installation At the command line: $ easy_install mutant Or, if you have virtualenvwrapper installed: $ mkvirtualenv mutant $ pip install mutant 5
Python Schema Generator Documentation, Release 1.0.0 6 Chapter 2. Installation
CHAPTER 3 Tutorial Mutant converts from one entity defition to set of other formats. Consider for example, having an application, that stores basic User information. Let s pretend it s not an abstract user, but Author. Here is how a Author entity can be described using Mutant YAML format: 1 Author: 2 username: {type: String, max_length: 30, primary-key: true} 3 email: Email 4 password: {type: String, private: true, max_length: 255, required: true} At the command line: $ mutate author.yml --format=django > author.py Will produce following django model definition file: 1 from django.db import models 2 3 4 class Author(models.Model): 5 class Meta: 6 verbose_name_plural = "Authors" 7 8 email = models.emailfield() 9 password = models.charfield(max_length=255) 10 username = models.charfield(primary_key=true, max_length=30) If application is meant to accept new entities, Cerberus validation rules may come in hand: 1 rules = { 2 "Author": { 3 "email": { 4 "type": "string", 5 }, 6 "password": { 7 "type": "string", 8 "required": True, 9 }, 10 "username": { 11 "type": "string", 12 "required": True, 13 }, 14 }, 15 } 7
Python Schema Generator Documentation, Release 1.0.0 It s the basics of Mutant - define entity schema once and derive (mutate) it to all forms you need. 8 Chapter 3. Tutorial
CHAPTER 4 Succinct syntax Mutant accepts (extendable) list of input formats. We ll use YAML for it s human-friendliness. Let s define blog entity structure. Each Blog has Posts, that link to Tags, which are simple strings: 1 Blog: 2 - title 3 - posts: 4 list-of: 5 - title 6 - body: Text 7 - tags: {list-of: Tag} 8 9 Tag: 10 - name: {type: String, primary-key: true} Here we see several features: Inline entity definition - Blog contains Posts; Many-to-Many relations - each Post can have many Tags and each Tag may be linked to many Posts; Here is Django s models.py: 1 from django.db import models 2 3 4 class Blog(models.Model): 5 class Meta: 6 verbose_name_plural = "Blogs" 7 8 title = models.charfield(max_length=255) 9 10 11 class Post(models.Model): 12 class Meta: 13 verbose_name_plural = "Posts" 14 15 title = models.charfield(max_length=255) 16 body = models.textfield() 17 blog = models.foreignkey("blog", on_delete=models.cascade) 18 19 20 class Tag(models.Model): 21 class Meta: 22 verbose_name_plural = "Tags" 9
Python Schema Generator Documentation, Release 1.0.0 23 24 name = models.charfield(primary_key=true, max_length=255) 25 26 27 class PostTag(models.Model): 28 class Meta: 29 verbose_name_plural = "PostTags" 30 31 post = models.foreignkey("post", primary_key=true, on_delete=models.cascade) 32 tag = models.foreignkey("tag", primary_key=true, on_delete=models.cascade) 10 Chapter 4. Succinct syntax
CHAPTER 5 Usage To use Python Schema Generator in a project: import mutant 11
Python Schema Generator Documentation, Release 1.0.0 12 Chapter 5. Usage
CHAPTER 6 Contributing Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given. You can contribute in many ways: 6.1 Types of Contributions 6.1.1 Report Bugs Report bugs at https://github.com/peterdemin/mutant/issues. If you are reporting a bug, please include: Your operating system name and version. Any details about your local setup that might be helpful in troubleshooting. Detailed steps to reproduce the bug. 6.1.2 Fix Bugs Look through the GitHub issues for bugs. Anything tagged with bug is open to whoever wants to implement it. 6.1.3 Implement Features Look through the GitHub issues for features. Anything tagged with feature is open to whoever wants to implement it. 6.1.4 Write Documentation Python Schema Generator could always use more documentation, whether as part of the official Python Schema Generator docs, in docstrings, or even on the web in blog posts, articles, and such. 6.1.5 Submit Feedback The best way to send feedback is to file an issue at https://github.com/peterdemin/mutant/issues. If you are proposing a feature: 13
Python Schema Generator Documentation, Release 1.0.0 Explain in detail how it would work. Keep the scope as narrow as possible, to make it easier to implement. Remember that this is a volunteer-driven project, and that contributions are welcome :) 6.2 Get Started! Ready to contribute? Here s how to set up mutant for local development. 1. Fork the mutant repo on GitHub. 2. Clone your fork locally: $ git clone git@github.com:your_name_here/mutant.git 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development: $ mkvirtualenv mutant $ cd mutant/ $ python setup.py develop 4. Create a branch for local development: $ git checkout -b name-of-your-bugfix-or-feature Now you can make your changes locally. 5. When you re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox: $ flake8 mutant tests $ python setup.py test $ tox To get flake8 and tox, just pip install them into your virtualenv. 6. Commit your changes and push your branch to GitHub: $ git add. $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature 7. Submit a pull request through the GitHub website. 6.3 Pull Request Guidelines Before you submit a pull request, check that it meets these guidelines: 1. The pull request should include tests. 2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst. 3. The pull request should work for Python 2.6, 2.7, 3.3, 3.4 and 3.5, and for PyPy. Check https://travisci.org/peterdemin/mutant/pull_requests and make sure that the tests pass for all supported Python versions. 14 Chapter 6. Contributing
Python Schema Generator Documentation, Release 1.0.0 6.4 Tips To run a subset of tests: $ python -m unittest tests.test_mutant 6.4. Tips 15
Python Schema Generator Documentation, Release 1.0.0 16 Chapter 6. Contributing
CHAPTER 7 Credits 7.1 Development Lead Peter Demin <peterdemin@gmail.com> 7.2 Contributors None yet. Why not be the first? 17
Python Schema Generator Documentation, Release 1.0.0 18 Chapter 7. Credits
CHAPTER 8 History 8.1 0.1.0 (2016-1-22) First release on PyPI. 19
Python Schema Generator Documentation, Release 1.0.0 20 Chapter 8. History
CHAPTER 9 Indices and tables genindex modindex search 21