Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?
Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:
mainscript.py:
---------------------------
AddPlot("scatte r", "coordinate ")
# set other things here
DrawPlots()
it works fine. But I want to be able to do this:
myModule.py:
----------------------
def defaultScatterP lot():
AddPlot("scatte r", "coordinate ")
#do other things
DrawPlots()
and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaul tScatterPlot()
This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?
Thank you in advance for any help.
RDB 12 1822
On Jun 11, 11:02 am, reubendb <reube...@gmail .comwrote:
Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?
Here is an example of what I meant. The function AddPlot() and
DrawPlots() are added to the global namespace by the software CLI. If
I do this:
mainscript.py:
---------------------------
AddPlot("scatte r", "coordinate ")
# set other things here
DrawPlots()
it works fine. But I want to be able to do this:
myModule.py:
----------------------
def defaultScatterP lot():
AddPlot("scatte r", "coordinate ")
#do other things
DrawPlots()
and then in mainscript.py:
---------------------------------------
import myModule
myModule.defaul tScatterPlot()
This won't work because myModule.py doesnot have access to AddPlot().
How do I do something like this ?
Thank you in advance for any help.
RDB
I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.
<code>
commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args , *kwargs):
# Do something
---------------------
mainProgram.py
------------------------
from commonMods import AddPlot
AddPlot("scatte r", "coordinate ")
# etc etc
-------------------------
myModule.py
------------------------
from commonMods import AddPlot
AddPlot("scatte r", "coordinate ")
# etc etc
-------------------------
</code>
Hope that helps.
Mike
On Jun 11, 1:37 pm, kyoso...@gmail. com wrote:
On Jun 11, 11:02 am, reubendb <reube...@gmail .comwrote:
Hello,
I am new to Python. I have the following question / problem.
I have a visualization software with command-line interface (CLI),
which essentially is a Python (v. 2.5) interpreter with functions
added to the global namespace. I would like to keep my own functions
in a separate module and then import that module to the main script
(that will be executed using the CLI interpreter). The problem is, I
cannot access the functions in the global namespace of the main script
from my module. Is there anyway to do that ?
<snip>
I think you're doing it backwards. If you want access to AddPlot, then
you should import mainscript into that module instead of the other way
around. When I have common methods I want to call from different
scripts, I put those methods/functions into their own module/file.
Then I just import the module and call whatever script I need.
<code>
commonMods.py
---------------------
AddPlot(*args, *kwargs):
# Do something
DrawPlots(*args , *kwargs):
# Do something
---------------------
Hi Mike,
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Thanks.
RDB
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <re******@gmail .comescribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.
--
Gabriel Genellina
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <re******@gmail .comescribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.
--
Gabriel Genellina
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail .comescribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that are
responsible for bringing AddPlot and DrawPlots into the current namespace?
Import the same things in your second module.
No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?
Thanks.
RDB
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail .comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail .com> escribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that are responsible for bringing AddPlot and DrawPlots into the current namespace? Import the same things in your second module.
No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?
Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:
import sys
from types import ModuleType as module
plotModule = module('plot')
for key,value in globals().items ():
if key[:2] != '__':
setattr(plotMod ule, key, value)
sys.modules['plot'] = plotModule
--
Gabriel Genellina
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail .comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail .com> escribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that are responsible for bringing AddPlot and DrawPlots into the current namespace? Import the same things in your second module.
No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?
Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:
import sys
from types import ModuleType as module
plotModule = module('plot')
for key,value in globals().items ():
if key[:2] != '__':
setattr(plotMod ule, key, value)
sys.modules['plot'] = plotModule
--
Gabriel Genellina
On Monday 11 June 2007 17:10:03 Gabriel Genellina wrote:
En Mon, 11 Jun 2007 17:29:35 -0300, reubendb <re******@gmail .comescribió:
On Jun 11, 3:30 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Mon, 11 Jun 2007 15:18:58 -0300, reubendb <reube...@gmail .com>
escribió:
The problem is I don't define the functions AddPlot() and DrawPlots().
It's built into the python interpreter of the CLI version of the
program I mentioned, and they are defined on the main script. I load
the main script using something like "software -cli -s
mainscript.py".
In the mainscript.py I import myModule, but of course myModule does
not have access to the functions defined in the global namespace of
mainscript.py.
Don't you have some import statements at the top of mainscript.py that
are
responsible for bringing AddPlot and DrawPlots into the current
namespace?
Import the same things in your second module.
No, I *don't* have any import statement mainscript.py. When using this
software's CLI, AddPlot and DrawPlots are available to me
automagically from mainscript.py. Hence my question: How do I make
this available from other module. Is there any way at all ?
Yes: create your own module on-the-fly, using the recipe posted earlier by
John Krukoff.
If there are many functions, try enumerating them all:
import sys
from types import ModuleType as module
plotModule = module('plot')
for key,value in globals().items ():
if key[:2] != '__':
setattr(plotMod ule, key, value)
sys.modules['plot'] = plotModule
Great ! That seems to work, thanks a lot.
One last question. Do I have to do this for ever script I write, or can I put
this into separate file and "include" it somehow ?
I am going to have several mainscripts.py, and all is going to import myModule
that will need access to this plots subroutine. It'll be great if I can put
this trick on a single file that is included by the main scripts, to avoid
violating DRY principle.
Thanks for all the help.
RDB
--
Reuben D. Budiardja
En Mon, 11 Jun 2007 22:19:15 -0300, Reuben D. Budiardja
<te******@pathf inder.phys.utk. eduescribió:
One last question. Do I have to do this for ever script I write, or can
I put
this into separate file and "include" it somehow ?
I am going to have several mainscripts.py, and all is going to import
myModule
that will need access to this plots subroutine. It'll be great if I can
put
this trick on a single file that is included by the main scripts, to
avoid
violating DRY principle.
According to your description on how things work, you will need the
globals() from mainscript.py; try this:
--- mainscript.py ---
import plot_setup
plot_setup.setu p(globals())
.... rest of script, perhaps importing myModule.py ...
--- plot_setup.py ---
import sys
from types import ModuleType as module
def setup(namespace ):
plotModule = module('plot')
for key,value in namespace.items ():
if key[:2] != '__':
setattr(plotMod ule, key, value)
sys.modules['plot'] = plotModule
--- myModule.py ---
import plot
def do_work():
plot.DrawPlot(. ..)
...
(Having to type two lines at the top of your main scripts doesn't look so
bad...)
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tim Tyler |
last post by:
PHP puts most of its functions into a big flat global namespace.
That leads to short function names - but creates a namespace
minefield for programmers.
Lots of the functions are legacies from the days before PHP
got object-oriented features.
For instance we currently have:
|
by: Tonguç Yumruk |
last post by:
I'm trying to build a completely plug-in based system. One of my
problems is importing a package dynamically. I'm trying to emulate the
import command. The __import__() function or imp module doesn't help me
much because they only return the module. I want to register the module
with it's name in the current namespace. I can do it by:
globals() = __import__(module_name)
But I don't think it's a good thing to access the global namespace...
|
by: Martin Drautzburg |
last post by:
Withing a module I can assign a value to a global var by assigning to
it in the outermost scope. Fine.
But how can I do this if the attribute name itself is kept in a
variable. Once the module is loaded I can access the module's
namespace no problem, but inside the module the dictionary is not yet
present right ?
IOW how can I write something like
|
by: Bryan Parkoff |
last post by:
….I would like to know which is the best optimization to use global
variable or global struct. I always tell C/C++ Compiler to turn on
optimization.
….I use underscore between first name and second name for better
readable. After optimization, global variables might be misaligned
because each global variables must be converted to 32 bits, but I do
see that C/C++ Compiler do padding between variables. Struct does the
same to do padding....
|
by: seberino |
last post by:
At top of a module I have an integer like so...
foo = 4
In a function in that module I know I need to do 'global foo' to get at
the value 4.
....
IIRC, for dictionaries you DO NOT have this issue?
| |
by: newbie |
last post by:
Hello,
I have questions about global variables in OOP (in general) and Python
(in specific). I understand (I think) that global variables are
generally not a good idea. However, if there are variables that need to
be accessed by a number of classes that exists in separate namespaces
(files), what would be the best way to do this?
So far, I have approached the problem by making the variables
attributes of one class and passing...
|
by: robert |
last post by:
Using global variables in Python often raises chaos. Other languages use
a clear prefix for globals.
* you forget to declare a global
* or you declare a global too much or in conflict
* you have a local identical variable name and want to save/load it
to/from the global with same name
* while you add code, the definition of globals moves more and more
apart from their use cases -> weirdness; programmers thinking is fragmented
* using...
|
by: abcd |
last post by:
I have a file, "a.py"
blah = None
def go():
global blah
blah = 5
....i was hoping to see "5" get printed out the second time I displayed
|
by: Anthony Kuhlman |
last post by:
Pythoners,
I'm having trouble understanding the behavior of global variables in a
code I'm writing. I have a file, test.py, with the following contents
foo =
def goo():
global foo
foo =
foo.append(2)
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |