Skip to main content

Commonly Consumed Code Commodities

Project description

Commonly Consumed Code Commodities Build Status Coverage Status

Overview

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

Example

import reusables

reusables.config_dict('my_config.cfg')
# {'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'

reusables.find_all_files(".", ext=reusables.exts.pictures)
# ['/home/user/background.jpg', '/home/user/private.png']

Design

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:

Globals

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

Functions

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

Extras

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)

print(namespace_breakfast.spam.eggs.sausage.bacon)
# yummy

print(namespace_breakfast.spam.eggs['sausage'].bacon)
# yummy

str(namespace_breakfast['spam'].eggs)
# "{'sausage': {'bacon': 'yummy'}}"

dict(namespace_breakfast.spam.eggs['sausage'])
# {'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


Download files

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

Source Distribution

reusables-0.1.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

reusables-0.1.3-py3.3.egg (23.3 kB view details)

Uploaded Egg

reusables-0.1.3-py3.2.egg (22.7 kB view details)

Uploaded Egg

reusables-0.1.3-py2.7.egg (22.4 kB view details)

Uploaded Egg

reusables-0.1.3-py2.6.egg (22.6 kB view details)

Uploaded Egg

File details

Details for the file reusables-0.1.3.tar.gz.

File metadata

  • Download URL: reusables-0.1.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for reusables-0.1.3.tar.gz
Algorithm Hash digest
SHA256 db77da4fd21c48dab9477361b910a63917d6240877d716b6837d9bf8c8cb276b
MD5 7575dd0825bff3ad8b3d7090f91a0c9a
BLAKE2b-256 e2d1716433889cbea291d2f07a3c3c59dfdb64814d163495a7a43266f715f8a5

See more details on using hashes here.

File details

Details for the file reusables-0.1.3-py3.3.egg.

File metadata

  • Download URL: reusables-0.1.3-py3.3.egg
  • Upload date:
  • Size: 23.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for reusables-0.1.3-py3.3.egg
Algorithm Hash digest
SHA256 bfee45049a9072b44bc874283bdfdaeb72e9f1f5885995d3b96e41d6297007fe
MD5 5f134b9b5aa484dff78bf868e5c76fac
BLAKE2b-256 ef11e646fe4ebf34b3c2f5f93926a06f3513e955b1725173846183e099b5b58e

See more details on using hashes here.

File details

Details for the file reusables-0.1.3-py3.2.egg.

File metadata

  • Download URL: reusables-0.1.3-py3.2.egg
  • Upload date:
  • Size: 22.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for reusables-0.1.3-py3.2.egg
Algorithm Hash digest
SHA256 1d0240f97a91a2905162bdebc9f91303867b280dbde35e0b953cc4fee56103b4
MD5 7896e769e2ed485111c56d1af4d9559f
BLAKE2b-256 2522935d7ed2072ab415f135b30f4d9d42c835177085090b5d43db27ddbddad9

See more details on using hashes here.

File details

Details for the file reusables-0.1.3-py2.7.egg.

File metadata

  • Download URL: reusables-0.1.3-py2.7.egg
  • Upload date:
  • Size: 22.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for reusables-0.1.3-py2.7.egg
Algorithm Hash digest
SHA256 69de61905f85fbc3d62910b2463fe6debcbbe8e75f76d16565f38421a27046a9
MD5 3ff08715e7911f5a055add0f775a43d9
BLAKE2b-256 73060185321bff7a8d48ed800df0e9020129edadd404a2d64502a5d6146ad7d0

See more details on using hashes here.

File details

Details for the file reusables-0.1.3-py2.6.egg.

File metadata

  • Download URL: reusables-0.1.3-py2.6.egg
  • Upload date:
  • Size: 22.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for reusables-0.1.3-py2.6.egg
Algorithm Hash digest
SHA256 562599e229ecfa84177594f439137b9143b6b9b013836434293a6a3859c852f2
MD5 dfd79fba9b88b4500765c2b712a1f81e
BLAKE2b-256 a85330fad45f8cf513e729b780b4a00f871ce1c992f2017b74fa11e9939a2b0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page