Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Running external module and accessing the created objects (http://www.velocityreviews.com/forums/t958509-running-external-module-and-accessing-the-created-objects.html)

Kene Meniru 03-09-2013 03:06 AM

Running external module and accessing the created objects
 
Program summary:

I have a module called user.py that imports another module called
app.py. Functions in app.py are used in user.py to describe 3D
objects. These objects are saved in another object described in
doc.py.

app.py contains a function called view(). When called in user.py, it
signals the end of object descriptions. Presently all objects
contained in doc.py are exported to either POV-Ray or OpenSCAD file
format depending on the argument given to view().

My Issues:

I have decided I want to provide a preview of the objects using opengl
(pyglet). So I am trying to create another module called appwin.py
which the user can launch with user.py as an argument. When each
object is described in user.py, I want the user to be able to switch
to appwin.py, provide a signal that makes appwin.py redraw the screen
to show any modifications (perhaps with the enter key).

I do not want to invest much time with appwin.py now as I am still
coding app.py. Right now, appwin.py just subclasses
pyglet.window.Window().

I do not want to merge app.py and appwin.py. I want them to be two
separate applications because I want to retain the option of either
console or many different window interfaces.

The problem then is:

How can I run appwin.py which will then execute user.py to create the
objects to be saved in doc.py. Then when view() is encountered to be
able to access the objects stored in doc.py in appwin.py?

Any ideas will help.

--

Kene
::::::::::::::::::
KeMeniru@gmail.com


Steven D'Aprano 03-09-2013 08:03 AM

Re: Running external module and accessing the created objects
 
On Fri, 08 Mar 2013 22:06:28 -0500, Kene Meniru wrote:

> Program summary:
>
> I have a module called user.py that imports another module called
> app.py. Functions in app.py are used in user.py to describe 3D objects.
> These objects are saved in another object described in doc.py.


What do you mean, "objects are saved in another object"?


> app.py contains a function called view(). When called in user.py, it
> signals the end of object descriptions. Presently all objects contained
> in doc.py are exported to either POV-Ray or OpenSCAD file format
> depending on the argument given to view().
>
> My Issues:
>
> I have decided I want to provide a preview of the objects using opengl
> (pyglet). So I am trying to create another module called appwin.py which
> the user can launch with user.py as an argument.


What happens if the user launches appwin with a different argument?

If appwin can only take one, compulsory, argument, then it's silly to
require it as an argument. Just have appwin automatically import user.py,
and do whatever it needs.


> When each object is
> described in user.py, I want the user to be able to switch to appwin.py,
> provide a signal that makes appwin.py redraw the screen to show any
> modifications (perhaps with the enter key).


This makes no sense to me. Are you saying that appwin opens a text editor
that allows the user to edit the user.py source code?


> I do not want to invest much time with appwin.py now as I am still
> coding app.py. Right now, appwin.py just subclasses
> pyglet.window.Window().
>
> I do not want to merge app.py and appwin.py. I want them to be two
> separate applications because I want to retain the option of either
> console or many different window interfaces.


Making them a single module still retains the option of console or many
different window interfaces.


> The problem then is:
>
> How can I run appwin.py which will then execute user.py to create the
> objects to be saved in doc.py.


I don't know. How does user.py create the objects? Suppose it users a
function called "create". Then you would do this in appwin:


import user
user.create()


> Then when view() is encountered to be
> able to access the objects stored in doc.py in appwin.py?


What do you mean by "view() is encounted"?

How would you access the objects stored in doc.py? Suppose you access
them using a list called "list_of_objects". Then in appwin.py:

import doc
for obj in doc.list_of_objects:
do_something_with(obj)

where you have to write the function "do_something_with", to do whatever
it is you want to do.


By the way, calling a module "doc" which is not for *documentation* is a
bad idea.



--
Steven

Kene Meniru 03-09-2013 11:05 AM

Re: Running external module and accessing the created objects
 
Steven D'Aprano wrote:


> What do you mean, "objects are saved in another object"?
>


doc.py has a dictionary. When the user describes a wall, it is passed
to the doc object to be saved in the dictionary.

>
> What happens if the user launches appwin with a different argument?
>
> If appwin can only take one, compulsory, argument, then it's silly

to
> require it as an argument. Just have appwin automatically import

user.py,
> and do whatever it needs.
>


I guess my description does not come across well.

I thought user.py sounded like it is, a user created file. It will be
named differently for each user to created different types of 3D
objects. It does not make sense to expect every user to name their
design file the same. So there will have to be a differently named
single argument for buildeswin.

>
> This makes no sense to me. Are you saying that appwin opens a text

editor
> that allows the user to edit the user.py source code?
>


No. appwin opens an opengl (pyglet.window.Window()) graphics window as
I mentioned.

>
> I don't know. How does user.py create the objects? Suppose it users

a
> function called "create". Then you would do this in appwin:
>
>
> import user
> user.create()
>


I have tried importing "user.py" and/or "app.py". However there is no
single command to call.

My program is designed to assist in the building design process. It is
a big program and the creation of building components takes quite a
few steps. I do not want to support these steps in a graphics window
which is why the user uses any text editor they prefer to create
"user.py". I want "appwin.py" (which has a graphics window) to be able
to access objects stored in "doc.py" (which has a dictionary) after a
command like "python user.py" so that the objects saved in "doc.py"
after execution can be retrieved and drawn in "appwin.py".

> What do you mean by "view() is encounted"?
>


This is a command that the user can enter in user.py. app.py will then
encounter this command as python parses the file.

> How would you access the objects stored in doc.py? Suppose you

access
> them using a list called "list_of_objects". Then in appwin.py:
>
> import doc
> for obj in doc.list_of_objects:
> do_something_with(obj)
>


I have tried this but there are no objects found in the dictionary in
doc.py. I am guessing that I have to execute appwin.py so that it
shares the same namespace with user.py or maybe a way to access that
namespace. This is the reason for my question.

> where you have to write the function "do_something_with", to do

whatever
> it is you want to do.
>
>
>


--

Kene
::::::::::::::::::
KeMeniru@gmail.com


Steven D'Aprano 03-09-2013 11:34 AM

Re: Running external module and accessing the created objects
 
On Sat, 09 Mar 2013 06:05:52 -0500, Kene Meniru wrote:

> I thought user.py sounded like it is, a user created file.


No. It sounded like a file called literally "user.py".

As for the rest, I shall think about it, and hopefully either I or
someone else will write back later with suggestions.


P.S. your posts have the followup header set to Gmane. Please do not do
that. Many people are reading this via the comp.lang.python newsgroup, or
via email.



--
Steven

Chris Angelico 03-09-2013 11:47 AM

Re: Running external module and accessing the created objects
 
On Sat, Mar 9, 2013 at 10:05 PM, Kene Meniru <Kene.Meniru@illom.org> wrote:
> I have tried importing "user.py" and/or "app.py". However there is no
> single command to call.


I haven't followed the thread in detail, but I gather you're trying to
import a file with a variable name? Instead of 'import user', try:

user = __import__("foobar")

You can then replace the quoted string with whatever you need (note,
leave off the .py extension). In your code, it'll be as if you did:

import foobar as user

but with the flexibility of using whatever run-time-chosen name you need.

ChrisA

Dave Angel 03-09-2013 12:02 PM

Re: Running external module and accessing the created objects
 
On 03/09/2013 06:05 AM, Kene Meniru wrote:
>
> (lots of stuff that was more confusing to me than helpful)


You use the words launch, encountered, execute, and others in ways that
do not make sense to me, or are at least ambiguous.

You have an explicitly named user.py, which apparently is *not*
generally named that.

I could give you lots of random facts and suggestions, and one of the
might hit home. For example the __import__() function can import a
module that you don't know the name of ahead of time. It's not often
the right answer, though, so I hesitate to suggest it.

For another example, if you import a module by two different names, or
if you import the "module" that is your starting script, then you can
end up with two instances of such a module, with all sorts of negative
implications about global data or class attributes stored in that
module, or even more subtle problems.

For a final example, having a circular import tree can cause problems if
any of those imports have any global code (like class initialization,
defining constants, etc.). It's generally much better to define a
strict hierarchy of who imports whom.

But I think instead that it'd be better for you to make a clearer
statement about how your code is structured.

I'm guessing that all of this is intended to be in one executable -- no
child processes, etc. So don't say launch, say import, or
function-call, or whatever you are really doing.

I'm guessing that you're running this on Python 3.3 under Linux.

I'm guessing that "user.py" is one possible name that a particular user
calls his script. And that script is what he runs on the Python
commandline.

python user.py

And that script calls functions in app.py, doc.py, and/or appwin.py.
And that currently, you're trying to import user.py from one of your own
modules, for purposes of either callback functions or global data
access. That's a mistake. When you import your *script* (using
__import__() as suggested above) you get a new copy of the script, and
new copies of all its global data.

