Seamlessly Read/Write datetime.date
Objects
#18
Closed
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.
Pyshp's API is able to cast Python string/byte/unicode values, as well as numeric values. Its handling of Date fields is not so seamless. Users are expected to write exactly eight characters in YYYYMMDD format, then get handed a tuple of [year, month, day] ints when reading.
This patch allows writing values as
datetime.date
(or any subclass, eg.datetime.datetime
). It also maintains backwards compatibility allowing the user to write a string value. It changes reading behavior, however, in that it now passes back adatetime.date
object rather than a [year, month, day] int tuple. Reader does not handle NULL date fields, because I didn't want to clobber the patch I submitted with PR #16 ; so I'll note here that this code will raise an exception when reading a 'D' field with value "00000000" - I can amend this PR if you prefer.This PR also adds a helper function
Writer.fieldDate(name)
so that users don't need to care about the underlying dBase storage whatsoever. I'd be happy to contribute more helper functions like this for other typical data types if you like.A unit test is included; however I used the Python 2.1+ standard library's 'unittest' framework rather than grafting more doctests to the README file. Documentation has not been updated.