Skip to main content

Library created to map keys in dictionaries.

Project description

With this library you can map your dictionaries easily, you just need to pass it:

1- output: the dictionary you want to receive, the variables you want to map must go with {}

from DictMaper import MapDict
result = MapDict(
    output={
        "{var_1}": "Hi! {var_2}, welcome to {company_name}"
        "company name":"{company_name}"
    }
    ......
)

2- context: context is the data from which the information will be obtained:

from DictMaper import MapDict
result = MapDict(
    output={
        "{var_1}": "Hi! {var_2}, welcome to {company_name}"
        "company name":"{company_name}"
    }
    context={
        "user": {
          "name":"user name",
          "email":"test_email@..."
        }
        "company":{
         "information":{
            "name":"Company name",
            "id":"1234"
            }
        }
   }
)

3- vars: vars are the variables you want to map to your output and the value is where they are located within context:

from DictMaper import MapDict
result = MapDict(
    output={
        "{var_1}": "Hi! {var_2}, welcome to {company_name}",
        "company":"{company_name}"
    }
    context={
        "user": {
          "name":"user name",
          "email":"test_email@..."
        },
        "company":{
         "information":{
            "name":"Company name",
            "id":"1234"
            }
        }
   }
   vars={
     "var_1": "user.email",
     "var_2": "user.name",
     "company_name": "company.information.name"
   }
)

That’s all, process the data making .process() and as a result you will have:

    from DictMaper import MapDict
    result = MapDict(...).process()
    {
        "test_email@...": "Hi! user name, welcome to Company name",
        "company":"Company name"
    }
    ```

If your context dictionary contains nested dictionaries or lists, you can use this flag variable complex_dict_mapping=True to process more complex dictionaries.
But you must take into account the following restrictions:
1- If the 'context' dictionary contains a key with a list, the name of the key must be the same in the 'vars' dictionary and in the 'output'
2- You can only search at level 1 depth in the keys that contain LISTS in the 'context' dictionary, where the paths defined in the 'variables' dictionary will be searched.
3- It is not possible to place a variable from the 'vars' dictionary in the 'output' dictionary as a key in any of the values ​​of the dictionaries that are within some list that are within the same 'output' dictionary.

Example:
1- output: the dictionary you want to receive, the variables you want to map must go with {}
```python
    output = {
        "contacts": [{
            "name": "{var_name}",
            "email": "{var_email}",
            "adresses": [{"main address": "{var_main_address}"}]
        }],
        "company name":"{company_name}"
    }

2- context: context is the data from which the information will be obtained:

context = {
    "contacts": [
        {
            "name":"user name",
            "email":"test_email@...",
            "address": {"main": "customer address"}
        },
        {
            "name":"user2 name2",
            "email":"test2_email2@...",
            "address": {"main": "customer address2"}
        }
    ],
    "company":{
        "information":{
            "name":"Company name",
            "id":"1234",
        }
    }
}

3- vars: vars are the variables you want to map to your output and the value is where they are located within context:

vars = {
    "contacts": [{
        "var_name": "name",
        "var_email": "email",
        "var_main_address": "address.main",
    }],
    "company_name": "company.information.name",
}

That’s all, process the data doing .process() and as a result you will have: `python from DictMaper import MapDict result = MapDict( output=output, context=context, vars=vars, complex_dict_mapping=True ) result.process() { "contacts":[ { "name":"user name", "email":"test_email@...", "adresses":[ { "main address":"customer address" } ] }, { "name":"user2 name2", "email":"test2_email2@...", "adresses":[ { "main address":"customer address2" } ] } ], "company name":"Company name" }

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

dictmaper-0.0.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictmaper-0.0.4-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file dictmaper-0.0.4.tar.gz.

File metadata

  • Download URL: dictmaper-0.0.4.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.17

File hashes

Hashes for dictmaper-0.0.4.tar.gz
Algorithm Hash digest
SHA256 747fc854dcde25ab1082ad8d4b56c10b7600e3cb94fc4c27f3f96d00967fa302
MD5 eb74bb71d3c5541aae7f716eb14dab02
BLAKE2b-256 74c69a0bc60652b47acf2852a7c679e55d685a73bfd94b668589699e21469d27

See more details on using hashes here.

File details

Details for the file dictmaper-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dictmaper-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.17

File hashes

Hashes for dictmaper-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b2caeb1581b74a80c601fea574e4fa95b881d58ed5d021908f24ebdb7faeda48
MD5 0c362a1ecf387739151b3499da45fa8f
BLAKE2b-256 5e083d3dcccf901d2d4bf6af22298249704965e3988af289b03d9107c8476636

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