from __future__ import annotations from copy import deepcopy from typing import Dict, Any from pyson.JsonGetter import JsonGetter from pyson.JsonValue import JsonValue class Parameters: ''' init parameters for a party. Object must be either a HashMap, List, String or Number. Other objects may not deserialize properly. We never use blanket Object deserializers (enableDefaultTyping) because of security reasons. ''' def __init__(self, parameters:Dict[str, Any]={}) : ''' @param parameters dict of key-value pairs. values can be primitive object, but also a list or dict. The value will be used 'as is', so this will not be parsed as a geniusweb object but kept as list/dict. ''' self._params = deepcopy(parameters); # deepcopy to ensure our copy is immutable def get(self, key:str) -> object: ''' @param key the key to get the value for. @return the raw value ''' return self._params[key] @JsonValue() def getParameters(self): return deepcopy(self._params) def isEmpty(self) -> bool: ''' @return true iff params is empty ''' return len(self._params)==0 def containsKey(self, key:str) -> bool: ''' @param key the key to be checked @return true iff params contains the given key ''' return key in self._params def With(self, key:str, val:object) -> Parameters : ''' @param key the key. Key may already in params. @param val the new value for the key @return new Parameters , which is copy of this but with key-value pair added/overridden ''' newparams = dict(self._params) # our params are immutable newparams[key]= val return Parameters(newparams) def __hash__(self): return sum( [ hash(k) + 31 * hash(v) for (k,v) in self._params.items()]) def __eq__(self, other): return isinstance(other, self.__class__) \ and self._params == other._params def __repr__(self): return str(self._params)