Request a short code review
Ivan Illarionov
ivan.illarionov at gmail.com
Thu Apr 17 19:19:37 EDT 2008
On Thu, 17 Apr 2008 15:11:40 -0700, james wrote:
>> I am not necessarily looking to make the code shorter or more
>> functional or anything in particular. However if you spot something to
>> improve then I am happy to learn.
>
> To give an example of what I mean I have already altered the code:
>
> def output_random_lesson_of_type(self, type=None):
> """Output a lesson of a specific type.
> If no type is passed in then output any type."""
> output_lessons = self.lesson_data["lessons"] if type:
> filtered_lessons = filter(lambda x: x["type"] == type,
> self.lesson_data["lessons"])
> if filtered_lessons:
> output_lessons = filtered_lessons
> else:
> print "Unable to find lessons of type %s." % type
> return self.output_random(output_lessons)
>
> Changes:
> - Respected a column width of 80
> - Created the output_lessons variable, assigned it to a default.
> This remove an else statement and reduced the call to
> self.output_random to a single instance in the return statement
>
> Cheers,
> James
I would write it like this:
def output_random_lesson_of_type(self, type=None):
"""\
Output a lesson of a specific type.
If no type is passed in then output any type.
"""
if type:
return self.output_random([x for x in self.lesson_data["lessons"]
if x["type"] == type])
return self.output_random(self.lesson_data["lessons"])
--
Ivan
More information about the Python-list
mailing list