Google Appengine ndb Property Generator written in python. You can convert json data into your custom ndb property by this library.
Project description
Google Appengine ndb Property Generator written in python. You can convert json data into your custom ndb property by this library.
What is this?
Simple generator of ndb Property Subclass. Using json to define the property of the class.
Source json sample
{
"name": "book",
"class": "Book",
"props": [
{
"name": "title",
"type": "String",
"default": ""
},
{
"name": "author",
"type": "String",
"default": ""
},
{
"name": "published",
"type": "DateTime",
"default": null
},
{
"name": "price",
"type": "Float",
"default": 10.0
},
{
"name": "read",
"type": "Bool",
"default": false
}
]
}
Generated python sample
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
__all__ = ["Book", "BookModel", "BookProperty", "LocalBookProperty"]
class Book(object):
def __init__(self, title="", author="", published=None, price=10.0, read=False):
self._title = title
self._author = author
self._published = published
self._price = price
self._read = read
@property
def title(self):
return self._title
@property
def author(self):
return self._author
@property
def published(self):
return self._published
@property
def price(self):
return self._price
@property
def read(self):
return self._read
def _prepare_for_put(self):
pass
def _has_repeated(self):
pass
def _to_dict(self):
pass
class BookModel(ndb.Model):
title = ndb.StringProperty(default="")
author = ndb.StringProperty(default="")
published = ndb.DateTimeProperty()
price = ndb.FloatProperty(default=10.0)
read = ndb.BooleanProperty(default=False)
class BookProperty(ndb.StructuredProperty):
def __init__(self, **kwds):
super(BookProperty, self).__init__(BookModel, **kwds)
def _validate(self, value):
assert isinstance(value, Book)
def _to_base_type(self, value):
return BookModel(
title=value.title,
author=value.author,
published=value.published,
price=value.price,
read=value.read,
)
def _from_base_type(self, value):
return Book(
title=value.title,
author=value.author,
published=value.published,
price=value.price,
read=value.read,
)
class LocalBookProperty(ndb.StructuredProperty):
def __init__(self, **kwds):
super(LocalBookProperty, self).__init__(BookModel, **kwds)
def _validate(self, value):
assert isinstance(value, Book)
def _to_base_type(self, value):
return BookModel(
title=value.title,
author=value.author,
published=value.published,
price=value.price,
read=value.read,
)
def _from_base_type(self, value):
return Book(
title=value.title,
author=value.author,
published=value.published,
price=value.price,
read=value.read,
)
Please see the example directory for more examples.
Usage
Install ndb_prop_gen via pip
pip install ndb_prop_gen
Call generate method
via command line
ndb_prop_gen test.json
via python code
import ndb_prop_gen as npg # noqa
# filename is the json's filename
npg.generate(filename)
LICENSE
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ndb_prop_gen-0.0.4.tar.gz
(4.2 kB
view details)
File details
Details for the file ndb_prop_gen-0.0.4.tar.gz
.
File metadata
- Download URL: ndb_prop_gen-0.0.4.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5657347bb296c059d977fe45df54bc3ebf3486b5a135b85255e02196407e68b |
|
MD5 | 2be8f14cc9ffac2970dcbdc12d45f8c9 |
|
BLAKE2b-256 | bf8afb05cc1feb7d9d7b6bbf46fe2c00f72b4f1e5980ea8df109b5e93f986170 |