Skip to content

Commit 33e82b2

Browse files
committed
Refactor dict initialization None pattern
1 parent 884db69 commit 33e82b2

File tree

2 files changed

+63
-105
lines changed

2 files changed

+63
-105
lines changed

sdcclient/_common.py

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -337,24 +337,16 @@ def post_event(self, name, description=None, severity=None, event_filter=None, t
337337
- `examples/post_event_simple.py <https://github.com/draios/python-sdc-client/blob/master/examples/post_event_simple.py>`_
338338
- `examples/post_event.py <https://github.com/draios/python-sdc-client/blob/master/examples/post_event.py>`_
339339
'''
340+
options = {
341+
'name': name,
342+
'description': description,
343+
'severity': severity,
344+
'filter': event_filter,
345+
'tags': tags
346+
}
340347
edata = {
341-
'event': {
342-
'name': name
343-
}
348+
'event': {k: v for k, v in options.items() if v is not None}
344349
}
345-
346-
if description is not None:
347-
edata['event']['description'] = description
348-
349-
if severity is not None:
350-
edata['event']['severity'] = severity
351-
352-
if event_filter is not None:
353-
edata['event']['filter'] = event_filter
354-
355-
if tags is not None:
356-
edata['event']['tags'] = tags
357-
358350
res = requests.post(self.url + '/api/events/', headers=self.hdrs, data=json.dumps(edata), verify=self.ssl_verify)
359351
return self._request_result(res)
360352

@@ -374,20 +366,13 @@ def get_events(self, name=None, from_ts=None, to_ts=None, tags=None):
374366
**Example**
375367
`examples/list_events.py <https://github.com/draios/python-sdc-client/blob/master/examples/list_events.py>`_
376368
'''
377-
params = {}
378-
379-
if name is not None:
380-
params['name'] = name
381-
382-
if from_ts is not None:
383-
params['from'] = from_ts
384-
385-
if to_ts is not None:
386-
params['to'] = to_ts
387-
388-
if tags is not None:
389-
params['tags'] = tags
390-
369+
options = {
370+
'name': name,
371+
'from': from_ts,
372+
'to': to_ts,
373+
'tags': tags
374+
}
375+
params = {k: v for k, v in options.items() if v is not None}
391376
res = requests.get(self.url + '/api/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
392377
return self._request_result(res)
393378

@@ -595,16 +580,11 @@ def create_user_invite(self, user_email, first_name=None, last_name=None, system
595580
return [False, 'user ' + user_email + ' already exists']
596581

597582
# Create the user
598-
user_json = {'username': user_email}
599-
600-
if first_name is not None:
601-
user_json['firstName'] = first_name
602-
603-
if last_name is not None:
604-
user_json['lastName'] = last_name
605-
606-
if system_role is not None:
607-
user_json['systemRole'] = system_role
583+
options = {'username': user_email,
584+
'firstName': first_name,
585+
'lastName': last_name,
586+
'systemRole': system_role}
587+
user_json = {k: v for k, v in options.items() if v is not None}
608588

609589
res = requests.post(self.url + '/api/users', headers=self.hdrs, data=json.dumps(user_json), verify=self.ssl_verify)
610590
return self._request_result(res)

sdcclient/_secure.py

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,15 @@ def get_policy_events_range(self, from_sec, to_sec, sampling=None, aggregations=
376376
`examples/get_secure_policy_events.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_policy_events.py>`_
377377
378378
'''
379-
ctx = {"from": int(from_sec) * 1000000,
380-
"to": int(to_sec) * 1000000,
381-
"offset": 0,
382-
"limit": 1000}
383-
384-
if sampling is not None:
385-
ctx["sampling"] = sampling
386-
if aggregations is not None:
387-
ctx["aggregations"] = aggregations
388-
if scope_filter is not None:
389-
ctx["scopeFilter"] = scope_filter
390-
if event_filter is not None:
391-
ctx["eventFilter"] = event_filter
392-
379+
options = {"from": int(from_sec) * 1000000,
380+
"to": int(to_sec) * 1000000,
381+
"offset": 0,
382+
"limit": 1000,
383+
"sampling": sampling,
384+
"aggregations": aggregations,
385+
"scopeFilter": scope_filter,
386+
"eventFilter": event_filter}
387+
ctx = {k: v for k, v in options.items() if v is not None}
393388
return self._get_policy_events_int(ctx)
394389

395390
def get_policy_events_duration(self, duration_sec, sampling=None, aggregations=None, scope_filter=None, event_filter=None):
@@ -415,23 +410,18 @@ def get_policy_events_duration(self, duration_sec, sampling=None, aggregations=N
415410
416411
'''
417412
epoch = datetime.datetime.utcfromtimestamp(0)
418-
419413
to_ts = (datetime.datetime.utcnow() - epoch).total_seconds() * 1000 * 1000
420414
from_ts = to_ts - (int(duration_sec) * 1000 * 1000)
421-
ctx = {"to": to_ts,
422-
"from": from_ts,
423-
"offset": 0,
424-
"limit": 1000}
425-
426-
if sampling is not None:
427-
ctx["sampling"] = sampling
428-
if aggregations is not None:
429-
ctx["aggregations"] = aggregations
430-
if scope_filter is not None:
431-
ctx["scopeFilter"] = scope_filter
432-
if event_filter is not None:
433-
ctx["eventFilter"] = event_filter
434415

416+
options = {"to": to_ts,
417+
"from": from_ts,
418+
"offset": 0,
419+
"limit": 1000,
420+
"sampling": sampling,
421+
"aggregations": aggregations,
422+
"scopeFilter": scope_filter,
423+
"eventFilter": event_filter}
424+
ctx = {k: v for k, v in options.items() if v is not None}
435425
return self._get_policy_events_int(ctx)
436426

437427
def get_policy_events_id_range(self, id, from_sec, to_sec, sampling=None, aggregations=None, scope_filter=None, event_filter=None):
@@ -458,21 +448,16 @@ def get_policy_events_id_range(self, id, from_sec, to_sec, sampling=None, aggreg
458448
`examples/get_secure_policy_events.py <https://github.com/draios/python-sdc-client/blob/master/examples/get_secure_policy_events.py>`_
459449
460450
'''
461-
ctx = {"id": id,
462-
"from": int(from_sec) * 1000000,
463-
"to": int(to_sec) * 1000000,
464-
"offset": 0,
465-
"limit": 1000}
466-
467-
if sampling is not None:
468-
ctx["sampling"] = sampling
469-
if aggregations is not None:
470-
ctx["aggregations"] = aggregations
471-
if scope_filter is not None:
472-
ctx["scopeFilter"] = scope_filter
473-
if event_filter is not None:
474-
ctx["eventFilter"] = event_filter
475-
451+
options = {"id": id,
452+
"from": int(from_sec) * 1000000,
453+
"to": int(to_sec) * 1000000,
454+
"offset": 0,
455+
"limit": 1000,
456+
"sampling": sampling,
457+
"aggregations": aggregations,
458+
"scopeFilter": scope_filter,
459+
"eventFilter": event_filter}
460+
ctx = {k: v for k, v in options.items() if v is not None}
476461
return self._get_policy_events_int(ctx)
477462

478463
def get_policy_events_id_duration(self, id, duration_sec, sampling=None, aggregations=None, scope_filter=None, event_filter=None):
@@ -499,24 +484,19 @@ def get_policy_events_id_duration(self, id, duration_sec, sampling=None, aggrega
499484
500485
'''
501486
epoch = datetime.datetime.utcfromtimestamp(0)
502-
503487
to_ts = (datetime.datetime.utcnow() - epoch).total_seconds() * 1000 * 1000
504488
from_ts = to_ts - (int(duration_sec) * 1000 * 1000)
489+
505490
ctx = {"id": id,
506491
"to": to_ts,
507492
"from": from_ts,
508493
"offset": 0,
509-
"limit": 1000}
510-
511-
if sampling is not None:
512-
ctx["sampling"] = sampling
513-
if aggregations is not None:
514-
ctx["aggregations"] = aggregations
515-
if scope_filter is not None:
516-
ctx["scopeFilter"] = scope_filter
517-
if event_filter is not None:
518-
ctx["eventFilter"] = event_filter
519-
494+
"limit": 1000,
495+
"sampling": sampling,
496+
"aggregations": aggregations,
497+
"scopeFilter": scope_filter,
498+
"eventFilter": event_filter}
499+
ctx = {k: v for k, v in options.items() if v is not None}
520500
return self._get_policy_events_int(ctx)
521501

522502
def get_more_policy_events(self, ctx):
@@ -841,16 +821,14 @@ def update_compliance_task(self, id, name=None, module_name=None, schedule=None,
841821
return ok, res
842822

843823
task = res
844-
if name is not None:
845-
task["name"] = name
846-
if module_name is not None:
847-
task["moduleName"] = module_name
848-
if schedule is not None:
849-
task["schedule"] = schedule
850-
if scope is not None:
851-
task["scope"] = scope
852-
if enabled is not None:
853-
task["enabled"] = enabled
824+
options = {
825+
'name': name,
826+
'moduleName': module_name,
827+
'schedule': schedule,
828+
'scope': scope,
829+
'enabled': enabled
830+
}
831+
task.update({k: v for k, v in options.items() if v is not None})
854832
res = requests.put(self.url + '/api/complianceTasks/{}'.format(id), data=json.dumps(task), headers=self.hdrs, verify=self.ssl_verify)
855833
return self._request_result(res)
856834

0 commit comments

Comments
 (0)