Utitilities API

Various utilities that ripozo uses. More or less a junk drawer

ripozo.utilities.convert_to_underscore(toconvert)[source]

Converts a string from CamelCase to underscore.

>>> convert_to_underscore('CamelCase')
'camel_case'
Parameters:toconvert (str) – The string to convert from CamelCase to underscore (i.e. camel_case)
Returns:The converted string
Return type:str
ripozo.utilities.get_or_pop(dictionary, key, default=None, pop=False)[source]

A simple helper for getting or popping a property from a dictionary depending on the `pop` parameter.

This is a helper method for relationships to easily update whether they keep or remove items from the parent

Parameters:
  • dictionary (dict) – The dictionary to check
  • key (object) – The key to look for in the dictionary
  • default (object) – The default value to return if nothing is found
  • pop (bool) – A boolean that removes the item from the dictionary if true. Otherwise it just gets the value from the dictionary
Returns:

The value of the requested object or the default if it was not found.

Return type:

object

ripozo.utilities.join_url_parts(*parts)[source]

Joins each of the parts with a ‘/’. Additionally, it prevents something like ‘something/’ and ‘/another’ from turning into ‘something//another’ instead it will return ‘something/another’.

>>> join_url_parts('first', 'second', 'last')
'first/second/last'
Parameters:parts (list[unicode|str|int]) – a list of strings to join together with a ‘/’
Returns:The url
Return type:unicode
ripozo.utilities.make_json_safe(obj)[source]

Makes an object json serializable. This is designed to take a list or dictionary, and is fairly limited. This is primarily for the managers when creating objects.

Parameters:obj (object) –
Returns:The json safe dictionary.
Return type:object|six.text_type|list|dict
ripozo.utilities.picky_processor(processor, include=None, exclude=None)[source]

A wrapper for pre and post processors that selectively runs pre and post processors. If the include keyword argument is set, then any method on the Resource that has the same name as the processor will be run. Otherwise it will not be run. On the other hand, if the exclude keyword argument is set then any method on then this preprocessor will not be run for any method on the resource that does have the same name as the strings in the exclude list

def my_preprocessor(resource_class, func_name, request):
    # Do something

class MyResource(CRUD):
    # Only gets run on create and delete
    _preprocessors = [picky_processor(my_preprocessor, include=['create', 'delete'])]
Parameters:
  • processor (method) – A pre or post processor on a ResourceBase subclass. This is the function that will be run if the it passes the include and exclude parameters
  • include (list) – A list of name strings that are methods on the class that for which this processor will be run.
  • exclude (list) –
Returns:

The wrapped function that only runs if the include and exclude parameters are fulfilled.

Return type:

method

ripozo.utilities.titlize_endpoint(endpoint)[source]

Capitalizes the endpoint and makes it look like a title Just to prettify the output of the actions. It capitalizes the first letter of every word and replaces underscores with spaces. It is opinionated in how it determines words. It simply looks for underscores and splits based on that.

Parameters:endpoint (unicode) – The endpoint name on the resource
Returns:The prettified endpoint name
Return type:unicode