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

Uploaded Source

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