472,353 Members | 1,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Python Importing and modifying print

Good Morning,

Is there a way for a function called within an _imported_
library to call back to _calling_ python program to run a
function? (Shown below)

Also can you overload the print function to have it do
redirect output to an array for a bit, and then switch
it back. (for example: right before importing make print
append each line to an array vice printing it, then going
back to normal after the import is complete.)

Many thanks!

#first_file.py
#!/usr/local/bin/python
import newlib
def main():
print 'hi'

#newlib.py
def go():
???????.main()
???????.main()
go()

#Desired results
% ./first_file.py
hi
hi
%
Jul 18 '05 #1
1 1715
ke****@netacc.net (J. Kenney) wrote in
news:97**************************@posting.google.c om:
Good Morning,

Is there a way for a function called within an _imported_
library to call back to _calling_ python program to run a
function? (Shown below)
Yes, you *could* do that, see the code below: the script is loaded into a
module called __main__ so all you have to do is import __main__ and you can
access the scripts variables.
#newlib.py import __main__
def go():
__main__.main()
__main__.main()
go()

This still won't have quite the results you want, as the function main
doesn't exist at the point when you import the other module. You need to
define it before the import (remember, the code in a module is executed
the first time the module is imported):

#first_file.py
#!/usr/local/bin/python
def main():
print 'hi'

import newlib
However, you probably don't want to do that. A much better solution is
either to put your 'main' function into another module, or to pass the
callback function around as a parameter:

#first_file.py
import newlib

def main():
print 'gi'

newlib.go(main)

#newlib.py
def go(where):
where()
where()


Also can you overload the print function to have it do
redirect output to an array for a bit, and then switch
it back. (for example: right before importing make print
append each line to an array vice printing it, then going
back to normal after the import is complete.)

Yes, the easiest way would be to reassign sys.stdout to put output into
your array using cStringIO and then restore sys.stdout after you have
finished. Again though, it isn't a good idea to do real work on an import;
much better to do the work in a function and have no real code in the
imported module.

Jul 18 '05 #2

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

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some...
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration...
3
by: cuties | last post by:
Hi all.... i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get...
11
by: walterbyrd | last post by:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax and non-Ajax solutions abound. With Python, finding such library, or apps....
10
by: sandipm | last post by:
Hi, In my application, I have some configurable information which is used by different processes. currently I have stored configration in a...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.