Package horizons :: Package util :: Package python
[hide private]
[frames] | no frames]

Source Code for Package horizons.util.python

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  """ 
23  Put all code here that is not directly related to the game, 
24  but rather a generic enhancement of the programming language. 
25  """ 
26   
27  import collections 
28  from typing import Any, Set, Type 
29   
30  from .decorators import * 
31   
32   
33 -class Const:
34 """An immutable type. Think C++-like const"""
35 - def __setattr__(self, name, value):
36 """Disallow changing an already set attribute. 37 An asymptote to const behavior, which is not supported by python""" 38 if name in self.__dict__: 39 raise Exception("Can't change a Const object") 40 super().__setattr__(name, value)
41 42
43 -def parse_port(port, allow_zero=True):
44 """str2int for network ports. Throws ValueError in case of error.""" 45 port_int = int(port) 46 if allow_zero and port_int == 0: 47 return port_int 48 if not (1 <= port_int <= 65535): 49 raise ValueError('Requires a port between 1 and 65535.') 50 return port_int
51 52
53 -def get_all_subclasses(cls):
54 """Recursivly find all subclasses of a given class.""" 55 result = set() # type: Set[type] 56 for subclass in cls.__class__.__subclasses__(cls): 57 if subclass not in result: 58 result.add(subclass) 59 result.update(get_all_subclasses(subclass)) 60 return result
61 62
63 -def map_balance(value, n, m):
64 """ 65 Maps balance value defined in codomain of [1/n, n] for n >= 1, into linear balance (codomain of [-m, m]), 66 having 1.0 as equal point for n-based balance, and 0.0 as equal point for linear balance. 67 68 @param value: value to be mapped 69 @type value: float 70 @param n: fraction denominator 71 @type n: float 72 @param m: linear boundary 73 @type m: float 74 @return: value mapped into new balance type 75 @rtype: float 76 """ 77 # normally we'd need to have special cases for value < 1.0 and value > 1.0 78 # but we turn cases of 1/n into n instead 79 if value < 1.0: 80 value = 1. / value 81 m *= -1 82 return ((value - 1.0) / (n - 1.0)) * m
83 84
85 -def trim_value(value, min, max):
86 if value < min: 87 return min 88 elif value > max: 89 return max 90 else: 91 return value
92