forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
executable file
·62 lines (48 loc) · 1.31 KB
/
utils.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
"""
module to define utils
"""
import re
def is_valid_param(param):
"""
Function to check if a string matches the pattern
Use re.match() to check if the string matches the pattern from the start
"""
pattern = r'^[a-zA-Z0-9_]+=[a-zA-Z0-9_."@-]+$'
return bool(re.match(pattern, param))
def parseArgs(args):
"""
function to parse the args
"""
splitedArgs = args.split()
domainDict = {}
for param in splitedArgs[1:]:
if is_valid_param(param):
key, value = param.split('=')
value = parseValue(value)
if value is None:
continue
domainDict[key] = value
return splitedArgs[0], domainDict
def parseValue(val):
"""
function to parse the value
"""
if val is not None and val.strip() != "":
if val[0] == "\"" and val[-1] == "\"":
return parseString(val)
elif '.' in val:
try:
val = float(val)
return val
except ValueError:
pass
else:
try:
val = int(val)
return val
except ValueError:
pass
def parseString(val):
result_string = val[1:-1].replace('"', '\\"')
return result_string.replace('_', " ")