-
Notifications
You must be signed in to change notification settings - Fork 9
WIP: Type conversions (ToInteger, ToString, ToBoolean, ToFloat) #50
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
Draft
japsu
wants to merge
2
commits into
master
Choose a base branch
from
feature/typeconv
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,56 @@ | ||
| from numbers import Number | ||
| from collections.abc import Mapping | ||
| from typing import Optional, Type, Union | ||
|
|
||
| from ..context import Context | ||
| from ..void import Void, VoidType | ||
| from .base import BaseTag | ||
|
|
||
|
|
||
| class _BaseToType(BaseTag): | ||
| """ | ||
| arguments: Data to convert. | ||
| example: "`!{name} ...`" | ||
| description: Converts the input to the desired type. | ||
| """ | ||
|
|
||
| value_types = (object,) | ||
| target_type: Type | ||
|
|
||
| def enrich(self, context: Context): | ||
| return self.target_type(context.enrich(self.data)) | ||
|
|
||
|
|
||
| class ToBoolean(_BaseToType): | ||
| __doc__ = _BaseToType.__doc__ | ||
| target_type = bool | ||
|
|
||
|
|
||
| class ToInteger(_BaseToType): | ||
| """ | ||
| arguments: Either single argument containing the data to convert, or an object with `value:` and `radix:`. | ||
| example: `!ToInteger "50"`, `!ToInteger value: "C0FFEE", radix: 16` | ||
| description: Converts the input to Python `int`. Radix is never inferred from input: if not supplied, it is always 10. | ||
| """ | ||
|
|
||
| target_type = int | ||
|
|
||
| def enrich(self, context: Context): | ||
| data = context.enrich(self.data) | ||
|
|
||
| if isinstance(data, Mapping): | ||
| value = data["value"] | ||
| radix = data.get("radix", 10) | ||
| return self.target_type(value, radix) | ||
| else: | ||
| return self.target_type(data) | ||
|
|
||
|
|
||
| class ToFloat(_BaseToType): | ||
| __doc__ = _BaseToType.__doc__ | ||
| target_type = float | ||
|
|
||
|
|
||
| class ToString(_BaseToType): | ||
| __doc__ = _BaseToType.__doc__ | ||
| target_type = str |
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| from numbers import Number | ||
| from typing import Optional, Union | ||
| from typing import Optional, Type, Union | ||
|
|
||
| from ..context import Context | ||
| from ..void import Void, VoidType | ||
|
|
@@ -12,8 +12,9 @@ class _BaseIsType(BaseTag): | |
| example: "`!{name} ...`" | ||
| description: Returns True if the value enriched is of the given type, False otherwise. | ||
| """ | ||
| requisite_type = None | ||
|
|
||
| value_types = (object,) | ||
| requisite_type: Type | ||
|
|
||
| def enrich(self, context: Context) -> bool: | ||
| return self.check(context.enrich(self.data)) | ||
|
|
@@ -67,4 +68,4 @@ class IsNone(_BaseIsType): | |
| """ | ||
|
|
||
| def check(self, value: Optional[Union[VoidType, str]]) -> bool: | ||
| return (value is None or value is Void) | ||
| return value is None or value is Void | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Black |
||
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 |
|---|---|---|
|
|
@@ -8,7 +8,10 @@ | |
|
|
||
|
|
||
| with open(os.path.join(source_dir, 'emrichen', '__init__.py')) as f: | ||
| version = re.search("__version__ = ['\"]([^'\"]+)['\"]", f.read()).group(1) | ||
| init_file = f.read() | ||
| match = re.search("__version__ = ['\"]([^'\"]+)['\"]", init_file) | ||
| assert match, "Failed to parse version from emrichen/__init__.py" | ||
| version = match.group(1) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pyrekt |
||
|
|
||
|
|
||
| with open(os.path.join(source_dir, 'README.md'), encoding='utf-8') as f: | ||
|
|
@@ -29,7 +32,7 @@ | |
| author='Santtu Pajukanta', | ||
| author_email='santtu@pajukanta.fi', | ||
| url='http://github.com/con2/emrichen', | ||
| packages = find_packages(exclude=["tests"]), | ||
| packages=find_packages(exclude=["tests"]), | ||
| zip_safe=True, | ||
| entry_points={ | ||
| 'console_scripts': [ | ||
|
|
||
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,30 @@ | ||
| import pytest | ||
|
|
||
| from emrichen import Template, Context | ||
| from emrichen.void import Void | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'tag, val, result', | ||
| [ | ||
| ('ToBoolean', 0, False), | ||
| # ('ToBoolean', "false", False), | ||
| ('ToBoolean', "TRUE", True), | ||
| ('ToInteger', "8", 8), | ||
| ('ToInteger', {"value": "0644", "radix": 8}, 420), | ||
| ('ToFloat', "8.2", 8.2), | ||
| ('ToFloat', 8, 8.0), | ||
| ('ToFloat', True, 1.0), | ||
| ('ToString', True, "True"), # TODO too pythonic? should we return lowercase instead? | ||
| ('ToString', 8, "8"), | ||
| # ('ToString', {'a': 5, 'b': 6}, "{'a': 5, 'b': 6}"), # TODO OrderedDict([('a', 5), ('b', 6)]) | ||
| ], | ||
| ) | ||
| def test_typeop(tag, val, result): | ||
| resolved = Template.parse(f"!{tag},Lookup 'a'").enrich(Context({'a': val}))[0] | ||
| assert resolved == result, f'{tag}({val!r}) returned {resolved}, expected {result}' | ||
|
|
||
| # type equivalence instead of isinstance is intended: want strict conformance | ||
| assert type(resolved) == type( | ||
| result | ||
| ), f'{tag}({val!r}) returned type {type(resolved)}, expected {type(result)}' |
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.
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.
Pyrekt