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
- MAX_LATITUDE = 90
- MAX_LONGITUDE = 180
- MIN_LATITUDE = -90
- MIN_LONGITUDE = -180
- distance(there: mpu.Location) float [source]
Calculate the distance from this location to there.
- Parameters
there (Location) –
- Returns
distance_in_m
- Return type
float
- property latitude: float
Getter for latitude.
- property longitude: float
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: Any, value: Any, tb: Optional[types.TracebackType]) None [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_interval(value: mpu.type.Comparable, min_value: mpu.type.Comparable, max_value: mpu.type.Comparable, name: str = 'variable') None [source]
Raise an exception if value is not in an interval.
- Parameters
value (Comparable) –
min_value (Comparable) –
max_value (Comparable) –
name (str) – Name of the variable to print in exception.
- mpu.parallel_for(loop_function: Callable[[Any], mpu.T], parameters: List[Tuple[Any, ...]], nb_threads: int = 100) List[mpu.T] [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.
nb_threads (int (default: 100)) – The number of threads to use.
- Returns
return_values
- Return type
list of return values