-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathexternal_api.py
43 lines (30 loc) · 1.14 KB
/
external_api.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
# Code Listing #6
"""
Showing how to debug programs with external dependencies such as an API
Note: This code is only for illustrative purposes and is not executable.
"""
import external_api
def process(json_data, skey='suspect_key',svalue='suspect_value'):
""" Fake the external API except for the suspect key & value """
# Assume each JSON element maps to a Python dictionary
for json_elem in json_data:
skip = False
for key in json_elem:
if key == skey:
if json_elem[key] == svalue:
# Suspect key,value combination - dont process
# this JSON element
skip = True
break
# Pass on to the API
if not skip:
external_api.process(json_elem)
def process_data(data):
""" Process data using external API """
# Clean up data - local function
data = clean_up(data)
# Drop duplicates from data - local function
data = drop_duplicates(data)
# Process line by line JSON
for json_elem in data:
external_api.process(json_elem)