Skip to main content

Google Appengine ndb Property Generator written in python. You can convert json data into your custom ndb property by this library.

Project description

https://travis-ci.org/taxpon/ndb_prop_gen.svg https://coveralls.io/repos/github/taxpon/ndb_prop_gen/badge.svg?branch=master

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

  1. Install ndb_prop_gen via pip
pip install ndb_prop_gen
  1. 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.

Files for ndb_prop_gen, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size ndb_prop_gen-0.0.4.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page