I can't even guess how you're intending to mix the commandline stuff of
app.py with gui stuff in one or more appwin.py variants. You'll have to
be explicit, if it even matters yet.

I suggest that rather than responding to these points, you restate your
problem, in one place, with coherent detail that eliminates these
guesses and replaces them with reality. Start with the environment this
is running in, and the commandline typically used to launch the code,
and the interdepencies of the various modules.



--
DaveA

Kene Meniru 03-09-2013 03:34 PM

Re: Running external module and accessing the created objects
 
OK. Sorry to have caused all the confusion. Let me try this again.

To use my program the user needs a script file I will call user.py.
Functions from my program must be imported into this file with
something like "from myapp import *".

myapp.py is a module in my program that has all the functions that the
user needs to describe building components. There is also a main
controller object that is imported into myapp.py module called app.

When python parses user.py module, the functions the user has provided
creates building components which are then saved in a dictionary
located in an object called doc that is part of my program.

The user is free to use the functions in myapp.py to describe building
components. When this process is complete or when the user wants to
view their work-in-progress, they have to place a function called
view() on the last line in user.py. This function currently exports
the components into POV-Ray or OpenSCAD format. These are CAD programs
that read script files to render or create images of the described
artifacts. So this means that to view their work the user needs to run
python on user.py to export the building and then run POV-Ray or
OpenSCAD on the exported file to see the building.

