sciris.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:
  • dcp(): shortcut to copy.deepcopy()

  • pp(): shortcut to pprint.pprint()

  • isnumber(): checks if something is any number type

  • tolist(): converts any object to a list, for easy iteration

  • toarray(): tries to convert any object to an array, for easy use with numpy

  • mergedicts(): merges any set of inputs into a dictionary

  • mergelists(): merges any set of inputs into a list

  • runcommand(): simple way of executing a shell command

  • download(): download multiple URLs in parallel

Functions

asciify

Convert an arbitrary Unicode string to ASCII.

checktype

A convenience function for checking instances.

compareversions

Function to compare versions, expecting both arguments to be a string of the format 1.2.3, but numeric works too.

cp

Shortcut to perform a shallow copy operation

dcp

Shortcut to perform a deep copy operation

download

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

fast_uuid

Create a fast UID or set of UIDs.

flexstr

Try converting any object to a "regular" string (i.e.

freeze

Alias for pip freeze.

getcaller

Try to get information on the calling function, but fail gracefully.

getplatform

Return the name of the current platform: 'linux', 'windows', 'mac', or 'other'.

gitinfo

Retrieve git info

htmlify

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

importbyname

Import modules by name.

isarray

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

isiterable

Simply determine whether or not the input is iterable.

islinux

Alias to sc.getplatform('linux')

ismac

Alias to sc.getplatform('mac')

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).

iswindows

Alias to sc.getplatform('windows')

mergedicts

Small function to merge multiple dicts together.

mergelists

Merge multiple lists together.

newlinejoin

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

pp

Shortcut for pretty-printing the object.

promotetoarray

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

promotetolist

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

require

Check whether environment requirements are met.

runcommand

Make it easier to run shell commands.

sha

Shortcut for the standard hashing (SHA) method

strjoin

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

strsplit

Convenience function to split common types of strings.

suggest

Return suggested item

swapdict

Swap the keys and values of a dictionary.

toarray

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

tolist

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

traceback

Shortcut for accessing the traceback

transposelist

Convert e.g.

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.

urlopen

Download a single URL.

uuid

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

wget

Download a single URL.

Classes

LazyModule

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

Link

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

autolist

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

prettyobj

Use pretty repr for objects, instead of just showing the type and memory pointer (the Python default for objects).

tryexcept

Simple class to catch exceptions in a single line -- effectively an alias to contextlib.suppress.

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.