Skip to main content

Swiss army knife of data extraction

Project description

glyphs

Examples

Sample response JSON from an API call looks as follows. Let's assume for the example that we keep a reference to that dictionary.

my_json_dict = {
    'expand': 'schema,names',
        'issues':[
                    {
                        'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
                        'fields': {
                            'status': {
                                'description': '',
                                'id': '10001',
                                'name': 'Done',
                                'statusCategory': 
                                            {
                                                'colorName': 'green',
                                                'id': 3,
                                                'key': 'done',
                                                'name': 'Done',
                                            }
                                        }
                                    },
                        'id': '10022',
                        'key': 'XXX-23'},
                     {
                        'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
                        'fields': {
                            'status': {
                                'description': '',
                                'id': '10001',
                                'name': 'Done',
                                'statusCategory': 
                                            {
                                                'colorName': 'green',
                                                'id': 3,
                                                'key': 'done',
                                                'name': 'Done',
                                            }
                                        }
                                    },
                        'id': '10021',
                        'key': 'XXX-22'},
                     ],
    'maxResults': 50,
    'startAt': 0,
    'total': 17
}

How would you get the status & statusCategory's id for each item of the "issue"?

    issues = my_json_dict['issues']

    if issues:
        # add a lot of code to check if the type returns worked for you
        # add a lot of code to handle exceptions

        for issue in issues:
            status_name = issue['fields']['status']['name']
            # add a lot of code to check if the type returned works for you
            # add a lot of code to handle exceptions
            # add a code convert the data or have the rest of your code handle unpredictable returns

            status_id = issue['fields']['status']['statusCategory']['id']
            # add a lot of code to check if the type returned works for you
            # add a lot of code to handle exceptions
            # add a code convert the data or have the rest of your code handle unpredictable returns

This is the shortest you could possibly write it. Now you have to hope for the best from there, write a lot of code to handle exception, data conversion etc.

With Glyphs:

    from glyphs.ro.ROGlyph import ROGlyph
    from glyphs.utils.DictUtils import DictUtils

    issues_glyph = ROGlyph('issues', r_translation_function=list, r_default_value=tuple())
    name_glyph = ROGlyph('fields>status>name', r_translation_function=unicode,)
    cat_id_glyph = ROGlyph('fields>statusCategory>id', r_translation_function=int, r_default_value=-1)

    issues = DictUtils.get(my_json_dict, issues_glyph)

    for issue in issues:
        status_name = DictUtils.get(my_json_dict, name_glyph)
        status_id = DictUtils.get(my_json_dict, cat_id_glyph)

Similar code with a twist:

  1. we know that what we got is precisely what we wanted.
  2. simpler errors and missing data are automatically handled for us (with tasteful defaults)
  3. it also supports functools.partial if we want to make our code shorter
  4. the glyphs are infinitely shareable!! reuse and abuse!
  5. we can swap DictUtils with a different util (any that you want) if the type of my_json_dict changes (in later versions of the code) and everything will still work as we intended.

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

glyphs-0.1.3.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

glyphs-0.1.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file glyphs-0.1.3.tar.gz.

File metadata

  • Download URL: glyphs-0.1.3.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for glyphs-0.1.3.tar.gz
Algorithm Hash digest
SHA256 df443eb4866b4350130b11ec8dd7660040f5d045ff50b1597c283ab0becdadb8
MD5 b0d765ef63f5b13347d011b53dd75d6d
BLAKE2b-256 85312c3a5ceb2245f54bcfaa2509ce2dff43cf1466432784e12733f3b40b24bd

See more details on using hashes here.

File details

Details for the file glyphs-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: glyphs-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for glyphs-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 904fe17af088c1a8b83ff79c5611f523cb83f75e9b514dfe5f17321be9abc57a
MD5 899274af3c47aa8289312014613fcb26
BLAKE2b-256 00c81bbac83820a5ea4653b30b790ddf2b216c86b8a25c78d208afdedbbb5124

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