-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
… likely to have them. natcap#49
@@ -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']), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
src/geometamaker/utils.py
Outdated
# 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @davemfish !
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,
instead of this double-quoted block,