-
Notifications
You must be signed in to change notification settings - Fork 30
General cardinality feature updates #382
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 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5428a92
[util] Add format_cardinality func
mpsonntag 5287065
[test/util] Add format_cardinality test
mpsonntag 019c87b
[property] Use util.format_cardinality
mpsonntag 5f10714
[util] Handle cardinality empty tuple edgecase
mpsonntag 7a5fd79
[util] Handle exact val cardinality edge case
mpsonntag b248516
[property] Move cardinality val to own func
mpsonntag c325e85
[property] Filter warnings on cardinality validate
mpsonntag 27cb745
[test/validation] Add clear output method
mpsonntag 6aa02e4
[util] Allow list in cardinality
mpsonntag 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,62 @@ | ||
| # -*- coding: utf-8 | ||
| """ | ||
| Module containing general utility functions. | ||
| """ | ||
|
|
||
|
|
||
| def format_cardinality(in_val): | ||
| """ | ||
| Checks an input value and formats it towards a custom tuple format | ||
| used in odml Section, Property and Values cardinality. | ||
|
|
||
| The following cases are supported: | ||
| (n, n) - default, no restriction | ||
| (d, n) - minimally d entries, no maximum | ||
| (n, d) - maximally d entries, no minimum | ||
| (d, d) - minimally d entries, maximally d entries | ||
|
|
||
| Only positive integers are supported. 'None' is used to denote | ||
| no restrictions on a maximum or minimum. | ||
|
|
||
| :param in_val: Can either be 'None', a positive integer, which will set | ||
| the maximum or an integer 2-tuple of the format '(min, max)'. | ||
|
|
||
| :returns: None or the value as tuple. A ValueError is raised, if the | ||
| provided value was not in an acceptable format. | ||
| """ | ||
| exc_msg = "Can only assign positive single int or int-tuples of the format '(min, max)'" | ||
|
|
||
| # Empty values reset the cardinality to None. | ||
| if not in_val: | ||
| return None | ||
|
|
||
| # Catch tuple edge cases (0, 0); (None, None); (0, None); (None, 0) | ||
| if isinstance(in_val, tuple) and len(in_val) > 1 and not in_val[0] and not in_val[1]: | ||
| return None | ||
|
|
||
| # Providing a single integer sets the maximum value in a tuple. | ||
| if isinstance(in_val, int) and in_val > 0: | ||
| return None, in_val | ||
|
|
||
| # Only integer 2-tuples of the format '(min, max)' are supported to set the cardinality | ||
| if isinstance(in_val, tuple) and len(in_val) == 2: | ||
| v_min = in_val[0] | ||
| v_max = in_val[1] | ||
|
|
||
| min_int = isinstance(v_min, int) and v_min >= 0 | ||
| max_int = isinstance(v_max, int) and v_max >= 0 | ||
|
|
||
| if max_int and min_int and v_max >= v_min: | ||
| return v_min, v_max | ||
|
|
||
| if max_int and not v_min: | ||
| return None, v_max | ||
|
|
||
| if min_int and not v_max: | ||
| return v_min, None | ||
|
|
||
| # Use helpful exception message in the following case: | ||
| if max_int and min_int and v_max < v_min: | ||
| exc_msg = "Minimum larger than maximum (min=%s, max=%s)" % (v_min, v_max) | ||
|
|
||
| raise ValueError(exc_msg) | ||
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,62 @@ | ||
| """ | ||
| This file tests odml util functions. | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from odml.util import format_cardinality | ||
|
|
||
|
|
||
| class TestUtil(unittest.TestCase): | ||
|
|
||
| def test_format_cardinality(self): | ||
| # Test empty set | ||
| self.assertIsNone(format_cardinality(None)) | ||
| self.assertIsNone(format_cardinality([])) | ||
| self.assertIsNone(format_cardinality({})) | ||
| self.assertIsNone(format_cardinality("")) | ||
| self.assertIsNone(format_cardinality(())) | ||
|
|
||
| # Test empty tuple edge cases | ||
| self.assertIsNone(format_cardinality((None, None))) | ||
| self.assertIsNone(format_cardinality((0, 0))) | ||
| self.assertIsNone(format_cardinality((None, 0))) | ||
| self.assertIsNone(format_cardinality((0, None))) | ||
|
|
||
| # Test single int max set | ||
| self.assertEqual(format_cardinality(10), (None, 10)) | ||
|
|
||
| # Test tuple set | ||
| set_val = (2, None) | ||
| self.assertEqual(format_cardinality(set_val), set_val) | ||
| set_val = (None, 2) | ||
| self.assertEqual(format_cardinality(set_val), set_val) | ||
| set_val = (2, 3) | ||
| self.assertEqual(format_cardinality(set_val), set_val) | ||
|
|
||
| # Test exact value tuple set | ||
| set_val = (5, 5) | ||
| self.assertEqual(format_cardinality(set_val), set_val) | ||
|
|
||
| # Test set failures | ||
| with self.assertRaises(ValueError): | ||
| format_cardinality("a") | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| format_cardinality(-1) | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| format_cardinality((1, "b")) | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| format_cardinality((1, 2, 3)) | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| format_cardinality((-1, 1)) | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| format_cardinality((1, -5)) | ||
|
|
||
| with self.assertRaises(ValueError) as exc: | ||
| format_cardinality((5, 1)) | ||
| self.assertIn("Minimum larger than maximum ", str(exc)) |
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
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.