-
Notifications
You must be signed in to change notification settings - Fork 8
Parse hours, SIREN and SIRET as text #42
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
8 commits
Select commit
Hold shift + click to select a range
651e53a
Add failing test case
AntoineAugusti f57e840
Add custom tester for time
AntoineAugusti 9585980
Patch agatesql
AntoineAugusti 4520a3c
Add more things to .gitignore
AntoineAugusti 8e35cb0
Add SirenSiret type tester
AntoineAugusti d9341b2
Small code improvements
AntoineAugusti fcfbca0
PR comments
AntoineAugusti a52c9e1
Add PR to CHANGELOG
AntoineAugusti 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ dbs/*.db | |
| *.pyc | ||
| build/ | ||
| dist/ | ||
| reports/ | ||
| .pytest_cache/ | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import re | ||
|
|
||
| from agate.data_types.base import DataType | ||
| from agate.data_types.boolean import Boolean | ||
| from agate.data_types.date import Date | ||
| from agate.data_types.date_time import DateTime | ||
| from agate.data_types.number import Number | ||
| from agate.data_types.text import Text | ||
| from agate.data_types.time_delta import TimeDelta | ||
| from agate.exceptions import CastError | ||
| from agate.type_tester import TypeTester | ||
|
|
||
| from agatesql import table as agatesqltable | ||
|
|
||
| from sqlalchemy.types import VARCHAR | ||
|
|
||
| from stdnum.fr.siren import is_valid as is_valid_siren | ||
| from stdnum.fr.siret import is_valid as is_valid_siret | ||
|
|
||
|
|
||
| class Time(DataType): | ||
| # Detect an hour minute string. | ||
| # Examples: 12:20, 9:50, 23:30 | ||
| def __init__(self, **kwargs): | ||
| super(Time, self).__init__(**kwargs) | ||
|
|
||
| def cast(self, d): | ||
| if re.match(r"^(?:[01]\d|2[0-3]|\d):[0-5]\d$", d): | ||
| return Text().cast(d) | ||
| raise CastError('Can not parse value "%s" as time.' % d) | ||
|
|
||
|
|
||
| class SirenSiret(DataType): | ||
| # Detect a SIREN or SIRET number | ||
| def __init__(self): | ||
| super(SirenSiret, self).__init__() | ||
|
|
||
| def cast(self, d): | ||
| if is_valid_siret(d) or is_valid_siren(d): | ||
| return Text().cast(d) | ||
| raise CastError('Can not parse value "%s" as a SIREN or SIRET.' % d) | ||
|
|
||
|
|
||
| # agatesql needs to know the SQL equivalent of a type. | ||
| # Tell agatesql how our custom types should be converted in SQL. | ||
| # | ||
| # Reference: | ||
| # https://github.com/wireservice/agate-sql/blob/7466073d81289323851c21817ea33170e36ce2a5/agatesql/table.py#L21-L28 | ||
| agatesqltable.SQL_TYPE_MAP[Time] = VARCHAR | ||
| agatesqltable.SQL_TYPE_MAP[SirenSiret] = VARCHAR | ||
AntoineAugusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def agate_tester(): | ||
| # Override the original list of type checkers present in agate | ||
| # to detect types. | ||
| # | ||
| # Original list here: | ||
| # https://github.com/wireservice/agate/blob/e3078dca8b3566e8408e65981f79918c2f36f9fe/agate/type_tester.py#L64-L71 | ||
| return TypeTester( | ||
| types=[ | ||
AntoineAugusti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Boolean(), | ||
| SirenSiret(), | ||
| Number(), | ||
| Time(), | ||
| TimeDelta(), | ||
| Date(), | ||
| DateTime(), | ||
| Text(), | ||
| ] | ||
| ) | ||
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
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.