sc_utils#

Miscellaneous utilities for type checking, printing, dates and times, etc.

Note: there are a lot! The design philosophy has been that it’s easier to ignore a function that you don’t need than write one from scratch that you do need.

Highlights:

Classes

autolist

A simple extension to a list that defines add methods to simplify appending and extension.

Link

A class to differentiate between an object and a link to an object.

LazyModule

Create a "lazy" module that is loaded if and only if an attribute is called.

tryexcept

Simple class to catch exceptions in a single line

Functions

fast_uuid

Create a fast UID or set of UIDs.

uuid

Shortcut for creating a UUID; default is to create a UUID4.

dcp

Shortcut to perform a deep copy operation

cp

Shortcut to perform a shallow copy operation

pp

Shortcut for pretty-printing the object.

sha

Shortcut for the standard hashing (SHA) method

traceback

Shortcut for accessing the traceback

getuser

Get the current username

getplatform

Return the name of the current "main" platform (e.g. 'mac').

iswindows

Alias to sc.getplatform('windows')

islinux

Alias to sc.getplatform('linux')

ismac

Alias to sc.getplatform('mac')

isjupyter

Check if a command is running inside a Jupyter notebook.

asciify

Convert an arbitrary Unicode string to ASCII.

urlopen

Download a single URL.

wget

Download a single URL.

download

Download one or more URLs in parallel and return output or save them to disk.

htmlify

Convert a string to its HTML representation by converting unicode characters, characters that need to be escaped, and newlines.

flexstr

Try converting any object to a "regular" string (i.e. str), but proceed if it fails.

sanitizestr

Remove all non-"standard" characters from a string

isiterable

Determine whether or not the input is iterable, with optional types to exclude.

checktype

A convenience function for checking instances.

isnumber

Determine whether or not the input is a number.

isstring

Determine whether or not the input is string-like (i.e., str or bytes).

isarray

Check whether something is a Numpy array, and optionally check the dtype.

toarray

Small function to ensure consistent format for things that should be arrays (note: sc.toarray() and sc.promotetoarray() are identical).

tolist

Make sure object is always a list (note: sc.tolist()/sc.promotetolist() are identical).

promotetoarray

Small function to ensure consistent format for things that should be arrays (note: sc.toarray() and sc.promotetoarray() are identical).

promotetolist

Make sure object is always a list (note: sc.tolist()/sc.promotetolist() are identical).

transposelist

Convert e.g. a list of key-value tuples into a list of keys and a list of values.

swapdict

Swap the keys and values of a dictionary.

mergedicts

Small function to merge multiple dicts together.

mergelists

Merge multiple lists together.

ifelse

For a list of inputs, return the first one that meets the condition

strjoin

Like string join(), but handles more flexible inputs, converts items to strings.

newlinejoin

Alias to strjoin(*args, sep='\n').

strsplit

Convenience function to split common types of strings.

runcommand

Make it easier to run shell commands.

uniquename

Given a name and a list of other names, find a replacement to the name that doesn't conflict with the other names, and pass it back.

suggest

Return suggested item

importbyname

Import modules by name.

importbypath

Import a module by path.

Exceptions

KeyNotFoundError

A tiny class to fix repr for KeyErrors.

LinkException

An exception to raise when links are broken, for exclusive use with the Link class.