Filter and sort to get only the relevant data
You now have all the raw data in a Table format. The specification laid down some rules for filtering that data.
This is your first minimum effort attempt at implementing some filtering and sorting:
from robocorp.tasks import task
from RPA.HTTP import HTTP
from RPA.JSON import JSON
from RPA.Tables import Tables
http = HTTP()
json = JSON()
table = Tables()
TRAFFIC_JSON_FILE_PATH = "output/traffic.json"
@task
def produce_traffic_data():
"""
Inhuman Insurance, Inc. Artificial Intelligence System automation.
Produces traffic data work items.
"""
http.download(
url="https://github.com/robocorp/inhuman-insurance-inc/raw/main/RS_198.json",
target_file=TRAFFIC_JSON_FILE_PATH,
overwrite=True,
)
traffic_data = load_traffic_data_as_table()
filtered_data = filter_and_sort_traffic_data(traffic_data)
@task
def consume_traffic_data():
"""
Inhuman Insurance, Inc. Artificial Intelligence System robot.
Consumes traffic data work items.
"""
print("consume")
def load_traffic_data_as_table():
json_data = json.load_json_from_file(TRAFFIC_JSON_FILE_PATH)
return table.create_table(json_data["value"])
def filter_and_sort_traffic_data(data):
table.filter_table_by_column(data, "NumericValue", "<", 5.0)
table.filter_table_by_column(data, "Dim1", "==", "BTSX")
table.sort_table_by_column(data, "TimeDim", False)
return data
The logic works technically, but you think you might be able to improve the maintainability of the code a bit.
Everything is clear at the very moment you are implementing something. However, you or someone else often needs to return and modify the robot code even after a long time. It could be weeks, months, or even years later.
Statements using technical terms like "NumericValue", "<", 5.0
, "Dim1", "==", "BTSX"
and table.sort_table_by_column(data, "TimeDim", False)
might be clear to you now. Still, you will forget what those mean in business terms sooner or later.
You could write some external documentation or link to the specification somewhere. Still, a better approach is to try and make your code document itself by using business terms in the code.
You decide to refactor your code to use named variables for those technical names and numbers:
from robocorp.tasks import task
from RPA.HTTP import HTTP
from RPA.JSON import JSON
from RPA.Tables import Tables
http = HTTP()
json = JSON()
table = Tables()
TRAFFIC_JSON_FILE_PATH = "output/traffic.json"
@task
def produce_traffic_data():
"""
Inhuman Insurance, Inc. Artificial Intelligence System automation.
Produces traffic data work items.
"""
http.download(
url="https://github.com/robocorp/inhuman-insurance-inc/raw/main/RS_198.json",
target_file=TRAFFIC_JSON_FILE_PATH,
overwrite=True,
)
traffic_data = load_traffic_data_as_table()
filtered_data = filter_and_sort_traffic_data(traffic_data)
@task
def consume_traffic_data():
"""
Inhuman Insurance, Inc. Artificial Intelligence System robot.
Consumes traffic data work items.
"""
print("consume")
def load_traffic_data_as_table():
json_data = json.load_json_from_file(TRAFFIC_JSON_FILE_PATH)
return table.create_table(json_data["value"])
def filter_and_sort_traffic_data(data):
rate_key = "NumericValue"
max_rate = 5.0
gender_key = "Dim1"
both_genders = "BTSX"
year_key = "TimeDim"
table.filter_table_by_column(data, rate_key, "<", max_rate)
table.filter_table_by_column(data, gender_key, "==", both_genders)
table.sort_table_by_column(data, year_key, False)
return data
The future you will thank the present you for taking the time to make their life slightly easier when they one day return to modify this robot!