Utils

exception psynet.utils.DuplicateKeyError[source]

Bases: ValueError

class psynet.utils.NoArgumentProvided[source]

Bases: object

We use this class as a replacement for None as a default argument, to distinguish cases where the user doesn’t provide an argument from cases where they intentionally provide None as an argument.

psynet.utils.cache(user_function, /)[source]

Simple lightweight unbounded cache. Sometimes called “memoize”.

psynet.utils.call_function(function, *args, **kwargs)[source]

Calls a function with *args and **kwargs, but omits any **kwargs that are not requested explicitly.

psynet.utils.classproperty(func)[source]

Defines an analogous version of @property but for classes, after https://stackoverflow.com/questions/5189699/how-to-make-a-class-property.

psynet.utils.countries(locale=None)[source]

List compiled using the pycountry package v20.7.3 with

sorted([(lang.alpha_2, lang.name) for lang in pycountry.countries
    if hasattr(lang, 'alpha_2')], key=lambda country: country[1])
psynet.utils.get_custom_sql_classes()[source]
Returns:

  • A dictionary of all custom SQLAlchemy classes defined in the local experiment

  • (excluding any which are defined within packages).

psynet.utils.get_language()[source]

Returns the language selected in config.txt. Throws a KeyError if no such language is specified.

Returns:

  • A string, for example “en”.

psynet.utils.get_object_from_module(module_name, object_name)[source]

Finds and returns an object from a module.

Parameters:
  • module_name (str) – The name of the module.

  • object_name (str) – The name of the object.

psynet.utils.is_method_overridden(obj, ancestor, method)[source]

Test whether a method has been overridden.

Parameters:
  • obj – Object to test.

  • ancestor (Type) – Ancestor class to test against.

  • method (str) – Method name.

Returns:

  • Returns True if the object shares a method with its ancestor,

  • or False if that method has been overridden.

psynet.utils.languages(locale=None)[source]

List compiled using the pycountry package v20.7.3 with

sorted([(lang.alpha_2, lang.name) for lang in pycountry.languages
    if hasattr(lang, 'alpha_2')], key=lambda country: country[1])
psynet.utils.linspace(lower, upper, length)[source]

Returns a list of equally spaced numbers between two closed bounds.

Parameters:
  • lower (number) – The lower bound.

  • upper (number) – The upper bound.

  • length (int) – The length of the resulting list.

psynet.utils.make_parents(path)[source]

Creates the parent directories for a specified file if they don’t exist already.

Returns:

  • The original path.

psynet.utils.merge_dicts(*args, overwrite)[source]

Merges a collection of dictionaries, with later dictionaries taking precedence when the same key appears twice.

Parameters:
  • *args – Dictionaries to merge.

  • overwrite (bool) – If True, when the same key appears twice in multiple dictionaries, the key from the latter dictionary takes precedence. If False, an error is thrown if such duplicates occur.

psynet.utils.merge_two_dicts(x, y, overwrite)[source]

Merges two dictionaries.

Parameters:
  • x (dict) – First dictionary.

  • y (dict) – Second dictionary.

  • overwrite (bool) – If True, when the same key appears twice in the two dictionaries, the key from the latter dictionary takes precedence. If False, an error is thrown if such duplicates occur.

psynet.utils.negate(f)[source]

Negates a function.

Parameters:

f – Function to negate.

psynet.utils.organize_by_key(lst, key, sort_key=None)[source]

Sorts a list of items into groups.

Parameters:
  • lst – List to sort.

  • key – Function applied to elements of lst which defines the grouping key.

Returns:

  • A dictionary keyed by the outputs of key.

psynet.utils.query_yes_no(question, default='yes')[source]

Ask a yes/no question via raw_input() and return their answer.

“question” is a string that is presented to the user. “default” is the presumed answer if the user just hits <Enter>.

It must be “yes” (the default), “no” or None (meaning an answer is required of the user).

The “answer” return value is True for “yes” or False for “no”.

psynet.utils.require_exp_directory(f)[source]

Decorator to verify that a command is run inside a valid PsyNet experiment directory.

psynet.utils.require_requirements_txt(f)[source]

Decorator to verify that a command is run inside a directory which contains a requirements.txt file.

psynet.utils.serialise(obj)[source]

Serialise objects not serialisable by default