473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Making "import" *SLIGHTLY* more verbose?

First of all, just let me say that I'm aware of the "-v" switch for
Python, and I don't want anything nearly that verbose.

I often long for the following behavior from Python when I'm running
interactively: When a new module is imported, I'd like the path to the
file providing the module to be printed out to the screen. If the
module is already in sys.modules, then don't worry about printing
anything.

The best thing that I can think of to do is something like:

<python>
__builtins__.__real_import__ = __builtins__.__import__

def noisy_import(name, globals = globals(), locals = locals(), fromlist= []):
printit = name not in sys.modules
mod = __real_import__(name, globals, locals, fromlist)
if printit:
try:
print '## Loading %s (%s)' % (mod.__name__, mod.__file__)
except AttributeError:
print '## Loading %s (built-in)' % mod.__name__
return mod

__builtins__.__import__ = noisy_import
</python>

Which seems to work okay for basic kinds of modules, but doesn't quite
work right for modules that belong to a particular class. Any
suggestions on what I might be missing here? I would imagine that
this is a cookbook type thing, and if there are any references on how
to do this, I'd greatly appreciate it (I couldn't come up with the
right Google magic words).

Thanks in advance for any help.

--
Steve Juranich
Tucson, AZ
USA
Oct 25 '05 #1
1 4629
On Tue, 25 Oct 2005 15:34:37 -0700, Steve Juranich <sj******@gmail.com> wrote:
First of all, just let me say that I'm aware of the "-v" switch for
Python, and I don't want anything nearly that verbose.

I often long for the following behavior from Python when I'm running
interactively: When a new module is imported, I'd like the path to the
file providing the module to be printed out to the screen. If the
module is already in sys.modules, then don't worry about printing
anything.

The best thing that I can think of to do is something like:

<python>
__builtins__.__real_import__ =3D __builtins__.__import__

def noisy_import(name, globals =3D globals(), locals =3D locals(), fromlist=
=3D []):
printit =3D name not in sys.modules
mod =3D __real_import__(name, globals, locals, fromlist)
if printit:
try:
print '## Loading %s (%s)' % (mod.__name__, mod.__file__)
except AttributeError:
print '## Loading %s (built-in)' % mod.__name__
return mod

__builtins__.__import__ =3D noisy_import
</python>

Which seems to work okay for basic kinds of modules, but doesn't quite
work right for modules that belong to a particular class. Any You want to go beyond just teasing, and tell us what "particular class"? ;-)
suggestions on what I might be missing here? I would imagine that
this is a cookbook type thing, and if there are any references on how
to do this, I'd greatly appreciate it (I couldn't come up with the
right Google magic words).

Thanks in advance for any help.

The imp module has a lot of info, and some example code that
might be useful.

I'm suspicious of the default values you provide in your noisy_import though.
They are all mutable, though I guess nothing should mutate them. But will they
be valid for all the contexts your hook will be invoked from? (Or are they actually
useless and always overridden?)

Regards,
Bengt Richter
Oct 26 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
2
by: Torsten Mohr | last post by:
Hi, i tried to find the file and line in the C sources of python where the command "import" is implemented. Can anybody give me some hint on this? Thanks, Torsten.
2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
9
by: monkey | last post by:
I just learn to make a blank windows frame with python and wxpython. I found the statment "import wx" cannot work as the original "from wxPython.wx import *". I see in the readme file of wxpython...
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.