Importing Modules

2010-03-10 Thread PEYMAN ASKARI
Hello

I frequent the PyGtk mailing list all the time, but this is the first time I am 
posting here =)

I wanted to know if imported classes are treated differently than internal 
classes.

I have attached a minimal example which points out what I mean. Essentially, if 
I create a class:

class A():
 __init__(self):
  pass

and call A, or A() I get something to the effect of  <__main__.A instance at 
0xb7f088ac>

but if I call from module_name import A and then call A or A() I get something 
to the effect of  

I was wondering if this would be a problem.

I have included the test_import.py file as well as 4 module files which it 
loads. Save the tes_import.py file to a folder, create another folder there, 
and save ONLY the 4 modules files to that new folder. In the code, update line 
26 to reflect to path of the newly created directory

Thanks in advanced


Peyman

#!/usr/bin/env python

import sys
import os

#This is a dummy class to determine how internal classes behave differenlt from those loaded out of a module
class A:
	def __init__(self):
		pass

def load_modules():
	"""Goes through the directory specified, and attempts to load in all the modules

	   NOTE: Change 'directory' to point to the location of your module_x.py files
	"""
	global simulatorModelPath
	global neuronModelPath
	global synapseModelPath
	global neuronFunctionPath
	global plasticityModelPath
	global connectionFunctionPath
	global environmentTypePath
	global navigationFunctionPath

	#folder containing modules modules_x.py with each module containg the class module_x
	directory='/home/peyman/simulator/sandbox/modules/'

	#add the folder to the path search directory
	sys.path.append(directory)

	#list containing names of all modules to load
	moduleNames=[]

	#list containing calls to __init__ (used for debugging later)
	initializers=[]

	#iterate over every file
	for fileName in os.listdir(directory):
		#only consider python scripts
		if fileName[len(fileName)-3:]=='.py':
			#strip trailing '.py'
			fileName=fileName[:len(fileName)-3]
			
			#add to module list
			moduleNames.append(fileName)

	#iterate over every module name and import it
	for moduleName in moduleNames:
		#import the module
		try:
			command='from %s import %s'%(moduleName,moduleName)
			exec command
		except:
			print 'could not import %s'%moduleName

			#append the initializer to the initializers list
		try:
			command='initializers.append(%s)'%moduleName
			exec command
		except:
			print 'could not append %s'%moduleName

	#lets see what was written to the initializers
	print 'Initializers are represented as module_x.module_x, while the internal class is represented as __main__.A:'
	for initializer in initializers:
		print '\t',initializer,' v ', A

	#lets see if we can create objects out of those initializers and contrast it with class A
	print '\nHere again objects are represented as module_x.module_x, while the internal class is represented as __main__.A:'
	for initializer in initializers:
		obj=initializer()
		objA=A()
		print '\t',obj,' v ',objA


if __name__ == '__main__':
	load_modules()



#!/usr/bin/env python

class module_1:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_2:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_3:
	def __init__(self):
		pass
#!/usr/bin/env python

class module_4:
	def __init__(self):
		pass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a newbie's question

2010-03-11 Thread PEYMAN ASKARI
Hello

I need some help dynamically reloading modules. As it turns out, it is not as 
simple as calling reload, as outlined here
http://pyunit.sourceforge.net/notes/reloading.html

Is there builtin support for this? The example they gave does not seem to work 
for me, and I am unclear as to what PyUnit is.

Thanks


Peyman

--- On Thu, 3/11/10, Lan Qing  wrote:

From: Lan Qing 
Subject: Re: a newbie's question
To: "Python List" 
Received: Thursday, March 11, 2010, 4:56 AM

hi Cheers,     Think you, that helps me a lot.

On Tue, Mar 9, 2010 at 10:00 PM, Simon Brunning  
wrote:

On 9 March 2010 13:51, Lan Qing  wrote:


> Hi all,

>       I'm a newbie of python programming language.



Welcome!



> I have used c/c++ for 5

> years, and one year experience in Lua programming language. Can any one give

> me some advice on learning python. Think you for any help!!



You'll find some useful starting points here -

.



--

Cheers,

Simon B.




-Inline Attachment Follows-

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


Importing modules

2010-03-19 Thread Peyman Askari
I want to write a function which imports modules the first time, and reloads 
them afterwards, but I am running into problems with global variables and exec. 
I will include a full script, but let me elaborate first.

Essentially what you need is

def import_or_reload():
 """assume we want to load or reload sys"""
 if 'sys' in dir():
  reload(sys)
else: 
  import sys

but this runs into the problem that sys is imported within the local scope of 
the function, so you insert a global statement



def import_or_reload2():

 """Add 'global sys'"""
 global sys

 if 'sys' in dir():

  reload(sys)

else: 

  import sys

'sys' is still not in dir() as dir() pertains to the local scope of the 
function, but one can get around this by creating a local modules list and 
adding the imported modules to it





def import_or_reload3():


 """Add 'global modules'"""

 global sys
 global modules



 if 'sys' in modules:


  reload(sys)


else: 


  import sys
  modules.append('sys')

now lets add a parameter to the function signature, so any module name can be 
passed as an argument and loaded





def import_or_reload4(module_name):



 """Add exec"""
 

exec 'global %s'%module_name

 global modules





 if module_name in modules:



  exec 'reload(%s)'%module_name



else: 



  exec 'import %s'%module_name

  exec 'modules.append(\'%s\')'%module_name

