Skip to main content

Import a json file as a fully functional module complete with classes etc

Project description

It is sometimes useful to be able to use json data to initialise classes and other data structures, giving your application a portable and human readable configuration capability. To do this you will probably write some level of functionality around the json standard library, and use the resulting data loaded from the json file, to populate classes and instances implemented in your application. This separates your data and functionality, which can often present challenges later down the line as you need to keep the data and functionality in step.

It would be better in many cases to be able to combine the data and functionality in a single place, and with the importjson library you can do that. The library allows you to import a json file direct into your python application. The library uses the json data to construct a set of classes, complete with class attributes, and instance data attributes (implemented with set and get descriptors). These classes are presented as a fully formed python module - just as if you had written the code yourself.

Example

The json file called classes.json exists in your applications current directory

{
    "__version__":"0.1",
    "__author__":"Tony",
    "__classes__" : {
    "point": {
        "__doc__": "Example class built from json",
        "__class_attributes__": {
                          "_val1": 1,
                          "_val2": 2
                        },
        "x": 0,
        "y": 0,
        "colour": [0,0,0]
         }
    }
}

Even with the json standard library only, it might take 10 lines or so to bring that json data into your application, and to populate instances of your classes.

With the importjson library you can import this json directly into your application, and create the classes with 2 lines as demonstrated here in the console

>>> import importjson # Importjson library - must be imported before any json files
>>> import classes          # Will import classes.json

The contents of the ‘’classes.json’’ file have now been translated into module attributes and classes, which can be used just as any other module or class. All the values defined in the json file have been translated into module attributes, classes, class attributes, or data attributes as appropriate. With the above json you get a point class with Instance Attributes of x, y & colour.

For full details see [importjson](http://tonyflury.github.io/py-importjson/)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

importjson-0.0.1a4-py2-none-any.whl (18.0 kB view hashes)

Uploaded Python 2

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