├── .gitignore ├── .travis.yml ├── MANIFEST.in ├── README.md ├── UNLICENSE ├── docs ├── Makefile ├── api.rst ├── conf.py ├── index.rst └── introduction.rst ├── micromodels ├── __init__.py ├── fields.py └── models.py ├── setup.py ├── tests.py └── twitterexample.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.swp 3 | .DS_Store 4 | .idea 5 | build 6 | dist 7 | MANIFEST 8 | docs/_* 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.6" 4 | - "2.7" 5 | install: pip install . 6 | script: python tests.py 7 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include UNLICENSE 3 | include tests.py 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # micromodels 2 | 3 | [![Build Status](https://secure.travis-ci.org/j4mie/micromodels.png)](http://travis-ci.org/j4mie/micromodels) 4 | 5 | A simple library for building model classes based on dictionaries of data. 6 | 7 | Perfect for (amongst other things) wrapping Python objects around JSON data returned from web-based APIs. 8 | 9 | **Authors**: Jamie Matthews () and Eric Martin (). 10 | 11 | ## Installation 12 | 13 | pip install micromodels 14 | 15 | ## Really simple example 16 | 17 | import micromodels 18 | 19 | class Author(micromodels.Model): 20 | first_name = micromodels.CharField() 21 | last_name = micromodels.CharField() 22 | date_of_birth = micromodels.DateField(format="%Y-%m-%d") 23 | 24 | @property 25 | def full_name(self): 26 | return "%s %s" % (self.first_name, self.last_name) 27 | 28 | 29 | douglas_data = { 30 | "first_name": "Douglas", 31 | "last_name": "Adams", 32 | "date_of_birth": "1952-03-11", 33 | } 34 | 35 | douglas = Author.from_dict(douglas_data) 36 | print "%s was born in %s" % (douglas.full_name, douglas.date_of_birth.strftime("%Y")) 37 | 38 | ## Slightly more complex example 39 | 40 | import json 41 | from urllib2 import urlopen 42 | 43 | import micromodels 44 | 45 | class TwitterUser(micromodels.Model): 46 | id = micromodels.IntegerField() 47 | screen_name = micromodels.CharField() 48 | name = micromodels.CharField() 49 | description = micromodels.CharField() 50 | 51 | def get_profile_url(self): 52 | return 'http://twitter.com/%s' % self.screen_name 53 | 54 | 55 | class Tweet(micromodels.Model): 56 | id = micromodels.IntegerField() 57 | text = micromodels.CharField() 58 | created_at = micromodels.DateTimeField(format="%a %b %d %H:%M:%S +0000 %Y") 59 | user = micromodels.ModelField(TwitterUser) 60 | 61 | 62 | json_data = urlopen('http://api.twitter.com/1/statuses/show/20.json').read() 63 | tweet = Tweet.from_dict(json_data, is_json=True) 64 | 65 | print tweet.user.name 66 | print tweet.user.get_profile_url() 67 | print tweet.id 68 | print tweet.created_at.strftime('%A') 69 | 70 | #new fields can also be added to the model instance 71 | #a method needs to be used to do this to handle serialization 72 | 73 | tweet.add_field('retweet_count', 44, micromodels.IntegerField()) 74 | print tweet.retweet_count 75 | 76 | #the data can be cast to a dict (still containing time object) 77 | print tweet.to_dict() 78 | 79 | #it can also be cast to JSON (fields handle their own serialization) 80 | print tweet.to_json() 81 | 82 | #tweet.to_json() is equivalent to this call 83 | json.dumps(tweet.to_dict(serial=True)) 84 | 85 | 86 | ## Field reference 87 | 88 | ### Field options 89 | 90 | The following optional argument is available for all field types. 91 | 92 | #### `source` 93 | 94 | By default, a model class will look for a key in its source data with the same name as each of its fields. For example: 95 | 96 | class ExampleModel(micromodels.Model): 97 | myfield = micromodels.CharField() 98 | 99 | >>> e = ExampleModel({'myfield': 'Some Value'}) 100 | >>> e.myfield 101 | u'Some Value' 102 | 103 | If you wish to change this, you can pass the 'source' argument to each field instance: 104 | 105 | class ExampleModel(micromodels.Model): 106 | myfield = micromodels.CharField() 107 | anotherfield = micromodels.CharField(source='some_other_field') 108 | 109 | >>> e = ExampleModel({'myfield': 'Some Value', 'some_other_field': 'Another Value'}) 110 | >>> e.anotherfield 111 | u'Another Value' 112 | 113 | ### Field types 114 | 115 | #### BaseField 116 | 117 | The simplest type of field - this simply passes through whatever is in the data dictionary without changing it at all. 118 | 119 | #### CharField 120 | 121 | A field for string data. **Will attempt to convert its supplied data to Unicode.** 122 | 123 | #### IntegerField 124 | 125 | Attempts to convert its supplied data to an integer. 126 | 127 | #### FloatField 128 | 129 | Attempts to convert its supplied data to a floating point value. 130 | 131 | #### BooleanField 132 | 133 | Attempts to convert its supplied data to a boolean. If the data is a string, `"true"` (case insensitive) will be converted to `True` and all other strings will be converted to `False`. If the supplied data is an integer, positive numbers will become `True` and negative numbers or zero will become `False`. 134 | 135 | #### DateTimeField 136 | 137 | Converts its supplied data to a Python `datetime.datetime` object as `ISO8601`. 138 | 139 | class MyModel(micromodels.Model): 140 | created_at = micromodels.DateTimeField() 141 | 142 | An optional format may be provided. 143 | 144 | class MyModel(micromodels.Model): 145 | created_at = micromodels.DateTimeField(format="%a %b %d %H:%M:%S +0000 %Y") 146 | 147 | See [the Python 148 | documentation](http://docs.python.org/library/datetime.html#strftime-strptime-behavior) 149 | for details of the format string. For example: 150 | 151 | #### DateField 152 | 153 | Converts its supplied data to a Python `datetime.date` object as 154 | `ISO8601` or using an option `format` argument (see `DateTimeField` 155 | for details) 156 | 157 | #### TimeField 158 | 159 | Converts its supplied data to a Python `datetime.time` object as 160 | `ISO8601` or using an option `format` argument (see `DateTimeField` for details). 161 | 162 | #### FieldCollectionField 163 | 164 | Use this field when your source data dictionary contains a list of items of the same type. It takes a single required argument, which is the field type that should be used to convert each item in the list. For example: 165 | 166 | some_data = { 167 | 'first_list': [0, 34, 42], 168 | 'second_list': ['first_item', 'second_item', 'third_item'], 169 | } 170 | 171 | class MyModel(micromodels.Model): 172 | first_list = micromodels.FieldCollectionField(micromodels.IntegerField) 173 | second_list = micromodels.FieldCollectionField(micromodels.CharField) 174 | 175 | >>> m = MyModel(some_data) 176 | >>> m.first_list 177 | [0, 34, 42] 178 | >>> m.second_list 179 | [u'first_item', u'second_item', u'third_item'] 180 | 181 | #### ModelField 182 | 183 | Use this field when you wish to nest one object inside another. It takes a single required argument, which is the nested class. For example, given the following dictionary: 184 | 185 | some_data = { 186 | 'first_item': 'Some value', 187 | 'second_item': { 188 | 'nested_item': 'Some nested value', 189 | }, 190 | } 191 | 192 | You could build the following classes (note that you have to define the inner nested models first): 193 | 194 | class MyNestedModel(micromodels.Model): 195 | nested_item = micromodels.CharField() 196 | 197 | class MyMainModel(micromodels.Model): 198 | first_item = micromodels.CharField() 199 | second_item = micromodels.ModelField(MyNestedModel) # pass the class of the nested model 200 | 201 | Then you can access the data as follows: 202 | 203 | >>> m = MyMainModel(some_data) 204 | >>> m.first_item 205 | u'Some value' 206 | >>> m.second_item.__class__.__name__ 207 | 'MyNestedModel' 208 | >>> m.second_item.nested_item 209 | u'Some nested value' 210 | 211 | `ModelField` takes an optional `related_name` argument. The 212 | `related_name` is the name to use for the related model to refer back 213 | to the outer model: 214 | 215 | class Person(Model): 216 | name = CharField() 217 | car = ModelField(Car, related_name="owner") 218 | 219 | class Car(Model): 220 | make = CharField() 221 | model = CharField() 222 | 223 | >>> person = Person.from_dict(some_data) 224 | >>> person.car.owner == person 225 | True 226 | 227 | #### ModelCollectionField 228 | 229 | Use this field when your source data dictionary contains a list of 230 | dictionaries. It takes a single required argument, which is the name 231 | of the nested class that each item in the list should be converted 232 | to. For example: 233 | 234 | some_data = { 235 | 'list': [ 236 | {'value': 'First value'}, 237 | {'value': 'Second value'}, 238 | {'value': 'Third value'}, 239 | ] 240 | } 241 | 242 | class MyNestedModel(micromodels.Model): 243 | value = micromodels.CharField() 244 | 245 | class MyMainModel(micromodels.Model): 246 | list = micromodels.ModelCollectionField(MyNestedModel) 247 | 248 | >>> m = MyMainModel(some_data) 249 | >>> len(m.list) 250 | 3 251 | >>> m.list[0].__class__.__name__ 252 | 'MyNestedModel' 253 | >>> m.list[0].value 254 | u'First value' 255 | >>> [item.value for item in m.list] 256 | [u'First value', u'Second value', u'Third value'] 257 | 258 | `ModelCollectionField` takes an optional `related_name` argument which 259 | serves the same purpose as it does with `ModelField`. 260 | 261 | ## (Un)license 262 | 263 | This is free and unencumbered software released into the public domain. 264 | 265 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. 266 | 267 | In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. 268 | 269 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 270 | 271 | For more information, please refer to 272 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | 15 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest 16 | 17 | help: 18 | @echo "Please use \`make ' where is one of" 19 | @echo " html to make standalone HTML files" 20 | @echo " dirhtml to make HTML files named index.html in directories" 21 | @echo " singlehtml to make a single large HTML file" 22 | @echo " pickle to make pickle files" 23 | @echo " json to make JSON files" 24 | @echo " htmlhelp to make HTML files and a HTML help project" 25 | @echo " qthelp to make HTML files and a qthelp project" 26 | @echo " devhelp to make HTML files and a Devhelp project" 27 | @echo " epub to make an epub" 28 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 29 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 30 | @echo " text to make text files" 31 | @echo " man to make manual pages" 32 | @echo " changes to make an overview of all changed/added/deprecated items" 33 | @echo " linkcheck to check all external links for integrity" 34 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 35 | 36 | clean: 37 | -rm -rf $(BUILDDIR)/* 38 | 39 | html: 40 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 41 | @echo 42 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 43 | 44 | dirhtml: 45 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 48 | 49 | singlehtml: 50 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 51 | @echo 52 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 53 | 54 | pickle: 55 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 56 | @echo 57 | @echo "Build finished; now you can process the pickle files." 58 | 59 | json: 60 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 61 | @echo 62 | @echo "Build finished; now you can process the JSON files." 63 | 64 | htmlhelp: 65 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 66 | @echo 67 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 68 | ".hhp project file in $(BUILDDIR)/htmlhelp." 69 | 70 | qthelp: 71 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 72 | @echo 73 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 74 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 75 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Micromodels.qhcp" 76 | @echo "To view the help file:" 77 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Micromodels.qhc" 78 | 79 | devhelp: 80 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 81 | @echo 82 | @echo "Build finished." 83 | @echo "To view the help file:" 84 | @echo "# mkdir -p $$HOME/.local/share/devhelp/Micromodels" 85 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Micromodels" 86 | @echo "# devhelp" 87 | 88 | epub: 89 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 90 | @echo 91 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 92 | 93 | latex: 94 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 95 | @echo 96 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 97 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 98 | "(use \`make latexpdf' here to do that automatically)." 99 | 100 | latexpdf: 101 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 102 | @echo "Running LaTeX files through pdflatex..." 103 | make -C $(BUILDDIR)/latex all-pdf 104 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 105 | 106 | text: 107 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 108 | @echo 109 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 110 | 111 | man: 112 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 113 | @echo 114 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 115 | 116 | changes: 117 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 118 | @echo 119 | @echo "The overview file is in $(BUILDDIR)/changes." 120 | 121 | linkcheck: 122 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 123 | @echo 124 | @echo "Link check complete; look for any errors in the above output " \ 125 | "or in $(BUILDDIR)/linkcheck/output.txt." 126 | 127 | doctest: 128 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 129 | @echo "Testing of doctests in the sources finished, look at the " \ 130 | "results in $(BUILDDIR)/doctest/output.txt." 131 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 1 | API 2 | =================== 3 | 4 | Models 5 | ------------------- 6 | 7 | .. autoclass:: micromodels.Model 8 | :no-show-inheritance: 9 | 10 | Fields 11 | ------------------- 12 | 13 | .. autoclass:: micromodels.BaseField 14 | :no-show-inheritance: 15 | 16 | Basic Fields 17 | ~~~~~~~~~~~~~~~~~~ 18 | 19 | .. autoclass:: micromodels.BooleanField 20 | .. autoclass:: micromodels.CharField 21 | .. autoclass:: micromodels.IntegerField 22 | .. autoclass:: micromodels.FloatField 23 | 24 | Datetime Fields 25 | ~~~~~~~~~~~~~~~~~~~~ 26 | 27 | .. autoclass:: micromodels.DateTimeField 28 | .. autoclass:: micromodels.DateField 29 | .. autoclass:: micromodels.TimeField 30 | 31 | Relationship Fields 32 | ~~~~~~~~~~~~~~~~~~~~ 33 | 34 | .. autoclass:: micromodels.ModelField 35 | .. autoclass:: micromodels.ModelCollectionField 36 | .. autoclass:: micromodels.FieldCollectionField 37 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Micromodels documentation build configuration file, created by 4 | # sphinx-quickstart on Tue Jan 4 11:19:27 2011. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | #sys.path.insert(0, os.path.abspath('.')) 20 | 21 | sys.path.append(os.path.abspath('..')) 22 | 23 | # -- General configuration ----------------------------------------------------- 24 | 25 | # If your documentation needs a minimal Sphinx version, state it here. 26 | #needs_sphinx = '1.0' 27 | 28 | # Add any Sphinx extension module names here, as strings. They can be extensions 29 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 30 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage'] 31 | 32 | # Add any paths that contain templates here, relative to this directory. 33 | templates_path = ['_templates'] 34 | 35 | # The suffix of source filenames. 36 | source_suffix = '.rst' 37 | 38 | # The encoding of source files. 39 | #source_encoding = 'utf-8-sig' 40 | 41 | # The master toctree document. 42 | master_doc = 'index' 43 | 44 | # General information about the project. 45 | project = u'micromodels' 46 | copyright = u'2011, Jamie Matthews and Eric Martin' 47 | 48 | # The version info for the project you're documenting, acts as replacement for 49 | # |version| and |release|, also used in various other places throughout the 50 | # built documents. 51 | # 52 | # The short X.Y version. 53 | version = '.1' 54 | # The full version, including alpha/beta/rc tags. 55 | release = '.1' 56 | 57 | # The language for content autogenerated by Sphinx. Refer to documentation 58 | # for a list of supported languages. 59 | #language = None 60 | 61 | # There are two options for replacing |today|: either, you set today to some 62 | # non-false value, then it is used: 63 | #today = '' 64 | # Else, today_fmt is used as the format for a strftime call. 65 | #today_fmt = '%B %d, %Y' 66 | 67 | # List of patterns, relative to source directory, that match files and 68 | # directories to ignore when looking for source files. 69 | exclude_patterns = ['_build'] 70 | 71 | # The reST default role (used for this markup: `text`) to use for all documents. 72 | #default_role = None 73 | 74 | # If true, '()' will be appended to :func: etc. cross-reference text. 75 | #add_function_parentheses = True 76 | 77 | # If true, the current module name will be prepended to all description 78 | # unit titles (such as .. function::). 79 | #add_module_names = True 80 | 81 | # If true, sectionauthor and moduleauthor directives will be shown in the 82 | # output. They are ignored by default. 83 | #show_authors = False 84 | 85 | # The name of the Pygments (syntax highlighting) style to use. 86 | pygments_style = 'sphinx' 87 | 88 | # A list of ignored prefixes for module index sorting. 89 | #modindex_common_prefix = [] 90 | 91 | 92 | # -- Options for HTML output --------------------------------------------------- 93 | 94 | # The theme to use for HTML and HTML Help pages. See the documentation for 95 | # a list of builtin themes. 96 | html_theme = 'default' 97 | 98 | # Theme options are theme-specific and customize the look and feel of a theme 99 | # further. For a list of options available for each theme, see the 100 | # documentation. 101 | #html_theme_options = {} 102 | 103 | # Add any paths that contain custom themes here, relative to this directory. 104 | #html_theme_path = [] 105 | 106 | # The name for this set of Sphinx documents. If None, it defaults to 107 | # " v documentation". 108 | #html_title = None 109 | 110 | # A shorter title for the navigation bar. Default is the same as html_title. 111 | #html_short_title = None 112 | 113 | # The name of an image file (relative to this directory) to place at the top 114 | # of the sidebar. 115 | #html_logo = None 116 | 117 | # The name of an image file (within the static path) to use as favicon of the 118 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 119 | # pixels large. 120 | #html_favicon = None 121 | 122 | # Add any paths that contain custom static files (such as style sheets) here, 123 | # relative to this directory. They are copied after the builtin static files, 124 | # so a file named "default.css" will overwrite the builtin "default.css". 125 | html_static_path = ['_static'] 126 | 127 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 128 | # using the given strftime format. 129 | #html_last_updated_fmt = '%b %d, %Y' 130 | 131 | # If true, SmartyPants will be used to convert quotes and dashes to 132 | # typographically correct entities. 133 | #html_use_smartypants = True 134 | 135 | # Custom sidebar templates, maps document names to template names. 136 | #html_sidebars = {} 137 | 138 | # Additional templates that should be rendered to pages, maps page names to 139 | # template names. 140 | #html_additional_pages = {} 141 | 142 | # If false, no module index is generated. 143 | #html_domain_indices = True 144 | 145 | # If false, no index is generated. 146 | #html_use_index = True 147 | 148 | # If true, the index is split into individual pages for each letter. 149 | #html_split_index = False 150 | 151 | # If true, links to the reST sources are added to the pages. 152 | #html_show_sourcelink = True 153 | 154 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 155 | #html_show_sphinx = True 156 | 157 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 158 | #html_show_copyright = True 159 | 160 | # If true, an OpenSearch description file will be output, and all pages will 161 | # contain a tag referring to it. The value of this option must be the 162 | # base URL from which the finished HTML is served. 163 | #html_use_opensearch = '' 164 | 165 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 166 | #html_file_suffix = None 167 | 168 | # Output file base name for HTML help builder. 169 | htmlhelp_basename = 'Micromodelsdoc' 170 | 171 | 172 | # -- Options for LaTeX output -------------------------------------------------- 173 | 174 | # The paper size ('letter' or 'a4'). 175 | #latex_paper_size = 'letter' 176 | 177 | # The font size ('10pt', '11pt' or '12pt'). 178 | #latex_font_size = '10pt' 179 | 180 | # Grouping the document tree into LaTeX files. List of tuples 181 | # (source start file, target name, title, author, documentclass [howto/manual]). 182 | latex_documents = [ 183 | ('index', 'Micromodels.tex', u'Micromodels Documentation', 184 | u'Jamie Matthews and Eric Martin', 'manual'), 185 | ] 186 | 187 | # The name of an image file (relative to this directory) to place at the top of 188 | # the title page. 189 | #latex_logo = None 190 | 191 | # For "manual" documents, if this is true, then toplevel headings are parts, 192 | # not chapters. 193 | #latex_use_parts = False 194 | 195 | # If true, show page references after internal links. 196 | #latex_show_pagerefs = False 197 | 198 | # If true, show URL addresses after external links. 199 | #latex_show_urls = False 200 | 201 | # Additional stuff for the LaTeX preamble. 202 | #latex_preamble = '' 203 | 204 | # Documents to append as an appendix to all manuals. 205 | #latex_appendices = [] 206 | 207 | # If false, no module index is generated. 208 | #latex_domain_indices = True 209 | 210 | 211 | # -- Options for manual page output -------------------------------------------- 212 | 213 | # One entry per manual page. List of tuples 214 | # (source start file, name, description, authors, manual section). 215 | man_pages = [ 216 | ('index', 'micromodels', u'Micromodels Documentation', 217 | [u'Jamie Matthews and Eric Martin'], 1) 218 | ] 219 | 220 | autodoc_default_flags = ['members', 'show-inheritance'] -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. Micromodels documentation master file, created by 2 | sphinx-quickstart on Tue Jan 4 11:19:27 2011. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to Micromodels's documentation! 7 | ======================================= 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | introduction 15 | api 16 | 17 | Indices and tables 18 | ================== 19 | 20 | * :ref:`genindex` 21 | * :ref:`modindex` 22 | * :ref:`search` 23 | 24 | -------------------------------------------------------------------------------- /docs/introduction.rst: -------------------------------------------------------------------------------- 1 | Introduction 2 | ================== 3 | 4 | Unstructured data is popular and convenient. Although weak structure may allow a 5 | better representation of data by the producer of the data, it also makes the 6 | data more difficult to consume and process. 7 | 8 | Micromodels add convenience and structure to unstructured data. 9 | 10 | Micromodels works with Python dictionaries. Convenience functions are 11 | included for the manipulation of JSON, but any other format of data will need to 12 | be manipulated into a dictionary before it can be used with micromodels. 13 | 14 | Quickstart 15 | ----------- 16 | 17 | -------------------------------------------------------------------------------- /micromodels/__init__.py: -------------------------------------------------------------------------------- 1 | from .models import Model 2 | from .fields import BaseField, CharField, IntegerField, FloatField,\ 3 | BooleanField, DateTimeField, DateField, TimeField,\ 4 | ModelField, ModelCollectionField, FieldCollectionField 5 | 6 | __version__ = '0.6.0' 7 | -------------------------------------------------------------------------------- /micromodels/fields.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import PySO8601 3 | 4 | 5 | class BaseField(object): 6 | """Base class for all field types. 7 | 8 | The ``source`` parameter sets the key that will be retrieved from the source 9 | data. If ``source`` is not specified, the field instance will use its own 10 | name as the key to retrieve the value from the source data. 11 | 12 | """ 13 | def __init__(self, source=None): 14 | self.source = source 15 | 16 | def populate(self, data): 17 | """Set the value or values wrapped by this field""" 18 | 19 | self.data = data 20 | 21 | def to_python(self): 22 | '''After being populated, this method casts the source data into a 23 | Python object. The default behavior is to simply return the source 24 | value. Subclasses should override this method. 25 | 26 | ''' 27 | return self.data 28 | 29 | def to_serial(self, data): 30 | '''Used to serialize forms back into JSON or other formats. 31 | 32 | This method is essentially the opposite of 33 | :meth:`~micromodels.fields.BaseField.to_python`. A string, boolean, 34 | number, dictionary, list, or tuple must be returned. Subclasses should 35 | override this method. 36 | 37 | ''' 38 | return data 39 | 40 | 41 | class CharField(BaseField): 42 | """Field to represent a simple Unicode string value.""" 43 | 44 | def to_python(self): 45 | """Convert the data supplied using the :meth:`populate` method to a 46 | Unicode string. 47 | 48 | """ 49 | if self.data is None: 50 | return '' 51 | return unicode(self.data) 52 | 53 | 54 | class IntegerField(BaseField): 55 | """Field to represent an integer value""" 56 | 57 | def to_python(self): 58 | """Convert the data supplied to the :meth:`populate` method to an 59 | integer. 60 | 61 | """ 62 | if self.data is None: 63 | return 0 64 | return int(self.data) 65 | 66 | 67 | class FloatField(BaseField): 68 | """Field to represent a floating point value""" 69 | 70 | def to_python(self): 71 | """Convert the data supplied to the :meth:`populate` method to a 72 | float. 73 | 74 | """ 75 | if self.data is None: 76 | return 0.0 77 | return float(self.data) 78 | 79 | 80 | class BooleanField(BaseField): 81 | """Field to represent a boolean""" 82 | 83 | def to_python(self): 84 | """The string ``'True'`` (case insensitive) will be converted 85 | to ``True``, as will any positive integers. 86 | 87 | """ 88 | if isinstance(self.data, basestring): 89 | return self.data.strip().lower() == 'true' 90 | if isinstance(self.data, int): 91 | return self.data > 0 92 | return bool(self.data) 93 | 94 | 95 | class DateTimeField(BaseField): 96 | """Field to represent a datetime 97 | 98 | The ``format`` parameter dictates the format of the input strings, and is 99 | used in the construction of the :class:`datetime.datetime` object. 100 | 101 | The ``serial_format`` parameter is a strftime formatted string for 102 | serialization. If ``serial_format`` isn't specified, an ISO formatted string 103 | will be returned by :meth:`~micromodels.DateTimeField.to_serial`. 104 | 105 | """ 106 | def __init__(self, format=None, serial_format=None, **kwargs): 107 | super(DateTimeField, self).__init__(**kwargs) 108 | self.format = format 109 | self.serial_format = serial_format 110 | 111 | def to_python(self): 112 | '''A :class:`datetime.datetime` object is returned.''' 113 | 114 | if self.data is None: 115 | return None 116 | 117 | # don't parse data that is already native 118 | if isinstance(self.data, datetime.datetime): 119 | return self.data 120 | elif self.format is None: 121 | # parse as iso8601 122 | return PySO8601.parse(self.data) 123 | else: 124 | return datetime.datetime.strptime(self.data, self.format) 125 | 126 | def to_serial(self, time_obj): 127 | if not self.serial_format: 128 | return time_obj.isoformat() 129 | return time_obj.strftime(self.serial_format) 130 | 131 | class DateField(DateTimeField): 132 | """Field to represent a :mod:`datetime.date`""" 133 | 134 | def to_python(self): 135 | # don't parse data that is already native 136 | if isinstance(self.data, datetime.date): 137 | return self.data 138 | 139 | dt = super(DateField, self).to_python() 140 | return dt.date() 141 | 142 | 143 | class TimeField(DateTimeField): 144 | """Field to represent a :mod:`datetime.time`""" 145 | 146 | def to_python(self): 147 | # don't parse data that is already native 148 | if isinstance(self.data, datetime.datetime): 149 | return self.data 150 | elif self.format is None: 151 | # parse as iso8601 152 | return PySO8601.parse_time(self.data).time() 153 | else: 154 | return datetime.datetime.strptime(self.data, self.format).time() 155 | 156 | 157 | class WrappedObjectField(BaseField): 158 | """Superclass for any fields that wrap an object""" 159 | 160 | def __init__(self, wrapped_class, related_name=None, **kwargs): 161 | self._wrapped_class = wrapped_class 162 | self._related_name = related_name 163 | self._related_obj = None 164 | 165 | BaseField.__init__(self, **kwargs) 166 | 167 | 168 | class ModelField(WrappedObjectField): 169 | """Field containing a model instance 170 | 171 | Use this field when you wish to nest one object inside another. 172 | It takes a single required argument, which is the nested class. 173 | For example, given the following dictionary:: 174 | 175 | some_data = { 176 | 'first_item': 'Some value', 177 | 'second_item': { 178 | 'nested_item': 'Some nested value', 179 | }, 180 | } 181 | 182 | You could build the following classes 183 | (note that you have to define the inner nested models first):: 184 | 185 | class MyNestedModel(micromodels.Model): 186 | nested_item = micromodels.CharField() 187 | 188 | class MyMainModel(micromodels.Model): 189 | first_item = micromodels.CharField() 190 | second_item = micromodels.ModelField(MyNestedModel) 191 | 192 | Then you can access the data as follows:: 193 | 194 | >>> m = MyMainModel(some_data) 195 | >>> m.first_item 196 | u'Some value' 197 | >>> m.second_item.__class__.__name__ 198 | 'MyNestedModel' 199 | >>> m.second_item.nested_item 200 | u'Some nested value' 201 | 202 | """ 203 | def to_python(self): 204 | if isinstance(self.data, self._wrapped_class): 205 | obj = self.data 206 | else: 207 | obj = self._wrapped_class.from_dict(self.data or {}) 208 | 209 | # Set the related object to the related field 210 | if self._related_name is not None: 211 | setattr(obj, self._related_name, self._related_obj) 212 | 213 | return obj 214 | 215 | def to_serial(self, model_instance): 216 | return model_instance.to_dict(serial=True) 217 | 218 | 219 | class ModelCollectionField(WrappedObjectField): 220 | """Field containing a list of model instances. 221 | 222 | Use this field when your source data dictionary contains a list of 223 | dictionaries. It takes a single required argument, which is the name of the 224 | nested class that each item in the list should be converted to. 225 | For example:: 226 | 227 | some_data = { 228 | 'list': [ 229 | {'value': 'First value'}, 230 | {'value': 'Second value'}, 231 | {'value': 'Third value'}, 232 | ] 233 | } 234 | 235 | class MyNestedModel(micromodels.Model): 236 | value = micromodels.CharField() 237 | 238 | class MyMainModel(micromodels.Model): 239 | list = micromodels.ModelCollectionField(MyNestedModel) 240 | 241 | >>> m = MyMainModel(some_data) 242 | >>> len(m.list) 243 | 3 244 | >>> m.list[0].__class__.__name__ 245 | 'MyNestedModel' 246 | >>> m.list[0].value 247 | u'First value' 248 | >>> [item.value for item in m.list] 249 | [u'First value', u'Second value', u'Third value'] 250 | 251 | """ 252 | def to_python(self): 253 | object_list = [] 254 | for item in self.data: 255 | obj = self._wrapped_class.from_dict(item) 256 | if self._related_name is not None: 257 | setattr(obj, self._related_name, self._related_obj) 258 | object_list.append(obj) 259 | 260 | return object_list 261 | 262 | def to_serial(self, model_instances): 263 | return [instance.to_dict(serial=True) for instance in model_instances] 264 | 265 | 266 | class FieldCollectionField(BaseField): 267 | """Field containing a list of the same type of fields. 268 | 269 | The constructor takes an instance of the field. 270 | 271 | Here are some examples:: 272 | 273 | data = { 274 | 'legal_name': 'John Smith', 275 | 'aliases': ['Larry', 'Mo', 'Curly'] 276 | } 277 | 278 | class Person(Model): 279 | legal_name = CharField() 280 | aliases = FieldCollectionField(CharField()) 281 | 282 | p = Person(data) 283 | 284 | And now a quick REPL session:: 285 | 286 | >>> p.legal_name 287 | u'John Smith' 288 | >>> p.aliases 289 | [u'Larry', u'Mo', u'Curly'] 290 | >>> p.to_dict() 291 | {'legal_name': u'John Smith', 'aliases': [u'Larry', u'Mo', u'Curly']} 292 | >>> p.to_dict() == p.to_dict(serial=True) 293 | True 294 | 295 | Here is a bit more complicated example involving args and kwargs:: 296 | 297 | data = { 298 | 'name': 'San Andreas', 299 | 'dates': ['1906-05-11', '1948-11-02', '1970-01-01'] 300 | } 301 | 302 | class FaultLine(Model): 303 | name = CharField() 304 | earthquake_dates = FieldCollectionField(DateField('%Y-%m-%d', 305 | serial_format='%m-%d-%Y'), 306 | source='dates') 307 | 308 | f = FaultLine(data) 309 | 310 | Notice that source is passed to to the 311 | :class:`~micromodels.FieldCollectionField`, not the 312 | :class:`~micromodels.DateField`. 313 | 314 | Let's check out the resulting :class:`~micromodels.Model` instance with the 315 | REPL:: 316 | 317 | >>> f.name 318 | u'San Andreas' 319 | >>> f.earthquake_dates 320 | [datetime.date(1906, 5, 11), datetime.date(1948, 11, 2), datetime.date(1970, 1, 1)] 321 | >>> f.to_dict() 322 | {'earthquake_dates': [datetime.date(1906, 5, 11), datetime.date(1948, 11, 2), datetime.date(1970, 1, 1)], 323 | 'name': u'San Andreas'} 324 | >>> f.to_dict(serial=True) 325 | {'earthquake_dates': ['05-11-1906', '11-02-1948', '01-01-1970'], 'name': u'San Andreas'} 326 | >>> f.to_json() 327 | '{"earthquake_dates": ["05-11-1906", "11-02-1948", "01-01-1970"], "name": "San Andreas"}' 328 | 329 | """ 330 | def __init__(self, field_instance, **kwargs): 331 | super(FieldCollectionField, self).__init__(**kwargs) 332 | self._instance = field_instance 333 | 334 | def to_python(self): 335 | def convert(item): 336 | self._instance.populate(item) 337 | return self._instance.to_python() 338 | return [convert(item) for item in self.data or []] 339 | 340 | def to_serial(self, list_of_fields): 341 | return [self._instance.to_serial(data) for data in list_of_fields] 342 | -------------------------------------------------------------------------------- /micromodels/models.py: -------------------------------------------------------------------------------- 1 | try: 2 | import json 3 | except ImportError: 4 | import simplejson as json 5 | 6 | from .fields import BaseField 7 | 8 | class Model(object): 9 | """The Model is the main component of micromodels. Model makes it trivial 10 | to parse data from many sources, including JSON APIs. 11 | 12 | You will probably want to initialize this class using the class methods 13 | :meth:`from_dict` or :meth:`from_kwargs`. If you want to initialize an 14 | instance without any data, just call :class:`Model` with no parameters. 15 | 16 | :class:`Model` instances have a unique behavior when an attribute is set 17 | on them. This is needed to properly format data as the fields specify. 18 | The variable name is referred to as the key, and the value will be called 19 | the value. For example, in:: 20 | 21 | instance = Model() 22 | instance.age = 18 23 | 24 | ``age`` is the key and ``18`` is the value. 25 | 26 | First, the model checks if it has a field with a name matching the key. 27 | 28 | If there is a matching field, then :meth:`to_python` is called on the field 29 | with the value. 30 | If :meth:`to_python` does not raise an exception, then the result of 31 | :meth:`to_python` is set on the instance, and the method is completed. 32 | Essentially, this means that the first thing setting an attribute tries 33 | to do is process the data as if it was a "primitive" data type. 34 | 35 | If :meth:`to_python` does raise an exception, this means that the data 36 | might already be an appropriate Python type. The :class:`Model` then 37 | attempts to *serialize* the data into a "primitive" type using the 38 | field's :meth:`to_serial` method. 39 | 40 | If this fails, a ``TypeError`` is raised. 41 | 42 | If it does not fail, the value is set on the instance, and the 43 | method is complete. 44 | 45 | If the instance doesn't have a field matching the key, then the key and 46 | value are just set on the instance like any other assignment in Python. 47 | 48 | """ 49 | class __metaclass__(type): 50 | '''Creates the metaclass for Model. The main function of this metaclass 51 | is to move all of fields into the _fields variable on the class. 52 | 53 | ''' 54 | def __init__(cls, name, bases, attrs): 55 | cls._clsfields = {} 56 | for key, value in attrs.iteritems(): 57 | if isinstance(value, BaseField): 58 | cls._clsfields[key] = value 59 | delattr(cls, key) 60 | 61 | def __init__(self): 62 | super(Model, self).__setattr__('_extra', {}) 63 | 64 | @classmethod 65 | def from_dict(cls, D, is_json=False): 66 | '''This factory for :class:`Model` 67 | takes either a native Python dictionary or a JSON dictionary/object 68 | if ``is_json`` is ``True``. The dictionary passed does not need to 69 | contain all of the values that the Model declares. 70 | 71 | ''' 72 | instance = cls() 73 | instance.set_data(D, is_json=is_json) 74 | return instance 75 | 76 | @classmethod 77 | def from_kwargs(cls, **kwargs): 78 | '''This factory for :class:`Model` only takes keywork arguments. 79 | Each key and value pair that represents a field in the :class:`Model` is 80 | set on the new :class:`Model` instance. 81 | 82 | ''' 83 | instance = cls() 84 | instance.set_data(kwargs) 85 | return instance 86 | 87 | def set_data(self, data, is_json=False): 88 | if is_json: 89 | data = json.loads(data) 90 | for name, field in self._clsfields.iteritems(): 91 | key = field.source or name 92 | if key in data: 93 | setattr(self, name, data.get(key)) 94 | 95 | def __setattr__(self, key, value): 96 | if key in self._fields: 97 | field = self._fields[key] 98 | field.populate(value) 99 | field._related_obj = self 100 | super(Model, self).__setattr__(key, field.to_python()) 101 | else: 102 | super(Model, self).__setattr__(key, value) 103 | 104 | @property 105 | def _fields(self): 106 | return dict(self._clsfields, **self._extra) 107 | 108 | def add_field(self, key, value, field): 109 | ''':meth:`add_field` must be used to add a field to an existing 110 | instance of Model. This method is required so that serialization of the 111 | data is possible. Data on existing fields (defined in the class) can be 112 | reassigned without using this method. 113 | 114 | ''' 115 | self._extra[key] = field 116 | setattr(self, key, value) 117 | 118 | 119 | def to_dict(self, serial=False): 120 | '''A dictionary representing the the data of the class is returned. 121 | Native Python objects will still exist in this dictionary (for example, 122 | a ``datetime`` object will be returned rather than a string) 123 | unless ``serial`` is set to True. 124 | 125 | ''' 126 | if serial: 127 | return dict((key, self._fields[key].to_serial(getattr(self, key))) 128 | for key in self._fields.keys() if hasattr(self, key)) 129 | else: 130 | return dict((key, getattr(self, key)) for key in self._fields.keys() 131 | if hasattr(self, key)) 132 | 133 | def to_json(self): 134 | '''Returns a representation of the model as a JSON string. This method 135 | relies on the :meth:`~micromodels.Model.to_dict` method. 136 | 137 | ''' 138 | return json.dumps(self.to_dict(serial=True)) 139 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | from setuptools import setup, find_packages 4 | 5 | rel_file = lambda *args: os.path.join(os.path.dirname(os.path.abspath(__file__)), *args) 6 | 7 | def read_from(filename): 8 | fp = open(filename) 9 | try: 10 | return fp.read() 11 | finally: 12 | fp.close() 13 | 14 | def get_long_description(): 15 | return read_from(rel_file('README.md')) 16 | 17 | def get_version(): 18 | data = read_from(rel_file('micromodels', '__init__.py')) 19 | return re.search(r"__version__ = '([^']+)'", data).group(1) 20 | 21 | setup( 22 | name='micromodels', 23 | description='Declarative dictionary-based model classes for Python', 24 | long_description=get_long_description(), 25 | version=get_version(), 26 | packages=find_packages(), 27 | url='https://github.com/j4mie/micromodels/', 28 | author='Jamie Matthews', 29 | author_email='jamie.matthews@gmail.com', 30 | license='Public Domain', 31 | install_requires=["PySO8601"], 32 | classifiers = [ 33 | 'Programming Language :: Python', 34 | 'Development Status :: 3 - Alpha', 35 | 'Intended Audience :: Developers', 36 | 'License :: Public Domain', 37 | 'Operating System :: OS Independent', 38 | 'Topic :: Software Development :: Libraries :: Python Modules', 39 | ], 40 | ) 41 | -------------------------------------------------------------------------------- /tests.py: -------------------------------------------------------------------------------- 1 | from datetime import date 2 | import unittest 3 | 4 | import micromodels 5 | from micromodels.models import json 6 | 7 | class ClassCreationTestCase(unittest.TestCase): 8 | 9 | def setUp(self): 10 | class SimpleModel(micromodels.Model): 11 | name = micromodels.CharField() 12 | field_with_source = micromodels.CharField(source='foo') 13 | self.model_class = SimpleModel 14 | self.instance = SimpleModel() 15 | 16 | def test_class_created(self): 17 | """Model instance should be of type SimpleModel""" 18 | self.assertTrue(isinstance(self.instance, self.model_class)) 19 | 20 | def test_fields_created(self): 21 | """Model instance should have a property called _fields""" 22 | self.assertTrue(hasattr(self.instance, '_fields')) 23 | 24 | def test_field_collected(self): 25 | """Model property should be of correct type""" 26 | self.assertTrue(isinstance(self.instance._fields['name'], micromodels.CharField)) 27 | 28 | def test_field_source_not_set(self): 29 | """Field without a custom source should have a source of None""" 30 | self.assertEqual(self.instance._fields['name'].source, None) 31 | 32 | def test_field_source_set(self): 33 | """Field with custom source specificied should have source property set correctly""" 34 | self.assertEqual(self.instance._fields['field_with_source'].source, 'foo') 35 | 36 | 37 | class BaseFieldTestCase(unittest.TestCase): 38 | 39 | def test_field_without_provided_source(self): 40 | """If no source parameter is provided, the field's source attribute should be None""" 41 | field = micromodels.fields.BaseField() 42 | self.assertTrue(hasattr(field, 'source')) 43 | self.assertTrue(field.source is None) 44 | 45 | def test_field_with_provided_source(self): 46 | """If a source parameter is provided, the field's source attribute should be set to the value of this parameter""" 47 | field = micromodels.fields.BaseField(source='customsource') 48 | self.assertEqual(field.source, 'customsource') 49 | 50 | 51 | class CharFieldTestCase(unittest.TestCase): 52 | 53 | def setUp(self): 54 | self.field = micromodels.CharField() 55 | 56 | def test_string_conversion(self): 57 | self.field.populate('somestring') 58 | self.assertEqual(self.field.to_python(), 'somestring') 59 | 60 | def test_none_conversion(self): 61 | """CharField should convert None to empty string""" 62 | self.field.populate(None) 63 | self.assertEqual(self.field.to_python(), '') 64 | 65 | 66 | class IntegerFieldTestCase(unittest.TestCase): 67 | 68 | def setUp(self): 69 | self.field = micromodels.IntegerField() 70 | 71 | def test_integer_conversion(self): 72 | self.field.populate(123) 73 | self.assertEqual(self.field.to_python(), 123) 74 | 75 | def test_float_conversion(self): 76 | self.field.populate(123.4) 77 | self.assertEqual(self.field.to_python(), 123) 78 | 79 | def test_string_conversion(self): 80 | self.field.populate('123') 81 | self.assertEqual(self.field.to_python(), 123) 82 | 83 | def test_none_conversion(self): 84 | """IntegerField should convert None to 0""" 85 | self.field.populate(None) 86 | self.assertEqual(self.field.to_python(), 0) 87 | 88 | 89 | class FloatFieldTestCase(unittest.TestCase): 90 | 91 | def setUp(self): 92 | self.field = micromodels.FloatField() 93 | 94 | def test_float_conversion(self): 95 | self.field.populate(123.4) 96 | self.assertEqual(self.field.to_python(), 123.4) 97 | 98 | def test_integer_conversion(self): 99 | self.field.populate(123) 100 | self.assertEqual(self.field.to_python(), 123.0) 101 | 102 | def test_string_conversion(self): 103 | self.field.populate('123.4') 104 | self.assertEqual(self.field.to_python(), 123.4) 105 | 106 | def test_none_conversion(self): 107 | """FloatField should convert None to 0.0""" 108 | self.field.populate(None) 109 | self.assertEqual(self.field.to_python(), 0.0) 110 | 111 | 112 | class BooleanFieldTestCase(unittest.TestCase): 113 | 114 | def setUp(self): 115 | self.field = micromodels.BooleanField() 116 | 117 | def test_true_conversion(self): 118 | self.field.populate(True) 119 | self.assertEqual(self.field.to_python(), True) 120 | 121 | def test_false_conversion(self): 122 | self.field.populate(False) 123 | self.assertEqual(self.field.to_python(), False) 124 | 125 | def test_string_conversion(self): 126 | """BooleanField should convert the string "True" (case insensitive) to True, all other values to False""" 127 | self.field.populate('true') 128 | self.assertEqual(self.field.to_python(), True) 129 | self.field.populate('True') 130 | self.assertEqual(self.field.to_python(), True) 131 | self.field.populate('False') 132 | self.assertEqual(self.field.to_python(), False) 133 | self.field.populate('asdfasfasfd') 134 | self.assertEqual(self.field.to_python(), False) 135 | 136 | def test_integer_conversion(self): 137 | """BooleanField should convert values <= 0 to False, all other integers to True""" 138 | self.field.populate(0) 139 | self.assertEqual(self.field.to_python(), False) 140 | self.field.populate(-100) 141 | self.assertEqual(self.field.to_python(), False) 142 | self.field.populate(100) 143 | self.assertEqual(self.field.to_python(), True) 144 | 145 | 146 | class DateTimeFieldTestCase(unittest.TestCase): 147 | 148 | def setUp(self): 149 | self.format = "%a %b %d %H:%M:%S +0000 %Y" 150 | self.datetimestring = "Tue Mar 21 20:50:14 +0000 2006" 151 | self.field = micromodels.DateTimeField(format=self.format) 152 | 153 | def test_format_conversion(self): 154 | import datetime 155 | self.field.populate(self.datetimestring) 156 | converted = self.field.to_python() 157 | self.assertTrue(isinstance(converted, datetime.datetime)) 158 | self.assertEqual(converted.strftime(self.format), self.datetimestring) 159 | 160 | def test_iso8601_conversion(self): 161 | import datetime 162 | from PySO8601 import Timezone 163 | 164 | field = micromodels.DateTimeField() 165 | field.populate("2010-07-13T14:01:00Z") 166 | result = field.to_python() 167 | expected = datetime.datetime(2010, 7, 13, 14, 1, 0, 168 | tzinfo=Timezone()) 169 | self.assertEqual(expected, result) 170 | 171 | 172 | field = micromodels.DateTimeField() 173 | field.populate("2010-07-13T14:02:00-05:00") 174 | result = field.to_python() 175 | expected = datetime.datetime(2010, 7, 13, 14, 2, 0, 176 | tzinfo=Timezone("-05:00")) 177 | 178 | self.assertEqual(expected, result) 179 | 180 | 181 | field = micromodels.DateTimeField() 182 | field.populate("20100713T140200-05:00") 183 | result = field.to_python() 184 | expected = datetime.datetime(2010, 7, 13, 14, 2, 0, 185 | tzinfo=Timezone("-05:00")) 186 | 187 | self.assertEqual(expected, result) 188 | 189 | 190 | def test_iso8601_to_serial(self): 191 | import datetime 192 | 193 | field = micromodels.DateTimeField() 194 | field.populate("2010-07-13T14:01:00Z") 195 | native = field.to_python() 196 | expected = "2010-07-13T14:01:00+00:00" 197 | result = field.to_serial(native) 198 | 199 | self.assertEqual(expected, result) 200 | 201 | field = micromodels.DateTimeField() 202 | field.populate("2010-07-13T14:02:00-05:00") 203 | native = field.to_python() 204 | expected = "2010-07-13T14:02:00-05:00" 205 | result = field.to_serial(native) 206 | 207 | self.assertEqual(expected, result) 208 | 209 | 210 | class DateFieldTestCase(unittest.TestCase): 211 | 212 | def setUp(self): 213 | self.format = "%Y-%m-%d" 214 | self.datestring = "2010-12-28" 215 | self.field = micromodels.DateField(format=self.format) 216 | 217 | def test_format_conversion(self): 218 | import datetime 219 | self.field.populate(self.datestring) 220 | converted = self.field.to_python() 221 | self.assertTrue(isinstance(converted, datetime.date)) 222 | self.assertEqual(converted.strftime(self.format), self.datestring) 223 | 224 | def test_iso8601_conversion(self): 225 | import datetime 226 | field = micromodels.DateField() 227 | field.populate("2010-12-28") 228 | result = field.to_python() 229 | expected = datetime.date(2010,12,28) 230 | self.assertEqual(expected, result) 231 | 232 | field = micromodels.DateField() 233 | field.populate("20101228") 234 | result = field.to_python() 235 | expected = datetime.date(2010,12,28) 236 | self.assertEqual(expected, result) 237 | 238 | 239 | class TimeFieldTestCase(unittest.TestCase): 240 | 241 | def setUp(self): 242 | self.format = "%H:%M:%S" 243 | self.timestring = "09:33:30" 244 | self.field = micromodels.TimeField(format=self.format) 245 | 246 | def test_format_conversion(self): 247 | import datetime 248 | self.field.populate(self.timestring) 249 | converted = self.field.to_python() 250 | self.assertTrue(isinstance(converted, datetime.time)) 251 | self.assertEqual(converted.strftime(self.format), self.timestring) 252 | 253 | def test_iso8601_conversion(self): 254 | import datetime 255 | field = micromodels.TimeField() 256 | field.populate("09:33:30") 257 | result = field.to_python() 258 | expected = datetime.time(9,33,30) 259 | self.assertEqual(expected, result) 260 | 261 | field = micromodels.TimeField() 262 | field.populate("093331") 263 | result = field.to_python() 264 | expected = datetime.time(9,33,31) 265 | self.assertEqual(expected, result) 266 | 267 | 268 | class InstanceTestCase(unittest.TestCase): 269 | 270 | def test_basic_data(self): 271 | class ThreeFieldsModel(micromodels.Model): 272 | first = micromodels.CharField() 273 | second = micromodels.CharField() 274 | third = micromodels.CharField() 275 | 276 | data = {'first': 'firstvalue', 'second': 'secondvalue'} 277 | instance = ThreeFieldsModel.from_dict(data) 278 | 279 | self.assertEqual(instance.first, data['first']) 280 | self.assertEqual(instance.second, data['second']) 281 | 282 | def test_custom_data_source(self): 283 | class CustomSourceModel(micromodels.Model): 284 | first = micromodels.CharField(source='custom_source') 285 | 286 | data = {'custom_source': 'somevalue'} 287 | instance = CustomSourceModel.from_dict(data) 288 | 289 | self.assertEqual(instance.first, data['custom_source']) 290 | 291 | 292 | class ModelFieldTestCase(unittest.TestCase): 293 | 294 | def test_model_field_creation(self): 295 | class IsASubModel(micromodels.Model): 296 | first = micromodels.CharField() 297 | 298 | class HasAModelField(micromodels.Model): 299 | first = micromodels.ModelField(IsASubModel) 300 | 301 | data = {'first': {'first': 'somevalue'}} 302 | instance = HasAModelField.from_dict(data) 303 | self.assertTrue(isinstance(instance.first, IsASubModel)) 304 | self.assertEqual(instance.first.first, data['first']['first']) 305 | 306 | def test_model_field_to_serial(self): 307 | class User(micromodels.Model): 308 | name = micromodels.CharField() 309 | 310 | class Post(micromodels.Model): 311 | title = micromodels.CharField() 312 | author = micromodels.ModelField(User) 313 | 314 | data = {'title': 'Test Post', 'author': {'name': 'Eric Martin'}} 315 | post = Post.from_dict(data) 316 | self.assertEqual(post.to_dict(serial=True), data) 317 | 318 | def test_related_name(self): 319 | class User(micromodels.Model): 320 | name = micromodels.CharField() 321 | 322 | class Post(micromodels.Model): 323 | title = micromodels.CharField() 324 | author = micromodels.ModelField(User, related_name="post") 325 | 326 | data = {'title': 'Test Post', 'author': {'name': 'Eric Martin'}} 327 | post = Post.from_dict(data) 328 | self.assertEqual(post.author.post, post) 329 | self.assertEqual(post.to_dict(serial=True), data) 330 | 331 | def test_failing_modelfield(self): 332 | class SomethingExceptional(Exception): 333 | pass 334 | 335 | class User(micromodels.Model): 336 | name = micromodels.CharField() 337 | 338 | @classmethod 339 | def from_dict(cls, *args, **kwargs): 340 | raise SomethingExceptional("opps.") 341 | 342 | class Post(micromodels.Model): 343 | title = micromodels.CharField() 344 | author = micromodels.ModelField(User) 345 | 346 | data = {'title': 'Test Post', 'author': {'name': 'Eric Martin'}} 347 | self.assertRaises(SomethingExceptional, Post.from_dict, 348 | data) 349 | 350 | 351 | class ModelCollectionFieldTestCase(unittest.TestCase): 352 | 353 | def test_model_collection_field_creation(self): 354 | class IsASubModel(micromodels.Model): 355 | first = micromodels.CharField() 356 | 357 | class HasAModelCollectionField(micromodels.Model): 358 | first = micromodels.ModelCollectionField(IsASubModel) 359 | 360 | data = {'first': [{'first': 'somevalue'}, {'first': 'anothervalue'}]} 361 | instance = HasAModelCollectionField.from_dict(data) 362 | self.assertTrue(isinstance(instance.first, list)) 363 | for item in instance.first: 364 | self.assertTrue(isinstance(item, IsASubModel)) 365 | self.assertEqual(instance.first[0].first, data['first'][0]['first']) 366 | self.assertEqual(instance.first[1].first, data['first'][1]['first']) 367 | 368 | def test_model_collection_field_with_no_elements(self): 369 | class IsASubModel(micromodels.Model): 370 | first = micromodels.CharField() 371 | 372 | class HasAModelCollectionField(micromodels.Model): 373 | first = micromodels.ModelCollectionField(IsASubModel) 374 | 375 | data = {'first': []} 376 | instance = HasAModelCollectionField.from_dict(data) 377 | self.assertEqual(instance.first, []) 378 | 379 | def test_model_collection_to_serial(self): 380 | class Post(micromodels.Model): 381 | title = micromodels.CharField() 382 | 383 | class User(micromodels.Model): 384 | name = micromodels.CharField() 385 | posts = micromodels.ModelCollectionField(Post) 386 | 387 | data = { 388 | 'name': 'Eric Martin', 389 | 'posts': [ 390 | {'title': 'Post #1'}, 391 | {'title': 'Post #2'} 392 | ] 393 | } 394 | 395 | eric = User.from_dict(data) 396 | processed = eric.to_dict(serial=True) 397 | self.assertEqual(processed, data) 398 | 399 | def test_related_name(self): 400 | class Post(micromodels.Model): 401 | title = micromodels.CharField() 402 | 403 | class User(micromodels.Model): 404 | name = micromodels.CharField() 405 | posts = micromodels.ModelCollectionField(Post, related_name="author") 406 | 407 | data = { 408 | 'name': 'Eric Martin', 409 | 'posts': [ 410 | {'title': 'Post #1'}, 411 | {'title': 'Post #2'} 412 | ] 413 | } 414 | 415 | eric = User.from_dict(data) 416 | processed = eric.to_dict(serial=True) 417 | for post in eric.posts: 418 | self.assertEqual(post.author, eric) 419 | 420 | self.assertEqual(processed, data) 421 | 422 | 423 | class FieldCollectionFieldTestCase(unittest.TestCase): 424 | 425 | def test_field_collection_field_creation(self): 426 | class HasAFieldCollectionField(micromodels.Model): 427 | first = micromodels.FieldCollectionField(micromodels.CharField()) 428 | 429 | data = {'first': ['one', 'two', 'three']} 430 | instance = HasAFieldCollectionField.from_dict(data) 431 | self.assertTrue(isinstance(instance.first, list)) 432 | self.assertTrue(len(data['first']), len(instance.first)) 433 | for index, value in enumerate(data['first']): 434 | self.assertEqual(instance.first[index], value) 435 | 436 | def test_field_collection_field_to_serial(self): 437 | class Person(micromodels.Model): 438 | aliases = micromodels.FieldCollectionField(micromodels.CharField()) 439 | events = micromodels.FieldCollectionField(micromodels.DateField('%Y-%m-%d', 440 | serial_format='%m-%d-%Y'), source='schedule') 441 | 442 | data = { 443 | 'aliases': ['Joe', 'John', 'Bob'], 444 | 'schedule': ['2011-01-30', '2011-04-01'] 445 | } 446 | 447 | p = Person.from_dict(data) 448 | serial = p.to_dict(serial=True) 449 | self.assertEqual(serial['aliases'], data['aliases']) 450 | self.assertEqual(serial['events'][0], '01-30-2011') 451 | 452 | class ModelTestCase(unittest.TestCase): 453 | 454 | def setUp(self): 455 | class Person(micromodels.Model): 456 | name = micromodels.CharField() 457 | age = micromodels.IntegerField() 458 | 459 | self.Person = Person 460 | self.data = {'name': 'Eric', 'age': 18} 461 | self.json_data = json.dumps(self.data) 462 | 463 | def test_model_creation(self): 464 | instance = self.Person.from_dict(self.json_data, is_json=True) 465 | self.assertTrue(isinstance(instance, micromodels.Model)) 466 | self.assertEqual(instance.name, self.data['name']) 467 | self.assertEqual(instance.age, self.data['age']) 468 | 469 | def test_model_reserialization(self): 470 | instance = self.Person.from_dict(self.json_data, is_json=True) 471 | self.assertEqual(instance.to_json(), self.json_data) 472 | instance.name = 'John' 473 | self.assertEqual(json.loads(instance.to_json())['name'], 474 | 'John') 475 | 476 | def test_model_type_change_serialization(self): 477 | class Event(micromodels.Model): 478 | time = micromodels.DateField(format="%Y-%m-%d") 479 | 480 | data = {'time': '2000-10-31'} 481 | json_data = json.dumps(data) 482 | 483 | instance = Event.from_dict(json_data, is_json=True) 484 | output = instance.to_dict(serial=True) 485 | self.assertEqual(output['time'], instance.time.isoformat()) 486 | self.assertEqual(json.loads(instance.to_json())['time'], 487 | instance.time.isoformat()) 488 | 489 | def test_model_add_field(self): 490 | obj = self.Person.from_dict(self.data) 491 | obj.add_field('gender', 'male', micromodels.CharField()) 492 | self.assertEqual(obj.gender, 'male') 493 | self.assertEqual(obj.to_dict(), dict(self.data, gender='male')) 494 | 495 | def test_model_late_assignment(self): 496 | instance = self.Person.from_dict(dict(name='Eric')) 497 | self.assertEqual(instance.to_dict(), dict(name='Eric')) 498 | instance.age = 18 499 | self.assertEqual(instance.to_dict(), self.data) 500 | instance.name = 'John' 501 | self.assertEqual(instance.to_dict(), dict(name='John', age=18)) 502 | instance.age = '19' 503 | self.assertEqual(instance.to_dict(), dict(name='John', age=19)) 504 | 505 | format = '%m-%d-%Y' 506 | today = date.today() 507 | today_str = today.strftime(format) 508 | 509 | instance.add_field('birthday', today_str, 510 | micromodels.DateField(format)) 511 | self.assertEqual(instance.to_dict()['birthday'], today) 512 | instance.birthday = today 513 | self.assertEqual(instance.to_dict()['birthday'], today) 514 | 515 | 516 | if __name__ == "__main__": 517 | unittest.main() 518 | -------------------------------------------------------------------------------- /twitterexample.py: -------------------------------------------------------------------------------- 1 | import json 2 | from urllib2 import urlopen 3 | 4 | import micromodels 5 | 6 | class TwitterUser(micromodels.Model): 7 | id = micromodels.IntegerField() 8 | screen_name = micromodels.CharField() 9 | name = micromodels.CharField() 10 | description = micromodels.CharField() 11 | 12 | def get_profile_url(self): 13 | return 'http://twitter.com/%s' % self.screen_name 14 | 15 | 16 | class Tweet(micromodels.Model): 17 | id = micromodels.IntegerField() 18 | text = micromodels.CharField() 19 | created_at = micromodels.DateTimeField(format="%a %b %d %H:%M:%S +0000 %Y") 20 | user = micromodels.ModelField(TwitterUser) 21 | 22 | 23 | json_data = urlopen('http://api.twitter.com/1/statuses/show/20.json').read() 24 | tweet = Tweet(json_data, is_json=True) 25 | 26 | print tweet.user.name 27 | print tweet.user.get_profile_url() 28 | print tweet.id 29 | print tweet.created_at.strftime('%A') 30 | 31 | #new fields can also be added to the model instance 32 | #a method needs to be used to do this to handle serialization 33 | 34 | tweet.add_field('retweet_count', 44, micromodels.IntegerField()) 35 | print tweet.retweet_count 36 | 37 | #the data can be cast to a dict (still containing time object) 38 | print tweet.to_dict() 39 | 40 | #it can also be cast to JSON (fields handle their own serialization) 41 | print tweet.to_json() 42 | 43 | #tweet.to_json() is equivalent to this call 44 | json.dumps(tweet.to_dict(serial=True)) 45 | 46 | 47 | --------------------------------------------------------------------------------