-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.py
43 lines (38 loc) · 1.01 KB
/
params.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import hashlib
from constants.parameters import QUERY_MARK
class ParamsDict(dict):
"""
Class used to manage parameters passed from POST and GET requests
It also, allows to calculate a unique hash for the dictionary,
base on the values of the items stored
"""
def parse(self, str):
"""
Read parameters from different requests
"""
# Read param string from a GET request
query_mark_pos = str.find(QUERY_MARK)
if query_mark_pos > -1:
str = str[query_mark_pos + 1:]
# Set values into the dictionary
for kv in str.split("&"):
pos = kv.index('=')
self[kv[:pos]] = kv[pos + 1:]
def hash(self):
"""
It also, allows to calculate a unique hash for the dictionary,
base on the values of the items stored
"""
values = self.values()
values.sort()
aux = ""
for value in values:
aux += value
return hashlib.md5(aux).hexdigest()
def __str__(self):
items = []
iteritems = self.items()
iteritems.sort()
for item in iteritems:
items.append("=".join(item))
return "&".join(items)