9
9
10
10
class TomtomJob :
11
11
"""Data structure allowing the manipulation of the tomtom jobs"""
12
+
12
13
job_name : str
13
14
date_ranges : List [TomtomDateRange ]
14
15
time_sets : List [TomtomTimeSet ]
15
- distance_unit : str = ' KILOMETERS'
16
- accept_mode : Optional [str ] = ' AUTO'
16
+ distance_unit : str = " KILOMETERS"
17
+ accept_mode : Optional [str ] = " AUTO"
17
18
# see here for more info about map versions
18
19
# https://developer.tomtom.com/traffic-stats/documentation/api/available-maps
19
- map_version : Optional [float ] = 2020.09
20
+ map_version : Optional [float ] = None
20
21
average_sample_size_threshold : Optional [int ] = None
21
22
22
23
def __init__ (
23
24
self ,
24
25
job_name : str ,
25
26
time_sets : List [TomtomTimeSet ],
26
- distance_unit : Literal [' KILOMETERS' , ' MILES' ] = ' KILOMETERS' ,
27
- accept_mode : Optional [Literal [' AUTO' , ' MANUAL' ]] = ' AUTO' ,
28
- map_version : Optional [float ] = 2020.09 ,
29
- average_sample_size_threshold : Optional [int ] = None
27
+ distance_unit : Literal [" KILOMETERS" , " MILES" ] = " KILOMETERS" ,
28
+ accept_mode : Optional [Literal [" AUTO" , " MANUAL" ]] = " AUTO" ,
29
+ map_version : Optional [float ] = None ,
30
+ average_sample_size_threshold : Optional [int ] = None ,
30
31
):
31
32
"""Data structure containing all the job information.
32
33
@@ -59,16 +60,16 @@ def __init__(
59
60
60
61
if len (time_sets ) > MAX_TIME_SETS_COUNT :
61
62
raise ValueError (
62
- f' Impossible to query for more than { MAX_TIME_SETS_COUNT } time sets, ({ len (time_sets )} given)'
63
+ f" Impossible to query for more than { MAX_TIME_SETS_COUNT } time sets, ({ len (time_sets )} given)"
63
64
)
64
65
self .time_sets = time_sets
65
66
66
- if distance_unit .upper () not in [' KILOMETERS' , ' MILES' ]:
67
- raise ValueError (f' The given `distance_unit` is invalid ({ distance_unit } )' )
67
+ if distance_unit .upper () not in [" KILOMETERS" , " MILES" ]:
68
+ raise ValueError (f" The given `distance_unit` is invalid ({ distance_unit } )" )
68
69
self .distance_unit = str (distance_unit ).upper ()
69
70
70
- if accept_mode is not None and accept_mode .upper () not in [' AUTO' , ' MANUAL' ]:
71
- raise ValueError (f' The given `accept_mode` is invalid ({ accept_mode } )' )
71
+ if accept_mode is not None and accept_mode .upper () not in [" AUTO" , " MANUAL" ]:
72
+ raise ValueError (f" The given `accept_mode` is invalid ({ accept_mode } )" )
72
73
self .accept_mode = accept_mode
73
74
74
75
self .map_version = map_version
@@ -77,19 +78,23 @@ def __init__(
77
78
@classmethod
78
79
def from_dict (cls , dict_object : Dict [str , Any ]) -> TomtomJob :
79
80
return cls (
80
- job_name = dict_object ['jobName' ],
81
- time_sets = [TomtomTimeSet .from_dict (t ) for t in dict_object ['timeSets' ]],
82
- distance_unit = dict_object ['distanceUnit' ],
83
- accept_mode = dict_object ['acceptMode' ],
84
- map_version = dict_object ['mapVersion' ],
85
- average_sample_size_threshold = None if 'averageSampleSizeThreshold' not in dict_object else dict_object [
86
- 'averageSampleSizeThreshold' ]
81
+ job_name = dict_object ["jobName" ],
82
+ time_sets = [TomtomTimeSet .from_dict (t ) for t in dict_object ["timeSets" ]],
83
+ distance_unit = dict_object ["distanceUnit" ],
84
+ accept_mode = dict_object ["acceptMode" ],
85
+ map_version = dict_object ["mapVersion" ],
86
+ average_sample_size_threshold = (
87
+ None
88
+ if "averageSampleSizeThreshold" not in dict_object
89
+ else dict_object ["averageSampleSizeThreshold" ]
90
+ ),
87
91
)
88
92
89
- def md5 (self , salt : str = '' ) -> str :
93
+ def md5 (self , salt : str = "" ) -> str :
90
94
import hashlib
95
+
91
96
json = self .to_json ()
92
- md5_hash = hashlib .md5 (f' { json } { salt } ' .encode (' utf-8' ))
97
+ md5_hash = hashlib .md5 (f" { json } { salt } " .encode (" utf-8" ))
93
98
return md5_hash .hexdigest ()
94
99
95
100
def to_dict (self ) -> Dict [str , Any ]:
@@ -102,21 +107,21 @@ def to_dict(self) -> Dict[str, Any]:
102
107
The dictionary with the keys specified in the documentation.
103
108
"""
104
109
job_dict = {
105
- ' jobName' : self .job_name ,
106
- ' distanceUnit' : self .distance_unit ,
107
- ' mapVersion' : None ,
108
- ' acceptMode' : None ,
109
- ' timeSets' : [t .to_dict () for t in self .time_sets ],
110
- ' averageSampleSizeThreshold' : None
110
+ " jobName" : self .job_name ,
111
+ " distanceUnit" : self .distance_unit ,
112
+ " mapVersion" : None ,
113
+ " acceptMode" : None ,
114
+ " timeSets" : [t .to_dict () for t in self .time_sets ],
115
+ " averageSampleSizeThreshold" : None ,
111
116
}
112
117
113
118
# manage optional fields
114
119
if self .accept_mode is not None :
115
- job_dict [' acceptMode' ] = self .accept_mode
120
+ job_dict [" acceptMode" ] = self .accept_mode
116
121
if self .map_version is not None :
117
- job_dict [' mapVersion' ] = self .map_version
122
+ job_dict [" mapVersion" ] = self .map_version
118
123
if self .average_sample_size_threshold is not None :
119
- job_dict [' averageSampleSizeThreshold' ] = self .average_sample_size_threshold
124
+ job_dict [" averageSampleSizeThreshold" ] = self .average_sample_size_threshold
120
125
121
126
job_dict = {k : v for k , v in job_dict .items () if v is not None }
122
127
0 commit comments