473,396 Members | 2,129 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,396 software developers and data experts.

Invoking Python from Python

Hi all,

I have a need to create a Python script on the fly from another Python
program and then execute the script so created. Do I need to invoke
Python through os.spawnl or is there a better way?

Thanks,

--
John

Nov 8 '05 #1
10 2183
Am Tue, 08 Nov 2005 08:10:25 -0800 schrieb John Henry:
Hi all,

I have a need to create a Python script on the fly from another Python
program and then execute the script so created. Do I need to invoke
Python through os.spawnl or is there a better way?


Hi,

creating source code with a script, is no good solution.

Once I had to maintain lisp code which stored its data in lisp code, too
(incl. conditions and loops). It was a nightmare.

Please explain what you want to do, and we will find a better solution.

HTH,
Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Nov 8 '05 #2
In article <pa****************************@thomas-guettler.de>,
Thomas Guettler <ni**************@thomas-guettler.de> wrote:
Nov 8 '05 #3
John Henry wrote:
Hi all,

I have a need to create a Python script on the fly from another Python
program and then execute the script so created. Do I need to invoke
Python through os.spawnl or is there a better way?


Could you import the generated script? This might be the way to go if,
e.g., you're generating a "template" configuration file that might
subsequently be edited by a human being.
Nov 8 '05 #4
John Henry wrote:
Hi all,

I have a need to create a Python script on the fly from another Python
program and then execute the script so created. Do I need to invoke
Python through os.spawnl or is there a better way?
When doing something similar to this I used the built-in 'execfile()'
function to execute the code created (and retrieve its results).

Cameron Laird wrote:Once I had to maintain lisp code which stored its data in lisp code, too
(incl. conditions and loops). It was a nightmare.


This is exactly what my Python program does, but I've found it to be a
very powerful and useful technique while remaining relatively easy to
maintain (the generated code doesn't contain any conditionals or loops,
however). Another nice by-product is that the data stored this way is
portable to different platforms.

-Martin

Nov 8 '05 #5
cl****@lairds.us (Cameron Laird) writes:
In article <pa****************************@thomas-guettler.de>,
Thomas Guettler <ni**************@thomas-guettler.de> wrote:
creating source code with a script, is no good solution.
Once I had to maintain lisp code which stored its data in lisp code, too
(incl. conditions and loops). It was a nightmare. Yes and no. There are times when it's justified. I ENTIRELY
agree, though, that many people who *think* that's what they
want to do simply don't understand how dynamic base Python is,
and therefore don't realize how much easier it can be to write
a single, unified application.


Yup. Python can do a lot of things directly that other languages might
solve with code that writes code. However, that's a *very* powerful
technic, and not everything it does can be done with lesser tools. On
the other hand, it's a *very* powerful technic, and abusing it can
easilyi create unmaintainable code.
At this point, 'twould be appropriate to describe an instance
or two in which code generation is a good idea. While I have
some, they're tedious to make clear. Maybe I'll do so in a
follow-up ...


Since Cameron didn't provide examples, let me grab a simple one. The
cheetah templating system works by creating Python programs from the
template. The programs, when run, output the "filled in" template. The
templates are generally more maintainable than the raw python - even
if you cleaned up all the things Cheetah does to make writing
templates easier. This model makes it possible for Cheetah templates
use inheritance - they can inherit from each other, from python
classes, and python classes can inherit from them.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 9 '05 #6
In article <86************@bhuda.mired.org>, Mike Meyer <mw*@mired.org> wrote:
Nov 9 '05 #7
I also think something along the lines of execfile() may serve the
original poster. There was a thread last month about compile()
and exec() with a concise example from Fredrik Lundh.
Google "Changing an AST" in this group.

With dynamically generated code I prefer the separate compile()
step so that I can catch the compile exceptions separately.

Nov 9 '05 #8
cl****@lairds.us (Cameron Laird) writes:
I'll rein myself in and suggest an even easier introduction
to this subject: configuration files. RARELY is the correct
answer to create a new syntax, although many development
organizations give the impression that's their first choice.
".ini"-speak is a safe-enough choice. Most interesting,
though, is to interpret Python or some subset as a configu-
ration specification, so that one has immediately not just
HOME = "/some/folder"
STEP_LIMIT = 16
but
pool_size = cpu_count * 30
and even
if today == "Sunday":
total_process_maximum = 8
available in the configuration language. Neat, eh?
I once carried this a step further, and used methodless classes as a
configuration mechanism:

class PlainFoo:
# Values for a plain foo

class FancyFoo(PlainFoo):
# Overrides for just the things that are different

The program that used this created needed lots of objects, in a
variety of different flavers that were literally specified as "Just
like PlainFoo, but with ...". Doing it this way made configuring
things trivial.

At the time, I attached "configuration variables" to instances. If I
were going to do it today, I'd look into making the parent classes of
the class that implements Foo dynanmic.

plwm (an X11 window manager - sort of - built in top of python-xlib)
carries this another step further. You configure your window manager
by creating a subclass of the WindowManager (or other) class that
mixes in the features you want, and sets the attributes to control
specific features.

It's very flexible - but at this point, the "configuration file" is a
Python program, and not really suitable to use by non-programmers.
But if configuration is *that* powerful, then it can also
do great damage. How does one make Python interpretation safe?
That's a subject for another day.


We're all waiting for this, somewhat impatiently :-).

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 9 '05 #9
In article <86************@bhuda.mired.org>, Mike Meyer <mw*@mired.org> wrote:
Nov 9 '05 #10
Cameron Laird wrote:
....
I should make that explicit: application developers, you
don't have to tell customers everything your programs do.
Your obligation is to make 'em meet requirements. If it
helps *you* that they do more, so be it.

I'd agree with the proviso that you at least inform your
customer if you are creating a security hole.

--
-Scott David Daniels
sc***********@acm.org
Nov 11 '05 #11

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

Similar topics

0
by: Anand K Rayudu | last post by:
Hi I have extended/embedded python into my application, it is working great, The documentation is too good, I could replace existing scripting tool to python in no time. I have one problem...
4
by: LarsenMTL | last post by:
Python Users: Based on previous suggestions from this group, I'm attempting to write a python CGI that takes input from an HTML form, invokes a second python script using this input, tells the...
4
by: Tian | last post by:
In Windows, I have been simply using os.system() to run command line program in python. but there will be a black console window. How can I run the program without invoking that window? i guess...
0
by: Prasad | last post by:
We are invoking a SQL DTS component (lets call it Comp1) built by us in another component (Comp2).Comp1 was built by creating the DTS package using the SQL DTS wizard and then saving it as a VB...
1
by: Mathias Dahl | last post by:
I am creating a small app called PyQe (http://klibb.com/cgi-bin/wiki.pl/PyQe) to launch commands and programs quickly. I works more or less as I want it now and I have managed to make my window...
25
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid,...
2
by: shanmani | last post by:
Hi, I am developing a .NET application which will invoke the methods from different COM / .NET DLLs. While invoking methods from .NET DLLs, I am encountering the following error. I have also...
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
4
by: Ant | last post by:
Hi all, Using cygwin and Python 2.5, I have the following scripts, one bash script and the other a python script: -------------------------------------------------------------------------------...
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,...
0
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...
0
tracyyun
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...
0
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,...
0
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...

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.