How would I determine zodiac / astrological star sign from a birthday in Python? How would I determine zodiac / astrological star sign from a birthday in Python? django django

How would I determine zodiac / astrological star sign from a birthday in Python?


I've done this before. The simplest solution that I ended up with was an array of the following key/values:

120:Cap, 218:Aqu, 320:Pis, 420:Ari, 521:Tau,621:Gem, 722:Can, 823:Leo, 923:Vir, 1023:Lib1122:Sco, 1222:Sag, 1231: Cap

Then you write the birth date in the mdd format, ie, month number (starting with 1 for January) and two digit day number (01-31). Iterate through the array, and if the date is less than or equal to an item in the array, you have your star sign.

EDITI needed this so here's this concept as a working function

zodiacs = [(120, 'Cap'), (218, 'Aqu'), (320, 'Pis'), (420, 'Ari'), (521, 'Tau'),           (621, 'Gem'), (722, 'Can'), (823, 'Leo'), (923, 'Vir'), (1023, 'Lib'),           (1122, 'Sco'), (1222, 'Sag'), (1231, 'Cap')]def get_zodiac_of_date(date):    date_number = int("".join((str(date.date().month), '%02d' % date.date().day)))    for z in zodiacs:        if date_number <= z[0]:            return z[1]


You could give them some more information about position of the planets and the stars.

import ephem >>> u = ephem.Uranus()>>> u.compute('1871/3/13')>>> print u.ra, u.dec, u.mag7:38:06.27 22:04:47.4 5.46>>> print ephem.constellation(u)('Gem', 'Gemini')


Using bisect is more efficient than iterating until you find a match, but a lookup table for each day of the year is faster still and really not that big.

from bisect import bisectsigns = [(1,20,"Cap"), (2,18,"Aqu"), (3,20,"Pis"), (4,20,"Ari"),         (5,21,"Tau"), (6,21,"Gem"), (7,22,"Can"), (8,23,"Leo"),         (9,23,"Vir"), (10,23,"Lib"), (11,22,"Sco"), (12,22,"Sag"),         (12,31,"Cap")]def zodiac_sign(m,d):    return signs[bisect(signs,(m,d))][2]assert zodiac_sign(3,9) == "Pis"assert zodiac_sign(6,30) == "Can"