Skip to main content

Library for defining structured documents (JSON, YAML) as classes. Created as a metaprogramming exercise

Project description

MetaDocuments

Installation

pip install metadocuments

Usage

Decorate the document with @Metadocument to specify that it (and all its children) should be treated as a metadocument. This adds functions to_dict, to_json and to_yaml to the class instance which then can be called to create a corresponding structure. Classes can contain methods, but those are ignored unless they are decorated with @property. Inheritance and multiple inheritance do work, the @Metadocument decorator is only required for the parent class.

Example

@MetaDocument
class SubObject:
    a = 2
    b = "c"

@Metadocument
class MyClass:
    a = 1
    b = SubObject()

print(MyClass().to_json(indent=4))
# {
#     "a": 1,
#     "b": {
#         "a": 2,
#         "b": "c"
#     }
# }

Example 2

@Metadocument
class MyClass:
    a = 1

class Child(MyClass):
    b = 2
    c = "c"

print(Child().to_json(indent=4))
# {
#     "a": 1,
#     "b": 2,
#     "c": "c"
# }

Example 3

Using FromKeywords helper class. This can be helpful when there are structures that one does not want to define as class. All the keywords are mapped 1:1 to a attribute / key.

if __name__ == "__main__":
    @Metadocument
    class Foobar():
        a = 1
        b = Field(key="v-1", value="somevalue")
        c = FromKeywords(
            d="a",
            e="b",
            f=FromKeywords(
                g=1,
                asd=FromKeywords(
                    a = "b"
                )
            )
        )
    print(Foobar().to_json(indent=4))

Limitations

Because of Python syntax abuse, there is no support for numeric keys unfortunately. If those are necessity, then old-school dicts are best option (for now).

FromKeywords does inherit dict so it is possible to use it inside a dictionary which then can be serialized to JSON.

if __name__ == "__main__":
    @Metadocument
    class Foobar:
        a = 1
        b = 2
        c = FromKeywords(
            d="a",
            e="b",
            f=FromKeywords(
                g=1,
                asd=FromKeywords(
                    a = "b"
                )
            )
        )

    some_dict = {
        "foo": "bar",
        1: "asd",
        "afgg": FromKeywords(
            a=1, b=2
        ),
        "ccc": Foobar().to_dict()
    }
    print(Foobar().to_json(indent=4))
    print(json.dumps(some_dict))

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

metadocuments-1.4.1.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file metadocuments-1.4.1.tar.gz.

File metadata

  • Download URL: metadocuments-1.4.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.12

File hashes

Hashes for metadocuments-1.4.1.tar.gz
Algorithm Hash digest
SHA256 11a82b1382c305310f8466ecf00d1b000d2ccbbcef31575a4c3cedd727e2be09
MD5 938d1d8da0d8cc8dacf21f460922cfc2
BLAKE2b-256 e4371833241d7249aaca2bd8a5e37057388ec7edab25de6dc9158fc6ecf69459

See more details on using hashes here.

Supported by

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