Skip to content

Instantly share code, notes, and snippets.

@angstwad
Last active December 2, 2024 22:38
Show Gist options
  • Save angstwad/40339694f1230c3eb2ad81c5ae51c1bf to your computer and use it in GitHub Desktop.
Save angstwad/40339694f1230c3eb2ad81c5ae51c1bf to your computer and use it in GitHub Desktop.
Python defaultdict which can be assigned to an arbitrary depth
import collections
def infinidict(*args, **kwargs):
""" Magical defaultdict that allows key assignments to an aribtrary depth
Example:
d = infinidict()
d['foo']['bar']['baz'] == 'quux'
d['foo']['bar']['baz'] # result is 'quux'
"""
return collections.defaultdict(infinidict, *args, **kwargs)
# Copyright 2024 Paul Durivage <[email protected]>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment