Saturday, February 13. 2010Absolute paths must dieOk guys, it's pretty simple: if you're working in a Turing-complete environment and you've got paths which start with a '/', you're doing it wrong. Very wrong! Todays offender: WSGI scripts. Here's the first line of a common WSGI script:
See a problem there? That's right, the absolute path! How could this be solved? Simple: put the WSGI script in </rant> Sunday, January 17. 2010Django settings.py mini-tip: the `path` lambdaThere are a few things I do every time I start a new Django site, and one of those things is add my
From then on, it's drop-dead simple to create absolute paths which are relative to the root of the project (or, at least, the directory which contains settings.py). For example:
And some other places where it is useful:
Tuesday, February 10. 2009str(...): 'yer probably doin' it wrong.Unicode is an ugly beast... And until people start standardizing on Python 3k*, we're going to have to live with the eccentricities of Python 2's strings. But, fear not! There is (at least some) hope. By changing a few patterns in the way you code, you can alleviate the bulk of Unicode-related problems. First, using the str function. In just about every case, if you're using the str function, you're probably doing it wrong. Let me demonstrate:
Cool, we can hash things then print out the hash:
But, wait... What happens if the thing we're hashing isn't a string (even though it can be represented as a string):
Oh no! Ok, let's fix the code:
Great -- we can hash numbers now:
And, for most people who only speak English, this is a perfect place to stop.
After all, everything is a Well... No. What happens if the input is a
Crap. Where the heck is 'ascii' coming from? Well, it's a long story (which I've covered over at Encoding and Decoding Text
in
Python),
but basically the "Alright...", you're probably thinking, "If the problem is with Ok, let's see what happens.
Yup, that's right -- you just can't win What's happening here? Well, this time, the Confused yet? So how can we save ourselves from all this insanity? Actually, it's not too difficult:
And what about those
So there you have it.
A quick and fairly easy way to avoid many of your encoding-related options If you're still not quite feeling comfortable with all of this, though, take a read over Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) *: My newly installed Debian 4 machine is still running Python 2.4 (released December 2004)... So that wait might be a while. Thursday, May 29. 2008Python Brain-TeaserI'm working on pulling some functionality out of one object and putting it in another, and I came across this interesting problem: class Foo:
me = "foo"
class Bar:
me = "bar"
def get_me(self):
return self.me
Foo.get_me = Bar.get_me
x = Foo()
print x.get_me() What does this print? And, next question, why is that? After lunch I'll post my thoughts Continue reading "Python Brain-Teaser" Friday, April 11. 2008SSH and HTTPS on the same port?!A friend of mine was over the other night, and we were talking about how much we like running SSH on port 443. It gets you around all but the most insane firewalls, and to anyone but the keenest of observers it just looks like HTTPS traffic. But running SSH over port 443 makes it particularly difficult to run a secure web server as well. So what can be done? Well, it turns out that when an SSH client initiates a connection, it waits for the server to send a banner ( Knowing this, it didn't take long to formulate a plan: write a little program which will listen on 443. When it gets a connection, it figures out if the remote end is trying to speak SSH or HTTPS, makes the appropriate connection on the local end, the passes the data between the two. It only took a couple of hours to write a small proof-of-concept... And, believe it or not, it even works! Disclaimer: this particular implementation is not quite suitable for any real use. Anyway, this may not be the most efficient way of doing things... But that's no matter. It's still neat Code: sstp.py Friday, February 8. 2008Reverting Changes in SVN (or: it's not as easy as svn up -r)If there is one thing that I've found universally confusing about version control systems, it's how to revert back to a previous revision, then move on from there. Conventional wisdom would dictate that So, besides The trick is that you can
[wolever@thebes] ~/test_dr/All svn diff -r 2:1
Index: stuff
===================================================================
--- stuff (revision 2)
+++ stuff (revision 1)
@@ -1,4 +1,3 @@
Let us endeavor so to live that when we come to die even the undertaker will be
sorry.
-- Mark Twain, "Pudd'nhead Wilson's Calendar"
-Increased knowledge will help you now. Have mate's phone bugged.
Index: docs.html
Which means that merge will work both ways as well: [wolever@thebes] ~/test_dr/All svn merge -r 2:1 stuff U stuff [wolever@thebes] ~/test_dr/All svn ci -m "reverted changes" stuff [wolever@thebes] ~/test_dr/All svn diff -r 1:3 stuff [wolever@thebes] ~/test_dr/All Of course, this will also work with the myriad of other version control tools out there. In fact, if you're still using SVN, reverting old changes is probably the least of your problems... At least compared to, say, merging different branches ^_^ But that's a post for another day -- I need to stop writing about merging and actually get on with, err, doing it... Tuesday, June 12. 2007Amazon S3 (or, How I Solved My Image Hosting Woes)A couple months ago, I read Jeff Atwood's article on Using Amazon S3 as an Image Hosting Service, but because my favicon does not does not generate 27 GBs of traffic a month, I didn't think much of it. (if you're scratching your head wondering what Amazon's S3 is, I'll do my best to explain. It stands for Simple Storage Service, and it is just that: a simple service that allows you to store data. That data (or "those files", if you wish) can be world-readable (from a web browser) or private (many people are using it for backup). It's targeted at developers (Amazon only provides a set of APIs -- all GUIs are 3rd party) and it is dirt cheep: only $0.15 a gigabyte.) Then, a few weeks ago, I was taking some pictures of my sister's soccer practice, and realized that I needed some place to put them. After looking at a couple of cheep hosting plans, I decided that I didn't like them; $5 a month isn't much, but that's still $60 a year for a whole lot of space I won't be using. Then I remembered S3. After a night of playing around, I figured that it should be pretty simple. Write a script that will go through an HTML file looking for images, then upload them one at a time, replacing the links as it goes. html2s3 was born. It turns out that the S3 library provided by Amazon is very easy to use, with most of my time spent correcting file paths. A couple of neat bits: parser = OptionParser(usage = "usage: %prog [options] [FILES]\nWill process ..." parser.add_option("-b", "--bucket", action="store", dest="bucket", help=...) parser.add_option("-k", "--key-prefix", dest="remote_base_path", help="Prefix key ...") parser.add_option("-n", "--no-backup", dest="no_backup", action="store_true" ...) (options, args) = parser.parse_args() I had seen source = args or sys.stdin.xreadlines() for line in source: process_entry(line) One thing I loved about C was the ability to use assignment in loops ( Update: When I moved the script over to my server, it started telling me that I didn't have permission to list the bucket. So I started digging in to it a bit, and found out that I was getting the error Tuesday, May 29. 2007Debugging PythonThere are two modules that I use to debug Python code:
|
QuicksearchArchivesLinks
CategoriesSyndicate This Blog |
