Skip to main content

marshmallow-sa-core

SQLAlchemy-core integration with marshmallow (de)serialization library.

Declare table

table.json:

{
  "name": "market",
  "fields": [
    {"name": "id", "type": "int", "constraints": {"required": true}},
    {"name": "name", "type": "str", "constraints": {"required": true}},
    {"name": "type", "type": "str"},
    {"name": "city", "type": "str"},
    {"name": "tel", "type": "str"},
    {"name": "address", "type": "str", "constraints": {"unique": true}},
    {"name": "lng", "type": "float", "constraints": {"minimum": -180, "maximum": 180}},
    {"name": "lat", "type": "float", "constraints": {"minimum": -90, "maximum": 90}},
    {"name": "create_time", "type": "datetime"},
    {"name": "update_time", "type": "datetime"}
  ],
  "primaryKey": ["id"]
}

Generate(Deserialize) SQLAlchemy Table

>>> import json
>>> with open('table.json', 'r') as fp:
        table_definition = json.load(fp)
>>> from marshmallow_sa_core import JSONTableSchema
>>> schema = JSONTableSchema()
>>> table = schema.load(table_definition)
>>> table
Table('market',
      MetaData(),
      Column('id', Integer(), table=<market>, primary_key=True, nullable=False),
      Column('name', String(), table=<market>, nullable=False),
...

check DDL of create table:

>>> from sqlalchemy import create_engine
>>> from sqlalchemy.schema import CreateTable
>>> db = create_engine('sqlite:///:memory:')
>>> print(CreateTable(table).compile(db))

CREATE TABLE market (
        id INTEGER NOT NULL, 
        name VARCHAR NOT NULL, 
        type VARCHAR, 
        city VARCHAR, 
        tel VARCHAR, 
        address VARCHAR, 
        lng FLOAT CHECK ("lng" <= 180.0) CHECK ("lng" >= -180.0), 
        lat FLOAT CHECK ("lat" <= 90.0) CHECK ("lat" >= -90.0), 
        create_time DATETIME, 
        update_time DATETIME, 
        PRIMARY KEY (id), 
        UNIQUE (address)
)

Serialize SQLAlchemy Table object

>>> from sqlalchemy import Table, MetaData, Column, Integer, String, DateTime
>>> table = Table(
  'employee',
  MetaData(),
  Column('id', Integer, primary_key=True),
  Column('LastName', String, nullable=False),
  Column('FirstName', String, nullable=False),
  Column('Title', String),
  Column('BirthDate', DateTime),
  Column('HireDate', DateTime),
)
>>> from marshmallow_sa_core import JSONTableSchema
>>> from pprint import pp
>>> pp(JSONTableSchema().dump(table))
{'schema': None,
 'fields': [{'type': 'int',
             'constraints': {'required': True},
             'name': 'id',
             '__version__': '0.0.4'},
            {'type': 'str',
             'constraints': {'required': True},
             'name': 'LastName',
             '__version__': '0.0.4'},
            {'type': 'str',
             'constraints': {'required': True},
             'name': 'FirstName',
             '__version__': '0.0.4'},
            {'type': 'str', 'name': 'Title', '__version__': '0.0.4'},
            {'type': 'datetime', 'name': 'BirthDate', '__version__': '0.0.4'},
            {'type': 'datetime', 'name': 'HireDate', '__version__': '0.0.4'}],
 'name': 'employee',
 'primaryKey': ['id'],
 '__version__': '0.0.4'}

Get it now

$ pip install marshmallow-sa-core

License

MIT licensed. See the bundled LICENSE file for more details.

Metadata

Release files for marshmallow-sa-core 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marshmallow-sa-core 0.0.5
File Size Uploaded Provenance Actions
marshmallow_sa_core-0.0.5.tar.gz 8.9 kB Without provenance Details for marshmallow_sa_core-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for marshmallow-sa-core 0.0.5
File Interpreter ABI Platform Actions
marshmallow_sa_core-0.0.5-py3-none-any.whl Python 3 none any Details for marshmallow_sa_core-0.0.5-py3-none-any.whl

Total release size: 18.5 kB

Release files / marshmallow_sa_core-0.0.5.tar.gz

Download URL marshmallow_sa_core-0.0.5.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b63208cbb8a4219470b8da3f9df7c5672ebe8888e5a04d2553150794ead6074d
BLAKE2b-256 checksum
How to use checksums
1bf7fbfb51f4aa5fc7942e4a10ee837837072e9b64775993a710e16b2140cc56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / marshmallow_sa_core-0.0.5-py3-none-any.whl

Download URL marshmallow_sa_core-0.0.5-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42f4d530ecac8ecc37d3e1d3ce9a986601aa2beb81a6480f3392da31c92bcce3
BLAKE2b-256 checksum
How to use checksums
224a9f6c7f0136d5cc57025c6953132fc80d22b988dff6c26f8a58211a5f68ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Security for marshmallow-sa-core 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page