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
% 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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?
|
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/
| |
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
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |