source: geniuswebcore/geniusweb/references/Parameters.py@ 78

Last change on this file since 78 was 73, checked in by Bart Vastenhouw, 3 years ago

Fix for IssueValue hashcode.

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