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

Feature: respect newline characters in strings #51

Merged
merged 7 commits into from
Nov 5, 2024

Conversation

davemfish
Copy link
Collaborator

@davemfish davemfish commented Nov 4, 2024

Instead of dumping a string with newline characters directly, we can add a yaml block indicator ( | ) to indicate that the dumper should treat newline chars as newlines. https://yaml-multiline.info/

The upshot is that we get this "literal" block,

lineage: |-
  Created by natcap.invest.annual_water_yield.execute(
  {'biophysical_table_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\biophysical_table_gura.csv',
   'depth_to_root_rest_layer_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\depth_to_root_restricting_layer_gura.tif',
   'eto_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\reference_ET_gura.tif',
   'lulc_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\land_use_gura.tif',
   'pawc_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\plant_available_water_fraction_gura.tif',
   'precipitation_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\precipitation_gura.tif',
   'results_suffix': 'gura',
   'seasonality_constant': '5',
   'sub_watersheds_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\subwatersheds_gura.shp',
   'watersheds_path': 'C:\\Users\\dmf\\projects\\invest\\data\\invest-sample-data\\Annual_Water_Yield\\watershed_gura.shp',
   'workspace_dir': 'runs/awy'})
  Version 3.14.1.post384+g36834555f.d20241029

instead of this double-quoted block,

lineage: "Created by natcap.invest.annual_water_yield.execute(\n{'biophysical_table_path':\
  \ 'C:\\\\Users\\\\dmf\\\\projects\\\\invest\\\\data\\\\invest-sample-data\\\\Annual_Water_Yield\\\
  \\biophysical_table_gura.csv',\n 'depth_to_root_rest_layer_path': 'C:\\\\Users\\\
  \\dmf\\\\projects\\\\invest\\\\data\\\\invest-sample-data\\\\Annual_Water_Yield\\\
  \\depth_to_root_restricting_layer_gura.tif',\n 'eto_path': 'C:\\\\Users\\\\dmf\\\
  \\projects\\\\invest\\\\data\\\\invest-sample-data\\\\Annual_Water_Yield\\\\reference_ET_gura.tif',\n\
  \ 'lulc_path': 'C:\\\\Users\\\\dmf\\\\projects\\\\invest\\\\data\\\\invest-sample-data\\\
  \\Annual_Water_Yield\\\\land_use_gura.tif',\n 'pawc_path': 'C:\\\\Users\\\\dmf\\\
  \\projects\\\\invest\\\\data\\\\invest-sample-data\\\\Annual_Water_Yield\\\\plant_available_water_fraction_gura.tif',\n\
  \ 'precipitation_path': 'C:\\\\Users\\\\dmf\\\\projects\\\\invest\\\\data\\\\invest-sample-data\\\
  \\Annual_Water_Yield\\\\precipitation_gura.tif',\n 'results_suffix': 'gura',\n 'seasonality_constant':\
  \ '5',\n 'sub_watersheds_path': 'C:\\\\Users\\\\dmf\\\\projects\\\\invest\\\\data\\\
  \\invest-sample-data\\\\Annual_Water_Yield\\\\subwatersheds_gura.shp',\n 'watersheds_path':\
  \ 'C:\\\\Users\\\\dmf\\\\projects\\\\invest\\\\data\\\\invest-sample-data\\\\Annual_Water_Yield\\\
  \\watershed_gura.shp',\n 'workspace_dir': 'runs/awy'})\nVersion 3.14.1.post384+g36834555f.d20241029"

@@ -261,7 +261,7 @@ def describe_raster(source_dataset_path, scheme):
b = i + 1
bands.append(models.BandSchema(
index=b,
gdal_type=info['datatype'],
gdal_type=gdal.GetDataTypeName(info['datatype']),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not related to the rest of the PR, but we should have been using the human-readable names all along.

@davemfish davemfish marked this pull request as ready for review November 4, 2024 20:36
@davemfish davemfish requested a review from phargogh November 4, 2024 20:36
Copy link
Member

@phargogh phargogh left a comment

Choose a reason for hiding this comment

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

Thanks, @davemfish ! This makes sense to me, and thank you for illuminating the | vs > behavior in YAML ... that was a really helpful resource.

I really just have one question, which has to do with the modification of the yaml module's state and whether it might make sense to put that into the dumper subclass we have defined here in utils. I don't know enough about the yaml module to be able to say for sure what would be best, but it would be very common to have these functional updates represented in a subclass. Curious what you think!

Comment on lines 11 to 21
# Patch the default string representer so that it uses
# a literal block style when the data contain newline characters
yaml.SafeDumper.add_representer(str, represent_str)


# https://stackoverflow.com/questions/13518819/avoid-references-in-pyyaml
class _NoAliasDumper(yaml.SafeDumper):
"""Keep the yaml human-readable by avoiding anchors and aliases."""

def ignore_aliases(self, data):
return True
Copy link
Member

Choose a reason for hiding this comment

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

Since we are subclassing _NoAliasDumper from yaml.SafeDumper, is self.add_representer something that we could or should call in an __init__(...) method of _NoAliasDumper? It makes sense to me that we would want to modify behavior, but I guess I'm just wondering if we can do this without modifying global state, which would put us at risk of a race condition that could break expected behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@phargogh great idea, thank you! I pushed this change.

@davemfish davemfish requested a review from phargogh November 5, 2024 18:14
Copy link
Member

@phargogh phargogh left a comment

Choose a reason for hiding this comment

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

Thanks @davemfish !

@phargogh phargogh merged commit a02862a into natcap:main Nov 5, 2024
10 checks 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.

2 participants