Tidbits @ Kassemi

A collection of opinions, thoughts, tricks and misc. information.

Saturday, September 30, 2006

 

Use __import__ on an actual path...

Now, this is definitely not the wisest thing to do, but sometimes you'll want to simply import a module when you know its path. The built in __import__ function won't let you go about doing this. If you're looking for something that works, but might be a little dirty, I've got the trick for you.


# 2006 James Kassemi, (james@tweekedideas.com)
# ================================================
# Import all from all modules in this directory...

import os, imp

def import_path(path, return_name=False):
''' Use like __import__, but with the path to a module. '''
if os.path.isdir(path):
mn = path.split('/')[-1]
path = os.path.join(path, '__init__.py')
if not os.path.exists(path):
path += 'c'
else:
mn = path.split('/')[-1]
mn = mn[:mn.rfind('.')]

suffixes = imp.get_suffixes()
for suffix, mode, type in suffixes:
if path.endswith(suffix):
fp = open(path, mode)
m = imp.load_module(mn, fp, path, (suffix, mode, type))
fp.close()
if return_name: m = (m, mn)
return m
raise ImportError("Not a module: %s" % path)

focal_dir = os.path.dirname(__file__)
for f in os.listdir(focal_dir):
if f.startswith('.') or f.startswith('_'): continue
temp_mod = import_path(os.path.join(focal_dir, f))
for k, v in temp_mod.__dict__.iteritems():
globals()[k] = v



Place the above contents in an __init__.py file and it will automatically load all of the contents from all of the sibling files in the directory. No more specifying an __all__, either, as you can quickly hack this to just import the module itself as well:


focal_dir = os.path.dirname(__file__)
for f in os.listdir(focal_dir):
if f.startswith('.') or f.startswith('_'): continue
module, name = import_path(os.path.join(focal_dir, f), True)
globals()[name] = module


Now there are some concerns with approaching the problem this way... Mainly compatibility with other import statements in the same namespace, sys.modules mapping, etc, but it will work when you're in a pinch.

James

Saturday, September 23, 2006

 

Use GPL v. 2 in your application...

Those of us that write open source applications have something to worry about, and it comes in the form of the GPL agreement:


one line to give the program's name and an idea of what it does.
Copyright (C) yyyy name of author

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.


This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


Notice the statement in bold... Some of us are quite comfortable with version 2 at the moment, and version 3 is starting a pretty nice controversy (search slashdot for articles related to gpl v3 and read up).

Do yourself a favor when writing an open source application under the GPL license and avoid the issues with the upcoming GPL v3. Use this text instead of what you're getting from the gnu site at the moment:


Copyright (C) YEAR YOUR_NAME

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License Version 2,
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


Change is scary, remember?

Thursday, September 21, 2006

 

Soon forget: Daily impact

Ah. The event that most hit me this evening came from what seems an unlikely source, a Pearl Jam song, "Soon forget." Not certain whether or not it's a cover, but that's beside the point at the moment...

"Sorry is the fool who trades his soul for a corvette,
Thinks he'll get the girl, he'll only get the mechanic.
What's missing? He's living a day he'll soon forget.

That's one more time around, the sun is going down,
The moon is out, but he's drunk and shouting, putting people down.
He's pissing, he's living a day he'll soon forget.

Counts his money every morning,
the only thing that keeps him horny.
Locked in a giant house, that's alarming.
The townsfolk, they all laugh.

Sorry is the fool who trades his love for a high-rise rent.
Seems the more you make equals the loneliness you get,
and it's fitting, he's barely living, a day he'll soon forget.

That's one more time around, there is not a sound,
he's lying dead clutching benjamins.
Never put the money down. He's stiffening, we're all
whistling, a man we'll soon forget.
"

Although a bit crudely put for such a slow, harmonious piece,
I tend to trust the wealthy, drugged up artists' interpretation
of money and success more than I would Mr. Average Joe. Although
I'm not necessarily rolling in the dough, I do long to have
an easy life.

A man we'll soon forget... Hopefully not reader, hopefully not.

Archives

August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006  

This page is powered by Blogger. Isn't yours?