The Python’s built-in range() is an extremely useful function, but has a little problem: it doesn’t include the right extreme of the range. For example, a call to range(1, 10) will be evaluated to this a list of numbers from 1 to 9 (not including 10):
>>> range(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Today I need for a work a range() function that includes the right extreme, so I had to develop mine. Here it is:
def inclusive_range(start, stop, step=1):
"""
A range() clone, but this includes the extremes
"""
l = []
x = start
while x <= stop:
l.append(x)
x += step
return l
Of course there are faster implementations of this function around here (and if you know one, please let me know) and surely this one is not one of the fastest, but it works and that solves my problem right now.
Yesterday I had a meeting with a customer about a new site I should develop for them. Since they’re a book publisher, they wanted an online book store. Apart from the technical details (the site isn’t as simple as you may believe, they need a lot of not-so-easy-to-do stuff), the most important point we focused on is the fact that they have an internal IT technician that handles all their computer needs. If you’re asking yourself why this matters, keep reading:
- me (to be precise, my company) stopped development of PHP sites about one year ago in favor of Python
- we release the web site’s code to them
- for this project, we haven’t been asked any kind of future support; this means that when the site is finished, we won’t touch the product anymore (unless they don’t pay us to do the modifies they need)
- but they don’t want to pay us to these modifies, because they have their internal IT technician
- their technician knows only PHP (and he never even known the Python’s existence until yesterday)
(Continue reading…)
Just a quick tip. If you’re using the python’s os.listdir() function you may be not happy at all with the way it orders results. The following code snippet orders the result of the listdir so it shows directories first and then files, and sorts these two groups alphabetically:
import os
def custom_listdir(path):
"""
Returns the content of a directory by showing directories first
and then files by ordering the names alphabetically
"""
dirs = sorted([d for d in os.listdir(path) if os.path.isdir(path + os.path.sep + d)])
dirs.extend(sorted([f for f in os.listdir(path) if os.path.isfile(path + os.path.sep + f)]))
return dirs
I have a love/hate relationship against the hello messages shown in all the world languages on flickr. That’s why this morning I was talking with a friend on IRC about this and a bad idea jumped in my mind: do an IRC bot that says hello in many languages when someone joins a channel.
It’s from several years that I don’t do anything IRC related, but this time I had two special weapons in my backpack: python and twisted. The final bot is ~90 lines of code, half of the which are for the hello list and the entire coding process took less than 20 minutes.
(Continue reading…)