safe.common.utilities module

Utilities for InaSAFE

class safe.common.utilities.MEMORYSTATUSEX[source]

Bases: _ctypes.Structure

This class is used for getting the free memory on Windows.


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member


Structure/Union member

safe.common.utilities.add_to_list(my_list, my_element)[source]

Helper function to add new my_element to my_list based on its type . Add as new element if it’s not a list, otherwise extend to the list if it’s a list. It’s also guarantee that all elements are unique

  • my_list (list) – A list
  • my_element (str, list) – A new element

A list with unique element

Return type:



Generate list of color in hexadecimal.

This will generate colors using hsv model by playing around with the hue (the saturation and the value are all set to 1).

Parameters:number_of_colour (int) – The number of intervals between R and G spectrum.
Returns:List of color.
Return type:list

Convert a variable to list.

Parameters:var – The variable to be converted.
safe.common.utilities.create_classes(class_list, num_classes)[source]

Create classes from class_list.

Classes will use linspace from numpy. It will extend from min and max of elements in class_list. If min == 0, it won’t be included. The number of classes is equal to num_classes. Please see the unit test for this function for more explanation

  • class_list (list) – All values as a basis to create classes.
  • num_classes (int) – The number of class to hold all values in class_list.
safe.common.utilities.create_label(label_tuple, extra_label=None)[source]

Return a label based on my_tuple (a,b) and extra label.

a and b are string.

The output will be something like:
[a - b] extra_label
safe.common.utilities.feature_attributes_as_dict(field_map, attributes)[source]

Converts list of attributes to dict of attributes.

  • field_map (dict) – Dictionary {‘FieldName’: FieldIndex}.
  • attributes (list) – list of field’s values

Dictionary {‘FieldName’: FieldValue}

Return type:


safe.common.utilities.format_decimal(interval, value)[source]

Return formatted decimal according to interval decimal place For example: interval = 0.33 (two decimal places) my_float = 1.1215454 Return 1.12 (return only two decimal places as string) If interval is an integer return integer part of my_number If my_number is an integer return as is


Format integer with separator between thousands.

x: int - a number to be formatted in a locale friendly way.
str - a locale friendly formatted string e.g. 1,000,0000.00
representing the original x. If a ValueError exception occurs, x is simply returned.

# FIXME (Ole) Currently not using locale coz broken

Instead use this:


Return decimal separator according to the locale.


Return current free memory on the machine. Currently supported for Windows, Linux Return in MB unit


Return current free memory on the machine for linux. Warning : this script is really not robust Return in MB unit


Return current free memory on the machine for mac os. Warning : this script is really not robust Return in MB unit


Return current free memory on the machine for windows. Warning : this script is really not robust Return in MB unit


Return list of key from a list of dictionary.

Parameters:list_dict (list) – List of dict, each dict has key as dictionary key.
Returns:A list of key.
Return type:list
safe.common.utilities.get_non_conflicting_attribute_name(default_name, attribute_names)[source]

Get a non conflicting attribute name from a set of attribute names.

It also complies the shp attribute name restriction that the name length must be less than 10 character.

  • default_name (basestring) – The default name for the attribute.
  • attribute_names (list) – Set of attribute names that should not be conflicted.
safe.common.utilities.get_osm_building_usage(attribute_names, feature)[source]

Get the usage of a row of OSM building data.

  • attribute_names (list) – The list of attribute of the OSM building data.
  • feature (dict) – A row of data representing an OSM building.

The usage of the feature. Return None if it does not find any.

Return type:



Return a truncated decimal by last three digit after leading zero.


Return decimal separator according to the locale.

safe.common.utilities.get_utm_epsg(longitude, latitude)[source]

Return epsg code of the utm zone.

The code is based on the code:


Return utm zone.


Return humanize interval of an array.

For example:

Original Array:                     Result:
1.1  -  5754.1                      0  -  1
5754.1  -  11507.1                  1  -  5,754
                                    5,754  -  11,507

Original Array:                     Result:
0.1  -  0.5                         0  -  0.1
0.5  -  0.9                         0.1  -  0.5
                                    0.5  -  0.9

Original Array:                     Result:
7.1  -  7.5                         0  -  7.1
7.5  -  7.9                         7.1  -  7.5
                                    7.5  -  7.9

Original Array:                     Result:
6.1  -  7.2                         0  -  6
7.2  -  8.3                         6  -  7
8.3  -  9.4                         7  -  8
                                    8  -  9

