Dreams
Spring Summer Fall Winter and Spring
Life doesn’t make sense
Steve Jobs
Dr. Gonzo
Simplistic vs. Simplicity
Simplicity is not what you think it is. Garr Reynold's presentation Simplistic vs. Simplicity in an eye opener for what it means to achieve simplicity. Here are my notes about his presentation, which you should watch here — it's highly recommended!
![]()
![]()
A Universe Not Made For Us
Implementing file locks using Python's with statement![]() I really love Python's with statement. It's great for implementing locks and transactions. Today we had a problem where a script is run multiple times simultaneously. The easy way to solve this issue is via file locks. The standard way of implementing a file lock could be like this: import fcntl
def lockFile(lockfile):
fp = open(lockfile, 'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
return False
return True
if not lockFile(".lock.pod"):
sys.exit(0)
This works, but it can be much more beautiful and pythonic using the with statement. Here is how a file lock implemented via with statement looks like: import time
with file_lock('/tmp/my_script.lock'):
print "I am here and I am sleeping for 10 sec..."
time.sleep(10)
Here is the implementation (should be cross platform, but not tested): import os, sys
from contextlib import contextmanager
@contextmanager
def file_lock(lock_file):
if os.path.exists(lock_file):
print 'Only one script can run at once. '\
'Script is locked with %s' % lock_file
sys.exit(-1)
else:
open(lock_file, 'w').write("1")
try:
yield
finally:
os.remove(lock_file)
Further reading:
Avoid Disaster: Script backups easily to Amazon S3![]() I released avoid_disaster today, it let's you do following things:
The script is super simple, but it should be quite useful for anyone that wants to create cheap backups. You are also welcome to fork the code on GitHub and supply extensions and patches :-) Example usageFirst install boto and avoid_disaster: $ sudo easy_install boto $ sudo easy_install avoid_disaster Here is some example code that can get you started: import os
from avoid_disaster import S3Uploader, gunzip_dir, generate_file_key
#--- Globals ----------------------------------------------
AWS_KEY = 'YOUR AWS KEY'
AWS_SECRET = 'YOUR AWS SECRET'
s3_uploader = S3Uploader(AWS_KEY,
AWS_SECRET,
'backups.your_domain.com')
#--- Easy usage ----------------------------------------------
#Daily
s3_uploader.compress_and_upload('test_dir/',
'test_dir.%(weekday)s.tgz',
replace_old=True)
#Monthly
s3_uploader.compress_and_upload('test_dir/',
'test_dir.%(month_name)s.tgz',
replace_old=True)
#Weekly
s3_uploader.compress_and_upload('test_dir/',
'test_dir.%(week_number)s.tgz',
replace_old=True)
#--- Generic usage ----------------------------------------------
file_key = generate_file_key('test_dir.%(weekday)s.tgz')
gz_filename = gunzip_dir('test_dir/', file_key)
s3_uploader.upload(file_key, gz_filename, replace_old=True)
os.remove(gz_filename)
I think it's better to accept danger...
and live life to the fullest!
|
Blog labels |