Utilities¶
Miscellaneous utilities.
-
class
brainscopypaste.utils.
Namespace
(init_dict)[source]¶ Bases:
object
Convert a dict to a namespace by creating a class out of it.
Parameters: 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.
-
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.
Parameters: engine :
sqlalchemy.engine.Engine
The engine to run statement on.
statement : str
A valid SQL statement for engine.
-
brainscopypaste.utils.
find_parent_rel_dir
(rel_dir)[source]¶ Find a relative directory in parent directories.
Searches for directory rel_dir in all parent directories of the current directory.
Parameters: rel_dir : string
The relative directory to search for.
Returns: d : string
Full path to the first found directory.
Raises: NotFoundError
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.
-
brainscopypaste.utils.
init_db
(echo_sql=False)[source]¶ Connect to the database and bind
db
‘s Session object to it.Uses the
DB_USER
andDB_PASSWORD
credentials to connect to PostgreSQL databaseDB_NAME
. It binds the Session object indb
to this engine, and returns the engine object. Note that once this is done, you can directly usesession_scope()
since it uses the right Session object.Parameters: echo_sql : bool, optional
If True, print to stdout all SQL commands sent to the engine; defaults to False.
Returns: The engine connected to the database.
-
brainscopypaste.utils.
is_int
(s)[source]¶ 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).
-
brainscopypaste.utils.
iter_parent_dirs
(rel_dir)[source]¶ Iterate through parent directories of current working directory, appending rel_dir to those successive directories.
-
brainscopypaste.utils.
levenshtein
(s1, s2)[source]¶ Compute the levenshtein distance between strings or lists s1 and s2.
-
brainscopypaste.utils.
memoized
(f)[source]¶ 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).
-
brainscopypaste.utils.
mpl_palette
(n_colors, variation='Set2')[source]¶ Get any seaborn palette as a usable matplotlib colormap.
-
brainscopypaste.utils.
session_scope
()[source]¶ 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.
unpickle
(filename)[source]¶ Load a pickle file at path filename.
This function is
memoized()
so a file is only loaded the first time.