namedtuple from dict / valid identifier from string
Project description
namedtuple from dict / valid identifier from string
# Tested with:
# Python 3.9.13
# Windows 10
pip install namedtuple-from-dict
from namedtuple_from_dict import create_valid_identifier, dict_to_namedtuple
from string import printable
from random import choices, randrange
# startwith will be added if the original string starts with a number
# 9times6 -> aa_9times6
testdict = {
1: "hallo",
"n k q": 223,
(1, 2, 4): {222, 333},
"validkey1": "test",
"validkey2": "tes1t",
"notvalidkey!1": "tes1t",
}
testtuple = dict_to_namedtuple(dic=testdict, name="testdata", startwith="aa_")
print(testtuple)
for i in range(10):
randomchars = "".join(choices(printable, k=randrange(3, 10)))
varname = create_valid_identifier(randomchars, startwith="aa_")
print(f"{randomchars=}")
print(f"{varname=}")
# testdata(aa_1='hallo', n_k_q=223, aa_1_2_4={333, 222}, validkey1='test', validkey2='tes1t', notvalidkey_1='tes1t')
# randomchars=':ga(F'
# varname='ga_F'
# randomchars='lh[)iN>A/'
# varname='lh_iN_A'
# randomchars='fI&mE)'
# varname='fI_mE'
# randomchars='//YW'
# varname='YW'
# randomchars='aY\x0c'
# varname='aY'
# randomchars='k|\ts \x0b'
# varname='k_s'
# randomchars='!kGhJ`'
# varname='kGhJ'
# randomchars='8u*s'
# varname='aa_8u_s'
# randomchars='\\z;_cP'
# varname='z__cP'
# randomchars='e)=Qb\t7p'
# varname='e_Qb_7p'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for namedtuple_from_dict-0.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfa3c0073e7ef58786df4692b829d12ac9f459b2eeb7bcd9600c90c592cf064d |
|
MD5 | d177d32834702b9774e70d5c695a5ff9 |
|
BLAKE2b-256 | 17f16420f251666fb7a7ea74fb66acb7065191b9f49aec9afa05f0066e9c96a3 |
Close
Hashes for namedtuple_from_dict-0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fc67e685acddae8d2533a93e6652cfb82f4428b58e95c8b1edff373bc73724c |
|
MD5 | be43ac152506907d18cade5709683b7e |
|
BLAKE2b-256 | 146c8960080071e75fac40f8327b5f93d025bf109db8b01fd6378b80611e7bb0 |