Single .py file contains submodule?
Guido wrote:
(Barry prefers that there's only one class per file; fortunately I don't have that hangup. :-)
So how about a single .py file containing a module and one-or-more submodules? (Sounds like Barry will definitely not like it... ;-) ) Specifically, I wanted to allow easy isolated import of the PyUnit assertion functions, something like: from unittest.assertions import * There are a few ways of refactoring unittest.py to support this, but part of the beauty of PyUnit is the single, independent file. Consulting the Python Reference Manual, I couldn't find anything that forbade a single file from containing submodules, so I came up with the following implementation, which works in Jython 2.1 and Python 2.2: import new import sys import types class Assertions: failureException = AssertionError def failUnless(self, expr, msg=None): """Fail the test unless the expression is true.""" if not expr: raise self.failureException, msg #... def __publishAsModule( name, dict ): submodule = new.module( name ) submodule.__dict__.update( dict ) sys.modules[ name ] = submodule return submodule def __getMethods( instance ): methods = {} for name, member in instance.__class__.__dict__.items(): if type( member ) is types.FunctionType: methods[name] = getattr( instance, name ) return methods assertions = __publishAsModule( __name__ + ".assertions", __getMethods( Assertions() ) ) So, is this an egregious abuse of a regrettable oversight in the specification, or a reasonable way to publish multiple modules from a single .py file? Thanks kb
So, is this an egregious abuse of a regrettable oversight in the specification, or a reasonable way to publish multiple modules from a single .py file?
I think it's much better to split it up in multiple files than to use all those hacks. Those hacks are hard to understand for someone trying to read the code. --Guido van Rossum (home page: http://www.python.org/~guido/)
"KB" == Kevin Butler <kbutler@campuspipeline.com> writes:
KB> So how about a single .py file containing a module and KB> one-or-more submodules? (Sounds like Barry will definitely not KB> like it... ;-) ) Maybe I would, if I understood what that meant ;). [...FAST code omitted...] KB> So, is this an egregious abuse of a regrettable oversight in KB> the specification, or a reasonable way to publish multiple KB> modules from a single .py file? That's just sick. :) -Barry
Kevin Butler wrote:
Guido wrote:
(Barry prefers that there's only one class per file; fortunately I don't have that hangup. :-)
So how about a single .py file containing a module and one-or-more submodules? (Sounds like Barry will definitely not like it... ;-) )
Specifically, I wanted to allow easy isolated import of the PyUnit assertion functions, something like:
from unittest.assertions import *
That's bad style (at least for modules which don't only include constants). Why would you want to enable this ? -- Marc-Andre Lemburg CEO eGenix.com Software GmbH ______________________________________________________________________ Company & Consulting: http://www.egenix.com/ Python Software: http://www.egenix.com/files/python/ Meet us at EuroPython 2002: http://www.europython.org/
Kevin Butler wrote:
Guido wrote:
(Barry prefers that there's only one class per file; fortunately I don't have that hangup. :-)
So how about a single .py file containing a module and one-or-more submodules?
I have been asking this myself, too. I definitely see an advantage beeing able to put modules and submodules in one file. IMO it's a logical structure IMO and it is easier to distribute. If you just want to send someone a module to work/test with (without requiring setup.py etc.pp.) a file is a nicer unit. The next better step involves creating a (distutils-) package. But i don't always want to create packages for a module. Maybe there is an elegant way to achieve this? I don't find Kevin's code too hard to understand, btw... holger
I have been asking this myself, too. I definitely see an advantage beeing able to put modules and submodules in one file.
Trust me. Don't go there. --Guido van Rossum (home page: http://www.python.org/~guido/)
[holger krekel]
I have been asking this myself, too. I definitely see an advantage beeing able to put modules and submodules in one file.
[Guido]
Trust me. Don't go there.
In Perl you spell this "package NAME;". All "dynamic" bindings from then until the next package statement (or end of enclosing block, etc -- it's Perl <wink>) then end up in the NAME namespace. This is also how you declare a class in Perl. This is what it leads to <wink>: If you have a package called m, s, or y, then you can't use the qualified form of an identifier because it would be instead interpreted as a pattern match, a substitution, or a transliteration.
participants (6)
-
barry@zope.com -
Guido van Rossum -
holger krekel -
Kevin Butler -
M.-A. Lemburg -
Tim Peters