but this doesn't work as global does not cooperate with exec
is there a __reload__('X') function like there is an __import__(‘X’) function? 

Also is there a better way to import modules at run time?

Cheers and here is the test script in case you can't access the attachment

def a():
    global modules
    global sys
    import sys

    modules.append('sys')

def b():
    global modules
    global sys

    reload(sys)

def c(module_name):
    global modules
    exec 'global %s'%module_name
    exec 'import %s'%module_name

    modules.append(module_name)

def test():
    global modules
    global sys

    #create the module list to contain all the modules
    modules=[]

    print 'originally dir() returns:'
    print dir()

    a()
    print 'function a() properly imports the following module:'
    print sys
    print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
    
    b()
    print 'function b() properly reloads the following module:'
    print sys
    print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)

    try:
        c('os')
        print 'function c() properly imports the following module:'
    except:
        print 'function c() failed to import module os'
        print 'is %s in %s->%s'%('os',modules,'os' in modules)

    try:
        print os
        print 'is %s still in %s->%s'%('os',modules,'os' in modules)
    except:
        print 'os was loaded, but is not visible outside of the scope of c()'
--- On Fri, 3/19/10, python-list-requ...@python.org 
 wrote:

From: python-list-requ...@python.org 
Subject: Python-list Digest, Vol 78, Issue 192
To: python-list@python.org
Received: Friday, March 19, 2010, 7:05 AM

Send Python-list mailing list submissions to
    python-list@python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
    python-list-requ...@python.org

You can reach the person managing the list at
    python-list-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."
Today's Topics:

   1. Re: import antigravity (John Bokma)
   2. Re: Truoble With A Search Script for ImageBin.org
      (Cameron Simpson)
   3. example of ssl with SimpleXMLRPCServer in 2.6? (Rowland Smith)
   4. Re: multiprocessing on freebsd (Tim Arnold)
   5. Re: Python bindings tutorial (Tim Roberts)
   6. Re: sqlite3, memory db and multithreading (John Nagle)
   7. Re: GC is very expensive: am I doing something wrong?
      (Patrick Maupin)
   8. Bug in Python APscheduler module. (anand jeyahar)
   9. Re: Bug in Python APscheduler module. (Terry Reedy)
  10. Re: Python bindings tutorial (Alf P. Steinbach)
-- 
http://mail.python.org/mailman/listinfo/python-list


  __
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/
def a():
	global modules
	global sys
	import sys

	modules.append('sys')

def b():
	global modules
	global sys

	reload(sys)

def c(module_name):
	global modules
	exec 'global %s'%module_name
	exec 'import %s'%module_name

	modules.append(module_name)

def test():
	global modules
	global sys

	#create the module list to contain all the modules
	modules=[]

	print 'originally dir() returns:'
	print dir()

	a()
	print 'function a() properly imports the following module:'
	print sys
	print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
	
	b()
	print 'function b() properly reloads the following module:'
	print sys
	print 'is %s still in %s->%s'%('s

Python Line Intersection

2010-04-09 Thread Peyman Askari
Hello

This is partly Python related, although it might end up being more math related.

I am using PyGTK (GUI builder for Python) and I need to find the intersection 
point for two lines. It is easy to do, even if you only have the four points 
describing line segments 
(http://www.maths.abdn.ac.uk/~igc/tch/eg1006/notes/node23.html). However, it 
requires that you solve for two equations. How can I do this in Python, either 
solve equations, or calculating intersection points some other way?

Cheers


Peyman Askari




  __
Connect with friends from any web browser - no download required. Try the new 
Yahoo! Canada Messenger for the Web BETA at 
http://ca.messenger.yahoo.com/webmessengerpromo.php-- 
http://mail.python.org/mailman/listinfo/python-list


long int OverflowException

2010-04-28 Thread Peyman Askari
I am seriously stumped by this one problem

I essentially have two functions like so

def a():
 try:
  b()
 except:
  print sys.exc_info()[0]
  print sys.exc_info()[1]
  print sys.exc_info()[2]
 
def b():
 print x.__class__.__name__
 return x 


the print statement in b() says it is an int, but the exception raised in a() 
claims the long int causes an overflow when trying to cast it to int (I am 
running Python 2.5)

I can't provide the full code as it is part of a larger code, and I can't 
decouple it. What I don't understand is why a return value that is clearly an 
int, is being converted into a long int, and then leading to an exception.

Cheers


Peyman Askari




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


PyMPI comm.gather problem

2010-05-11 Thread Peyman Askari
Hi

I have run into a serious problem with PyMPI (Python bindings for the Message 
Passing Interface). Unfortunately I can not provide any example code as it is a 
massive program (38,000+ lines) and it is very difficult to break the program 
down due to multiple inheritance.

When I run the program (on multiple nodes), there is a linear increase in time 
per iteration. Every 1,500 iterations, it gains a tenth of a second per 
iteration. I timed different regions of my code which were engaged in messaging 
(broadcasting and gathering) and I found the increase to be in a gather 
statement, much like so

IO_list=self.comm.gather(IO,root=0)

each iteration this single command requires more and more time to pass 
information. I checked the size of IO being sent from every node (including 
root node) and it always remains constant. I also checked the size of the 
IO_list, and it too is always constant. Furthermore, I added a sleep(5) command 
before the line to make sure it was not a synchronization issue. 

Has anyone got any possible ideas as to what could possibly be going wrong? 

Cheers


Peyman Askari



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