Miscellaneous utilities.

class brainscopypaste.utils.Namespace(init_dict)[source]

Bases: object

Convert a dict to a namespace by creating a class out of it.


init_dict : dict

The dict you wish to turn into a namespace.

exception brainscopypaste.utils.NotFoundError[source]

Bases: Exception

Signal a file or directory can’t be found.

class brainscopypaste.utils.Stopwords[source]

Bases: object

Detect if a word is a stopword.

Prefer using this module’s stopwords instance of this class for stopword-checking.


Read and load the underlying stopwords file.

class brainscopypaste.utils.cache(method, name=None)[source]

Bases: object

Compute an attribute’s value and cache it in the instance.

This is meant to be used as a decorator on class methods, to turn them into cached computed attributes: the value is computed the first time you access the attribute, and this decorator then replaces the method with the computed value. Any subsequent access gives you the cached value immediately.

Taken from the Python Cookbook (Denis Otkidach).

brainscopypaste.utils.execute_raw(engine, statement)[source]

Execute the raw SQL statement statement on SQLAlchemy engine engine.

Useful to run ANALYZE or VACUUM operations on the database.


engine : sqlalchemy.engine.Engine

The engine to run statement on.

statement : str

A valid SQL statement for engine.


Find a relative directory in parent directories.

Searches for directory rel_dir in all parent directories of the current directory.


rel_dir : string

The relative directory to search for.


d : string

Full path to the first found directory.



If no relative directory is found in the parent directories.

brainscopypaste.utils.grouper(iterable, n, fillvalue=None)[source]

Iterate over n-wide slices of iterable, filling the last slice with fillvalue.

See grouper_adaptive() for a version of this that doesn’t fill the last slice.

brainscopypaste.utils.grouper_adaptive(iterable, n)[source]

Iterate over n-wide slices of iterable, ending the last slice once iterable is empty.

See grouper_adaptive() for a version of this that fills the last slice with a value of your choosing.

brainscopypaste.utils.hamming(s1, s2)[source]

Compute the hamming distance between strings or lists s1 and s2.


Connect to the database and bind db‘s Session object to it.

Uses the DB_USER and DB_PASSWORD credentials to connect to PostgreSQL database DB_NAME. It binds the Session object in db to this engine, and returns the engine object. Note that once this is done, you can directly use session_scope() since it uses the right Session object.


echo_sql : bool, optional

If True, print to stdout all SQL commands sent to the engine; defaults to False.



The engine connected to the database.


Test if s is a string that represents an integer; returns True if so, False in any other case.

brainscopypaste.utils.is_same_ending_us_uk_spelling(w1, w2)[source]

Test if w1 and w2 differ by only the last two letters inverted, as in center/centre (words must be at least 4 letters).


Iterate through parent directories of current working directory, appending rel_dir to those successive directories.


Detect the language of sentence.

brainscopypaste.utils.levenshtein(s1, s2)[source]

Compute the levenshtein distance between strings or lists s1 and s2.


Decorate a function to cache its return value the first time it is called.

If called later with the same arguments, the cached value is returned (not reevaluated).


Create folder if it doesn’t exist.

brainscopypaste.utils.mpl_palette(n_colors, variation='Set2')[source]

Get any seaborn palette as a usable matplotlib colormap.


Provide an SQLAlchemy transactional scope around a series of operations.

Wrap your SQLAlchemy operations (queries, insertions, modifications, etc.) in a with session_scope() as session block to deal with sessions easily. Changes are committed when the block finishes. If an exception occurrs in the block, the session is rolled back and the exception propagated.

brainscopypaste.utils.stopwords = <brainscopypaste.utils.Stopwords object>

Instance of Stopwords to be used for stopword-testing.

brainscopypaste.utils.subhamming(s1, s2)[source]

Compute the minimum hamming distance between s2 and all sublists of s1 as long as s2, returning (distance, sublist start in s1).

brainscopypaste.utils.sublists(s, l)[source]

Get all sublists of s of length l.


Load a pickle file at path filename.

This function is memoized() so a file is only loaded the first time.