[Tutor] functions
I am greatly impressed by the most useful instruction I have received since I raised my need for help on this subject. The more I read the more things fall into place. My grateful thanks to one and all and please keep the good stuff flowing. Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] extentions and osX
I have a problem with a c++ extension into a python program. I am trying to run a program written by someone else. It has been written and tested on linux/intel but I don't have access to a linux machine at the moment, so I am attempting to run it on osX (G4). When the program runs I get a segmentation fault. This happens when it reaches the line import _contributionForPixel which has previously been created by setup.py which is as follows: #!/usr/bin/env python # To use: # python setup.py install # import os, sys, string, re from glob import glob import distutils #try: import distutils from distutils.command.install import install from distutils.core import setup, Extension # except: #raise SystemExit, "Distutils problem, see Numeric README." headers = glob (os.path.join ("Include","*.h") ) header = headers + glob (os.path.join ("Include/Numeric","*.h") ) # The version is set in Lib/numeric_version.py setup (name = "extension", description = " Extension to Python", ext_modules = [Extension('_contributionForPixel', ['contribution.cc', 'contribution_wrap.cxx' ] , include_dirs = ['./'] ), ] ) Can someone point me in the right direction? Is it likely there is a problem with the original contribution.cc file (even though setup python build) runs fine, or is there some problem with the way it is linked. I am using python 2.4.1 on osX 10.4 Thanks Tom. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.listdir blocks threads?
Hi, I wrote a quick module for spawning a subprocess and run the function, just as an excercise, after I read your post... It will spawn a subprocess, then pass the serialized return values to the parent. So, if you want to do: lala = os.listdir(".") you do lala = fmg(os.listdir, ".") This will not block, but it will run only on UNIX/Linux, unless there is a version of python that includes support for cygwin, which has fork() for Win32. The module is below: ### """ fmg.py As all threads will block on a call to some os functions, this implements a function for doing thread-safe syscalls/C library functions using a child process. Availability: UNIX """ import os import cPickle def fork_me_gently(myfunct, *myargs): """fork_me_gently(callable, arguments) -> retval Make an arbitrary function pseudo thread-safe by running it in a child process. Return what the passed callable returns.""" #prepare a pipe for child-parent communication r_end, w_end = os.pipe() pid = os.fork() if pid == 0: #child retval = myfunct(*myargs) #Serialize list for sending to pipe #dump() needs a file object, not a fd w_end_fo = os.fdopen(w_end, 'w') cPickle.dump(retval, w_end_fo) w_end_fo.close() # DIE, CHILD! raise SystemExit else: #parent # load() needs a file object r_end_fo = os.fdopen(r_end) retval = cPickle.load(r_end_fo) #clean up after yourself before continuing os.waitpid(pid, 0) return(retval) fmg = fork_me_gently if __name__ == "__main__": #temp = ts_listdir(".") temp = fmg(os.listdir, ".") for i in temp: print i Wolfgang Braun wrote: > Hello List, > > > I try to read large directories off network shares (samba3,NT) with > os.listdir(). The listdir() takes up to 5 minutes and blocks the rest of > the program (gui refreshes, ...) > > The idea now is to put the listdir call into a separate thread so the > program can go on doing stuff (see below). > > Unfortunately, as soon as I start a rdir instance the whole python > process locks up and waits for the os.listdir to return. > > Plan b was to popen('/bin/ls -1 '%dir) in the rdir thread which works > better but is a bit kludgy (needs to work on NT, too) and the program > cannot be shudown cleanly unless the rdir thread has finished. > > > Obviously I'm doing something wrong? What would be the Right Way to > handle this situation? > > > Thanks, > Wolfgang > > > # --- skeleton listdir in a thread > > class rdir(threading.Thread): > def __init__(self,dir,glob=None): > super(rdir,self).__init__() > self.dir=dir > self.content=() > > def start(self): > self.setDaemon(True) > super(dircache,self).start() > > def run(self): > self.content=os.listdir(self.dir) > # alternatively os.popen(' /bin/ls -1U '%dir) > # stuff to keep run() from returning > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Assign to vars by iteration
I've got a bunch of pickled class instances, and I'm trying to load them as variables using a function. The class has a self.name attribute, and I've got a list of self.name for all the instances pickled separately. When I would like to attach the names of each instance to the corresponding class instance (using self.name def loadClassInst(): classInstFile = "filepath" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Please excuse the previous message sent in error
Sincerely, Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2)
On 10/16/05, Danny Yoo <[EMAIL PROTECTED]> wrote: On Sat, 15 Oct 2005, Dave Shea wrote:> Python installed without a complaint. However, when I fire up IDLE,> there is an hour glass shown for a couple of seconds, then nothing else> happens. When I fire up Python (command line) no problem, the DOS box > opens and Python starts. I've tried re-starting, using Explorer instead> of the Start menu, all the usual voodoo but still the IDLE refuses to> start.Hi Dave,According to: http://python.org/2.4.2/bugs.htmlIDLE might not work well if Python has been installed into Program Files.Do you know if you've done this? If so, try reinstalling and just leave the installation path at the defaults (Python should install under"C:\PYTHON24", I think.) yep, danny is right. there always seems to be problems when there is a DOS (C:\PROGRA~1) vs Win32 (C:\Program Files) filenaming issue. i ran into this in previous versions of Python, and have never installed it anywhere else other than C:\Python2x ever again. if it still doesn't work, then i too, would recommend downloading the latest copy of win32all and PythonWin. good luck! -- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"Core Python Programming", Prentice Hall, (c)2006,2001http://corepython.comwesley.j.chun :: wescpy-at-gmail.comcyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hand-holding for web development
You need an Apache config section to tell it what to use mod_python on and how. For example, in httpd.conf or a separate file in your conf.d directory for apache: AddHandler mod_python .py PythonHandler test PythonDebug On tells Apache to use mod_python on all .py files in my /var/www/html directory (which is also the document root on my server). Once that's loaded you should be able to run whatever your python code through a handler. For example, saving this as test.py: # #mod_python super basic test script # from mod_python import apache def handler(req): req.content_type = "text/html" req_file = basename(req.uri) if req.header_only: return apache.OK else: req.write("mod_python is working") return apache.OK # end of script This should print "mod_python is working" to the browser if you visit http://yoursite.com/test.py Remember that you need to restart/reload apache after any config change. The basic gist is that any .py file you load in the /var/www/html runs the script "test.py" (PythonHandler test) and executes the "handler()" function within the script. NOTE: this can be very non-intuitive, becuase running http://yoursite.com/test.py has exactly the same result as http;//yoursite.com/foo.py - because they're both simply running the handler script, test.py, for ALL .py files in the directory. That means that your PythonHandler script needs to dispatch other scripts or load special pages in order to get different code to run. Also note that there are some pre-written Handler scripts that come with mod_python that can often do most of your work for you. You'll probably want a good tutorial on using mod_python to make sense of all this, because it's not really intuitive and it works differently than traditional cgi programming. Take a look at: http://modpython.org/live/current/doc-html/ to get you started, it's a lot more detailed and will probably answer your questions better. -Jay On Thursday 13 October 2005 3:49 pm, nitin chandra wrote: > Hi!... > i am new to Python and i want to develop a website with forms; data > submitted through forms will be stored in PostgreSQL. > I am working on derivant of FC3, OpenLX. > I have Apache 2.0.54 version installed, which is pre-configured with > mod_python. > how do i load and view my .py/.html web page (file) on the browser. > i need the initial hand holding/guidance of how to start, configure , and > develop modules. > thank in advance. > Nitin Chandra ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) (fwd)
[Keeping tutor in CC] -- Forwarded message -- Date: Mon, 17 Oct 2005 22:03:22 +1300 From: Dave Shea <[EMAIL PROTECTED]> To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) Hi Danny, Thanks for your note. I tried your original suggestion of using the C:\Python24 as the home directory but to no avail. So now I am a bit stuck on your and Alan's next suggestion which is to use PythonWin. I downloaded: http://optusnet.dl.sourceforge.net/sourceforge/pywin32/pywin32-204.win32-py2 .4.exe and it installed fine. However, it is listed as "Python for Windows extensions" so I assume that installing this whilst still having Pythin24 installed was the way to go. The installer seemed to be OK about this but when I went to start PythonWin I could not actually find anything to start. I think I may be missing something here. Is PythonWin a separate installation of Python with an IDE/GUI ? Or is PythonWin simply something to sit over the top of a (any) Python installation. I'm a bit lost, as you may tell so any help would be greatly accepted. Many thanks. Dave Shea Wellington New Zealand. - Original Message - From: "Danny Yoo" <[EMAIL PROTECTED]> To: "Dave Shea" <[EMAIL PROTECTED]> Cc: Sent: Monday, October 17, 2005 5:17 PM Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) > > > On Sat, 15 Oct 2005, Dave Shea wrote: > > > Python installed without a complaint. However, when I fire up IDLE, > > there is an hour glass shown for a couple of seconds, then nothing else > > happens. When I fire up Python (command line) no problem, the DOS box > > opens and Python starts. I've tried re-starting, using Explorer instead > > of the Start menu, all the usual voodoo but still the IDLE refuses to > > start. > > Hi Dave, > > According to: > > http://python.org/2.4.2/bugs.html > > IDLE might not work well if Python has been installed into Program Files. > > Do you know if you've done this? If so, try reinstalling and just leave > the installation path at the defaults (Python should install under > "C:\PYTHON24", I think.) > > IDLE has unfortunately been a bit problematic in the Python 2.4 release, > so if you continue to run into issues, I'd second Alan's suggestion about > trying Pythonwin instead. > > > Good luck! > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) (fwd)
dave, you should be able to start PythonWin in one of 2 ways: 1) Start menu -> Programs -> Python 2.4 -> PythonWin or 2) C:\Python24\Lib\site-packages\pythonwin\Pythonwin.exe hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2006,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) (fwd)
I would suggest that you use "add Remove Programs" in the control panel and remove the python and win32 installations that you have installed Then visit www.activestate.com and download the package for activePython (http://activestate.com/Products/Download/Download.plex?id=ActivePython) You may need to download the latest MSI (MS installer) files from microsoft as Win95 does not understand them Activestate has a nice installer and you would be up and running then. cheers On 10/17/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > [Keeping tutor in CC] > > -- Forwarded message -- > Date: Mon, 17 Oct 2005 22:03:22 +1300 > From: Dave Shea <[EMAIL PROTECTED]> > To: Danny Yoo <[EMAIL PROTECTED]> > Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) > > Hi Danny, > > Thanks for your note. I tried your original suggestion of using the > C:\Python24 as the home directory but to no avail. So now I am a bit stuck > on your and Alan's next suggestion which is to use PythonWin. > > I downloaded: > http://optusnet.dl.sourceforge.net/sourceforge/pywin32/pywin32-204.win32-py2 > .4.exe > > and it installed fine. > > However, it is listed as "Python for Windows extensions" so I assume that > installing this whilst still having Pythin24 installed was the way to go. > The installer seemed to be OK about this but when I went to start PythonWin > I could not actually find anything to start. I think I may be missing > something here. Is PythonWin a separate installation of Python with an > IDE/GUI ? Or is PythonWin simply something to sit over the top of a (any) > Python installation. > > I'm a bit lost, as you may tell so any help would be greatly accepted. > > Many thanks. > > Dave Shea > Wellington > New Zealand. > - Original Message - > From: "Danny Yoo" <[EMAIL PROTECTED]> > To: "Dave Shea" <[EMAIL PROTECTED]> > Cc: > Sent: Monday, October 17, 2005 5:17 PM > Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) > > > > > > > > On Sat, 15 Oct 2005, Dave Shea wrote: > > > > > Python installed without a complaint. However, when I fire up IDLE, > > > there is an hour glass shown for a couple of seconds, then nothing else > > > happens. When I fire up Python (command line) no problem, the DOS box > > > opens and Python starts. I've tried re-starting, using Explorer instead > > > of the Start menu, all the usual voodoo but still the IDLE refuses to > > > start. > > > > Hi Dave, > > > > According to: > > > > http://python.org/2.4.2/bugs.html > > > > IDLE might not work well if Python has been installed into Program Files. > > > > Do you know if you've done this? If so, try reinstalling and just leave > > the installation path at the defaults (Python should install under > > "C:\PYTHON24", I think.) > > > > IDLE has unfortunately been a bit problematic in the Python 2.4 release, > > so if you continue to run into issues, I'd second Alan's suggestion about > > trying Pythonwin instead. > > > > > > Good luck! > > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- -- Paul Brian m. 07875 074 534 t. 0208 352 1741 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] multiple assignment
I was fascinated when I learned that I can do this in Python: (str1, str2, str3, rest) = str.split(" ", 3) Later that I realize that str could contain values of less than 4 strings, in which case it would complain something like -- ValueError: unpack list of wrong size. Now I don't want to spoil the fun. In essence, I'd like to capture the first 3 words in a string. If str is less than 3 words then any or all of the containers could have a None value. TIA. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] multiple assignment
On 18/10/05, Vincent Gulinao <[EMAIL PROTECTED]> wrote: > I was fascinated when I learned that I can do this in Python: > > (str1, str2, str3, rest) = str.split(" ", 3) > [...] > In essence, I'd like to capture the first 3 words in a string. If str is > less than 3 words then any or all of the containers could have a None value. I'm not aware of any simple syntax... You could do something with itertools. eg: >>> from itertools import islice, chain, repeat >>> lst = [1, 2, 3] >>> a, b, c, d, e, f, g = islice(chain(lst, repeat(None)), 7) >>> print a, b, c, d, e, f, g 1 2 3 None None None None >>> a, b, c, d = islice(chain(lst, repeat(None)), 4) >>> print a, b, c, d 1 2 3 None >>> a, b = islice(chain(lst, repeat(None)), 2) >>> print a, b 1 2 Not quite as elegant as the simple case, but it's the best I can think of right now... -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor