Modules and Namespaces

2005-10-20 Thread Jelle Feringa / EZCT Architecture Design Research
##I'm sorry to stir up such a well discussed topic yet again, but namespaces
are a point of confusion to me...

I took the effort of organizing my Python code (scripting a cad program
calles Rhino) in well defined classes, which would be a terrific thing if I
didn't got stuck in namespace issues.

I have a module that launches the application I'm scripting via win32com;
rhino.load

from rhino import load
RS = load.RS

So the application, with all its methods are now available through the RS
(RhinoScript) object

from rhino import SRF # is where things get stuck

The RS object is the application scripted via COM, where all its method
reside.
In my module, SRF, I'm not importing anything, though it refers to the RS
object all the time.
Such as:

class srfBase:
'''Base class inherited by the srf* classes, binding general Rhino
surface functionality to a particular
surface generation method'''
def __init__(self):
self.id   = 'self.id srfBase'
pass
def isBrep(self):
return RS.IsBrep(self.id)
def isPointInSurface(self, coord):
return RS.IsPointInSurface(self.id, coord)


How do I make the RS object available to the imported SRF module, such that
my module code and program code both refer to RS as the application object
being scripted?

Cheers,

Jelle.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Modules and Namespaces

2005-10-20 Thread Steve Holden
Jelle Feringa / EZCT Architecture  Design Research wrote:
 ##I'm sorry to stir up such a well discussed topic yet again, but namespaces
 are a point of confusion to me...
 
 I took the effort of organizing my Python code (scripting a cad program
 calles Rhino) in well defined classes, which would be a terrific thing if I
 didn't got stuck in namespace issues.
 
 I have a module that launches the application I'm scripting via win32com;
 rhino.load
 
 from rhino import load
 RS = load.RS
 
 So the application, with all its methods are now available through the RS
 (RhinoScript) object
 
 from rhino import SRF # is where things get stuck
 
 The RS object is the application scripted via COM, where all its method
 reside.
 In my module, SRF, I'm not importing anything, though it refers to the RS
 object all the time.
 Such as:
 
 class srfBase:
 '''Base class inherited by the srf* classes, binding general Rhino
 surface functionality to a particular
 surface generation method'''
 def __init__(self):
 self.id   = 'self.id srfBase'
 pass
 def isBrep(self):
 return RS.IsBrep(self.id)
 def isPointInSurface(self, coord):
 return RS.IsPointInSurface(self.id, coord)
 
 
 How do I make the RS object available to the imported SRF module, such that
 my module code and program code both refer to RS as the application object
 being scripted?
 
One relatively simple way (though not a perfect solution) is to provide 
a function in the SRF module that allows the caller to provide a 
reference to the RS object, which is then stored as a module global. 
Similar to this:

# Somewhere in SRF's code

RS = None   # This RS is global to the SRF module

def RSreg(rs):
 global RS  # Ensures it isn't treated as function-local
 RS = rs

Then all you have to do is

from rhino import load
RS = load.RS

from rhino import SRF
SRF.RSreg(RS)

and everything inside SRF can refer to RS as a module global quite 
happily. Note that this falls over horribly if you ever want to handle 
several RS objects at the same time. In that case you might be better 
explicitly passing RS references into each function that uses them.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Modules and Namespaces

2005-10-20 Thread Larry Bates
One way is to pass the RS object when you instantiate
an instance of srfBase, something like:

 class srfBase:
 '''Base class inherited by the srf* classes, binding general Rhino
 surface functionality to a particular
 surface generation method'''
 def __init__(self, RS):
 self.id   = 'self.id srfBase'
 self.RS=RS
 return
 def isBrep(self):
 return self.RS.IsBrep(self.id)
 def isPointInSurface(self, coord):
 return self.RS.IsPointInSurface(self.id, coord)

This is how most of wxWindows seems to do things.

-Larry Bates

Jelle Feringa / EZCT Architecture  Design Research wrote:
 ##I'm sorry to stir up such a well discussed topic yet again, but namespaces
 are a point of confusion to me...
 
 I took the effort of organizing my Python code (scripting a cad program
 calles Rhino) in well defined classes, which would be a terrific thing if I
 didn't got stuck in namespace issues.
 
 I have a module that launches the application I'm scripting via win32com;
 rhino.load
 
 from rhino import load
 RS = load.RS
 
 So the application, with all its methods are now available through the RS
 (RhinoScript) object
 
 from rhino import SRF # is where things get stuck
 
 The RS object is the application scripted via COM, where all its method
 reside.
 In my module, SRF, I'm not importing anything, though it refers to the RS
 object all the time.
 Such as:
 
 class srfBase:
 '''Base class inherited by the srf* classes, binding general Rhino
 surface functionality to a particular
 surface generation method'''
 def __init__(self):
 self.id   = 'self.id srfBase'
 pass
 def isBrep(self):
 return RS.IsBrep(self.id)
 def isPointInSurface(self, coord):
 return RS.IsPointInSurface(self.id, coord)
 
 
 How do I make the RS object available to the imported SRF module, such that
 my module code and program code both refer to RS as the application object
 being scripted?
 
 Cheers,
 
 Jelle.
 
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Modules and Namespaces

2005-10-20 Thread jelle
Dear Steve  Larry,

Both your methods worked flawless, thanks to both of you!
I have to say Larry's way wins on style points, doens't it?
What an awefull thing to get stuck on something that simple, what a
gorgeous solution, thanks so much!

-Jelle

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Modules and Namespaces

2005-10-20 Thread jelle
Ooops, Larry, forgive me being to overhauled here:
Actually self.RS = RS does not make the RS object available in the
module, Steve's method does however.

-Jelle

-- 
http://mail.python.org/mailman/listinfo/python-list


modules and namespaces

2005-04-19 Thread Mage
   Hello,

I thought that this will work:

#m1.py
def f1():
return string.join('a','a')

#m2.py
def f2():
return string.join('b','b')

#main.py
import string
import m1
import m2

print f1()
print f2()

-

However it doesn't work until I import the string module into m1 and m2
modules. I found in the manual that imported modules will be searched in
the container module first. Is it more efficient to import the string
module into main and m1 and m2 than importing only into m1 and m2?

   Mage


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: modules and namespaces

2005-04-19 Thread Laszlo Zsolt Nagy

However it doesn't work until I import the string module into m1 and m2
modules. I found in the manual that imported modules will be searched in
the container module first. Is it more efficient to import the string
module into main and m1 and m2 than importing only into m1 and m2?
 

I bet the most efficient is
str.join( ('a','b'))
The reason is that 'str' is a built-in type. But since new style classes 
were introduced, they are also real objects with methods. :-)

p.s.: Hello Mage. I'm also known as nagylzs at enternet dot hu. Do you 
remember me from the SQL list? Good to see you here. :-)

--
_
 Laszlo Nagy  web: http://designasign.biz
 IT Consultantmail: [EMAIL PROTECTED]
Python forever!
--
http://mail.python.org/mailman/listinfo/python-list


Re: modules and namespaces

2005-04-19 Thread Jaime Wyant
Each module has its own namespace, which is like a dictionary of
objects that the module can see.  I use the term dicitionary because
locals() and globals() both return dictionaries -- someone may correct
me on this (or confirm what I say)...

You have local and global variables.

Locals are variables in the scope of a function.

def myfun():
localvar = 1

`globals' are really 'module' global only.

# mymodule
global_var = 3

def myfun()
print global_var

# This is a *gotcha* -- you can't change global variables this way.
# here, a new local variable global_var is initialized.
global_var = 3

def changeglobal():
# you have to use `global' to instruct python to use the `global'
instance of the variable
# instead of creating a new one when you assign to it.
global global_var 
global_var = 3

You can only see variables you've created or modules you've imported. 
Becase you haven't imported string in m2 or m3, you can't see them.

hth,
jw

On 4/19/05, Mage [EMAIL PROTECTED] wrote:
Hello,
 
 I thought that this will work:
 
 #m1.py
 def f1():
 return string.join('a','a')
 
 #m2.py
 def f2():
 return string.join('b','b')
 
 #main.py
 import string
 import m1
 import m2
 
 print f1()
 print f2()
 
 -
 
 However it doesn't work until I import the string module into m1 and m2
 modules. I found in the manual that imported modules will be searched in
 the container module first. Is it more efficient to import the string
 module into main and m1 and m2 than importing only into m1 and m2?
 
Mage
 
 --
 http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list


Re: modules and namespaces

2005-04-19 Thread Berthold Hllmann
Laszlo Zsolt Nagy [EMAIL PROTECTED] writes:

However it doesn't work until I import the string module into m1 and m2
modules. I found in the manual that imported modules will be searched in
the container module first. Is it more efficient to import the string
module into main and m1 and m2 than importing only into m1 and m2?


 I bet the most efficient is

But is is not working.

 str.join( ('a','b'))

Python 2.4.1 (#1, Mar 31 2005, 09:19:04) 
[GCC 3.2.2] on linux2
Type help, copyright, credits or license for more information.
 str.join(('a','b'))
Traceback (most recent call last):
  File stdin, line 1, in ?
TypeError: descriptor 'join' requires a 'str' object but received a 'tuple'
 str.join('a','bcdefg')
'bacadaeafag'
 str().join(('a','b'))
'ab'

Kind regards
Berthold
-- 
[EMAIL PROTECTED]  __   Address:
G /  \ L Germanischer Lloyd
phone: +49-40-36149-7374-++- Vorsetzen 35   P.O.Box 111606
fax  : +49-40-36149-7320  \__/   D-20459 HamburgD-20416 Hamburg
-- 
http://mail.python.org/mailman/listinfo/python-list