-
Notifications
You must be signed in to change notification settings - Fork 171
Add tests for new FieldSet object #1998
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cd998d6
Add U and V fields to example datasets
VeckoTheGecko 16cfc20
Remove reference to _parent_mesh
VeckoTheGecko 4fd4620
Split reprs into separate file
VeckoTheGecko b22c649
Add fieldset tests
VeckoTheGecko f0269e8
Add check for field type in add_field
VeckoTheGecko fd3b78a
Add fieldset.constants
VeckoTheGecko 6cd46ad
Add FieldSet.__getattr__
VeckoTheGecko 2abd829
Fix FieldSet.add_constant_field
VeckoTheGecko 4854a53
Add test stub
VeckoTheGecko 833363c
Update to use attr access
VeckoTheGecko f51ce89
Add type guard in FieldSet.__init__
VeckoTheGecko be8c886
Remove xfail on test_field_structured_grid_creation
VeckoTheGecko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Parcels reprs""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import textwrap | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from parcels import Field, FieldSet, ParticleSet | ||
|
|
||
|
|
||
| def field_repr(field: Field) -> str: | ||
| """Return a pretty repr for Field""" | ||
| out = f"""<{type(field).__name__}> | ||
| name : {field.name!r} | ||
| data : {field.data!r} | ||
| extrapolate time: {field.allow_time_extrapolation!r} | ||
| """ | ||
| return textwrap.dedent(out).strip() | ||
|
|
||
|
|
||
| def _format_list_items_multiline(items: list[str], level: int = 1) -> str: | ||
| """Given a list of strings, formats them across multiple lines. | ||
|
|
||
| Uses indentation levels of 4 spaces provided by ``level``. | ||
|
|
||
| Example | ||
| ------- | ||
| >>> output = _format_list_items_multiline(["item1", "item2", "item3"], 4) | ||
| >>> f"my_items: {output}" | ||
| my_items: [ | ||
| item1, | ||
| item2, | ||
| item3, | ||
| ] | ||
| """ | ||
| if len(items) == 0: | ||
| return "[]" | ||
|
|
||
| assert level >= 1, "Indentation level >=1 supported" | ||
| indentation_str = level * 4 * " " | ||
| indentation_str_end = (level - 1) * 4 * " " | ||
|
|
||
| items_str = ",\n".join([textwrap.indent(i, indentation_str) for i in items]) | ||
| return f"[\n{items_str}\n{indentation_str_end}]" | ||
|
|
||
|
|
||
| def particleset_repr(pset: ParticleSet) -> str: | ||
| """Return a pretty repr for ParticleSet""" | ||
| if len(pset) < 10: | ||
| particles = [repr(p) for p in pset] | ||
| else: | ||
| particles = [repr(pset[i]) for i in range(7)] + ["..."] | ||
|
|
||
| out = f"""<{type(pset).__name__}> | ||
| fieldset : | ||
| {textwrap.indent(repr(pset.fieldset), " " * 8)} | ||
| pclass : {pset.pclass} | ||
| repeatdt : {pset.repeatdt} | ||
| # particles: {len(pset)} | ||
| particles : {_format_list_items_multiline(particles, level=2)} | ||
| """ | ||
| return textwrap.dedent(out).strip() | ||
|
|
||
|
|
||
| def fieldset_repr(fieldset: FieldSet) -> str: | ||
| """Return a pretty repr for FieldSet""" | ||
| fields_repr = "\n".join([repr(f) for f in fieldset.get_fields()]) | ||
|
|
||
| out = f"""<{type(fieldset).__name__}> | ||
| fields: | ||
| {textwrap.indent(fields_repr, 8 * " ")} | ||
| """ | ||
| return textwrap.dedent(out).strip() | ||
|
|
||
|
|
||
| def default_repr(obj: Any): | ||
| if is_builtin_object(obj): | ||
| return repr(obj) | ||
| return object.__repr__(obj) | ||
|
|
||
|
|
||
| def is_builtin_object(obj): | ||
| return obj.__class__.__module__ == "builtins" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.