mpu

mpu: Martins Python Utilities.

class mpu.Location(latitude: float, longitude: float)[source]

Bases: object

Define a single point.

Parameters
  • latitude (float) – in [-90, 90] - from North to South

  • longitude (float) – in [-180, 180] - from West to East

distance(there: mpu.Location) → float[source]

Calculate the distance from this location to there.

Parameters

there (Location) –

Returns

distance_in_m

Return type

float

Get a Google Maps link to this location.

property latitude

Getter for latiutde.

property longitude

Getter for longitude.

mpu.clip(number: Union[int, float], lowest: Union[None, int, float] = None, highest: Union[None, int, float] = None) → Union[int, float][source]

Clip a number to a given lowest / highest value.

Parameters
  • number (number) –

  • lowest (number, optional) –

  • highest (number, optional) –

Returns

clipped_number

Return type

number

Examples

>>> clip(42, lowest=0, highest=10)
10
mpu.consistent_shuffle(*lists: List[List[Any]]) → Tuple[List[Any], …][source]

Shuffle lists consistently.

Parameters

*lists – Variable length number of lists

Returns

shuffled_lists – All of the lists are shuffled consistently

Return type

tuple of lists

Examples

>>> import mpu, random; random.seed(8)
>>> mpu.consistent_shuffle([1,2,3], ['a', 'b', 'c'], ['A', 'B', 'C'])
([3, 2, 1], ['c', 'b', 'a'], ['C', 'B', 'A'])
mpu.exception_logging(exctype, value, tb)[source]

Log exception by using the root logger.

Use it as sys.excepthook = exception_logging.

Parameters
  • exctype (type) –

  • value (NameError) –

  • tb (traceback) –

mpu.haversine_distance(origin: Tuple[float, float], destination: Tuple[float, float]) → float[source]

Calculate the Haversine distance.

Parameters
  • origin (Tuple[float, float]) – (lat, long)

  • destination (Tuple[float, float]) – (lat, long)

Returns

distance_in_km

Return type

float

Examples

>>> munich = (48.1372, 11.5756)
>>> berlin = (52.5186, 13.4083)
>>> round(haversine_distance(munich, berlin), 1)
504.2
>>> new_york_city = (40.712777777778, -74.005833333333)  # NYC
>>> round(haversine_distance(berlin, new_york_city), 1)
6385.3
mpu.is_in_intervall(value, min_value, max_value, name='variable')[source]

Raise an exception if value is not in an interval.

Parameters
  • value (orderable) –

  • min_value (orderable) –

  • max_value (orderable) –

  • name (str) – Name of the variable to print in exception.

mpu.parallel_for(loop_function: Callable[Any, Any], parameters: List[Tuple[Any, …]], nb_threads: int = 100) → List[Any][source]

Execute the loop body in parallel.

Note

Race-Conditions Executing code in parallel can cause an error class called “race-condition”.

Parameters
  • loop_function (Callable) – Python function which takes a tuple as input

  • parameters (List[Tuple]) – Each element here should be executed in parallel.

Returns

return_values

Return type

list of return values