J
james
Hi all,
I am moving my server based scripting over to Python and I am
attempting to write more idiomatic python. I have 8 years
professional programming experience and I am looking to get to a very
high standard of python coding. As such, I was wondering if it was
appropriate to post code snippets I am not 100% happy with in order to
find a more elegant python way for coding.
Here is a method I came across that I would like to clean up:
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:
filtered_lessons = filter(lambda x: x["type"] == type,
self.lesson_data["lessons"])
if filtered_lessons:
lesson = self.output_random(filtered_lessons)
else:
print "Unable to find lessons of type %s." % type
else:
lesson = self.output_random(self.lesson_data["lessons"])
return lesson
Where 'type' is a string, and 'self.lesson_data["lessons"]' is an
array of dictionaries where each item is guaranteed to have string
value with a key 'type'
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.
Any and all comments appreciated.
Cheers,
James.
I am moving my server based scripting over to Python and I am
attempting to write more idiomatic python. I have 8 years
professional programming experience and I am looking to get to a very
high standard of python coding. As such, I was wondering if it was
appropriate to post code snippets I am not 100% happy with in order to
find a more elegant python way for coding.
Here is a method I came across that I would like to clean up:
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:
filtered_lessons = filter(lambda x: x["type"] == type,
self.lesson_data["lessons"])
if filtered_lessons:
lesson = self.output_random(filtered_lessons)
else:
print "Unable to find lessons of type %s." % type
else:
lesson = self.output_random(self.lesson_data["lessons"])
return lesson
Where 'type' is a string, and 'self.lesson_data["lessons"]' is an
array of dictionaries where each item is guaranteed to have string
value with a key 'type'
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.
Any and all comments appreciated.
Cheers,
James.