Skip to content

Document the purpose and use of clean_value#2295

Merged
sphuber merged 2 commits into
aiidateam:provenance_redesignfrom
ConradJohnston:fix_2038_clean_value
Dec 7, 2018
Merged

Document the purpose and use of clean_value#2295
sphuber merged 2 commits into
aiidateam:provenance_redesignfrom
ConradJohnston:fix_2038_clean_value

Conversation

@ConradJohnston
Copy link
Copy Markdown
Contributor

@ConradJohnston ConradJohnston commented Dec 5, 2018

Fixes #2038 and #1498

  • Update the docs to explain the use of clean_value and its behaviour.
  • Update clean_value doc string to better explain its purpose.
  • Change collection imports from, for example, "import collections.Iterable" to
    "from collections import Iterable" to comply with a forthcoming
    deprecation that will break these imports in Python 3.8.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

idempotent is not the correct term here. An action is said to be idempotent if it can be invoked any number of times and the aggregate result is always the same, regardless of the number of times it was called. What I think we want to say here is that the clean_value will transform the value such, that a serialization/deserialization round trip through the database will return the same value.

Also we should add after the mention that Iterables are converted to lists, that Mappings will be converted into plain dictionaries.

Finally, there is a small typo defualt -> default

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Changes made.

- Update the docs to explain the use of clean_value and its behaviour.
- Update clean_value doc string to better explain its purpose.
- Change collection imports from, for example, "import collections.Iterable" to
"from collections import Iterable" to comply wth a forthcoming
deprecation that will break these imports in Python 3.8.

Node methods
******************
- :py:meth:`~aiida.orm.implementation.general.node.clean_value` takes a value and returns an object which can be serialized for storage in the database. Such an object must be able to be subsequently deserialized without changing value. If a simple datatype is passed (integer, float, etc.), a check is performed to see if it has a value of ``nan`` or ``inf``, as these cannot be stored. Otherwise, if a list, tuple, dictionary, etc., is passed, this check is performed for each value it contains. This is done recursively, automatically handling the case of nested objects. It is important to note that iterable type objects are converted to lists during this process, and mappings, such as dictionaries, are converted to normal dictionaries. This cleaning process is used by default when setting node attributes via :py:meth:`~aiida.orm.implementation.general.node.AbstractNode._set_attr` and :py:meth:`~aiida.orm.implementation.general.node.AbstractNode._append_to_attr`, although it can be disabled by setting ``clean=False``. Values are also cleaned when setting extras on a stored node using :py:meth:`~aiida.orm.implementation.general.node.AbstractNode.set_extras` or :py:meth:`~aiida.orm.implementation.general.node.AbstractNode.reset_extras`, but this cannot be disabled.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A couple of small notes here:

  • I don't think NaN and inf are already checked - this is part of SQLalchemy: storing ParameterData object with "nan" value of an attribute raises an exception #1498 (for me it's fine if you want to solve both issues in this PR but then you need to add the logic to check for these values).
  • maybe a short mention that at .store() clean_value is not called, so if you use clean=False you should be very sure that you are only using base types (e.g. not tuples but lists etc.) otherwise e.g. the hashing will be wrong, and depending on the DB backend you can either get an exception while storing (I guess in the case of numpy arrays), or the type can be converted silently (I guess in the case of tuples).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, you are right! I didn't realise this was already fixed in #2129 but we didn't close #1498

Copy link
Copy Markdown
Member

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

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

See my comment

@coveralls
Copy link
Copy Markdown

coveralls commented Dec 8, 2018

Coverage Status

Coverage decreased (-0.003%) to 69.009% when pulling 7929d23 on ConradJohnston:fix_2038_clean_value into 85d7309 on aiidateam:provenance_redesign.

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