Skip to main content

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))

Metadata

Release files for metadocuments 1.4.1

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

Source distribution (sdist)

Source distribution for metadocuments 1.4.1
File Size Uploaded Provenance Actions
metadocuments-1.4.1.tar.gz 5.3 kB Without provenance Details for metadocuments-1.4.1.tar.gz

Release files / metadocuments-1.4.1.tar.gz

Download URL metadocuments-1.4.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
11a82b1382c305310f8466ecf00d1b000d2ccbbcef31575a4c3cedd727e2be09
BLAKE2b-256 checksum
How to use checksums
e4371833241d7249aaca2bd8a5e37057388ec7edab25de6dc9158fc6ecf69459
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for metadocuments 1.4.1

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