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.

dwLength

Structure/Union member

dwMemoryLoad

Structure/Union member

sullAvailExtendedVirtual

Structure/Union member

ullAvailPageFile

Structure/Union member

ullAvailPhys

Structure/Union member

ullAvailVirtual

Structure/Union member

ullTotalPageFile

Structure/Union member

ullTotalPhys

Structure/Union member

ullTotalVirtual

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

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

A list with unique element

Return type:

list

safe.common.utilities.color_ramp(number_of_colour)[source]

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
safe.common.utilities.convert_to_list(var)[source]

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

Parameters:
  • 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.

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

Dictionary {‘FieldName’: FieldValue}

Return type:

dict

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

safe.common.utilities.format_int(x)[source]

Format integer with separator between thousands.

Args:
x: int - a number to be formatted in a locale friendly way.
Returns:
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.
Raises:
None
From http://stackoverflow.com/questions/5513615/
add-thousands-separators-to-a-number

# FIXME (Ole) Currently not using locale coz broken

Instead use this: http://docs.python.org/library/string.html#formatspec

safe.common.utilities.get_decimal_separator()[source]

Return decimal separator according to the locale.

safe.common.utilities.get_free_memory()[source]

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

safe.common.utilities.get_free_memory_linux()[source]

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

safe.common.utilities.get_free_memory_osx()[source]

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

safe.common.utilities.get_free_memory_win()[source]

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

safe.common.utilities.get_list_key(list_dict)[source]

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.

Parameters:
  • 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.

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

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

Return type:

str

safe.common.utilities.get_significant_decimal(my_decimal)[source]

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

safe.common.utilities.get_thousand_separator()[source]

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: http://gis.stackexchange.com/questions/34401

safe.common.utilities.get_utm_zone(longitude)[source]

Return utm zone.

safe.common.utilities.humanize_class(my_classes)[source]

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
safe.common.utilities.humanize_file_size(size)[source]

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.

Parameters:
  • 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.
Returns:

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

Return type:

tuple

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

Check if a key is in list_dictionary’s key

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

True if exist, else False

Return type:

bool

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.

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

boolean of the membership

Return type:

bool

safe.common.utilities.log_file_path()[source]

Get InaSAFE log file path.

Returns:InaSAFE log file path.
Return type:str
safe.common.utilities.project_list(the_list, field)[source]
safe.common.utilities.romanise(number)[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

safe.common.utilities.round_thousand(value)[source]

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

safe.common.utilities.temp_dir(sub_dir='work')[source]

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.

Note

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.
safe.common.utilities.unhumanize_class(my_classes)[source]

Return class as interval without formatting.

safe.common.utilities.unhumanize_number(number)[source]

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:

safe.common.utilities.unique_filename(**kwargs)[source]

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:

:file:’/tmp/inasafe/<dd-mm-yyyy>/<user>/impacts’

See http://docs.python.org/library/tempfile.html 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.

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

VerificationError

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: http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.2.0/LICENSE

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.

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

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

Return type:

C{list}

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

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

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