Skip to content

Instantly share code, notes, and snippets.

@armancohan
Last active October 5, 2018 17:24
Show Gist options
  • Select an option

  • Save armancohan/082767d05a7eeabd9da287be0acecd40 to your computer and use it in GitHub Desktop.

Select an option

Save armancohan/082767d05a7eeabd9da287be0acecd40 to your computer and use it in GitHub Desktop.
Useful Commands

Things that are often needed and faster to find here than Google.

Create a directory and exist ok:

import pathlib
# create parent of a file
pathlib.Path('file_path').parent.mkdir(parents=True, exist_ok=True)
# create directory
pathlib.Path('directory_path').mkdir(parents=True, exist_ok=True)

Return structure of a dictionary object (without all the values):

import typing


def return_dict_structure(obj):
    """Return the structure of a possibly nested dictionary object."""
    new_obj = {}
    if isinstance(obj, typing.List) or isinstance(obj, typing.Tuple):
        if obj:
            return [return_dict_structure(obj[0]), '...']
        else:
            return []
    elif isinstance(obj, typing.Dict):
        for k, v in obj.items():
            new_obj[k] = return_dict_structure(v)
    else:
        return type(obj)
    return new_obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment