jeudi 28 mai 2015

Design pattern for refactoring

I have those three functions and I find it ugly to repeat all the same code, this is not DRY at all. At the same time, I'm not sure how could I refactor those functions to keep clear expression. Could you recommend anything?

Thanks

def download_loc(instance, filename):
    username = instance.username_or_anonymous()
    slug = instance.slug
    filename_with_timestamp = filename_timestamped(instance, filename)
    return "%s/%s/stl/%s" % (username, slug, filename_with_timestamp)

def preview_loc(instance, filename):
    username = instance.username_or_anonymous()
    slug = instance.slug
    filename_with_timestamp = filename_timestamped(instance, filename)
    return "%s/%s/preview/%s" % (username, slug, filename_with_timestamp)

def screenshot_loc(instance, filename):
    username = instance.username_or_anonymous()
    slug = instance.slug
    filename_with_timestamp = filename_timestamped(instance, filename)
    return "%s/%s/screenshot/%s" % (username, slug, filename_with_timestamp)

Aucun commentaire:

Enregistrer un commentaire