Return humanize size from bytes.

Parameters:size (float) – The size to humanize in bytes.
Returns:Human readable size.
Return type:unicode
safe.common.utilities.humanize_min_max(min_value, max_value, interval)[source]

Return humanize value format for max and min. If the range between the max and min is less than one, the original value will be returned.

  • min_value (int, float) – Minimum value
  • max_value (int, float) – Maximim value
  • interval (float, int) – The interval between classes in the class list where the results will be used.

A two-tuple consisting of a string for min_value and a string for max_value.

Return type:


safe.common.utilities.is_key_exist(key, list_dictionary)[source]

Check if a key is in list_dictionary’s key

  • key (str) – The key
  • list_dictionary (list) – List of dictionary

True if exist, else False

Return type:


safe.common.utilities.is_subset(element, container)[source]

Check the membership of element from container.

It will check based on the type. Only valid for string and list.

  • element (list, str) – Element that will be searched for in container.
  • container (list, str) – Container that will be checked.

boolean of the membership

Return type:



Get InaSAFE log file path.

Returns:InaSAFE log file path.
Return type:str
safe.common.utilities.project_list(the_list, field)[source]

Return the roman numeral for a number.

Note that this only works for number in interval range [0, 12] since at the moment we only use it on realtime earthquake to conver MMI value.

Parameters:number (float) – The number that will be romanised

:return Roman numeral equivalent of the value :rtype: str


Round an integer to the nearest thousand if my_int is more than a thousand


Obtain the temporary working directory for the operating system.

An inasafe subdirectory will automatically be created under this and if specified, a user subdirectory under that.


You can use this together with unique_filename to create a file in a temporary directory under the inasafe workspace. e.g.

tmpdir = temp_dir(‘testing’) tmpfile = unique_filename(dir=tmpdir) print tmpfile /tmp/inasafe/23-08-2012/timlinux/testing/tmpMRpF_C

If you specify INASAFE_WORK_DIR as an environment var, it will be used in preference to the system temp directory.

Parameters:sub_dir (str) – Optional argument which will cause an additional subdirectory to be created e.g. /tmp/inasafe/foo/
Returns:Path to the temp dir that is created.
Return type:str
Raises:Any errors from the underlying system calls.

Return class as interval without formatting.


Return number without formatting. if something goes wrong in the conversion just return the passed number We catch AttributeError in case the number has no replace method which means it is not a string but already an int or float We catch ValueError if number is a sting but not parseable to a number like the ‘no data’ case

@param number:


Create new filename guaranteed not to exist previously

Use mkstemp to create the file, then remove it and return the name

If dir is specified, the tempfile will be created in the path specified otherwise the file will be created in a directory following this scheme:


See for details.

Example usage:

tempdir = temp_dir(sub_dir=’test’) filename = unique_filename(suffix=’.keywords’, dir=tempdir) print filename /tmp/inasafe/23-08-2012/timlinux/test/tmpyeO5VR.keywords

Or with no preferred subdir, a default subdir of ‘impacts’ is used:

filename = unique_filename(suffix=’.shp’) print filename /tmp/inasafe/23-08-2012/timlinux/impacts/tmpoOAmOi.shp

safe.common.utilities.verify(statement, message=None)[source]

Verification of logical statement similar to assertions.

  • statement (type, bool) – Expression
  • message (str) – error message in case statement evaluates as False


safe.common.utilities.which(name, flags=1)[source]

Search PATH for executable files with the given name.

..note:: This function was taken verbatim from the twisted framework,
licence available here:

On newer versions of MS-Windows, the PATHEXT environment variable will be set to the list of file extensions for files considered executable. This will normally include things like ”.EXE”. This function will also find files with the given name ending with any of these extensions.

On MS-Windows the only flag that has any meaning is os.F_OK. Any other flags will be ignored.

  • name (C{str}) – The name for which to search.
  • flags (C{int}) – Arguments to L{os.access}.

A list of the full paths to files found, in the order in which they were found.

Return type:


safe.common.utilities.zip_shp(shp_path, extra_ext=None, remove_file=False)[source]

Zip shape file and its gang (.shx, .dbf, .prj).

  • shp_path: str - path to the main shape file.
  • extra_ext: [str] - list of extra extensions related to shapefile.
str: full path to the created shapefile