Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
// | |
// Regular Expression for URL validation | |
// | |
// Author: Diego Perini | |
// Created: 2010/12/05 | |
// Updated: 2018/09/12 | |
// License: MIT | |
// | |
// Copyright (c) 2010-2018 Diego Perini (http://www.iport.it) | |
// |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
Now located at https://github.com/JeffPaine/beautiful_idiomatic_python.
Github gists don't support Pull Requests or any notifications, which made it impossible for me to maintain this (surprisingly popular) gist with fixes, respond to comments and so on. In the interest of maintaining the quality of this resource for others, I've moved it to a proper repo. Cheers!
class DotDict(dict): | |
""" A dictionary whose attributes are accessible by dot notation. | |
This is a variation on the classic `Bunch` recipe (which is more limited | |
and doesn't give you all of dict's methods). It is just like a dictionary, | |
but its attributes are accessible by dot notation in addition to regular | |
`dict['attribute']` notation. It also has all of dict's methods. | |
.. doctest:: |