Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

[Quality] Code cleanup #67

Closed
wants to merge 5 commits into from
Closed

[Quality] Code cleanup #67

wants to merge 5 commits into from

Conversation

obulat
Copy link
Contributor

@obulat obulat commented May 17, 2021

This PR fixes several types of code problems highlighted by Pycharm's code inspection.

Changes:

  • Replace mutable default parameters
    Python creates a default parameter once when the function is defined. The same value is used every time the function is called. This causes a problem if the value is mutable, such as a dictionary or a list. If this value is ever changed inside the function, the changed value is used for subsequent function calls, instead of the default value.

  • Make function names lower case, separating words with underscores: sanitize_string instead of sanitizeString

  • Refactor to remove variables that might be referenced before assignment

  • Remove unused variables and imports

  • Add some type hinting

- Replace mutable default function parameters
- Make function names lower case
- Refactor to remove variables that might be referenced before assignment
- Add some type hinting
- Remove unused variables and imports
@obulat obulat requested review from dhruvkb and zackkrida May 17, 2021 11:15
Base automatically changed from airflow_update to master May 24, 2021 21:19
obulat added 2 commits May 25, 2021 13:56
The functions that use etlMods module cannot be tested at the moment, so the changes have been reversed
@zackkrida zackkrida requested a review from krysal May 25, 2021 12:55
Comment on lines 94 to 96
def _handle_response(
batch
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format of this param looks a bit odd here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely! Thank you for catching this!

krysal
krysal previously approved these changes May 28, 2021
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are a ton of improvements! Thanks for taking care of this.

I left comments with some minor questions but everything seems fine to move on. 🙌

Comment on lines +67 to 68
response_json, total_images, tries = None, 0, 0
for tries in range(retries):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@obulat
Copy link
Contributor Author

obulat commented May 29, 2021

Apparently, the code problems fixed in this PR do cause errors when running the scripts using Airflow dags, like this:

[2021-05-29 05:05:05,083] {requester.py:44} INFO - Received response from https://api.finna.fi/api/v1/search?filter%5B%5D=format%3A%220%2FImage%2F%22&filter%5B%5D=building%3A%220%2FMuseovirasto%2F%22&limit=100&page=6
[2021-05-29 05:05:05,088] {taskinstance.py:1482} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.9/site-packages/airflow/models/taskinstance.py", line 1138, in _run_raw_task
    self._prepare_and_execute_task_with_callbacks(context, task)
  File "/usr/local/airflow/.local/lib/python3.9/site-packages/airflow/models/taskinstance.py", line 1311, in _prepare_and_execute_task_with_callbacks
    result = self._execute_task(context, task_copy)
  File "/usr/local/airflow/.local/lib/python3.9/site-packages/airflow/models/taskinstance.py", line 1341, in _execute_task
    result = task_copy.execute(context=context)
  File "/usr/local/airflow/.local/lib/python3.9/site-packages/airflow/operators/python.py", line 117, in execute
    return_value = self.execute_callable()
  File "/usr/local/airflow/.local/lib/python3.9/site-packages/airflow/operators/python.py", line 128, in execute_callable
    return self.python_callable(*self.op_args, **self.op_kwargs)
  File "/usr/local/airflow/dags/provider_api_scripts/finnish_museums.py", line 39, in main
    _get_object_list(building)
  File "/usr/local/airflow/dags/provider_api_scripts/finnish_museums.py", line 59, in _get_object_list
    total_images = _process_object_list(object_list)
  File "/usr/local/airflow/dags/provider_api_scripts/finnish_museums.py", line 96, in _process_object_list
    total_images = _process_object(obj)
  File "/usr/local/airflow/dags/provider_api_scripts/finnish_museums.py", line 117, in _process_object
    license_url=license_url,
UnboundLocalError: local variable 'license_url' referenced before assignment

So, it is important to fix them.

@krysal krysal self-requested a review June 2, 2021 20:58
@obulat
Copy link
Contributor Author

obulat commented Jun 9, 2021

I'm closing this PR because it was split into 2: #100 and #103 to make it more manageable.

@obulat obulat closed this Jun 9, 2021
@obulat obulat deleted the remove_mutable_defaults branch June 10, 2021 06:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants