Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I had a question regarding my code which downloads a file from S3 with the highest (most recent) timedated filename format:

YYYYMMDDHHMMSS.zip

from boto.s3.connection import S3Connection
from boto.s3.key import Key
import sys
conn = S3Connection('____________________', '________________________________________')
bucket = conn.get_bucket('bucketname')
rs = bucket.list("FamilyPhotos")

for key in rs:
    print key.name

keys = [(key,datetime.strptime(key.name,'%Y%m%d%H%M%S.zip')) for key in rs]
sorted(keys, key = lambda element : element[1])
latest = keys[0][1]
latest.get_contents_to_filename()

I havent done a lot of python before so I would really appreciate some feedback.

share|improve this question
    
What are you looking for specifically? –  Michel Ayres Dec 18 '12 at 12:16
1  
The part of the script that works out the most recently dated filename. I am sure there is a better way of doing it. –  Jimmy Dec 18 '12 at 12:34
add comment

1 Answer

up vote 2 down vote accepted

The names will properly compare lexically even without converting them to datetimes, and you can just use the max function:

from boto.s3.connection import S3Connection
from boto.s3.key import Key

conn = S3Connection(XXX, YYY)
bucket = conn.get_bucket('bucketname')
rs = bucket.list("FamilyPhotos")
latest = max(rs, key=lambda k: k.name)
latest.get_contents_to_filename()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.