disbi.utils module

Some utility functions used throughout the DISBi app.

disbi.utils.camelize(string, uppercase_first_letter=True)[source]

Convert a string with underscores to a camelCase string.

Inspired by inflection.camelize() but even seems to run a little faster.

Parameters:
  • string (str) – The string to be converted.
  • uppercase_first_letter (bool) – Determines whether the first letter of the string should be capitalized.
Returns:

str – The camelized string.

disbi.utils.clean_set(cleaned_formset_data)[source]

Return a dictionary with keys from POST and lists as values.

The cleaned_data of a formset is a list of dictionaries that can have overlapping keys. The function changes that into a dictionary where each key corresponds to list of values that belonged to the same key.

Returns:dict – Dictionary with items joined on keys.
disbi.utils.construct_none_displayer(entries, placeholder='-')[source]

Return a string to be used as a placeholder for an empty option.

The length of the placeholder is based on the length of the longest option in a list of entries.

disbi.utils.get_choices(choice_tup, style='db')[source]

Return the choices given on a model Field.

Parameters:choice_tup – The choice tuple given in the model.
Keyword Arguments:
 style – Determines whether the human readable “display” values should be returned or those from the “db”.
disbi.utils.get_hr_val(choices, db_val)[source]

Get the human readable value for the DB value from a choice tuple.

Parameters:
  • choices (tuple) – The choice tuple given in the model.
  • db_val – The respective DB value.
Returns:

The matching human readable value.

disbi.utils.get_id_str(objects, delimiter='_')[source]

Get a string of sorted ids, separated by a delimiter.

Parameters:objects (Model) – An iterable of model instances.
Keyword Arguments:
 delimiter (str) – The string the ids will be joined on.
Returns:str – The joined and sorted id string.
disbi.utils.get_ids(string, delimiter='_')[source]

Get sorted ids.

disbi.utils.get_optgroups(choice_tup, style='db')[source]

Parse the choices given on a model Field and map them to their groups.

Parameters:choice_tup – The choice tuple given in the model.
Keyword Arguments:
 style – Determines whether the human readable “display” values should be returned or those from the “db”.
Returns:dict – A list of choices mapped to their optgroup.
disbi.utils.get_unique(items)[source]

Get a list of unique items, even for non hashable items.

disbi.utils.merge_dicts(a, b)[source]

Merge two dicts without modifying them inplace.

Parameters:
  • a (dict) – The first dict.
  • b (dict) – The second dict. Overrides a on conflicts.
Returns:

dict – A merged dictionary.

disbi.utils.object_view(obj, cols)[source]

Construct an external representation of an object based on a tuple of field/column names.

Each column name is either expected to be a method or an attribute of the object. For the keys of the dict, the short_description is preferred. For attributes verbose_name is preferred over name.

Parameters:
  • obj – Any object with cols as attributes.
  • cols (tuple) – The column names.
Returns:

OrderedDict – The headers as keys and the entries as values.

disbi.utils.remove_optgroups(choices)[source]

Remove optgroups from a choice tuple.

Parameters:choices (tuple) – The choice tuple given in the model.
Returns:The n by 2 choice tuple without optgroups.
disbi.utils.reverse_dict(dic)[source]

Return a reversed dictionary.

Each former value will be the key of a list of all keys that were mapping to it in the old dict.

disbi.utils.sort_by_other(sequence, order)[source]

Order a list a another list that contains the desired order.

Parameters:
  • sequence (list) – The list that is ordered. All items must be contained in order.
  • order (list) – The list containing the order.
Returns:

list – The sequence ordered according to order.

disbi.utils.zip_dicts(a, b)[source]

Merge two dicts, choose none empty value on key conflicts.

The dicts are not modified inplace, but returned.

Parameters:
  • a (dict) – The first dict.
  • b (dict) – The second dict. Overrides a on conflicts, when both values are none emtpy.
Returns:

dict – A merged dictionary.