Login

Astrological sign theme filter

Author:
macat
Posted:
November 18, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

I returns the astrological sign.

http://en.wikipedia.org/wiki/Zodiac

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@register.filter
def astrological_sign(bday):
    if bday.month == 1 and bday.day >= 20 or bday.month == 2 and bday.day < 18:
        return 'aquarius'
    if bday.month == 2 and bday.day >= 18 or bday.month == 3 and bday.day < 20:
        return 'pisces'
    if bday.month == 3 and bday.day >= 20 or bday.month == 4 and bday.day < 20:
        return 'aries'
    if bday.month == 4 and bday.day >= 20 or bday.month == 5 and bday.day < 21:
        return 'taurus'
    if bday.month == 5 and bday.day >= 21 or bday.month == 6 and bday.day < 21:
        return 'gemini'
    if bday.month == 6 and bday.day >= 21 or bday.month == 7 and bday.day < 22:
        return 'cancer'
    if bday.month == 7 and bday.day >= 22 or bday.month == 8 and bday.day < 23:
        return 'leo'
    if bday.month == 8 and bday.day >= 23 or bday.month == 9 and bday.day < 23:
        return 'virgo'
    if bday.month == 9 and bday.day >= 23 or bday.month == 10 and bday.day < 23:
        return 'libra'
    if bday.month == 10 and bday.day >= 23 or bday.month == 11 and bday.day < 22:
        return 'scorpio'
    if bday.month == 11 and bday.day >= 22 or bday.month == 12 and bday.day < 22:
        return 'sagittarius'
    return 'capricorn'

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

willhardy (on November 18, 2010):

Here is a more pythonic version:

# Dictionary mapping month number to a function that takes a
# day and returns the sign. Use like this: SIGNS[month](day)
SIGNS = {
  1: lambda d: d >= 20 and 'aquarius' or 'capricorn',
  2: lambda d: d >= 18 and 'pisces' or 'aquarius',
  3: lambda d: d >= 20 and 'aries' or 'pisces',
  4: lambda d: d >= 20 and 'taurus' or 'aries',
  5: lambda d: d >= 21 and 'gemini' or 'taurus',
  6: lambda d: d >= 21 and 'cancer' or 'gemini',
  7: lambda d: d >= 22 and 'leo' or 'cancer',
  8: lambda d: d >= 23 and 'virgo' or 'leo',
  9: lambda d: d >= 23 and 'libra' or 'virgo',
  10: lambda d: d >= 23 and 'scorpio' or 'libra',
  11: lambda d: d >= 22 and 'sagittarius' or 'scorpio',
  12: lambda d: d >= 22 and 'capricorn' or 'sagittarius',
}

@register.filter
def astrological_sign(bday):
  return SIGNS[bday.month](bday.day)

#

sberlotto (on November 18, 2010):

Great filter macat. Great version willhardy!

#

Please login first before commenting.