Skip to main content

Commonly Consumed Code Commodities

Project description

Commonly Consumed Code Commodities Build Status Coverage Status


The reusables library is a reference of python functions and globals that programmers may find themselves often recreating.


import reusables

# {'Section 1': {'key 1': 'value 1', 'key2': 'Value2'}, 'Section 2': {}}

reusables.safe_path('/home/user/eViL User\0\\/newdir$^&*/new^%file.txt')
# '/home/user/eViL User__/newdir____/new__file.txt'

# ['/home/user/background.jpg', '/home/user/private.png']


Most python libraries are designed with the mindset of ‘do exactly what the input dictates, nothing else.’ Which in general is the better way to approach the problem allowing for the developer to fix their code. However reusables is made to work with more human input. The idea is that it will be used in cases like reading user inputted options or working with python directly from the terminal or ipython.

Reusables with try smooth input into what you really wanted it to say. Let’s use joining paths as an example, it’s uncommon to join two root paths together and actually want just the second root path. Nor is it common to have spaces before and after the path or filename and actually want them there.

Reusables fixes your blunders for you:

join_paths('/home', '/user/', ' Desktop/example.file ')
# '/home/user/Desktop/example.file'

However in these cases there is also a ‘strict’ option provided, which should be used if you actually want exactly what you inputted, and are just using the library for reference convenience.

join_paths('/home', '/user/', ' Desktop/example.file ', strict=True)
# '/user/ Desktop/example.file '

What’s in the box?

Here are some of the more useful features included:


Global Definition
exts tuples of common file extensions (documents, music, video and pictures)
regex regular expressions of interest
python3x and python2x, boolean variables
win_based and nix_based, to see what type of platform you are on


Function Definition
join_paths like os.path.join but will remove whitespace and extra separators
join_root base path is automatically current working directory
config_dict reads config file(s) and returns results as a dictionary
config_namespace automatically turns results of config_dict into a Namespace
check_filename see’s if a filename is safe and human readable
safe_filename converts a string into a safe, human readable string that can be used as a filename
safe_path converts a potentially dangerous path into a safe one
file_hash returns a hexdigest of a file’s hash, read in blocks to avoid memory issues
find_all_files hybrid of os.walk and glob.glob, search for files in directory, can specific name and/or extension


Also included is a Namespace class, similar to Bunch but designed so that dictionaries are recursively made into namespaces, and can be treated as either a dict or a namespace when accessed.

from reusables import Namespace

my_breakfast = {"spam" : {"eggs": {"sausage": {"bacon": "yummy"}}}}
namespace_breakfast = Namespace(**my_breakfast)

# yummy

# yummy

# "{'sausage': {'bacon': 'yummy'}}"

# {'sausage': {'bacon': 'yummy'}}

Additional Info

This does not claim to provide the most accurate, fastest or ‘pythonic’ way to implement these useful snippets, this is simply designed for easy reference. Any contributions that would help add functionality or improve existing code is warmly welcomed!

Copyright (c) 2014 - Chris Griffith - MIT License

Project details

Release history Release notifications

History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


This version
History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
reusables-0.1.3-py2.6.egg (22.6 kB) Copy SHA256 hash SHA256 Egg 2.6 Feb 14, 2014
reusables-0.1.3-py2.7.egg (22.4 kB) Copy SHA256 hash SHA256 Egg 2.7 Feb 14, 2014
reusables-0.1.3-py3.2.egg (22.7 kB) Copy SHA256 hash SHA256 Egg 3.2 Feb 14, 2014
reusables-0.1.3-py3.3.egg (23.3 kB) Copy SHA256 hash SHA256 Egg 3.3 Feb 14, 2014
reusables-0.1.3.tar.gz (10.2 kB) Copy SHA256 hash SHA256 Source None Feb 14, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page