Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exposing object model in Python?

0 views
Skip to first unread message

MK

unread,
Jun 26, 2003, 2:55:30 PM6/26/03
to
Hi all,

What would be the best way to expose the object model
of a Python application?

If I package my app with py2exe, can I ship Python
intepreter and access py2exe-cised app through Python
scripts then?


Aahz

unread,
Jun 26, 2003, 8:55:46 PM6/26/03
to
In article <bdffi1$skho2$1...@ID-174077.news.dfncis.de>, MK <M...@foo.com> wrote:
>
>What would be the best way to expose the object model of a Python
>application?

Depends what you want to do. Are you trying to expose only methods of
objects that you provide, or give full access to Python? Or something
else?

>If I package my app with py2exe, can I ship Python intepreter and
>access py2exe-cised app through Python scripts then?

Basic answer is yes, depending on exactly what libraries you ship.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Usenet is not a democracy. It is a weird cross between an anarchy and a
dictatorship.

Graham Fawcett

unread,
Jun 27, 2003, 12:13:00 AM6/27/03
to
"MK" <M...@foo.com> wrote in message news:<bdffi1$skho2$1...@ID-174077.news.dfncis.de>...

> Hi all,
>
> What would be the best way to expose the object model
> of a Python application?

There's no best way, and there are many very good ones to choose from.
I think the best-fit solution depends much on the specifics of your
problem. Perhaps you could clarify the nature of the application, the
environment in which you intend to deploy it, the intended audience
for the app, and some common use-cases in which you would wish to
write scripts against your model.


> If I package my app with py2exe, can I ship Python
> intepreter and access py2exe-cised app through Python
> scripts then?

The .exe created by py2exe *is* a Python interpreter. The tail-end of
the file is ZIP data containing the .pyc files of your application.
Practically speaking, the files in the tail-end are intended only for
the direct access of the interpreter in the head. So I don't think
py2exe is what you're looking for, unless you intend to run a
py2exe-based server of some kind which your scripts will connect to.

-- Graham

MK

unread,
Jun 27, 2003, 4:35:57 AM6/27/03
to
"MK" <M...@foo.com> wrote

[...]

Thanks for your input. Apologies for not being clear enough.

I'm writing a classical desktop application, using wxPython
and some other libraries. I'm interested to expose its
object model a la Microsoft's VBA. That is, I want to allow
my users to tinker with the app, i.e. write their own macros
in Python in a miniature IDE, within the app. I'd also like to ship
the program as a standalone app, using py2exe.


Steve Holden

unread,
Jun 27, 2003, 9:15:20 AM6/27/03
to
"MK" <M...@foo.com> wrote in message
news:bdgvk5$sid7t$1...@ID-174077.news.dfncis.de...> > [...]

If you can get a look at "Python Programming on Win32" by Mark Hammond and
Andy Robinson that will show you a number of ways to do this, including
writing a COM server in Python and providing Python scripting facilities to
your users.

Unfortunately the techniques used are a little too complicated to describe
in a newsgroup posting.

with-apologies-to-pierre-de-fermat-ly y'rs - steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/

MK

unread,
Jun 27, 2003, 10:00:40 AM6/27/03
to
"Steve Holden" <sho...@holdenweb.com> wrote

> If you can get a look at "Python Programming on Win32" by Mark Hammond and
> Andy Robinson that will show you a number of ways to do this, including
> writing a COM server in Python and providing Python scripting facilities
to
> your users.


Is COM platform-specific? I believe it is, but I may be wrong.
(Anyway I'm sure it's platform specific, no matter what MS says.)
If it is platform-specific, then that's not what I need.
I'd like to expose my object model on every Python-enabled
platform.


Fredrik Lundh

unread,
Jun 27, 2003, 9:53:12 AM6/27/03
to
Steve Holden wrote:

> > I'm writing a classical desktop application, using wxPython
> > and some other libraries. I'm interested to expose its
> > object model a la Microsoft's VBA. That is, I want to allow
> > my users to tinker with the app, i.e. write their own macros
> > in Python in a miniature IDE, within the app. I'd also like to ship
> > the program as a standalone app, using py2exe.

> Unfortunately the techniques used are a little too complicated to describe


> in a newsgroup posting.
>
> with-apologies-to-pierre-de-fermat-ly y'rs - steve

macrosource = getmacro(macroname)
code = compile(macrosource, macroname, "exec")
context = {}
# populate context with fun things
context["app"] = object_representing_my_app
exec code in context

(add exception handling as necessary)

</F>


Brian Kelley

unread,
Jun 27, 2003, 10:37:06 AM6/27/03
to

First thing, if you don't have Python Programming on Win32 you need to
purchase it post haste.

Second thing - the problem you are going to find is having COM talk to
the wxPython application. wxPython when run, expects to be the main
thread of execution which means that it is slightly difficult for COM to
start and talk to wxPython. I have "solved" this problem using the
following methodology.

I

COM Server
py2exe wrapped
using windows registry
to keep track of where
GUI executable is
stored.

^
|
| Com Server Starts
| GUI and connects
| through sockets
|
V
II

GUI is Threaded and
periodically checks
for commands on the
socket.

Note that the COM Server blocks when sending commands. This is mainly
since I don't really understand COM threading. This shouldn't be too
much of a problem though.

Steve Holden

unread,
Jun 27, 2003, 10:47:10 AM6/27/03
to
"Fredrik Lundh" <fre...@pythonware.com> wrote ...

You just *love* proving me wrong, don't you? Lucky for you I'm wrong so
often.

regards

Steve Holden

unread,
Jun 27, 2003, 10:48:42 AM6/27/03
to
"MK" <M...@foo.com> wrote ...

In which case look at the bit about providing scripting facilities (the
Effbot has pointed the way). You're right, COM is platform-specific, despite
MS protestations.

regards

Robert Kern

unread,
Jun 27, 2003, 1:33:49 PM6/27/03
to
In article <bdgvk5$sid7t$1...@id-174077.news.dfncis.de>,

Boudewijn Rempt's book _GUI Programming with Python: QT Edition_ has a chapter
(Chapter 20 to be exact) on this. The concepts should transfer over to wxPython
easily.

http://www.opendocs.org/pyqt/

--
Robert Kern
ke...@caltech.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Fredrik Lundh

unread,
Jun 27, 2003, 12:10:36 PM6/27/03
to
Steve Holden wrote:

> > > Unfortunately the techniques used are a little too complicated to
> > > describe in a newsgroup posting.
> > >
> > > with-apologies-to-pierre-de-fermat-ly y'rs - steve
> >
> > macrosource = getmacro(macroname)
> > code = compile(macrosource, macroname, "exec")
> > context = {}
> > # populate context with fun things
> > context["app"] = object_representing_my_app
> > exec code in context
> >
> > (add exception handling as necessary)
>
> You just *love* proving me wrong, don't you?

Oh, it's just that my news reader has wider margins.

</F>


Jason Whitlark

unread,
Jul 1, 2003, 12:05:19 AM7/1/03
to
"Fredrik Lundh" <fre...@pythonware.com> wrote in message news:<mailman.1056722085...@python.org>...

> macrosource = getmacro(macroname)
> code = compile(macrosource, macroname, "exec")
> context = {}
> # populate context with fun things
> context["app"] = object_representing_my_app
> exec code in context
>
> (add exception handling as necessary)
>
> </F>

Excellent, I was interested in that too.

Ok, here's something similar I've struggled with... How do you
provide a macro recorder? I have some idea how to just play back a
sequence of actions, (just store signals in a list, then fire them in
order), but how do you turn that back into python code, like in Word
VB?

0 new messages