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.

Source Distribution

ndb_prop_gen-0.0.4.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file ndb_prop_gen-0.0.4.tar.gz.

File metadata

File hashes

Hashes for ndb_prop_gen-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a5657347bb296c059d977fe45df54bc3ebf3486b5a135b85255e02196407e68b
MD5 2be8f14cc9ffac2970dcbdc12d45f8c9
BLAKE2b-256 bf8afb05cc1feb7d9d7b6bbf46fe2c00f72b4f1e5980ea8df109b5e93f986170

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page