Skip to content

simple library to create namedtuple immutable recursively to use dict as global application configuration

License

Notifications You must be signed in to change notification settings

dduraipandian/immutable-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Readme File

Travis CI

codecov test coverage

MIT License

Immutable-Config

Immutable-config library helps to convert python dictionary to namedtuples and converting values to immutable datastructure recursively. Namedtuples, by default, are immutable. You can't modify them. But If you have nested data structures, you can still modify them.

Immutable-config library tries to provides a function to convert them immutable and creates namedtuple from the converted dict.

Dict -> MappingProxyType

List -> Tuple

Set -> Frozenset

UseCase

When "global variables as configuration" should be shared across the project, library will help to make config immutable.

How to install

pip install immutable-config

Requirements

Python 3.6 and above

Code Example

from immutable.immutable import immutable

LOGGING = {
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'myproject.custom': {
            'handlers': {'console', 'mail_admins'},
            'level': 'INFO',
            'filters': ['special']
        }
    }
}

config = immutable('LoggingConfig', LOGGING, only_const=False, recursive=True)

Options

only_const (default = False)

It will create namedtuple from all dictionary keys. When it is set to True, it will only create namedtuple from constants (all caps variables).

recursive (default = False)

Only first level of key-vales are immuted, when it is set to False. When it is set to True, all the key-values are traversed and converted to immutable.

clone (default = True)

Clone the given dictionary and mutates it. deepcopy is a costly operation when dictionary is to many levels of nested with basic python data structure (list, tuple, set, dict).

Setting it to False will improve the performance, but it will mutate the data given to immutable function. So use False, when you do not need to access the data after making it immutable.

properties

Immutable function takes all the key from data given and creates namedtuple from that. If you want to create namedtuple with selected keys, you can pass properties iterable to filter from data.

License

This project is licensed under the MIT License - see the LICENSE file for details

About

simple library to create namedtuple immutable recursively to use dict as global application configuration

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages