Module: safe.common.utilities

Utilities for InaSAFE

class safe.common.utilities.MEMORYSTATUSEX[source]

This class is used for getting the free memory on Windows

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

Return formated 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_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 (Ismail)

Args:
None
Returns:
int: Return in MB unit
Raises:
ValueError: You should wrap calls to this function in a try...except
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.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_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.

Args:
  • min_value

  • max_value

  • interval - (float): the interval between classes in the 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.
safe.common.utilities.round_thousand(my_int)[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.

Args:
sub_dir str - optional argument which will cause an additional
subirectory to be created e.g. /tmp/inasafe/foo/
Returns:
Path to the output clipped layer (placed in the system temp dir).
Raises:
Any errors from the underlying system calls.
safe.common.utilities.ugettext(s)[source]

Translation support

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

Return class as interval without formating

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 Input

statement: expression message: error message in case statement evaluates as False
Output
None
Raises
VerificationError in case statement evaluates to False
safe.common.utilities.zip_shp(shp_path, extra_ext=None, remove_file=False)[source]

Zip shape file and its gang (.shx, .dbf, .prj) and extra_file is a list of another ext related to shapefile, if exist The zip file will be put in the same directory

This module forms part of the InaSAFE tool.

Previous topic

Module: safe.common.test_geodesy

Next topic

Module: safe.common.interpolation2d

This Page