Skip to main content

Advanced Python Dictionary which can link it-self keys into it-self values

Project description

Linked Dictionary

Advanced python dictionary(hash-table), which can link it-self keys and make calculations into the keys of the dict

Installation

Use the package manager pip to install foobar.

pip install linked-dict

Usage

The syntax of expressions:

{'key' : '...$(expression)$...'}

Initialization:

from linked_dict import LinkedDict
from json import dumps

dictionary = LinkedDict({})  # {} - your dict

Examples:

Simple link:

example = LinkedDict(
    {
        'a': 5,
        'b': '$(a)$'
    }
)

print(dumps(example))

    {
       'a': 5,
       'b': 5
    }

Expression with one key. As you can see now I added a space outside this expression, and the final value is string:

example = LinkedDict(
    {
        'a': 5,
        'b': '$(a * 2)$ ' # here with a space
    }
)

print(dumps(example))

    {
       'a': 5,
       'b': '10 '
    }

Link other expressions:

example = LinkedDict(
    {
        'a': 5,
        'b': 100,
        'c': '$(b + d)$',
        'd': '$(b + a)$'
    }
)

print(dumps(example))

    {
        'a': 5,
        'b': 100,
        'c': 205,
        'd': 105
    }

Using all built-in types:

example = LinkedDict(
    {
        'a': ['one_item'],
        'b': '$(a + ["another_item"])$'
    }
)

print(dumps(example))

    {
        'a': ['one_item'],
        'b': ['one_item', 'another_item']
    }

Using your own functions into expressions:

some_func = lambda arg: arg + 1

example = LinkedDict(
    {
        'a': 5,
        'b': '$(some_func(a))$'
    },
    loc=locals(), # to be able to use your functions
    glob=globals() # to be able to use your functions
) 

print(dumps(example))

    {
       'a': 5,
       'b': 5
    }

Changing:

When you change a value, all values that link it change their values too. But links work only in one direction

example = LinkedDict(
    {
        'a': 5,
        'b': '$(a)$'
    }
)
print(example) 
# >>> {'a': 5, 'b': 5}
example['a'] = 'another_val'
print(example)
# >>> {'a': 'another_val', 'b': 'another_val'}
example['b'] = '4'
print(example) 
# >>> {'a': 'another_val', 'b': 4}

# !!! 'b' links 'a', but 'a' doesn't link 'b'

Warning:

1. Keys of your dict must be only strings

{5: 'abc', True: []} # is prohibited
{'5': 'abc', 'True': []} # is allowed

2. Don't make loops of links. Dictionary is protected of this, but you will get the Error

Contributing:

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

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

linked-dict-0.0.2.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file linked-dict-0.0.2.tar.gz.

File metadata

  • Download URL: linked-dict-0.0.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for linked-dict-0.0.2.tar.gz
Algorithm Hash digest
SHA256 655785e2d9f4ca3d014aed7dd81ead868a5567e880d2daf391b803d9bcca834e
MD5 199ffabade8211a9cd812be22ecad5d7
BLAKE2b-256 f049f338bb533db7dfca716d72ca3749d890455b4235a3b2de4af9c99e0ad3a4

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