import datetime
def date_to_julian_day(my_date):
"""Returns the Julian day number of a date."""
a = (14 - my_date.month)//12
y = my_date.year + 4800 - a
m = my_date.month + 12*a - 3
return my_date.day + ((153*m + 2)//5) + 365*y + y//4 - y//100 + y//400 - 32045
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Friday, January 18, 2013
Julian date in python
I was recently looking for simple function to convert date to Julian Date in python.
Julian date is number of days since November 24, 4714 BC. It's used by astronomers and by Google in datarange queries ;] I've found working, pretty and simple function here: http://code.activestate.com/recipes/117215/
Here is the code:
Tuesday, April 24, 2012
Python tricks
I'm taking part in Udacity course, of computer program design with Peter Norvig. Nice thing to do in your spare time. Whole course is based on Python ;) So the first Unit is over. Material didn't get hardcore yet ( I hope it will in the future ), but I've learned some python tricks.
First one is how to assign number to an item in an elegant way. Let's say we have card ranks (first udacity unit is about poker) encoded as follows:
First one is how to assign number to an item in an elegant way. Let's say we have card ranks (first udacity unit is about poker) encoded as follows:
2 3 4 5 6 7 8 9 T J Q K A, and we want to give them priorities according to their importance: 2->2, 3->3, ... , T->10, J->11, Q->12, K->13, A->14. Nothing simplier:
rank = 'Q' #our rank to check '--23456789TJQKA'.index(rank)Second trick is how to make list have only unique elements. In python it shows to be as simple as that:
my_list = [1, 1, 2, 3, 3, 3, 4, 5] my_list = list(set(my_list))After this my_list has only one occurrence of each value. Be aware that the ordering of elements in list may change.
Subscribe to:
Posts (Atom)