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

Last change on this file since 63 was 59, checked in by Wouter Pasman, 3 years ago

#44 manual commit of first public release, because this will cause the dist directory to move

File size: 1.9 KB
Line 
1from __future__ import annotations
2
3from copy import deepcopy
4from typing import Dict, Any
5
6from pyson.JsonGetter import JsonGetter
7from pyson.JsonValue import JsonValue
8
9
10class Parameters:
11 '''
12 init parameters for a party. Object must be either a HashMap, List, String or
13 Number. Other objects may not deserialize properly. We never use blanket
14 Object deserializers (enableDefaultTyping) because of security reasons.
15 '''
16
17 def __init__(self, parameters:Dict[str, Any]={}) :
18 '''
19 @param parameters dict of key-value pairs. values can be primitive
20 object, but also a list or dict. The value will be used 'as is',
21 so this will not be parsed as a geniusweb object but kept as list/dict.
22 '''
23 self._params = deepcopy(parameters);
24 # deepcopy to ensure our copy is immutable
25
26 def get(self, key:str) -> object:
27 '''
28 @param key the key to get the value for.
29 @return the raw value
30 '''
31 return self._params[key]
32
33 @JsonValue()
34 def getParameters(self):
35 return deepcopy(self._params)
36
37
38 def isEmpty(self) -> bool:
39 '''
40 @return true iff params is empty
41 '''
42 return len(self._params)==0
43
44
45 def containsKey(self, key:str) -> bool:
46 '''
47 @param key the key to be checked
48 @return true iff params contains the given key
49 '''
50 return key in self._params
51
52
53 def With(self, key:str, val:object) -> Parameters :
54 '''
55 @param key the key. Key may already in params.
56 @param val the new value for the key
57 @return new Parameters , which is copy of this but with key-value pair
58 added/overridden
59 '''
60 newparams = dict(self._params) # our params are immutable
61 newparams[key]= val
62 return Parameters(newparams)
63
64
65 def __hash__(self):
66 return sum( [ hash(k) + 31 * hash(v) for (k,v) in self._params.items()])
67
68 def __eq__(self, other):
69 return isinstance(other, self.__class__) \
70 and self._params == other._params
71
72 def __repr__(self):
73 return str(self._params)
74
Note: See TracBrowser for help on using the repository browser.