disbi.utils module¶
Some utility functions used throughout the DISBi app.
-
disbi.utils.camelize(string, uppercase_first_letter=True)¶ Convert a string with underscores to a camelCase string.
Inspired by
inflection.camelize()but even seems to run a little faster.Parameters: Returns: str – The camelized string.
-
disbi.utils.clean_set(cleaned_formset_data)¶ 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='-')¶ 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')¶ 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)¶ 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='_')¶ 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='_')¶ Get sorted ids.
-
disbi.utils.get_optgroups(choice_tup, style='db')¶ 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)¶ Get a list of unique items, even for non hashable items.
-
disbi.utils.merge_dicts(a, b)¶ Merge two dicts without modifying them inplace.
Parameters: Returns: dict – A merged dictionary.
-
disbi.utils.object_view(obj, cols)¶ 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)¶ 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)¶ 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)¶ Order a list a another list that contains the desired order.
Parameters: Returns: list – The sequence ordered according to order.