Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDRP- 997 Replace toml.load with tomli.load #376

Merged
merged 6 commits into from
Dec 9, 2024

Conversation

woodac
Copy link
Collaborator

@woodac woodac commented Nov 13, 2024

Replaced toml.load with tomli.load, as it is faster and is supported by Python 3.11.

Added toml decode error exception as a co-pilot suggestion, not 100% its effective as it is not returning anything at the end of the statement.

Code

  • Code runs The code runs on my machine and/or CDSW
  • Conflicts resolved There are no conflicts (I have performed a rebase if necessary)
  • Requirements My/our code functions according to the requirements of the ticket
  • Dependencies I have updated the environment yaml so it includes any new libraries I have used
  • Configuration file updated any high level parameters that the user may interact with have been put into the config file (and imported to the script)
  • Clean Code
    • Code is as PEP 8 compliant as I can humanly make it
    • Code passess flake8 linting check
    • Code adheres to DRY
  • Type hints All new functions have type hints

Documentation

Any new code includes all the following forms of documentation:

  • Function Documentation Docstrings within the function(s')/methods have been created
    • Includes Args and returns for all major functions
    • The docstring details data types
  • Updated Documentation: User and/or developer working doc has been updated

Data

  • All data needed to run this script is available in Dev/Test
  • All data is excluded from this pull request
  • Secrets checker pre-commit passes

Testing

  • Unit tests Unit tests have been created and are passing or a new ticket to create tests has been created

Peer Review Section

  • All requirements install from (updated) requirements.txt
  • Documentation has been created and is clear - check the working document
  • Doctrings (Google format) have been created and accurately describe the function's functionality
  • Unit tests pass, or if not present a new ticket to create tests has been created
  • Code runs The code runs on reviewer's machine and/or CDSW

Final approval (post-review)

The author has responded to my review and made changes to my satisfaction.

  • I recommend merging this request.

Review comments

Insert detailed comments here!

These might include, but not exclusively:

  • bugs that need fixing (does it work as expected? and does it work with other code
    that it is likely to interact with?)
  • alternative methods (could it be written more efficiently or with more clarity?)
  • documentation improvements (does the documentation reflect how the code actually works?)
  • additional tests that should be implemented (do the tests effectively assure that it
    works correctly?)
  • code style improvements (could the code be written more clearly?)
  • Do the changes represent a change in functionality so the version number should increase? Start a discussion if so.
  • As a review you can generates the same outputs from running the code

Your suggestions should be tailored to the code that you are reviewing.
Be critical and clear, but not mean. Ask questions and set actions.

@woodac woodac changed the title Replaced toml.load with tomli.load as it runs faster in python 3.11 RDRP- 997 Replace toml.load with tomli.load Nov 13, 2024
@AnneONS AnneONS self-assigned this Nov 19, 2024
src/outputs/export_files.py Show resolved Hide resolved
src/staging/validation.py Show resolved Hide resolved
@@ -1,6 +1,7 @@
"""Define helper functions to be used throughout the pipeline.."""
import yaml
import toml
import tomli
Copy link
Collaborator

Choose a reason for hiding this comment

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

the toml package is no longer used after switching to tomli. "import toml" can therefore be removed

@@ -49,7 +50,7 @@ def get_schema_headers(config: dict):

# Get the headers for each
schema_headers_dict = {
output_name: toml.load(path) for output_name, path in schema_paths.items()
output_name: tomli.load(path) for output_name, path in schema_paths.items()
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlike in "toml" , in "tomli" method load can't take in a string: it takes a binary (the one you would get with open(..) as f.
In order to read a string try "loads" (with an s) instead.

src/staging/validation.py Show resolved Hide resolved
@@ -44,7 +45,7 @@ def user_config_reader(configfile: str = user_config_path) -> dict:
{'title': 'TOML Example config', 'period': {'start_period':
datetime.date(1990, 10, 10), 'end_period': datetime.date(2000, 10, 5)}}
"""
toml_dict = toml.load(configfile)
toml_dict = tomli.load(configfile)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unlike in "toml" , in "tomli" method load can't take in a string: it takes a binary (the one you would get with open(..) as f.
In order to read a string try "loads" (with an s) instead.

Copy link

github-actions bot commented Nov 29, 2024

Percentage Coverage for this PR

Detailed Coverage Report
FileStmtsMissCoverMissing
src
   __init__.py00100% 
src/construction
   __init__.py00100% 
   all_data_construction.py48480%1–2, 4–5, 7, 9, 17, 23, 46–47, 50, 55–56, 60–62, 66–67, 70, 77–79, 84, 87–91, 93, 98–99, 104–105, 108–110, 114, 118–121, 126–127, 129, 134–135, 139, 141
   construction_helpers.py1041486%31–42, 237, 304
   construction_main.py27270%2–3, 5, 7, 11–12, 15, 18, 54–57, 64, 68–71, 74, 78–81, 84, 89–91, 93
   construction_read_validate.py34340%2–3, 5, 8, 11, 16, 18, 21, 46–49, 51–52, 55–56, 59, 66–68, 72–73, 79, 82, 101–103, 105–106, 109, 116–117, 122, 129
   construction_validation.py102991%83, 86, 113, 121–122, 127, 138, 168, 218
   postcode_construction.py21210%1–2, 4–6, 9, 26, 29, 32, 37, 40–41, 44, 47–49, 51, 55, 59, 62–63
src/estimation
   __init__.py00100% 
   apply_weights.py18288%44–45
   calculate_weights.py520100% 
   estimation_main.py22220%2–4, 6–8, 10, 13, 29, 35, 38, 41–42, 44–51, 53
src/freezing
   __init__.py00100% 
   freezing_apply_changes.py732171%34–37, 40–41, 44–46, 49–52, 56, 64–67, 71–72, 74
   freezing_compare.py682267%111–112, 160–161, 185, 188, 191–193, 197–199, 201–203, 205–206, 238, 261, 264, 267, 270
   freezing_main.py46460%1–3, 5, 7–12, 15, 18, 42–43, 46–47, 49–53, 55, 63, 66–68, 71–72, 76–78, 83–84, 86–87, 90–91, 95, 98, 109–112, 114–116
   freezing_utils.py110100% 
src/imputation
   MoR.py142596%46–47, 95, 314, 464
   __init__.py00100% 
   apportionment.py361072%124, 126, 141, 143, 145, 157–159, 162, 164
   expansion_imputation.py373018%23–24, 26–27, 30, 33–35, 39, 42, 44, 47–48, 50, 53, 56, 76–78, 82, 85, 88, 91, 97, 102, 105, 107, 113, 116, 120
   imputation_helpers.py1295358%63, 65, 78, 91–94, 97–98, 100–101, 103–104, 106–107, 109–110, 112–113, 115–116, 118, 127, 129–130, 132–133, 302, 304–305, 309–310, 312, 316, 337, 340, 344, 347–348, 350, 435, 437–438, 441, 453–454, 456, 458, 474–475, 478–479, 481
   imputation_main.py73730%2–5, 7–17, 20, 23, 55, 58, 62–64, 67–68, 71–72, 75, 77, 82, 85–86, 89, 92–93, 96, 98, 101, 104, 108–110, 114–115, 118, 121–123, 125, 129, 132–133, 135–139, 142–144, 146–150, 153, 156, 159–164, 166
   impute_civ_def.py994752%165, 168, 198, 200–201, 204–205, 208–210, 215–216, 218, 221, 223–226, 228, 230, 235–236, 238, 241, 243–245, 248, 250–251, 253–254, 256–257, 270, 273–277, 279–280, 283–284, 286–287, 289
   manual_imputation.py19190%1–2, 4, 6, 9–10, 28–29, 33, 35, 37, 44, 59, 61–62, 64–65, 67–68
   sf_expansion.py746117%24, 27, 30–31, 34–35, 38, 41, 44–45, 48, 50, 57–58, 61, 66–67, 70, 72, 75–76, 78, 80, 83, 87, 89, 100, 103, 107, 109–110, 112, 114, 117, 120, 129, 132, 135, 144, 147, 149, 155, 161, 173, 176, 181–182, 184, 192–193, 196, 200, 204, 207, 215, 220–221, 224, 226, 230, 232
   short_to_long.py21210%1, 3–4, 7, 20, 23, 25, 32–33, 35, 37–38, 40–41, 43–45, 47, 49, 53, 55
   tmi_imputation.py18112729%28–29, 57, 59, 65–67, 71, 77, 80, 82, 87, 92, 97, 218, 221, 223, 226, 230, 234, 256–257, 260, 263–264, 267, 270–271, 274, 276–277, 279, 281, 284–285, 287, 289, 292, 295–296, 298, 301–303, 305–308, 310, 326, 328, 335, 337–338, 340–341, 343, 345, 347, 349, 352, 355–356, 359, 361, 363, 381–382, 384, 386–387, 389–391, 393–394, 397, 399–400, 403, 405–406, 424, 426, 429–431, 433, 435–436, 438–439, 442, 444–445, 448, 450, 452–453, 473, 477, 480–481, 484–485, 488, 491, 493, 498, 500, 505, 507, 514–515, 520, 527–528, 530, 533–534, 536, 539, 541, 543, 548, 550–551
src/mapping
   __init__.py00100% 
   cellno_mapping.py17476%49–51, 53
   itl_mapping.py100100% 
   mapping_helpers.py55590%24–27, 69
   mapping_main.py45450%2–4, 6–13, 15, 18, 43, 48, 57, 66, 73, 76, 83, 86–88, 95, 97, 100, 102–104, 107, 110, 115–116, 119, 121–123, 127–128, 130–132, 135, 138, 141
   pg_conversion.py40880%52, 55, 58, 122, 125, 128, 162–163
   ultfoc_mapping.py34488%49–51, 95
src/northern_ireland
   __init__.py00100% 
   ni_headcount_fte.py29290%2–4, 6, 8, 11, 25, 27, 29, 31, 33, 35, 37, 40, 55, 57–59, 61, 63–64, 66–67, 69, 72, 81, 83–84, 86
   ni_main.py21210%3–9, 11, 14, 34, 37–39, 45, 52–54, 62, 64–65, 67
   ni_staging.py33330%3–6, 8–9, 11, 14, 22, 25, 27–29, 31, 34, 40, 44, 46, 49, 78–79, 82, 87, 91, 94, 97–102, 104, 106
src/outlier_detection
   __init__.py00100% 
   auto_outliers.py810100% 
   manual_outliers.py160100% 
   outlier_main.py39390%2–4, 6–8, 10, 13, 42, 44–46, 48–49, 52, 55, 58–63, 65, 70–71, 74, 79–82, 85–89, 91, 94, 96, 98
src/outputs
   __init__.py00100% 
   export_files.py92920%5–11, 13–14, 18, 24, 42, 44, 51–54, 61, 68, 73, 76, 103, 109, 112, 119, 121, 124, 130, 132–133, 136–140, 143–144, 147, 158–160, 162, 165, 178, 180–181, 183, 186, 206, 209, 212–213, 220, 224, 227–229, 232, 234, 236, 238–239, 241, 244–248, 250–251, 253, 256–258, 261, 264, 267, 282, 285–286, 294, 297, 299, 301, 311, 313–315, 324, 326, 329–330
   form_output_prep.py181327%25–27, 29–30, 34, 36–37, 39, 41, 43, 47, 49
   frozen_group.py43430%3–4, 6, 8–11, 13, 16, 45, 47–50, 53, 71, 123, 134, 161, 164–168, 170, 172, 175, 178–179, 181, 184, 187, 192–193, 196–197, 200, 203–205, 208–209, 211
   gb_sas.py23230%2–4, 6–9, 11, 14, 30, 33, 38, 41, 44, 47, 52, 55, 58–60, 63–64, 66
   intram_by_civil_defence.py210100% 
   intram_by_itl.py541572%44, 46–48, 52, 142, 145, 148–150, 153–154, 158–159, 168
   intram_by_pg.py350100% 
   intram_by_sic.py33330%2–5, 7, 10, 30–31, 34–35, 38–39, 41, 44–46, 51, 67–71, 73, 76, 79, 82, 85, 88–89, 92–93, 96, 98
   intram_totals.py14140%3–4, 6–7, 9, 12, 29–30, 33, 36, 39–40, 42–43
   long_form.py17170%2–4, 6–9, 11, 14, 29, 32, 35, 38–40, 42–43
   manifest_output.py82820%1–5, 9, 12–13, 16, 34, 50–53, 56–57, 61–62, 67–68, 76, 79–83, 86–92, 94, 112–113, 118, 120–121, 126, 129, 131, 133, 135, 139, 149, 154–155, 161, 164–165, 167–168, 174–177, 183–185, 187, 194, 196, 201, 203–205, 207–208, 210–211, 213–216, 218, 221, 223, 229–230, 233–234
   map_output_cols.py38781%150–151, 153, 155, 158, 161, 163
   ni_sas.py19190%2–8, 10, 13, 26, 29, 32, 35, 40, 43–45, 48–49
   outputs_helpers.py170100% 
   outputs_main.py83830%3–4, 7, 10–22, 24, 27, 49, 54–56, 61, 64, 67–69, 74, 77–78, 81–83, 89, 92–94, 97, 100–102, 104–105, 110, 113–115, 124, 127–129, 133–134, 143, 146–148, 155, 158–160, 164–165, 173, 176–178, 185, 188–190, 195, 198–200, 207, 210–212, 214–216, 218
   short_form.py341361%78, 85, 87, 104, 107, 110, 113, 116, 119–121, 123–124
   tau.py25250%2–8, 10, 13, 30, 34, 37, 40, 43, 46, 51, 54, 56–57, 60–62, 65–66, 68
   total_fte.py12120%2–5, 8, 11, 24, 26–27, 33, 38–39
src/site_apportionment
   __init__.py00100% 
   output_status_filtered.py261542%25, 27, 31–32, 34–35, 37, 44–45, 72, 75, 77, 79–80, 82
   site_apportionment.py126397%466, 468–469
   site_apportionment_main.py23230%2–4, 6–7, 11, 13, 16, 39, 41, 44–45, 48–49, 51–52, 55, 60–63, 65–66
src/staging
   __init__.py00100% 
   postcode_validation.py56394%131–132, 220
   spp_parser.py140100% 
   spp_snapshot_processing.py340100% 
   staging_helpers.py861582%173, 176, 178, 181–182, 185–186, 188–189, 192, 196–197, 199, 203, 209
   staging_main.py98980%3–5, 7, 9–11, 15, 18, 64–66, 69, 71, 75–77, 79–80, 84, 86–87, 90, 93, 96–97, 99–100, 105–107, 111–114, 116–117, 123, 128–130, 132–133, 136, 148–150, 155, 158, 162, 164, 166–168, 171, 175, 177, 179–183, 186, 189, 191–192, 195, 197, 200–204, 207, 211–212, 215–219, 221, 224, 232, 241–242, 244–249, 251, 253–254, 256, 259, 270
   validation.py1434965%34–36, 39, 42, 88–89, 100, 122, 135–136, 141, 145, 153–154, 160–161, 200–201, 207, 209, 212, 214, 221–222, 301, 303–304, 307–308, 311–312, 315, 318–319, 322, 324, 326–327, 341, 343–349, 352, 355
src/utils
   __init__.py00100% 
   breakdown_validation.py1304565%29, 186–188, 190–194, 196, 198, 201–202, 208–209, 212, 215, 217, 220, 222, 234–237, 240–243, 259–260, 265, 308–309, 311, 317–318, 360–363, 365–368, 371
   config.py149298%157–158
   defence.py200100% 
   helpers.py731875%19–20, 24–25, 27, 134–137, 139–140, 143–145, 154, 156–157, 215
   local_file_mods.py1204760%47–53, 98, 148–150, 199–203, 214–215, 226, 237, 248–249, 251, 262–263, 274–275, 284, 292, 303, 305–306, 308–309, 313–315, 319, 333–334, 336–337, 340–341, 343, 360, 365
   path_helpers.py139894%96, 304, 306, 308–309, 311–312, 314
   postcode_reduction_helper.py38380%11, 13–14, 16, 19, 22, 25, 27–28, 30–31, 34–35, 38, 40–41, 43–44, 47, 60, 62, 65, 68, 71–72, 74–75, 77, 80, 95, 97, 100–101, 104–105, 107, 109–110
   s3_mods.py1271270%21–22, 26–27, 31, 41, 45–47, 51, 65, 67–68, 71–74, 76–77, 80, 91, 94, 99, 102, 105, 108, 119–120, 122, 125, 139, 143–144, 146, 149, 159, 166, 169, 171, 174, 176, 179, 190–191, 193, 196, 204–205, 208, 217–218, 222–225, 228, 239–240, 243–244, 247, 252, 255, 266–267, 269–272, 274–275, 278, 284, 287, 300, 303, 306, 308, 311, 317, 320, 323, 326, 329, 337–339, 341, 344, 348–349, 351, 354, 383, 387, 389, 396, 399, 404–406, 413, 416, 431, 433–434, 436–437, 441, 443, 445, 448, 450–452, 454, 457, 467, 470–471, 474, 477, 479–480, 483–485
   singleton_boto.py20200%4–5, 8–10, 12–13, 15–19, 23–25, 27–31
TOTAL3939190251% 

Summary of tests

Tests Skipped Failures Errors Time
259 2 💤 0 ❌ 0 🔥 3.362s ⏱️

Copy link
Collaborator

@Ryan2Y79 Ryan2Y79 left a comment

Choose a reason for hiding this comment

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

Just one question to address. Cheers.

src/staging/validation.py Show resolved Hide resolved
@AnneONS AnneONS merged commit a4e50c4 into develop Dec 9, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants