473,786 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1784
ke****@netacc.n et (J. Kenney) wrote in
news:97******** *************** ***@posting.goo gle.com:
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
3692
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 gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
4
2756
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 expected. For example in C, I can have status = get_network_info (strIpAddress, &strHostname, &nPortNumber) where this fictitious function returns a status, but also returns modified values for a hostname and a port number. In Python, there does not seem to be an easy way to have functions return...
0
2509
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
3
3019
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 guide for my problem... Here is the script i already wrote but i'm having problem to move forward. my problem is : 1. how do i assign each checkbox to have equal value with the value of the d_id?
11
4365
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. seems to be much more difficult to find. I thought django might be a good way, but I can not seem to get an answer on that board. I would like to put together a CRUD grid with editable/deletable/
10
3261
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 conf.py file as name=value pairs, and I am importing conf.py file to use this variable. it works well import conf print conf.SomeVariable but if I need to change some configuration parameteres, it would need
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.