I have an Event model, and each event will have different shows.
class Event(models.Model):
title = models.CharField(max_length=200)
class Show(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
date_time = models.DateTimeField(unique=True)
I have another Ticket model. Each ticket should be unique. Meaning each ticket will be unique and relate to a Show and a Seat.
class Ticket(models.Model):
show = models.ForeignKey(Show)
seat = models.ForeignKey(Seat)
class Meta:
unique_together = ('show', 'seat')
I need to create shows based on the start date and end date provide by the user. Suppose this is a JSON post:
{
"event_id": 1,
"start_date": "2018-02-16",
"end_date": "2018-02-20",
"time_list": ["11:00 AM", "8:00 PM"]
}
From the above JSON example, I need to create Show starting like this:
# Start with the start_date as the date, and for each time from the time_list
Show.objects.create(
event = 1,
date_time = datetime.strptime('2018-02-16 11:00 AM', "%Y-%m-%d %I:%M %p")
)
Show.objects.create(
event = 1,
date_time = datetime.strptime('2018-02-16 8:00 PM', "%Y-%m-%d %I:%M %p")
)
# Next date after the start_date, i.e., 16+1 = 17
Show.objects.create(
event = 1,
date_time = datetime.strptime('2018-02-17 8:00 PM', "%Y-%m-%d %I:%M %p")
)
.
.
.
# Create Show objects till the end_date and for each time from the time_list
Show.objects.create(
event = 1,
date_time = datetime.strptime('2018-02-20 8:00 PM', "%Y-%m-%d %I:%M %p")
)
Right now this is how I am creating Show objects:
def create_show_by_datetime(self, request):
event_id = request.data['event_id']
try:
event = Event.objects.get(id=event_id)
except Event.DoesNotExist:
return Response(
{'error': 'event with id: %s does not exist.' % event_id},
status=status.HTTP_400_BAD_REQUEST
)
start_date = request.data['start_date']
end_date = request.data['end_date']
time_list = request.data['time_list']
date_format = '%Y-%m-%d'
time_format = "%I:%M %p"
try:
datetime.strptime(start_date, date_format)
datetime.strptime(end_date, date_format)
for i in range(len(time_list)):
time = datetime.strptime(time_list[i], time_format)
except ValueError as e:
return Response(
{'error': 'Time was not in a supported format. %s' % e},
status=status.HTTP_400_BAD_REQUEST
)
delta_days = datetime.strptime(end_date, date_format).date() - datetime.strptime(start_date, date_format).date()
delta_days = delta_days.days + 1
dt = None
try:
with transaction.atomic():
for i in range(delta_days):
day = datetime.strptime(start_date, date_format) + timedelta(days=i)
for i in range(len(time_list)):
hrs = datetime.strptime(time_list[i], time_format).hour
mins = datetime.strptime(time_list[i], time_format).minute
dt = day + timedelta(hours=hrs, minutes=mins)
show = Show.objects.create(
event=event,
date_time=dt
)
return Response({"data": 'Post succesfull'}, status=status.HTTP_201_CREATED)
except IntegrityError as e:
return Response(
{
'error': "event with date and time already exsits. %s-%s-%s at %s:%s" % (
dt.day, dt.month, dt.year, dt.hour, dt.minute),
'detail': str(e)
}, status=status.HTTP_400_BAD_REQUEST
But I am hoping there's much more elegant way than how I am doing. I am using python 3, django 2 and django rest frameowork. How can I create Shows with the event, and date_time based on the event_id, start_date, end_date and the time_list?
My approach is a little bit different. You said in the question tag that you are using django-rest-framework.. So where are the serializers? :)
Lets create two serializers, one for user data validation (Because we don't trust the USER!) and one for multi data insert.
I haven't checked the code! But you can use it was an example...
class ShowEventSerializer(serializers.Serializer):
event_id = serializers.IntegerField()
start_date = serializers.DateField(required=True)
end_date = serializers.DateField(required=True)
time_list = serializers.ListField(
child=serializers.TimeField()
)
class Meta:
fields = ('event_id', 'start_date', 'end_date', 'time_list')
class ShowSerializer(serializers.Serializer):
date_time = serializers.DateTimeField()
class Meta:
model = Show
fields = ('event', 'date_time')
Now, with the serializers, we are going to validate the user data and then to create a json data object:
def create_show_by_datetime(self, request):
show_event_serializer = ShowEventSerializer(data=request.data)
if not show_event_serializer.is_valid():
return Response({'error': show_event_serializer.errors},status=status.HTTP_400_BAD_REQUEST)
event_id = show_event_serializer.data['event_id']
try:
event = Event.objects.get(id=event_id)
except Event.DoesNotExist:
return Response({'error': 'event with id: %s does not exist.' % event_id},status=status.HTTP_400_BAD_REQUEST)
start_date = show_event_serializer.data['start_date']
end_date = show_event_serializer.data['end_date']
time_list = show_event_serializer.data['time_list']
date_format = '%Y-%m-%d'
time_format = "%I:%M %p"
try:
datetime.strptime(start_date, date_format)
datetime.strptime(end_date, date_format)
for i in range(len(time_list)):
time = datetime.strptime(time_list[i], time_format)
except ValueError as e:
return Response(
{'error': 'Time was not in a supported format. %s' % e},
status=status.HTTP_400_BAD_REQUEST
)
delta_days = datetime.strptime(end_date, date_format).date() - datetime.strptime(start_date, date_format).date()
delta_days = delta_days.days + 1
dt = None
show_data = []
for i in range(delta_days):
day = datetime.strptime(start_date, date_format) + timedelta(days=i)
for i in range(len(time_list)):
hrs = datetime.strptime(time_list[i], time_format).hour
mins = datetime.strptime(time_list[i], time_format).minute
dt = day + timedelta(hours=hrs, minutes=mins)
show_data.append({
"event": event,
"date_time": dt
})
try:
with transaction.atomic():
show_serializer = ShowSerializer(data=show_data, many=True)
if show_serializer.is_valid():
show_serializer.save()
return Response({"data": 'Post succesfull'}, status=status.HTTP_201_CREATED)
except IntegrityError as e:
return Response(
{
'error': "event with date and time already exsits. %s-%s-%s at %s:%s" % (
dt.day, dt.month, dt.year, dt.hour, dt.minute),
'detail': str(e)
}, status=status.HTTP_400_BAD_REQUEST
So this code is basically the same as yours, with the difference of the way the objects are saved using the DRF. Look the show_data variable.
This solution is just a different way of looking at the question.
GOOD LUCK!
http://www.django-rest-framework.org/api-guide/serializers/
http://www.django-rest-framework.org/api-guide/fields/
Then your Show model should look like
from django.contrib.postgres.fields import ArrayField
class Show(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
start_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
board = ArrayField(
models.TimeField(blank=True, null=True),
size=10, # specify max array size
)
so you'll have model Show with specified DateFields and array of TimeFields.
django docs: Array Field, Time Field, Date Field
There are some tools in the datetime library that can give you a more streamlined approach to generating your times. You can use toordinal to turn a date into an integer and fromordinal to turn an integer back into a date; this makes a nice way to create a range of dates. And you can use combine to merge a date object and a time object into a datetime. I'd create the following function:
from datetime import datetime, date
def get_showtimes(post):
start = datetime.strptime(post['start_date'], '%Y-%m-%d')
end = datetime.strptime(post['end_date'], '%Y-%m-%d')
times = [datetime.strptime(t, '%I:%M %p').time() for t in post['time_list']]
for ordinal in range(start.toordinal(), end.toordinal() + 1):
date = date.fromordinal(date)
for time in times:
yield datetime.combine(date, time)
then, in your code, replace the second try: except: block and what follows it with:
try:
showtimes = list(get_showtimes(post))
except ValueError as e:
return Response(
{'error': 'Time was not in a supported format. %s' % e},
status=status.HTTP_400_BAD_REQUEST
)
try:
with transaction.atomic():
for showtime in showtimes:
show = Show.objects.create(event=event, date_time=showtime)
except IntegrityError as e:
# etc.
I am leaving the validation part and only focusing on generating Show objects from the given data:
data = request.data
date_format = '%Y-%m-%d'
time_format = "%I:%M %p"
show_time_format = f"{date_format} {time_format}"
# get the total number of days by parsing start and end dates
start_date = datetime.strptime(data['start_date'], date_format)
end_date = datetime.strptime(data['end_date'], date_format)
total_days = (end_date - start_date).days + 1
# Get the timings for the first day.
# We will use this to generate the timings for the rest of the days.
first_day_timings = [
datetime.strptime(f"{data['start_date']} {show_time}", show_time_format)
for show_time in data['time_list']
]
# generate all show objects using list comprehension and bulk create later
show_objects = [
Show(event=event, date_time=first_day_timing + timedelta(days=day_cnt))
for day_cnt in range(total_days)
for first_day_timing in first_day_timings
]
Show.objects.bulk_create(show_objects)
Enhancements made on the existing code:
Reduced the number of instances date/time is getting parsed and hours/minutes are getting computed.
Use list comprehension to generate show objects and bulk create instead of creating one object at a time in a transaction.
Related
I am using odoo 12 and want to show Date field record from a model into Reports, how can i do this?
Still facing error
time data 'False' does not match format '%d %B, %Y'
here is my code
#api.multi
def _get_student_dob_in_words(self, student_id):
date_of_birth = fields.Date('BirthDate', default=fields.date.today(),
states={'done': [('readonly', True)]})
student = self.env['student.student'].search([('id','=',student_id)])
date_of_birth = student.date_of_birth
# date_of_birth = date.today()
# %Y-%m-%d %H:%M:%S
strp_date = datetime.strptime(str(date_of_birth),"%d %B, %Y")
# dob_result = datetime.strptime(date_of_birth, DEFAULT_SERVER_DATETIME_FORMAT).date()
# str_dob_result = str(date_of_birth)
strp_dob_result = num2words(strp_date)
strp_dob_result = strp_dob_result.upper()
return [{
'dob_in_words': strp_dob_result}]
You don't need strptime at all, you may use strftime. You still have to test if date is present. And i would make it as computed field as well.
_{name|inherit} = 'student.student'
#date_of_birth = fields.Date(...
date_of_birth_words = fields.Char(compute='_compute_dob_in_words', store=True)
#api.depends('date_of_birth')
def _compute_dob_in_words(self):
for student in self:
if student.date_of_birth:
date_of_birth = student.date_of_birth
month = date_of_birth.strftime('%B') #date_of_birth.month
day = num2words.num2words(date_of_birth.day, to='ordinal')
year = num2words.num2words(date_of_birth.year)
student.date_of_birth_words = "{} {} in year {}".format(day, month, year).upper()
Im trying to use the eventful api to get information about only music events (concerts) between two dates. For example I want to get the below information about each concert from 20171012 to 20171013:
- city
- performer
- country
- latitude
- longitude
- genre
- title
- image
- StarTime
Im using a python example available online and change it to get the data above. But for now its not working Im just able to get this information:
{'latitude': '40.4',
'longitude': '-3.68333',
'start_time': '2017-10-12 20:00:00',
'city_name': 'Madrid', 'title': 'Kim Waters & Maysa Smooth en Hot Jazz Festival'}
But the performer, genre country and image url its not working. Do you know how to get that information? When I change the python example below to get this information it returns always a empty array.
python example working: (However, without getting the performer, genre, country and image url, if I add theese elements to the event_features I get an empty array)
import requests
import datetime
def get_event(user_key, event_location , start_date, end_date, event_features, fname):
data_lst = [] # output
start_year = int(start_date[0:4])
start_month = int(start_date[4:6])
start_day = int(start_date[6:])
end_year = int(end_date[0:4])
end_month = int(end_date[4:6])
end_day = int(end_date[6:])
start_date = datetime.date(start_year, start_month, start_day)
end_date = datetime.date(end_year, end_month, end_day)
step = datetime.timedelta(days=1)
while start_date <= end_date:
date = str(start_date.year)
if start_date.month < 10:
date += '0' + str(start_date.month)
else:
date += str(start_date.month)
if start_date.day < 10:
date += '0' + str(start_date.day)
else:
date += str(start_date.day)
date += "00"
date += "-" + date
url = "http://api.eventful.com/json/events/search?"
url += "&app_key=" + user_key
url += "&location=" + event_location
url += "&date=" + date
url += "&page_size=250"
url += "&sort_order=popularity"
url += "&sort_direction=descending"
url += "&q=music"
url+= "&c=music"
data = requests.get(url).json()
try:
for i in range(len(data["events"]["event"])):
data_dict = {}
for feature in event_features:
data_dict[feature] = data["events"]["event"][i][feature]
data_lst.append(data_dict)
except:
pass
print(data_lst)
start_date += step
def main():
user_key = ""
event_location = "Madrid"
start_date = "20171012"
end_date = "20171013"
event_location = event_location.replace("-", " ")
start_date = start_date
end_date = end_date
event_features = ["latitude", "longitude", "start_time"]
event_features += ["city_name", "title"]
event_fname = "events.csv"
get_event(user_key, event_location, start_date, end_date, event_features, event_fname)
if __name__ == '__main__':
main()
You should debug your problem and not to ignore all exceptions.
Replace lines try: ... except: pass by:
data = requests.get(url).json()
if "event" in data.get("event", {}):
for row in data["events"]["event"]:
# print(row) # you can look here what are the available data, while debugging
data_dict = {feature: row[feature] for feature in features}
data_lst.append(data_dict)
else:
pass # a problem - you can do something here
You will see a KeyError with a name of the missing feature that is not present in "row". You should fix missing features and read documentation about API of that service. Country feature is probably "country_name" similarly to "city_name". Maybe you should set the "include" parameter to specify more sections of details in search than defaults only.
An universal try: ... except: pass should never used, because "Errors should never pass silently." (The Zen of Python)
Read Handling Exceptions:
... The last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way! ...
A more important command where unexpected exceptions are possible is requests.get(url).json(), e.g. TimeoutException. Anyway you should not continue the "while" loop if there is a problem.
If you look at the data returned by eventful.com, a few things are clear:
For country, the field to be used is country_name. This was missing from your "event_features" list
There can be multiple performers for each event. To get all the performers, you need to add "performers" to your "event_features" list
There is no field named Genre and hence you cannot find Genre
The "image" field is always None. This means there is no image available.
Here is modified code. Hopefully it works much better and it will help you move forward.
import datetime
import requests
data_lst = [] # output
event_features = ["latitude", "longitude", "start_time", "city_name",
"country_name", "title", "image", "performers"]
def get_event(user_key, event_location, start_date, end_date):
start_year = int(start_date[0:4])
start_month = int(start_date[4:6])
start_day = int(start_date[6:])
end_year = int(end_date[0:4])
end_month = int(end_date[4:6])
end_day = int(end_date[6:])
start_date = datetime.date(start_year, start_month, start_day)
end_date = datetime.date(end_year, end_month, end_day)
step = datetime.timedelta(days=1)
while start_date <= end_date:
date = str(start_date.year)
if start_date.month < 10:
date += '0' + str(start_date.month)
else:
date += str(start_date.month)
if start_date.day < 10:
date += '0' + str(start_date.day)
else:
date += str(start_date.day)
date += "00"
date += "-" + date
url = "http://api.eventful.com/json/events/search?"
url += "&app_key=" + user_key
url += "&location=" + event_location
url += "&date=" + date
url += "&page_size=250"
url += "&sort_order=popularity"
url += "&sort_direction=descending"
url += "&q=music"
url += "&c=music"
data = requests.get(url).json()
print "==== Data Returned by eventful.com ====\n", data
try:
for i in range(len(data["events"]["event"])):
data_dict = {}
for feature in event_features:
data_dict[feature] = data["events"]["event"][i][feature]
data_lst.append(data_dict)
except IndexError:
pass
print "===================================="
print data_lst
start_date += step
def main():
user_key = "Enter Your Key Here"
event_location = "Madrid"
start_date = "20171012"
end_date = "20171013"
event_location = event_location.replace("-", " ")
start_date = start_date
end_date = end_date
#event_fname = "events.csv"
get_event(user_key, event_location, start_date, end_date)
if __name__ == '__main__':
main()
I was able to successfully pull data from the Eventful API for the performer, image, and country fields. However, I don't think the Eventful Search API supports genre - I don't see it in their documentation.
To get country, I added "country_name", "country_abbr" to your event_features array. That adds these values to the resulting JSON:
'country_abbr': u'ESP',
'country_name': u'Spain'
Performer also can be retrieved by adding "performers" to event_features. That will add this to the JSON output:
'performers': {
u'performer': {
u'name': u'Kim Waters',
u'creator': u'evdb',
u'url': u'http://concerts.eventful.com/Kim-Waters?utm_source=apis&utm_medium=apim&utm_campaign=apic',
u'linker': u'evdb',
u'short_bio': u'Easy Listening / Electronic / Jazz', u'id': u'P0-001-000333271-4'
}
}
To retrieve images, add image to the event_features array. Note that not all events have images, however. You will either see 'image': None or
'image': {
u'medium': {
u'url': u'http://d1marr3m5x4iac.cloudfront.net/store/skin/no_image/categories/128x128/other.jpg',
u'width': u'128',
u'height': u'128'
},
u'thumb': {
u'url': u'http://d1marr3m5x4iac.cloudfront.net/store/skin/no_image/categories/48x48/other.jpg',
u'width': u'48',
u'height': u'48'
}
}
Good luck! :)
I have two date field.
from_date and
to_date
In One2many line item, there is three float fields
from_time ,to_time and interval
Slot have to be created based on the above parameters.
Example:
from_date = '2017-07-21'
to_date = '2017-07-21'
the duration is one day.
The One2many line items have the values
from_time = 9.0
to_time = 10.0
interval = 30(in minutes)
The output should generate two slots
1. '2017-07-21 09:00:00' '2017-07-21 09:30:00'
2. '2017-07-21 09:30:00' '2017-07-21 10:00:00'
It should generate two line items.
If the duration is for week.
it should generate 2 * 7 = 14 slots.
I have used the code which generates for one day.
#api.one
def generate(self):
cr = self.env.cr
uid = self.env.uid
context = self.env.context
event = self.pool.get('calendar.event')
slot = self.pool.get('slot.booking')
old_data_id = slot.search(cr, uid, [('slot_id', '=',self.id)], context=context)
slot.unlink(cr, uid ,old_data_id)
for each in self.shift_line:
if each.interval > 60 or each.interval == 0:
raise osv.except_osv(_('Attention!'), _('Please enter interval timings in minutes range like (10-60) '))
interval = each.interval
fmt = "%Y-%m-%d"
start_date = datetime.strptime(self.from_date, fmt)
end_date = datetime.strptime(self.to_date, fmt)
days = []
date = start_date
pdb.set_trace()
str_start_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.from_time * 60, 60)))+':00'
str_end_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.to_time * 60, 60)))+':00'
time = datetime.strptime(str_start_time, '%Y-%m-%d %H:%M:%S')
end = datetime.strptime(str_end_time, '%Y-%m-%d %H:%M:%S')
while date <= end_date:
hours = []
while time <= end:
hours.append(time.strftime("%Y-%m-%d %H:%M:%S"))
time += timedelta(minutes=interval)
date += timedelta(days=1)
time += timedelta(days=1)
end += timedelta(days=1)
days.append(hours)
print "\n\n\n\n\nn\+++++++++++++++++++++days",days
for hours in days[0][:-1]:
val = datetime.strptime(hours, '%Y-%m-%d %H:%M:%S')
val = val + timedelta(minutes=interval)
values = {
'name' : 'Slot for ' + self.employee_id.name,
'start_datetime' : hours,
'stop_datetime' : str(val),
'slot_id' : self.id,
'shift_lines_id' : each.id,
'partner_id': self.employee_id.id,
'duration' : each.interval,
}
print "\n\n\n\n\n\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++values",values
slot.create(cr, uid, values, context=context)
Any help for multiple days is appreciated.
ist_timedelta = timedelta(seconds=((time in seconds)-10800))
adding this will give the exact answer, here 10800 is because of 3 hours difference of UTC to KSA.
I have three fields start_date, end_date and total_days.
If i enter start_date and end_date field it should automatically display total_days.
This is my Function which is not working.
<field name="start_date" on_change="get_number_of_days(start_date,end_date)"/>
<field name="end_date" on_change="get_number_of_days(end_date,start_date)"/>
<field name="total_days" />
def get_number_of_days(self, cr, uid, ids, start_date, end_date):
"""Returns a float equals to the timedelta between two dates given as string."""
result = self.browse(cr, uid, ids)
if (end_date and start_date) and (start_date <= end_date):
DATETIME_FORMAT = "%d/%m/%Y %H:%M:%S"
from_dt = datetime.datetime.strptime(start_date, DATETIME_FORMAT)
to_dt = datetime.datetime.strptime(end_date, DATETIME_FORMAT)
timedelta = to_dt - from_dt
diff_day = timedelta.days + float(timedelta.seconds) / 86400
result['value']['total_days'] = round(math.floor(diff_day))+1
return result
Is there any other method? or what may be the issue
The section:
diff_day = timedelta.days + float(timedelta.seconds) / 86400
result['value']['total_days'] = round(math.floor(diff_day))+1
is largely unnecessary as datetime.timedelta already has the number of whole days as a value timedelta.days - I suspect that your problem is that you are not converting to a string try:
result['value']['total_days'] = str(timedelta.days)
You should also consider catching problems when the strptime function cannot convert the current value, e.g. when it is blank.
If your dates are given as strings (as your docstring says), the second part of your conditional may behave unexpectedly (depending on the date/time formatting), because you perform a lexicographical comparison:
if (end_date and start_date) and (start_date <= end_date):
# ...
You should convert them to datetime-objects first and then check your conditional.
Thank You,
I wrote new function for this which helped to give total days:
def _get_days(self, cr, uid, ids, field_name, arg ,context=None):
res = {}
fmt = '%Y-%m-%d'
for object in self.browse(cr, uid, ids, context=context):
res[object.id] = {'total_days':0, }
from_date = object.start_date
to_date = object.end_date
d1 = datetime.strptime(from_date, fmt)
d2 = datetime.strptime(to_date, fmt)
daysDiff = str((d2-d1).days+1)
res[object.id]['total_days'] = daysDiff
return res
'start_date': fields.date('Start Date', required=True),
'end_date': fields.date('End Date', required=True),
'total_days': fields.function(_get_days, string="Diff days", multi='sums', store=True),
I have a form that has an initial end_date. I am having a Value error because this year is a leap year and we are currently in February.
My code has a end day of 30 but I am having trouble figuring out how to write the code that will discover if its a leap year and set the initial end_date to the correct last day of february.
Here is my forms.py that controls the end_date initial value
class MaturityLetterSetupForm(forms.Form):
def __init__(self, *args, **kwargs):
from datetime import datetime
today = datetime.today()
start_year = today.year
start_month = today.month
start_date = datetime(start_year, start_month, 1)
try:
end_date = datetime(start_year, start_month, 30)
except ValueError:
end_date = datetime(start_year, start_month, ?)
super(MaturityLetterSetupForm, self).__init__(*args, **kwargs)
self.fields['start_date'] = forms.DateField(initial=start_date.strftime("%B %d, %Y"),
widget=forms.TextInput(attrs={'class':'datepicker', 'value': today }))
self.fields['end_date'] = forms.DateField(initial=end_date.strftime("%B %d, %Y"),
widget=forms.TextInput(attrs={'class':'datepicker', 'value': today }))
EDIT
After speaking to #Paul
my init became:
def __init__(self, *args, **kwargs):
from datetime import datetime
import calendar
today = datetime.today()
start_year = today.year
start_month = today.month
start_date = datetime(start_year, start_month, 1)
if calendar.isleap(start_year) and today.month == 2:
end_date = datetime(start_year, start_month, calendar.mdays[today.month]+1)
else:
end_date = datetime(start_year, start_month, calendar.mdays[today.month])
super(MaturityLetterSetupForm, self).__init__(*args, **kwargs)
self.fields['start_date'] = forms.DateField(initial=start_date.strftime("%B %d, %Y"),
widget=forms.TextInput(attrs={'class':'datepicker', 'value': today }))
self.fields['end_date'] = forms.DateField(initial=end_date.strftime("%B %d, %Y"),
widget=forms.TextInput(attrs={'class':'datepicker', 'value': today }))
Which finds the last day of the current month.
How about calendar.isleap(year) ?
Also, don't use try/except to handle this but an if conditional. Something like:
if calendar.isleap(year):
do_stuff
else:
do_other_stuff