[Ask for Review] Scalpl: A lightweight wrapper to operate on nested dictionaries (yet another)
Hi everyone :) I wanted to share with you the work I have done for the past few days. It is the first time for me to make my code public, so I would really appreciate if some of you find time to give me feedbacks and tips regarding this project :) So, here is Scalpl ! https://github.com/ducdetronquito/scalpl It is a lightweight wrapper that helps you to operate on nested dictionaries through the built-in dict API, by using dot-separated string keys. You might find it useful when working with document-oriented database queries, REST APIs, configuration files, etc... It's *not* a drop-in replacement for your dictionaries, just syntactic sugar to avoid this['annoying']['kind']['of']['things'] and prefer['a.different.approach']. The benefits of Scalpl are the following: - Faster than addict or Box. - Allows you to use the entire dict API 'with.this.kind.of.keys'. - Almost no instantiation/conversion cost, it's just a wrapper. You can install it via pip (Python3 only): pip3 install scalpl Have a great week :) ! Guillaume -- https://mail.python.org/mailman/listinfo/python-list
Re: Circular iteration on tuple starting from a specific index
Hi Beppe ! There are some powerful tools in the standard *itertools* module, you should have a look at it :) https://docs.python.org/3/library/itertools.html This is what I would do to cycle over you iterable without making several copies of it. ``` from itertools import islice, chain def cycle_once(iterable, start): return chain(islice(iterable, start, None), islice(iterable, start)) my_iterable = ('A','B','C','D','E','F','G','H') for e in cycle_once(my_iterable, 2): print(i) ``` Ps: I am pretty new to how a mailing list works. If I answered the wrong way, do not hesitate to tell me :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Circular iteration on tuple starting from a specific index
@Gregory Ewing: you were right, your version without *chain* is faster and I quiet like it :) ``` from timeit import timeit from itertools import chain, cycle, islice def cycle_once_with_chain(sequence, start): return chain(islice(sequence, start, None), islice(sequence, start)) def cycle_once_without_chain(sequence, start): return islice(cycle(sequence), start, start + len(sequence)) sequence = tuple(i for i in range(100)) time_with_chain = timeit( stmt='cycle_once_with_chain(sequence, 50)', number=100, globals=globals() ) print('Method with *chain* took: ', (time_with_chain /100), ' per call.') # Method with *chain* took: 5.02595758977e-07 per call. time_without_chain = timeit( stmt='cycle_once_without_chain(sequence, 50)', number=100, globals=globals() ) print('Method without *chain* took: ', (time_without_chain /100), ' per call.') #Method without *chain* took: 3.5880194699984714e-07 per call. ``` @Ian: Good point here, these two methods only works with sequences (list, tuple, string...). I renamed it appropriately in the above sample code :) -- https://mail.python.org/mailman/listinfo/python-list
New release of Scalpl (v0.2.4) ✨🍰✨
Good morning evernyone ! I released a new version (0.2.4) of Scalpl yesterday evening which is available on PyPI :) https://github.com/ducdetronquito/scalpl https://pypi.python.org/pypi/scalpl/ Scalpl is a lightweight wrapper that helps you to operate on nested dictionaries through the built-in dict API, by using dot-separated string keys. It's not a drop-in replacement for your dictionnaries, just syntactic sugar to avoid this['annoying']['kind']['of']['things'] and prefer['a.different.approach']. It aims to keep the power of the standard dict API while being lighter and faster that Box or Addict. This new release allows you to traverse nested list in your dictionnaries: ``` from scalpl import Cut data = {...} proxy = Cut(Data) proxy.update({'users[42][0].skills', 'Python'}) # data['users'][42][0]['skills'] == 'Python ``` It also contains: * A pretty good refactoring of the code base. * Better exceptions * More tests I also tried to improve the README, with a benchmark section and a FAQ. I would really appreciate your feedbacks to improve this project ! Have a great day :) ✨🍰✨ -- https://mail.python.org/mailman/listinfo/python-list
Release of Scalpl (v0.2.5) ✨🍰✨ - a lightweight wrapper for your nested dictionaries
Hi everyone ! I released a new version (0.2.5) of **Scalpl** which is available on PyPI :) https://github.com/ducdetronquito/scalpl You can install it via pip: pip3 install scalpl Scalpl is a lightweight wrapper that helps you to operate on nested dictionaries through the built-in dict API, by using dot-separated string keys. You might find it useful when working with document-oriented database queries, REST APIs, configuration files, etc... It's not a drop-in replacement for your dictionaries, just syntactic sugar to avoid this['annoying']['kind']['of']['things'] and prefer['a.different.approach']. The benefits of Scalpl are the following: * Faster than addict or Box. * Allows you to use the entire dict API 'with.this.kind.of.keys'. * Almost no instantiation/conversion cost, it's just a wrapper. This new release (0.2.5) is just a small API improvement. In the previous version of Scalpl, if you wanted to iterate a list of dictionaries and and operate on it, you would have done the following: ``` data = { 'pokemons': [ { 'name': 'Bulbasaur', 'type': ['Grass', 'Poison'], 'category': 'Seed', 'ability': 'Overgrow' }, { 'name': 'Charmander', 'type': 'Fire', 'category': 'Lizard', 'ability': 'Blaze', }, { 'name': 'Squirtle', 'type': 'Water', 'category': 'Tiny Turtle', 'ability': 'Torrent', } ], 'trainers': [ { 'name': 'Ash', 'hometown': 'Pallet Town' } ] } proxy = Cut(data) pokemons = proxy['pokemons'] for pokemon in Cut.all(pokemons): pokemon.setdefault('moves.Scratch', {'power': 40}) ``` Now, the API allows you to provied composite key directly to the Cut.all method: ``` for pokemon in proxy.all('pokemons'): pokemon.setdefault('moves.Scratch', {'power': 40}) ``` Do not hesitate to give me feedbacks on the module itself, it is one of my first public project ! Have a great afternoon :) -- https://mail.python.org/mailman/listinfo/python-list