June 22nd, 2007
Steve Yegge has a damn good post about why knowing how compilers work, I mean really work, can be one of the most import skills for any programmer. Personally I’ve only dabbled with compilers and I have to say that I agree with everything he says on the subject. Oh, has anyone figured out what his NBL is yet?
Posted in aside, programming | 2 Comments »
June 20th, 2007
Guido has a good chunk on information up on his blog about Python 3000’s updates. There is a lot of good information here, though one thing which stuck to me was “Python 3.0 will break backwards compatibility”. I highly recommend anyone who uses python to check out this post.
Posted in aside, news, programming, python | No Comments »
June 20th, 2007
The CEO of WebIS posted an open letter to warez users on Monday, explaining how pirating software not only hurts the people who make the software, but ultimately those who use it as well.
Posted in aside, finance, software | No Comments »
May 29th, 2007
If you have a need to import a module in python and, for whatever reason, you will not know the name of the module until run-time, python provides the built-in __import__ function.
This allows you to put the name of the module to be imported into a string and assign the returned module object to any variable. This is can useful if your plan on dynamically importing a module whether it is stored in a configuration file, database, or in another way.
Here is an example:
module_name = "string"
module = __import__(module_name)
If you wish to use the “from module import…” method you will have a bit more work to do. At a basic level you will build a string which represents your import command, and then pass it to ‘exec‘ as such:
import_string = "from string import ascii_letters"
exec import_string
Here is an example of a second method which I used while modifying a custom solution to replace the default UNIX crontab. This specific script uses a set of configuration files to build a static host specific configuration object. This object is then pickled and pushed out to each host using rdist (tunnled through SSH).
import os
import sys
import glob
from cPickle import dump as pkl_dump
from crons.cron_config import CRONS
PATH = '/home/kevin/hosts'
DIRLIST = glob.glob(PATH + '/hostname*')
for ob in DIRLIST:
CRONTAB = []
HOSTNAME = ""
if os.path.isdir(ob):
HOSTNAME = ob.split('/')[1]
for app in CRONS:
exec 'from crons.' + app + '_crontab import APP_CRON'
for entry in APP_CRON:
if 'hostname' in entry:
if entry['hostname'] == HOSTNAME:
CRONTAB.append(entry)
else:
continue
del APP_CRON
fo = open("%s" % ob + "/cron.pkl","wb")
pkl_dump(CRONTAB,fo,1)
fo.close()
del fo
del CRONTAB
del HOSTNAME
There is also the ‘imp‘ module which exposes an interface to the mechanisms used to implement the import statement. This allows you to make a completely custom import method. I’ve included the example from the Python docs for completeness sake. This example emulates the built-in import statement.
import imp
import sys
def __import__(name, globals=None, locals=None, fromlist=None):
# Fast path: see if the module has already been imported.
try:
return sys.modules[name]
except KeyError:
pass
# If any of the following calls raises an exception,
# there's a problem we can't handle -- let the caller handle it.
fp, pathname, description = imp.find_module(name)
try:
return imp.load_module(name, fp, pathname, description)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
As you can see there are many options when it comes to dynamically loading a module in Python. If any of you know of additional examples, or better ways to implement my examples, please leave a comment below.
Posted in feature, programming, python | 16 Comments »
May 16th, 2007
Freelance Switch has a nice post going over some of pro’s and con’s of working a Freelance career on the side, rather than being a full-time freelancer. Figured I would post in here since this is something I know a lot of programmers (myself included) struggle with.
Posted in aside, freelancing | No Comments »
May 11th, 2007
I’m looking for a good (and easy to use) way to automagically highlight code (from several languages) in posts. Optimally I want something that uses CSS and based upon a language attribute would highlight the code without using any external applications. Any ideas if there is something like this already out there?
Posted in aside, blogging, css, programming | No Comments »
May 1st, 2007
Gregory Szorc raises an interesting question on his blog regarding why PHP4 is still used by so many web apps, including Wordpress (used by MANY blogs, including this one).
Personally, I haven’t touched PHP in any serious way since before PHP5 came out. If I was going to do serious work customizing the code behind this blog I would probably go through the effort of learning PHP5 and using that, but since I’m not I’ll probably just continue hacking up PHP4.
Posted in aside, php, programming | No Comments »
April 23rd, 2007
Python annouced their 2.5.1 Release on the 18th. This is a bugfix release of the popular programming language.
Posted in aside, news, python | No Comments »