I want to make it possible for the user to preview the building
components without having to use the external rendering programs. I
can currently have the app object provide this preview but it means
that the user will have to process user.py with python then exit the
graphic window each time they need to see changes.

So the solution I am looking for is to have a graphic window open that
watches user.py for changes. If there is a change the graphic window
updates the rendition of the created components without further
intervention by the user. However this means that the graphic must
somehow run python to parse user.py and then be able to access the
objects stored in doc so that the coordinates can be used to update
the view. This is where I am having difficulty.

I hope this is clearer.
--

Kene
::::::::::::::::::
KeMeniru@gmail.com


Dave Angel 03-09-2013 04:12 PM

Re: Running external module and accessing the created objects
 
On 03/09/2013 10:34 AM, Kene Meniru wrote:
> OK. Sorry to have caused all the confusion. Let me try this again.
>


Thank you very much. This is much clearer, though it's not all here.

> To use my program the user needs a script file I will call user.py.
> Functions from my program must be imported into this file with
> something like "from myapp import *".


And does the user run this script by doing
python user.py

>
> myapp.py is a module in my program that has all the functions that the
> user needs to describe building components. There is also a main
> controller object that is imported into myapp.py module called app.
>
> When python parses user.py module,


You presumably mean, "When Python runs the script user.py"

> the functions the user has provided
> creates building components which are then saved in a dictionary
> located in an object called doc that is part of my program.


What program is that? Looks to me like you're writing a series of
modules, a framework perhaps, that is invoked by the user script.

>
> The user is free to use the functions in myapp.py to describe building
> components. When this process is complete or when the user wants to
> view their work-in-progress, they have to place a function called
> view() on the last line in user.py.


Don't you mean the code in user.py has to *call* the function app.view()
when they're all done with defining the components? That has nothing to
do with being the last line.

> This function currently exports


You mean writes files into the file system?

> the components into POV-Ray or OpenSCAD format. These are CAD programs
> that read script files to render or create images of the described
> artifacts. So this means that to view their work the user needs to run
> python on user.py to export the building and then run POV-Ray or
> OpenSCAD on the exported file to see the building.


At that point, the user.py script has completed, and Python is done, right?

>
> I want to make it possible for the user to preview the building
> components without having to use the external rendering programs. I
> can currently have the app object provide this preview but it means
> that the user will have to process user.py with python then exit the
> graphic window each time they need to see changes.


What changes are those? You can't change a script while it's executing.
Could you clarify this so I can rethink the following paragraph?

>
> So the solution I am looking for is to have a graphic window open that
> watches user.py for changes. If there is a change the graphic window
> updates the rendition of the created components without further
> intervention by the user. However this means that the graphic must
> somehow run python to parse user.py and then be able to access the
> objects stored in doc so that the coordinates can be used to update
> the view. This is where I am having difficulty.
>
> I hope this is clearer.
>




--
DaveA

Rick Johnson 03-09-2013 04:51 PM

Re: Running external module and accessing the created objects
 
On Saturday, March 9, 2013 9:34:53 AM UTC-6, Kene Meniru wrote:
> OK. Sorry to have caused all the confusion. Let me try
> this again.


Sounds to me like you should solve this problem in two manners:

================================================== ==========
Interactive Input
================================================== ==========

Create an interactive environment where the user can enter commands directly into the namespace using your API in real time, then when he is ready to see the result of those commands, he can call "display command" or push a "display button" and the App will run the appropriate outside programs to show the visualization.

