Comments (4)
remap is just letting you know that with that enter, absolutely no remapping would occur. If the very first enter returns False
, it's like passing an object that isn't iterable. I can look at improving the error message, perhaps?
from boltons.
Maybe the error message could be improved, but I don't know how to formulate it better :-/
How then I would avoid entering some items? Does this looks good from your point of view:
def enter(path, key, value):
# I'd like to skip `baz's`
if 'baz' in value and isinstance(value, dict):
value.pop('baz')
return default_enter(path, key, value)
print(remap({'foo': 'bar', 'baz': 'qux', 'data': ['foo', 'bar', 'baz']}, enter=enter))
>>> {'foo': 'bar', 'data': ['foo', 'bar', 'baz']}
Originally my problem is next: I have a nested dictionaries, which represents hierarchical configuration object. All children should inherit parent properties, they can append to them or override them. And what I need is to transpose this hierarchical structure, so I can ask each node about its properties taking into account parent properties.
Again, here is an example of my current implementation:
import copy
d = {
'name': 'ROOT',
'properties': ['foo'],
'includes': [
{
'name': 'nested-1',
'properties-override': ['bar', 'baz'],
'includes': [
{'id': '1'},
{'id': '2', 'properties': ['quz']},
{'id': '3'},
]
}
]
}
def traverse(data, parents=None):
result = list()
if parents is None:
parents = list()
if 'id' in data:
assert 'includes' not in data
data['parents'] = copy.deepcopy(parents)
return [data]
else:
includes = data.pop('includes', list())
parents.append(data)
if not isinstance(includes, list):
includes = [includes]
for inc in includes:
child = traverse(inc, parents)
result.extend(child)
parents.pop()
return result
if __name__ == '__main__':
for item in traverse(d):
print(item)
>>> {'id': '1', 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
>>> {'id': '2', 'properties': ['quz'], 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
>>> {'id': '3', 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
Can the same problem be solved with remap
functionality?
Sorry, maybe that's too big/complex question for the ticket.
from boltons.
Okay, looks like I implemented the same traverse method with remap functionality, but I don't know whether it's optimal or not:
import copy
from boltons.iterutils import remap, default_enter, default_exit
d = {
'name': 'ROOT',
'properties': ['foo'],
'includes': [
{
'name': 'nested-1',
'properties-override': ['bar', 'baz'],
'includes': [
{'id': '1'},
{'id': '2', 'properties': ['quz']},
{'id': '3'},
]
}
]
}
def traverse(data=None):
if not data:
data = d
result = list()
parents = list()
def enter(path, key, value):
if 'includes' in value:
includes = value.pop('includes')
parents.append(value)
value = includes
res = default_enter(path, key, value)
return res
def visit(path, key, value):
if 'id' in value and isinstance(value, dict):
item = copy.deepcopy(value)
item['parents'] = copy.deepcopy(parents)
result.append(item)
return key, value
def exit(path, key, old_parent, new_parent, new_items):
if old_parent == parents[-1]:
parents.pop()
res = default_exit(path, key, old_parent, new_parent, new_items)
return res
remap(data, visit=visit, enter=enter, exit=exit)
return result
if __name__ == '__main__':
for item in traverse(d):
print(item)
>>> {'id': '1', 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
>>> {'id': '2', 'properties': ['quz'], 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
>>> {'id': '3', 'parents': [{'name': 'ROOT', 'properties': ['foo']}, {'properties-override': ['bar', 'baz'], 'name': 'nested-1'}]}
from boltons.
Ah, it's still not perfect, but won't bother you with opened ticket.
Thank you :)
from boltons.
Related Issues (20)
- Function to format a list with commas and a final 'and'/'or'
- attrs/dataclasses-style decorator helper
- Mfnvhudhvsdmbc HOT 1
- Test failure with Python 3.11 HOT 1
- Tag for the 23.0.0 release is missing HOT 1
- Include tests in future pypi sdist tarball HOT 5
- Convert list of dict items to list of string items
- `ParsedException.from_string(text).to_string() == text` property violated due to anchors
- `boltons.ecoutils` `23.0.0` breaks `pdb` interactive prompt in `pytest` debug sessions HOT 4
- Non-empty `dictutils.OMD` cannot be loaded from `pickle` HOT 1
- RFC: Make boltons Python 3.7+ only. HOT 8
- Support in-place union for `dictutils.OrderedMultiDict` HOT 1
- [Feature request] Parametrize the delimiter to make glom use any kind of Path delimiter, not just `.` HOT 1
- wraps loses keywords
- chunked filter HOT 3
- tracking some ideas HOT 3
- LRU .values() and dict return old entries HOT 3
- call _orig_default identity
- Names in `boltons.strutils.__all__` with no definitions
- Missing git tags for 23.1.0 & 23.1.1 releases HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from boltons.