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
[Review]: Clean up parse_time
API
#887
Conversation
I have a déjà vu … |
I closed the previous request |
""" | ||
if isinstance(time_string, datetime): | ||
return time_string | ||
elif isinstance(time_string, tuple): | ||
return datetime(*time_string) | ||
elif tai and ( isinstance( time_string, int) or isinstance( time_string, float)): |
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.
fix the formatting to: elif tai and isinstance(time_string, int) or isinstance(time_string, float):
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.
Doing the above was clashing with other unit test cases
def test_parse_time_int():
> assert parse_time(765548612.0) == datetime(2003, 4, 5, 12, 23, 32)
E assert datetime.datetime(1982, 4, 5, 12, 23, 32) == datetime.datetime(2003, 4, 5, 12, 23, 32)
E + where datetime.datetime(1982, 4, 5, 12, 23, 32) = parse_time(765548612.0)
E + and datetime.datetime(2003, 4, 5, 12, 23, 32) = datetime(2003, 4, 5, 12, 23, 32)
time/tests/test_time.py:24: AssertionError
""" | ||
if isinstance(time_string, datetime): | ||
return time_string | ||
elif isinstance(time_string, tuple): | ||
return datetime(*time_string) | ||
elif tai and isinstance( time_string, int) or tai and isinstance( time_string, float) : |
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.
A question to the most pythonists! Would it be consider a bad practice to put this as:
elif (tai and isinstance( time_string, int)) or (tai and isinstance (time_string, float)):
? This makes it, at least to me, easier to read.
But, in any case, could we not just do this as:
elif tai and (isinstance( time_string, int)) or isinstance (time_string, float)):
I believe in this case the ()
are essential, otherwise we could get True
by just having time_string
as float
.
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.
and
has a higher priority than or
. The syntax of Python is defined in a way that (expr)
is the same as expr
, the parentheses only determine the precedence level. (those two things are very common in all languages I know that contain some form of and
/&&
and parentheses to denote the precedence level). Another important thing to know is that for boolean algebra the distributive law holds: A AND (B OR C) = A AND B OR A AND C
.
To answer your question @dpshelio: keeping these facts in mind, the first example tai and isinstance(time_string, int) or tai and isinstance(time_string, float)
(I removed unnecessary parens) is equivalent to tai and (isinstance(time_string, int) or isinstance(time_string, float))
I agree with both of you ,from a personal opinion I prefer the style with the parenthesis,gives an easier to understand code |
I didn't share my opinion. I just explained which expressions are equivalent. Now to share my opinion: with only two extra expressions (those that check the type) I don't have any strong feelings. But especially if it gets more, I'm in favor of the solution which is of the form |
I am a bit confused,should I change it back? |
Well, at least you should follow PEP 8 and also remove unnecessary parens. The other thing is subjective for such a short line. |
Thanks @derdon for the explanation and your point of view. I also prefer the: tai and (A or B or C ...) approach. @gunner272, could you make the changes suggested in the docstring and change the comparison from above obeying DRY. |
@@ -23,6 +23,16 @@ def test_parse_time_tuple(): | |||
def test_parse_time_int(): | |||
assert parse_time(765548612.0) == datetime(2003, 4, 5, 12, 23, 32) | |||
assert parse_time(1009685652.0) == datetime(2010, 12, 30, 4, 14, 12) | |||
|
|||
def test_parse_time_tai(): |
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.
Also, could you insert a commented line or docstring in here pointing us to the source you've used to get the TAI values? webpage, algorithm, etc..
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.
I am not sure about the resource because these values were manually calculated by me,I believe they would be correct ,but please feel free to cross check the calculations
@dpshelio ,Could you please put a line note on the docstring I should edit |
@gunner272 I think is failing because the GOES api gone by the wind.... |
What's the status of this, now that the GOES lightcurve source has been fixed by #942? |
Can you change this so that it would look more like this: parse_time(time, format='tai') can you also add like this: |
Copied from 'anytim.pro': UTIME - Utime format, Real*8 seconds since 1-jan-79 |
oohhhhh solarsoft ... bite me |
👍 |
Now resolved |
|
||
.. todo:: | ||
add ability to parse tai (International Atomic Time seconds since | ||
Jan 1, 1958) | ||
""" | ||
if isinstance(time_string, datetime): | ||
return time_string | ||
elif isinstance(time_string, tuple): |
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.
this would be better if it tested for all iterables that are not strings rather than just tuples.
there is a is_iterable function in astropy somewhere.
Btw, |
@gunner272 Something has upset things. Looks like the API has changed in a non-backwards compatible way. specifically I think that it no longer assumes 'utime' if a float is passed in. (https://travis-ci.org/sunpy/sunpy/jobs/26442840#L1351) I suggest two things,
|
@Cadair : like the change above |
@gunner272 that is the simple solution, that doesn't change the API. I suppose for now that is the best bet. Can you make sure that behaviour is documented, that it assumes floats are 'utime'? |
return time_string | ||
elif isinstance(time_string, tuple): | ||
return datetime(*time_string) | ||
elif isinstance(time_string, int) or isinstance(time_string, float): | ||
elif time_format == 'utime' or ( isinstance(time_string, int) or isinstance(time_string, float) ) : |
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.
elif time_format == 'utime' or isinstance(time_string, (int, float)):
does the same.
|
||
time_string : [ int, float, time_string, datetime ] | ||
Date to parse which can be either time_string, int, datetime object. | ||
format : [ basestring, utime, datetime ] |
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.
this should be time_format, right? This doc should also define each of the possibilities (such as what is utime).
|
||
|
||
Note: | ||
If time_string is an instance of float, then it is assumed to be in unix time format. |
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.
this should be "assumed to be utime" right?
👍 |
@@ -256,18 +260,15 @@ def day_of_year(time_string): | |||
time_diff = time - datetime(time.year, 1, 1, 0, 0, 0) | |||
return time_diff.days + time_diff.seconds / SECONDS_IN_DAY + 1 | |||
|
|||
|
|||
def break_time(t='now'): | |||
def break_time(t=None, time_format=''): |
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.
Should this None
not be 'now'
?
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.
yes it should, good spot.
👍 |
@gunner272 you have a doc fail: https://travis-ci.org/sunpy/sunpy/jobs/29926813#L1485 |
👍 |
[Review]: Clean up `parse_time` API
I added the small ability to parse international time seconds(time elapsed in seconds from 1958,1,1) to time.parse_time function ,Please provide feedback
Thank You