-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Description
Fix Google Custom Search (GCS) off by one index issue
Technical details
Data fix script
scripts/1-fetch/fix_index.py
#!/usr/bin/env python
"""
Fix Google Custom Search (GCS) off by one index issue
"""
# Standard library
import csv
import os
import sys
import textwrap
import traceback
# Third-party
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import PythonTracebackLexer
# Add parent directory so shared can be imported
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
# First-party/Local
import shared # noqa: E402
# Setup
LOGGER, PATHS = shared.setup(__file__)
# Constants
FILE1_COUNT = os.path.join(PATHS["data_phase"], "gcs_1_count.csv")
FILE2_LANGUAGE = os.path.join(
PATHS["data_phase"], "gcs_2_count_by_language.csv"
)
FILE3_COUNTRY = os.path.join(PATHS["data_phase"], "gcs_3_count_by_country.csv")
def read_data(file_path):
data = []
with open(file_path, "r", newline="") as file_obj:
data = list(csv.reader(file_obj, dialect="unix"))
return data
def increment_index(data):
for i, row in enumerate(data):
if i == 0:
continue
row[0] = int(row[0]) + 1
data[i] = row
return data
def write_data(file_path, data):
with open(file_path, "w", newline="") as file_obj:
writer = csv.writer(file_obj, dialect="unix")
writer.writerows(data)
def main():
for file_path in [FILE1_COUNT, FILE2_LANGUAGE, FILE3_COUNTRY]:
data = read_data(file_path)
data = increment_index(data)
write_data(file_path, data)
if __name__ == "__main__":
try:
main()
except shared.QuantifyingException as e:
if e.exit_code == 0:
LOGGER.info(e.message)
else:
LOGGER.error(e.message)
sys.exit(e.exit_code)
except SystemExit as e:
LOGGER.error(f"System exit with code: {e.code}")
sys.exit(e.code)
except KeyboardInterrupt:
LOGGER.info("(130) Halted via KeyboardInterrupt.")
sys.exit(130)
except Exception:
traceback_formatted = textwrap.indent(
highlight(
traceback.format_exc(),
PythonTracebackLexer(),
TerminalFormatter(),
),
" ",
)
LOGGER.exception(f"(1) Unhandled exception:\n{traceback_formatted}")
sys.exit(1)Checklist
- My pull request has a descriptive title (not a vague title like
Update index.md). - My pull request targets the default branch of the repository (
mainormaster). - My commit messages follow best practices.
- My code follows the established code style of the repository.
- I added or updated tests for the changes I made (if applicable).
- I added or updated documentation (if applicable).
- I tried running the project locally and verified that there are no
visible errors.
Developer Certificate of Origin
For the purposes of this DCO, "license" is equivalent to "license or public domain dedication," and "open source license" is equivalent to "open content license or public domain dedication."
Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Originally posted by @TimidRobot in creativecommons/quantifying#143
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels