[90] | 1 | from __future__ import annotations
|
---|
| 2 |
|
---|
| 3 | from copy import deepcopy
|
---|
| 4 | from email._header_value_parser import Parameter
|
---|
| 5 | from typing import Dict, Any, List, Optional
|
---|
| 6 | from numbers import Number
|
---|
| 7 |
|
---|
| 8 | from pyson.JsonGetter import JsonGetter
|
---|
| 9 | from pyson.JsonValue import JsonValue
|
---|
| 10 | from geniusweb.utils import toStr, HASH
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | class Parameters:
|
---|
| 14 | '''
|
---|
| 15 | init parameters for a party. Object must be either a HashMap, List, String or
|
---|
| 16 | Number. Other objects may not deserialize properly. We never use blanket
|
---|
| 17 | Object deserializers (enableDefaultTyping) because of security reasons.
|
---|
| 18 | '''
|
---|
| 19 |
|
---|
| 20 | def __init__(self, parameters:Dict[str, Any]={}) :
|
---|
| 21 | '''
|
---|
| 22 | @param parameters dict of key-value pairs. values can be primitive
|
---|
| 23 | object, but also a list or dict. The value will be used 'as is',
|
---|
| 24 | so this will not be parsed as a geniusweb object but kept as list/dict.
|
---|
| 25 | '''
|
---|
| 26 | self._params = deepcopy(parameters);
|
---|
| 27 | # deepcopy to ensure our copy is immutable
|
---|
| 28 |
|
---|
| 29 | def get(self, key:str) -> object:
|
---|
| 30 | '''
|
---|
| 31 | @param key the key to get the value for.
|
---|
| 32 | @return the raw value, or none if key not in parameters.
|
---|
| 33 | '''
|
---|
| 34 | if not key in self._params:
|
---|
| 35 | return None
|
---|
| 36 | return self._params[key]
|
---|
| 37 |
|
---|
| 38 | @JsonValue()
|
---|
| 39 | def getParameters(self) -> Dict[str, Any]:
|
---|
| 40 | return deepcopy(self._params)
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | def isEmpty(self) -> bool:
|
---|
| 44 | '''
|
---|
| 45 | @return true iff params is empty
|
---|
| 46 | '''
|
---|
| 47 | return len(self._params)==0
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | def containsKey(self, key:str) -> bool:
|
---|
| 51 | '''
|
---|
| 52 | @param key the key to be checked
|
---|
| 53 | @return true iff params contains the given key
|
---|
| 54 | '''
|
---|
| 55 | return key in self._params
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | def With(self, key:str, val:object) -> "Parameters" :
|
---|
| 59 | '''
|
---|
| 60 | @param key the key. Key may already in params.
|
---|
| 61 | @param val the new value for the key
|
---|
| 62 | @return new Parameters , which is copy of this but with key-value pair
|
---|
| 63 | added/overridden
|
---|
| 64 | '''
|
---|
| 65 | newparams = dict(self._params) # our params are immutable
|
---|
| 66 | newparams[key]= val
|
---|
| 67 | return Parameters(newparams)
|
---|
| 68 |
|
---|
| 69 | def WithParameters(self, parameters:"Parameters"):
|
---|
| 70 | '''
|
---|
| 71 | @param parameters the parameters to be added/overridden
|
---|
| 72 | @return new Parameters, with new parameters ADDED to existing. The new
|
---|
| 73 | parameters override existing ones.
|
---|
| 74 | '''
|
---|
| 75 | newparams = dict(self._params)
|
---|
| 76 | newparams.update(parameters._params)
|
---|
| 77 | return Parameters(newparams)
|
---|
| 78 |
|
---|
| 79 | def getType(self, paramname:str, classType):
|
---|
| 80 | '''
|
---|
| 81 | Getter with type-check
|
---|
| 82 |
|
---|
| 83 | @param <T> the expected type
|
---|
| 84 | @param paramname the parameter name that must be in the map
|
---|
| 85 | @param classType the expected value type for this parameter
|
---|
| 86 | @return object of requested type.
|
---|
| 87 | @throws IllegalArgumentException if the object is not available.
|
---|
| 88 | '''
|
---|
| 89 | if not paramname in self._params \
|
---|
| 90 | or not type(self._params[paramname])==classType:
|
---|
| 91 | raise ValueError(" Missing a parameter "+ paramname + " with a " + str(classType) + " value");
|
---|
| 92 | return self._params[paramname]
|
---|
| 93 |
|
---|
| 94 | def getDouble(self, name:str, defaultval:Optional[float], minv:Optional[float],
|
---|
| 95 | maxv:Optional[float]) -> Optional[float]:
|
---|
| 96 | '''
|
---|
| 97 | @param name the parameter name
|
---|
| 98 | @param defaultval the default value to return if parameter is not
|
---|
| 99 | available or not inside the given range. Can be null
|
---|
| 100 | @param minv the minimum value, or null if not applicable
|
---|
| 101 | @param maxv the maximum value, or null if not applicable
|
---|
| 102 | @return the double value contained inside the parameter with the given
|
---|
| 103 | name, or defaultval if the parameter does not exist, does not
|
---|
| 104 | contain a double, or is outside [min, max].
|
---|
| 105 | '''
|
---|
| 106 | if not name in self._params or not isinstance(self._params[name], Number):
|
---|
| 107 | return defaultval;
|
---|
| 108 | val:float = self._params[name]
|
---|
| 109 | if minv and val < minv:
|
---|
| 110 | return defaultval;
|
---|
| 111 | if maxv and val > maxv:
|
---|
| 112 | return defaultval;
|
---|
| 113 | return val;
|
---|
| 114 |
|
---|
| 115 | def __hash__(self):
|
---|
| 116 | return HASH(self._params)
|
---|
| 117 |
|
---|
| 118 | def __eq__(self, other):
|
---|
| 119 | return isinstance(other, self.__class__) \
|
---|
| 120 | and self._params == other._params
|
---|
| 121 |
|
---|
| 122 | def __repr__(self):
|
---|
| 123 | return toStr(self._params)
|
---|
| 124 |
|
---|