-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom_dictionaries.py
26 lines (23 loc) · 1003 Bytes
/
Custom_dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from collections import UserDict
from numbers import Real
class IntDict(UserDict):
def __setitem__(self, key, value):
if not isinstance(value, Real):
raise ValueError('Value must be a Real Number')
super().__setitem__(key, value)
def __getitem__(self, key):
return int(super().__getitem__(key))
class LimitedDict(UserDict):
def __init__(self, keyset, min_value, max_value, *args, **kwargs):
self._keyset = keyset
self._min_value = min_value
self._max_value = max_value
super().__init__(*args, **kwargs)
def __setitem__(self, key, value):
if key not in self._keyset:
raise KeyError('Invalid KeyName')
if not isinstance(value, int):
raise ValueError('Value must be an Integer type')
if value< self._min_value or value>self._max_value:
raise ValueError(f'Value must be between {self._min_value} and {self._max_value}')
super().__setitem__(key, value)