GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — v1 (#16)
by Maksim
03:39
created

Optimization.vehicle_id()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 1
cts 2
cp 0.5
crap 1.125
1
# -*- coding: utf-8 -*-
2
3 1
import pydash
4
5
# reimport enums for convenience:
6 1
from ..enums import AlgorithmTypeEnum
7 1
from ..enums import OptimizationStateEnum
8 1
from ..enums import OptimizationQualityEnum
9 1
from ..enums import OptimizationFactorEnum
10 1
from ..enums import RouteMetricEnum
11 1
from ..enums import TravelModeEnum
12 1
from ..enums import DeviceTypeEnum
13
14 1
from ..enums import AddressStopTypeEnum
15
16 1
from route4me.sdk._internals.decorators import dict_property
17 1
from route4me.sdk._internals.decorators import dict_enum_property
18 1
from route4me.sdk._internals import timestamp_and_seconds2datetime
19 1
from route4me.sdk._internals import datetime2timestamp_and_seconds
20
21
22 1
class BaseModel(dict):
23 1
	def __init__(self, raw=None):
24 1
		super(BaseModel, self).__init__(raw)
25
26 1
	@property
27
	def raw(self):
28
		"""
29
		Provides access to raw model data, as it would be sent to Route4Me API
30
31
		:getter: Get, property is readonly
32
		:rtype: dict
33
		"""
34 1
		return self
35
36
37 1
class TiedListWrapper(list):
38 1
	def __init__(self, parent, key, anytype):
39 1
		self._parent = parent
40 1
		self._key = key
41 1
		self._t = anytype
42
43 1
		r = self._raw
44 1
		if r is None:
45 1
			r = []
46
47 1
		super(TiedListWrapper, self).__init__(r)
48
49 1
	@property
50
	def _raw(self):
51
		"""
52
		Mostly - internal field.
53
54
		Raw - is a low-level content. It is a :class:`list` instance stored
55
		in the parent object. Raw - because it is a low-level presentation,
56
		which will be used to send data to R4M
57
58
		:returns: A raw object from parent
59
		:rtype: list
60
		"""
61 1
		return pydash.get(self._parent, self._key)
62
63 1
	def link(self):
64
		"""
65
		Put SELF instance to the parent object
66
67
		If parent contains None = put self
68
		If parent already contains SELF - do nothing
69
		If parent contains smth. - load content to self and connect
70
		"""
71 1
		r = self._raw
72
73 1
		if r is not None and r != self:
74
			super(TiedListWrapper, self).__init__(r)
75
76 1
		self._parent.raw[self._key] = self
77
78 1
	def unlink(self):
79 1
		self._parent.raw[self._key] = None
80
81 1
	def unset(self):
82 1
		self._parent.pop(self._key)
83
84 1
	def __getitem__(self, index):
85 1
		r = super(TiedListWrapper, self).__getitem__(index)
86 1
		return self._t(r)
87
88 1
	def __setitem__(self, index, value):
89
		self.link()
90
		r = value
91
		if isinstance(r, BaseModel):
92
			r = r.raw
93
		res = super(TiedListWrapper, self).__setitem__(index, r)
94
		return res
95
96 1
	def __iter__(self):
97 1
		for r in list.__iter__(self):
98 1
			yield self._t(r)
99
100 1
	def append(self, item):
101 1
		r = item
102 1
		if isinstance(r, BaseModel):
103 1
			r = r.raw
104
105 1
		self.link()
106 1
		res = super(TiedListWrapper, self).append(r)
107 1
		return res
108
109
110 1
class Address(BaseModel):
111
	"""
112
	Single *Address*, also known as *Route Destination*
113
114
	.. seealso::
115
		- **api doc**: https://route4me.io/docs/#addresses
116
		- **schema**: https://github.com/route4me/route4me-json-schemas/blob/master/Address.dtd
117
	"""
118
119 1
	def __init__(self, raw=None):
120
		"""
121
		Create instance **LOCALLY**.
122
123
		Use :meth:`~route4me.sdk.endpoints.optimizations.Optimizations.add_address`
124
		or :meth:`~route4me.sdk.endpoints.routes.Routes.add_address`
125
		to create new Address in the Route4Me API
126
127
		:param raw: Raw values for new address, example: \
128
			`add address to optimization \
129
			<https://route4me.io/docs/#insert-an-address-into-an-optimization>`_, \
130
			defaults to :data:`None`
131
		:type raw: dict, optional
132
		"""
133 1
		if raw is None:
134
			raw = {
135
				# 'manifest': {},
136
				# 'path_to_next': [],
137
				# 'directions': [],
138
			}
139 1
		super(Address, self).__init__(raw=raw)
140
141 1
	@property
142
	def ID(self):
143
		"""
144
		Route Destination ID
145
146
		Internal unique address identifier
147
148
		:getter: Gets value
149
		:setter: Sets value
150
		:rtype: str
151
		"""
152 1
		return self.raw.get('route_destination_id')
153
154 1
	@dict_property('alias', str)
155
	def name(self, value):
156
		"""
157
		Address Alias / Address Name
158
159
		.. note::
160
161
			In Route4Me API this field is known as ``alias``
162
163
		<AUTO>
164
		"""
165
		return value
166
167
	# ==========================================================================
168
169 1
	@dict_enum_property('address_stop_type', AddressStopTypeEnum)
170
	def address_stop_type(self, value):
171
		"""
172
		Address stop type
173
174
		<AUTO>
175
		"""
176
		return value
177
178
	# ==========================================================================
179
180 1
	@dict_property('address', str)
181
	def address(self, value):
182
		"""
183
		The route's Address Line
184
185
		.. note::
186
187
			In Route4Me API this field is known as ``address``
188
189
		<AUTO>
190
		"""
191
		return value
192
193 1
	@dict_property('lat', float)
194
	def latitude(self, value):
195
		"""
196
		Latitude
197
198
		Shoud be -90.0 ≤ lat ≤ 90
199
200
		.. note::
201
202
			In Route4Me API this field is known as ``lat``
203
204
		<AUTO>
205
		"""
206
		return value
207
208 1
	@dict_property('lng', float)
209
	def longitude(self, value):
210
		"""
211
		Longitude
212
213
		Shoud be -180.0 ≤ lng ≤ 180
214
215
		.. note::
216
217
			In Route4Me API this field is known as ``lng``
218
219
		<AUTO>
220
		"""
221
		return value
222
223 1
	@dict_property('route_id', str)
224
	def route_id(self, value):
225
		"""
226
		Route ID
227
228
		Parent route
229
230
		<AUTO>
231
		"""
232
		return value
233
234 1
	@dict_property('sequence_no', int)
235
	def sequence_no(self, value):
236
		"""
237
		The sequence number for the address
238
239
		<AUTO>
240
		"""
241
		return value
242
243 1
	@dict_property('time', int)
244
	def service_time_sec(self, value):
245
		"""
246
		Service time (seconds)
247
248
		.. note::
249
250
			In Route4Me API this field is known as ``time``
251
252
		<AUTO>
253
		"""
254
		return value
255
256 1
	@dict_property('is_depot', bool)
257
	def is_depot(self, value):
258
		"""
259
		Indicates that this address is a depot
260
261
		<AUTO>
262
		"""
263
		return value
264
265 1
	@dict_property('geocoded', bool)
266
	def geocoded(self, value):
267
		"""
268
		:data:`True` means the :attr:`address_string` field was successfully geocoded
269
270
		<AUTO>
271
		"""
272
		return value
273
274 1
	@dict_property('failed_geocoding', bool)
275
	def failed_geocoding(self, value):
276
		"""
277
		:data:`True` means there was a geocoding attempt which failed. \
278
		:data:`False` means success or no geocoding
279
280
		<AUTO>
281
		"""
282
		return value
283
284
285 1
class Optimization(BaseModel):
286
	"""
287
	Optimization problem (or simple *Optimization*)
288
289
	.. seealso::
290
		- **schema**: https://github.com/route4me/route4me-json-schemas/blob/master/Optimization_response.dtd
291
292
	"""
293
294 1
	def __init__(self, raw=None):
295
		"""
296
		Create instance **LOCALLY**.
297
298
		Use :meth:`~route4me.sdk.endpoints.optimizations.Optimizations.create`
299
		to create new Optimization Problem in the Route4Me API
300
301
		:param raw: Raw values for new optimization, example: \
302
			`create optimization <https://route4me.io/docs/#create-an-optimization>`_, \
303
			defaults to None
304
		:type raw: dict, optional
305
		"""
306 1
		if raw is None:
307 1
			raw = {
308
				# 'parameters': {
309
				# 	'store_route': True,
310
				# 	'route_max_duration': 24 * 60 * 60,
311
				# 	'route_time': 0,
312
				# 	'route_date': unix_timestamp_today()
313
				# },
314
				# 'user_errors': [],
315
				# 'optimization_errors': [],
316
				# 'links': {},
317
				# 'addresses': [],
318
				# 'routes': [],
319
			}
320 1
		super(Optimization, self).__init__(raw=raw)
321
322 1
		self._addresses = TiedListWrapper(
323
			parent=self,
324
			key='addresses',
325
			anytype=Address
326
		)
327
328
	# ==========================================================================
329
330 1
	@property
331
	def ID(self):
332 1
		return self.raw.get('optimization_problem_id')
333
334 1
	@dict_property('parameters.route_name', str)
335
	def name(self, value):
336
		"""
337
		The name of this optimization problem. This name will be accessible in
338
		the search API, and also will be displayed on the mobile device of
339
		a user
340
341
		<AUTO>
342
		"""
343
		return value
344
345
	# ==========================================================================
346
347 1
	@dict_enum_property('parameters.algorithm_type', AlgorithmTypeEnum)
348
	def algorithm_type(self, value):
349
		"""
350
		The algorithm type to be used
351
352
		<AUTO>
353
		"""
354 1
		return value
355
356 1
	@dict_enum_property('state', OptimizationStateEnum)
357
	def state(self, value):
358
		"""
359
		The current state of the optimization
360
361
		<AUTO>
362
		"""
363 1
		return value
364
365 1
	@dict_enum_property('parameters.optimization_quality', OptimizationQualityEnum)
366
	def quality(self, value):
367
		"""
368
		Optimization quality
369
370
		There are 3 types of optimization qualities that are optimizations
371
		goals, see :class:`~route4me.sdk.enums.OptimizationQualityEnum`
372
373
		<AUTO>
374
		"""
375
		return value
376
377 1
	@dict_enum_property('parameters.metric', RouteMetricEnum)
378
	def metric(self, value):
379
		"""
380
		Metric
381
382
		<AUTO>
383
		"""
384
		return value
385
386 1
	@dict_enum_property('parameters.travel_mode', TravelModeEnum)
387
	def travel_mode(self, value):
388
		"""
389
		Travel mode
390
391
		The mode of travel that the directions should be optimized for
392
393
		<AUTO>
394
		"""
395
		return value
396
397 1
	@dict_enum_property('parameters.device_type', DeviceTypeEnum)
398
	def device_type(self, value):
399
		"""
400
		Device type
401
402
		The type of the device that is creating this Optimization Problem
403
404
		<AUTO>
405
		"""
406
		return value
407
408 1
	@dict_enum_property('parameters.optimize', OptimizationFactorEnum)
409
	def optimization_factor(self, value):
410
		"""
411
		The driving directions can be generated biased for this selection. This
412
		has no impact on route sequencing.
413
414
		.. note::
415
416
			In Route4Me API this enum also known as ``optimize``
417
418
		<AUTO>
419
		"""
420 1
		return value
421
422
	# ==========================================================================
423
424
	# @dict_property('parameters.store_route', bool)
425
	# def store_route(self, value):
426
	# 	"""
427
	# 	Store Route
428
429
	# 	<AUTO>
430
	# 	"""
431
	# 	return value
432
433 1
	@dict_property('parameters.parts', int)
434
	def parts(self, value):
435
		"""
436
		Legacy feature which permits a user to request an example number of
437
		optimized routes
438
439
		<AUTO>
440
		"""
441
		return value
442
443 1
	@dict_property('parameters.disable_optimization', bool)
444
	def disable_optimization(self, value):
445
		"""
446
		By disabling optimization, the route optimization engine will not
447
		resequence stops in your optimization problem
448
449
		<AUTO>
450
		"""
451
		return value
452
453 1
	@dict_property('parameters.route_max_duration', int)
454
	def route_max_duration_sec(self, value):
455
		"""
456
		Route maximum duration
457
458
		How many seconds a route can last at most.
459
		Default is 86400 seconds = 24 hours
460
461
		<AUTO>
462
		"""
463
		return value
464
465 1
	@dict_property('parameters.rt', bool)
466
	def round_trip(self, value):
467
		"""
468
		Round Trip
469
470
		The tour type of this route. The optimization engine changes its
471
		behavior for round trip routes
472
473
		.. note::
474
475
			In Route4Me API this parameter is also known as ``parameters.rt``
476
477
478
		<AUTO>
479
		"""
480
		return value
481
482 1
	@dict_property('parameters.member_id', int)
483
	def member_id(self, value):
484
		"""
485
		Member ID
486
487
		User ID who is assigned to this Optimization Problem
488
489
		<AUTO>
490
		"""
491
		return value
492
493 1
	@dict_property('parameters.device_id', str)
494
	def device_id(self, value):
495
		"""
496
		Device ID
497
498
		32 Character MD5 String ID of the device that was used to plan this
499
		route
500
501
		<AUTO>
502
		"""
503
		return value
504
505 1
	@dict_property('parameters.vehicle_id', str)
506
	def vehicle_id(self, value):
507
		"""
508
		Vehicle ID
509
510
		The unique internal id of a vehicle
511
512
		<AUTO>
513
		"""
514
		return value
515
516
	# ==========================================================================
517 1
	@property
518
	def route_datetime(self):
519
		"""
520
		Route DateTime
521
522
		.. note::
523
524
			In Route4Me API this parameter is broken into 2 parts: fields
525
			``parameters.route_date`` and ``parameters.route_time``
526
527
		:getter: returns a :class:`~datetime.datetime` combining values from \
528
			two fields
529
		:setter: sets ``parameters.route_date`` and ``parameters.route_time`` \
530
			in the raw data
531
		:rtype: ~datetime.datetime
532
		"""
533
534
		# from JSON Schema:
535
		# "route_date": { "type": ["integer", "null"],
536
		# 	// ...
537
		# 	"description": "The route start date in UTC, unix timestamp seconds.
538
		# 		Used to show users when the route will begin, also used for
539
		# 		reporting and analytics"
540
		# },
541
		# "route_time": { "type": "integer",
542
		# 	// ...
543
		# 	"description": "Time when the route starts (relative to route_date)
544
		# 		(Seconds). UTC timezone as well"
545
		#
546
		# So, we have UNIX timestamp (seconds) and seconds from day start. Lets
547
		# create date
548
549 1
		d = pydash.get(self.raw, 'parameters.route_date')
550 1
		t = pydash.get(self.raw, 'parameters.route_time')
551
552 1
		return timestamp_and_seconds2datetime(d, t)
553
554 1
	@route_datetime.setter
555
	def route_datetime(self, value):
556 1
		d, t = datetime2timestamp_and_seconds(value)
557 1
		pydash.set_(self.raw, 'parameters.route_date', d)
558 1
		pydash.set_(self.raw, 'parameters.route_time', t)
559
560
	# ==========================================================================
561
562 1
	@property
563
	def addresses(self):
564
		"""
565
		Addresses included to this Optimization Problem
566
567
		<AUTO>
568
		"""
569 1
		return self._addresses
570
571 1
	@property
572
	def links(self):
573
		"""
574
		Links to the GET operations for the optimization problem
575
576
		:getter: Get
577
		:setter: Set
578
		:deleter: Del
579
		:rtype: dict or None
580
		"""
581 1
		return pydash.get(self.raw, 'links')
582
583 1
	@links.deleter
584
	def links(self, value):
585
		return self.raw.pop('links')
586