================================================== ==========
Scripts loaded at runtime
================================================== ==========

If you prefer the user to write "scripts", and then have your App read these scripts (and create visualizations from the commands within the scripts) then the only difference between step 1 and step 2 is *when* the commands are interpreted. In this case your app will *load* the scripts AFTER they are completely written (this could be done automatically by the app at runtime IF the path to these scripts is known, or could be accomplished by the user picking files in a dialog (Menu->RunScript).

I think you are trying to create step 1 without the interactive environment.. This is your mistake. Having users work with "raw files" and then *somehow* watching a "raw file" for certain "display commands" (in real time) is folly.

Heck, when you edit a file in a text editor you are only seeing a representation of the file from the last time it was saved, and your app could neverknow when the "file data" and the "file view" where synchronized; without an asinine amount of foolish programming of course.

If this is not what you want, then i suggest you create a simple representation of your code (or link to the actual code).

Kene Meniru 03-09-2013 04:56 PM

Re: Running external module and accessing the created objects
 
Dave Angel wrote:

> On 03/09/2013 10:34 AM, Kene Meniru wrote:


>> To use my program the user needs a script file I will call user.py.
>> Functions from my program must be imported into this file with
>> something like "from myapp import *".

>
> And does the user run this script by doing
> python user.py
>


Yes

>>
>> myapp.py is a module in my program that has all the functions that the
>> user needs to describe building components. There is also a main
>> controller object that is imported into myapp.py module called app.
>>
>> When python parses user.py module,

>
> You presumably mean, "When Python runs the script user.py"
>


When the user types and enters at the command line: "python user.py"

>> the functions the user has provided
>> creates building components which are then saved in a dictionary
>> located in an object called doc that is part of my program.

>
> What program is that? Looks to me like you're writing a series of
> modules, a framework perhaps, that is invoked by the user script.
>


Yes. I am writing many functions and classes in many modules that work
together.

>>
>> The user is free to use the functions in myapp.py to describe building
>> components. When this process is complete or when the user wants to
>> view their work-in-progress, they have to place a function called
>> view() on the last line in user.py.

>
> Don't you mean the code in user.py has to *call* the function app.view()
> when they're all done with defining the components? That has nothing to
> do with being the last line.


Well... yes. The function is app.view() but it is not called automatically
by the code in user.py. The following is an example of the contents of
user.py

################### begin user.py #############################
from buildes import *

site("Willow_Creek_Ct", 5)
"""Create a site with name and number of boundaries."""
level("level1", 3000)
level("level2", 3000)
"""Create levels with name and height."""
setLevel("level1")
"""Set the current level to place objects"""
space("Dining", 5)
"""Create a space with 5 sides (walls)"""
linearSide("Dining-S1", 3683, 152, 0)
"""Initialize the first side of space called Dining"""
offset("Dining-S1", (3000, 0, 0))
"""Install the first side of Dining space called Dining-S1"""
view(POV)
"""Exports objects to POV-Ray format"""
################### end user.py #############################

>
>> This function currently exports

>
> You mean writes files into the file system?
>


Yes.

>> the components into POV-Ray or OpenSCAD format. These are CAD programs
>> that read script files to render or create images of the described
>> artifacts. So this means that to view their work the user needs to run
>> python on user.py to export the building and then run POV-Ray or
>> OpenSCAD on the exported file to see the building.

>
> At that point, the user.py script has completed, and Python is done,
> right?
>


Yes

>>
>> I want to make it possible for the user to preview the building
>> components without having to use the external rendering programs. I
>> can currently have the app object provide this preview but it means
>> that the user will have to process user.py with python then exit the
>> graphic window each time they need to see changes.

>
> What changes are those? You can't change a script while it's executing.
> Could you clarify this so I can rethink the following paragraph?
>


Working on the user.py is an iterative process. The user adds new objects or
changes the parameters of the objects already there and envokes "python
user.py" each time to see the changes made. Just like using a program like
LaTeX. You edit your text file and run LaTeX on the file to see the changes
you made. The process continues until you finish the document.

>
>
> So the solution I am looking for is to have a graphic window open that
> watches user.py for changes. If there is a change the graphic window
> updates the rendition of the created components without further
> intervention by the user. However this means that the graphic must
> somehow run python to parse user.py and then be able to access the
> objects stored in doc so that the coordinates can be used to update
> the view. This is where I am having difficulty.


--

Kene
::::::::::::::::::
KeMeniru@gmail.com



All times are GMT. The time now is 10:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.