Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emolch/guts
Lightweight declarative YAML and XML data binding for Python
https://github.com/emolch/guts
Last synced: 2 months ago
JSON representation
Lightweight declarative YAML and XML data binding for Python
- Host: GitHub
- URL: https://github.com/emolch/guts
- Owner: emolch
- License: mit
- Created: 2013-01-08T13:33:49.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2021-03-23T15:22:12.000Z (over 3 years ago)
- Last Synced: 2024-09-28T10:02:59.493Z (3 months ago)
- Language: Python
- Size: 93.8 KB
- Stars: 18
- Watchers: 4
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README
- License: LICENSE
Awesome Lists containing this project
- awesome-python-models - guts - Lightweight declarative YAML and XML data binding for Python. (Model, Schema)
README
Guts
====
Lightweight declarative YAML and XML data binding for PythonGuts is written by Sebastian Heimann . It is
released under the MIT license. See the file LICENSE for more details.Prerequisites
-------------* PyYAML: Download it from http://pyyaml.org/wiki/PyYAML and follow the given
installation instructions or install it through your system's package
manager (e.g. the `python-yaml` package on Debian based Linuxes).Usage
-----A file `playlist.py` might look like:
```python
from guts import *class Song(Object):
name = String.T()
album = String.T(default='')
artist = String.T(default='')
year = Int.T(optional=True)class Playlist(Object):
xmltagname = 'playlist'
name = String.T(default='Untitled Playlist')
comment = String.T(optional=True)
song_list = List.T(Song.T())
```These classes come with automatic `__init__`:
```pycon
>>> from playlist import *
>>> song1 = Song(name='Metropolis', artist='Kraftwerk')
>>> song2 = Song(name='I Robot', artist='The Alan Parsons Project', album='I Robot')
>>> playlist = Playlist(song_list=[song1,song2])
```They serialize to YAML:
```pycon
>>> print song1.dump()
--- !playlist.Song
name: Metropolis
artist: Kraftwerk
```They also serialize to XML:
```pycon
>>> print playlist.dump_xml()Untitled Playlist
Metropolis
Kraftwerk
I Robot
I Robot
The Alan Parsons Project
```
Objects can validate themselves:
```pycon
>>> song1.validate()
>>> song2.year = 1977
>>> song2.validate()
>>> song2.year = 'abc'
>>> song2.validate()
Traceback (most recent call last):
...
guts.ValidationError: year: "abc" is not of type int
```Objects can regularize themselves:
```pycon
>>> song2.year = '1977'
>>> song2.regularize()
>>> song2.year
1977
>>> type(song2.year)```
They also deserialize from YAML and XML:
```pycon
>>> playlist2 = load_string(playlist.dump())
>>> playlist3 = load_xml_string(playlist.dump_xml())
```Incremental loading of large YAML or XML files is supported with the
`guts.iload_all()` and `guts.iload_all_xml()` functions, which are buildt to
return generators yielding Guts objects.This module comes with a rudimentary code generator `xmlschema-to-guts` to turn
(some) XML Schema definitions (XSD) into Python modules containing Guts class
hierarchies.Here is an example using the first example in the W3C XML Schema Primer. The
Schema shall be defined in `po.xsd`:```xml
Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.
```
A corresponding XML file `po.xml` might look like this:
```xml
Alice Smith
123 Maple Street
Mill Valley
CA
90952
Robert Smith
8 Oak Avenue
Old Town
PA
95819
Hurry, my lawn is going wild
Lawnmower
1
148.95
Confirm this is electric
Baby Monitor
1
39.98
1999-05-21
```
Using the Guts code generator
```bash
$ xmlschema-to-guts po.xsd > po.py
```will produce a Python module `po.py`:
```python
from guts import *class SKU(StringPattern):
pattern = '\\d{3}-[A-Z]{2}'class Comment(String):
xmltagname = 'comment'class Quantity(Int):
passclass USAddress(Object):
country = String.T(default='US', optional=True, xmlstyle='attribute')
name = String.T()
street = String.T()
city = String.T()
state = String.T()
zip = Float.T()class Item(Object):
part_num = SKU.T(xmlstyle='attribute')
product_name = String.T()
quantity = Quantity.T()
us_price = Float.T(xmltagname='USPrice')
comment = Comment.T(optional=True)
ship_date = DateTimestamp.T(optional=True)class Items(Object):
item_list = List.T(Item.T())class PurchaseOrderType(Object):
order_date = DateTimestamp.T(optional=True, xmlstyle='attribute')
ship_to = USAddress.T()
bill_to = USAddress.T()
comment = Comment.T(optional=True)
items = Items.T()class PurchaseOrder(PurchaseOrderType):
xmltagname = 'purchaseOrder'
```And we can use it e.g. to parse the example XML file `po.xml` from above:
```pycon
>>> from po import *
>>> order = load_xml(filename='po.xml')
>>> print order # dumps YAML
--- !po.PurchaseOrder
order_date: '1999-10-20'
ship_to: !po.USAddress
name: Alice Smith
street: 123 Maple Street
city: Mill Valley
state: CA
zip: 90952.0
bill_to: !po.USAddress
name: Robert Smith
street: 8 Oak Avenue
city: Old Town
state: PA
zip: 95819.0
comment: Hurry, my lawn is going wild
items: !po.Items
item_list:
- !po.Item
part_num: 872-AA
product_name: Lawnmower
quantity: 1
us_price: 148.95
comment: Confirm this is electric
- !po.Item
part_num: 926-AA
product_name: Baby Monitor
quantity: 1
us_price: 39.98
ship_date: '1999-05-21'
```