Re: [Tutor] how to instantiate a class
Le Thu, 26 Feb 2009 12:38:27 +0530, Abhishek Kumar abhishek.l...@gmail.com s'exprima ainsi: hello list, Below is the sample code of a class. import someStandardModule Class ABC: def __init__(self,a,b,c): statement 1 statement 2 def func(x,y): statement 1 statement 2 Here is the question: how to make an object of this class abc = ABC(1,2,3) and how to use the methods listed in the class. abc.func(foo,bar) do i need to create the object in a separate file or in the same file it can be done ?? Both are possible. If not in the same file, you must first import the file (module) where ABC is defined, then ABC will behave like an attribute of the module; or just the name ABC from this file, in which case ABC will be locally know: import ABCFile abc = ABCFile.ABC(1,2,3) or from ABCFile import ABC abc = ABC(1,2,3) denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] format a file
hello I find it difficult to use horizontal scroll bar to read text documents. So I want to split lines in the text file in to two lines. code def myforrmat(source,desty): so=open(source) de=open(desty,'w') for line in so: ? if len(line)60:de.write(line) ? if len(line)60: de.write(''.join(list(line)[:60])) de.write(''.join(list(line)[60:])) so.close() de.close() /code This code is not working.Destination file have the same length of lines to source file Some one please show where the problem is. I tried to do it using fileinput module and inplace flag. in vain. please some one show me how to do it. Thank you Prasad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] format a file
Try thinking what happens when you do this: line = 'this is a rellly long line\n' first = line[:20] second = line[20:] print first print second . . . . . . . . . . Have you got it? The first would contain this is a re and the second llly long line\n When you try to write to the file: de.write(first) de.write(second) why isn't there a new line between the first and second? Also, I think you should realize that with how you do it now, you may put line breaks in between words. Better to split on space line.split(' ') then ' '.join(first) + '\n' it together again. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Linear Algebra weirdness
I am getting some very strange behaviour from the Linear Algebra module. Look at this code: from LinearAlgebra import * a=5 print a And look at the output that I get when I run it: 50.0 0.0 0.25 0.0 0.5 0.0 0.75 0.0 1.0 [[ 2.5000e-01 -2.5000e-01 -4.3750e-01 ..., 3.73459082e+01 3.75116710e+01 3.76764961e+01] [ 5.e-01 -5.e-01 -6.2500e-06 ..., -4.3797e-01 -2.49373966e-03 4.3766e-01] [ 7.5000e-01 -7.5000e-01 0.e+00 ..., -7.5000e-01 0.e+00 7.5000e-01] [ 1.e+00 -1.e+00 0.e+00 ..., -1.e+00 0.e+00 1.e+00]] 5 Apparently Linear Algebra is giving me the output of another program along with the output for this one. Of course I don't need Linear Algebra to print the number 5, but I've been trying to make a program that uses Linear Algebra, and I'm always having the output come out twice each time I run it. When I edit the program, it always gives me the old output before the new output, even if I edit it right down to something as simple as a=5, print a. Is anybody familiar with this module and why it is doing this? I've used it before and never had this issue. -Gerard. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Convert a string of numbers to a list
Hi, I am trying to convert an output from subprocess.Popen() from a byte string to a list of numbers. This is what Popen returns: print SAL.stdout.readlines() ['[335, 180, 201, 241, 199]\r\n'] Except the string will be much longer in practice. I've experimented with .rstrip and .split and I've looked over the csv module but have not been able to figure anything out. My only restriction is to avoid writing to a file. I have a feeling I'm missing something incredibly simple. Any help would be greatly appreciated, kbk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] format a file
On Thu, Feb 26, 2009 at 4:09 AM, prasad rao prasadarao...@gmail.com wrote: hello I find it difficult to use horizontal scroll bar to read text documents. So I want to split lines in the text file in to two lines. code def myforrmat(source,desty): so=open(source) de=open(desty,'w') for line in so: ? if len(line)60:de.write(line) ? if len(line)60: de.write(''.join(list(line)[:60])) de.write(''.join(list(line)[60:])) You don't have to convert to list, strings are sequences and support slicing directly. As Lie notes, you have to write a newline between the lines! de.write(line[:60]) de.write('\n') de.write(line[60:]) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] learning new features of python
Dear Friends, Is there any website/tutorial that explains new features that are constantly getting embedded in python. This would be helpful for python lovers to grow with python and adopt new styles of codes. Just for an example, i read in Mark Luts learning python book, the various forms of except statements except name except name, value -- present style except name as value -- expected to be available in python 3.0 Similaraly in Alan Guald Learn to program link, he has given information on opening a file with file() and open() functions. Thanks, Bala ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert a string of numbers to a list
kkwai...@umich.edu wrote: Hi, I am trying to convert an output from subprocess.Popen() from a byte string to a list of numbers. This is what Popen returns: print SAL.stdout.readlines() ['[335, 180, 201, 241, 199]\r\n'] Except the string will be much longer in practice. I've experimented with .rstrip and .split and I've looked over the csv module but have not been able to figure anything out. My only restriction is to avoid writing to a file. Don't know about the csv module, but if you want to do it in plain Python: readlines() returns a list of lines, so do the following for each line in the list: 1. chop off the [ and ]\r\n 2. split on , 3. convert to integer values by means of int() I have a feeling I'm missing something incredibly simple. Any help would be greatly appreciated, Please post what you have, and what you are stuck on, then we can guide you through the problem. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Linear Algebra weirdness
On Thu, Feb 26, 2009 at 12:32 AM, Mr Gerard Kelly gerard.ke...@uqconnect.edu.au wrote: I am getting some very strange behaviour from the Linear Algebra module. Look at this code: from LinearAlgebra import * a=5 print a And look at the output that I get when I run it: 50.0 0.0 0.25 0.0 0.5 0.0 0.75 0.0 1.0 [[ 2.5000e-01 -2.5000e-01 -4.3750e-01 ..., 3.73459082e+01 3.75116710e+01 3.76764961e+01] [ 5.e-01 -5.e-01 -6.2500e-06 ..., -4.3797e-01 -2.49373966e-03 4.3766e-01] [ 7.5000e-01 -7.5000e-01 0.e+00 ..., -7.5000e-01 0.e+00 7.5000e-01] [ 1.e+00 -1.e+00 0.e+00 ..., -1.e+00 0.e+00 1.e+00]] 5 Apparently Linear Algebra is giving me the output of another program along with the output for this one. Of course I don't need Linear Algebra to print the number 5, but I've been trying to make a program that uses Linear Algebra, and I'm always having the output come out twice each time I run it. When I edit the program, it always gives me the old output before the new output, even if I edit it right down to something as simple as a=5, print a. Is this the LinearAlgebra package from Numeric? It's helpful if you don't assume we know what you are talking about :-) If so, shouldn't it be something like from Numeric.LinearAlgebra import * ? Anyway my guess is that you have another program called LinearAlgebra.py in your python path. *That* program probably has the correct import, but it also does some calculations and prints the result. Try this: import LinearAlgebra print LinearAlgebra.__file__ to find out what you are really importing. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert a string of numbers to a list
On Thu, Feb 26, 2009 at 2:00 AM, kkwai...@umich.edu wrote: Hi, I am trying to convert an output from subprocess.Popen() from a byte string to a list of numbers. This is what Popen returns: print SAL.stdout.readlines() ['[335, 180, 201, 241, 199]\r\n'] For one line: In [11]: s = '[335, 180, 201, 241, 199]\r\n' In [12]: map(int, s.strip('[]\r\n').split(', ')) Out[12]: [335, 180, 201, 241, 199] Modify appropriately to handle a list of lines. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
On Thu, Feb 26, 2009 at 6:28 AM, Bala subramanian bala.biophys...@gmail.com wrote: Dear Friends, Is there any website/tutorial that explains new features that are constantly getting embedded in python. Every new release comes with a What's New in Python xx document. This is often the best, and sometimes the only, available documentation for new features. For example: http://docs.python.org/whatsnew/2.6.html http://docs.python.org/3.0/whatsnew/3.0.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Linear Algebra weirdness
Hi, I didn't get nearly as far as you. In [2]: from LinearAlgebra import * --- ImportError Traceback (most recent call last) /home/bermanrl/ipython console in module() ImportError: No module named LinearAlgebra In [3]: I am running Ubuntu Linux. What OS are yiou running and what version of Python? Robert Berman Mr Gerard Kelly wrote: I am getting some very strange behaviour from the Linear Algebra module. Look at this code: from LinearAlgebra import * a=5 print a And look at the output that I get when I run it: 50.0 0.0 0.25 0.0 0.5 0.0 0.75 0.0 1.0 [[ 2.5000e-01 -2.5000e-01 -4.3750e-01 ..., 3.73459082e+01 3.75116710e+01 3.76764961e+01] [ 5.e-01 -5.e-01 -6.2500e-06 ..., -4.3797e-01 -2.49373966e-03 4.3766e-01] [ 7.5000e-01 -7.5000e-01 0.e+00 ..., -7.5000e-01 0.e+00 7.5000e-01] [ 1.e+00 -1.e+00 0.e+00 ..., -1.e+00 0.e+00 1.e+00]] 5 Apparently Linear Algebra is giving me the output of another program along with the output for this one. Of course I don't need Linear Algebra to print the number 5, but I've been trying to make a program that uses Linear Algebra, and I'm always having the output come out twice each time I run it. When I edit the program, it always gives me the old output before the new output, even if I edit it right down to something as simple as "a=5, print a". Is anybody familiar with this module and why it is doing this? I've used it before and never had this issue. -Gerard. ___ 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] re.format a file
helloThank you Lie and Kent. I forgot about newline character and the fact that string can be sliced. Thanks for your timely help BTW I have gone through the Python library reference and find no examples in fileinput module. z=fileinput.input(file,inplace=1) for line in z: ???if len(line)60:pass ???if len(line)60: ??line=line[:60]+'\n'+line[60:] Is it the right way to do? Thank you Prasad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re.format a file
prasad rao wrote: helloThank you Lie and Kent. I forgot about newline character and the fact that string can be sliced. Thanks for your timely help BTW I have gone through the Python library reference and find no examples in fileinput module. The fileinput module only deals with reading and writing data from/to files, it does not deal with manipulating that data. How to manipulate strings is in the 'strings' or 'text' section of a tutorial. z=fileinput.input(file,inplace=1) for line in z: ???if len(line)60:pass ???if len(line)60: ??line=line[:60]+'\n'+line[60:] Is it the right way to do? A nice step forward, I'd say. Did you consider what to do with ridiculous long lines, eg 200, 500, 1000 or 1 characters long? If you want to deal with them, you'd need to repeatedly split the line. You could use a while loop for it. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re.format a file
On Thu, Feb 26, 2009 at 8:43 AM, prasad rao prasadarao...@gmail.com wrote: hello Thank you Lie and Kent. I forgot about newline character and the fact that string can be sliced. Thanks for your timely help BTW I have gone through the Python library reference and find no examples in fileinput module. z=fileinput.input(file,inplace=1) for line in z: ???if len(line)60:pass No need for the above line, it does nothing. Kent ???if len(line)60: ??line=line[:60]+'\n'+line[60:] Is it the right way to do? Thank you Prasad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
Bala subramanian bala.biophys...@gmail.com wrote Is there any website/tutorial that explains new features that are constantly getting embedded in python. The documentation for each release has a whats new document that is always worth reading. Similaraly in Alan Guald Learn to program link, he has given information on opening a file with file() and open() functions. And in V3 they took that back out again :-( Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
On Thu, Feb 26, 2009 at 10:35 AM, Alan Gauld alan.ga...@btinternet.com wrote: Bala subramanian bala.biophys...@gmail.com wrote Similaraly in Alan Guald Learn to program link, he has given information on opening a file with file() and open() functions. And in V3 they took that back out again :-( ?? open() is in V3. file() is not. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cgi script to start another process in background
Ravi Kondamuru wrote: Hi, I am trying to write a python cgi script, that invokes another process and exists. Using the subprocess documentation on NO_WAIT, I am not having much success: pid = os.spawnlp(os.P_NOWAIT, /bin/mycmd, mycmd, myarg) == pid = Popen([/bin/mycmd, myarg]).pid The script seems to wait for the new process to exit before returning to the user. I tried doing the double-fork approach discussed here: http://code.activestate.com/recipes/66012/ Are you on some kind of Unix box here? The fork approach should definitely work for you. An even simpler example, which should still work for you, is here: import os , time , sys print 'hello before the fork' if os.fork(): print 'parent exiting' sys.exit() print 'hello from child: %d' % os.getpid() time.sleep(30) os._exit(0) You should be able to verify that you have that process still running after the main process exits. -- Jay Deiman \033:wq! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class definition...
I am looking for a good tutorial to walk through that really explains class definition. This has been one sticking point that always messes me up for the most part. That and when people use self. For some reason I just can't grasp what people say. Any good pointers to throw at me? I really want to get this down so I can move forward in Python. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
Similaraly in Alan Guald Learn to program link, he has given information on opening a file with file() and open() functions. And in V3 they took that back out again :-( ?? open() is in V3. file() is not. That's right, they have reverted to how it was in Python 1.X I changed all my pages to use file() in v2.2 then changed them to use a mixtyuure of file() and open() in 2.4 and now I'm changing them all back to open() for v3! :-( Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class definition...
Spencer Parker inthefri...@gmail.com wrote I am looking for a good tutorial to walk through that really explains class definition. This has been one sticking point that always messes me up I assume from that you have been through the basic tutors like mine? Have you tried the deeper material in Dive into Python and the official tutorial? If so then it is probnably better for you to give us some specific questions you have. Or post a bit of a tutorialyou don't understand and we can collectively try to clarify it. Specific questions are always easier to answer than generalities. the most part. That and when people use self. For some reason I just can't grasp what people say. Any good pointers to throw at me? OK, I explain self in my OOP tutor topic ( a sub heading under Using Classes), but again if thats not sufficient then you probably need to give us explicit examples of what you don't understand and your concerns. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
Le Thu, 26 Feb 2009 18:16:59 + (GMT), ALAN GAULD alan.ga...@btinternet.com s'exprima ainsi: Similaraly in Alan Guald Learn to program link, he has given information on opening a file with file() and open() functions. And in V3 they took that back out again :-( ?? open() is in V3. file() is not. That's right, they have reverted to how it was in Python 1.X I changed all my pages to use file() in v2.2 then changed them to use a mixtyuure of file() and open() in 2.4 and now I'm changing them all back to open() for v3! :-( Alan G. Does someone know why this choice has been made? After all, the result is a file object, so imo it's consistent to use file() like for any other type. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter Grid, Listbox expand
Hi, I'm writing a Tkinter program and having severe problems with getting my listbox to expand when I resize the window. Here's my code so far: 1 from Tkinter import * 2 import os, shutil, tkFileDialog, tkMessageBox 3 4 class TkSync: 5 def __init__(self, root): 6 self.sbox = Listbox(root) 7 self.sbox.grid(row=0, column=0, sticky=N+S+E+W) 8 root.grid_rowconfigure(0, minsize=400) 9 root.grid_columnconfigure(0, minsize=400) 10 11 root = Tk() 12 TkSync(root) 13 root.mainloop() When I run that, it sizes right to start out... but then it won't get any bigger. I've tried various different combinations and nothing has worked quite right. TIA, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cgi script to start another process in background
I am running this on a freebsd 4.9 system running apache server. I started with code that was as simple as yours. But the problem is apache does not print hello before the fork until the child exists.I have read at various forums that apache buffers till the script exists and in this case till the child exists and then the parent itself. So hello before the fork is printed after 30 secs on the browser. This approach I believe works when executed from the shell but not in the apache context. I finally got the script working after using the following recipe to make the process a daemon. http://code.activestate.com/recipes/278731/ In place of os.fork() in the script I called createDaemon mention from the module in the recipe and that did the trick. I had to convert the os._exit() to sys.exit() in the createDaemon function otherwise the was giving Premature end of script headers error. thanks, Ravi. On Thu, Feb 26, 2009 at 9:09 AM, Jay Deiman j...@splitstreams.com wrote: Ravi Kondamuru wrote: Hi, I am trying to write a python cgi script, that invokes another process and exists. Using the subprocess documentation on NO_WAIT, I am not having much success: pid = os.spawnlp(os.P_NOWAIT, /bin/mycmd, mycmd, myarg) == pid = Popen([/bin/mycmd, myarg]).pid The script seems to wait for the new process to exit before returning to the user. I tried doing the double-fork approach discussed here: http://code.activestate.com/recipes/66012/ Are you on some kind of Unix box here? The fork approach should definitely work for you. An even simpler example, which should still work for you, is here: import os , time , sys print 'hello before the fork' if os.fork(): print 'parent exiting' sys.exit() print 'hello from child: %d' % os.getpid() time.sleep(30) os._exit(0) You should be able to verify that you have that process still running after the main process exits. -- Jay Deiman \033:wq! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Grid, Listbox expand
Aha! I found the key - I was missing the weight argument: 1 from Tkinter import * 2 import os, shutil, tkFileDialog, tkMessageBox 3 4 class TkSync: 5 def __init__(self, root): 6 self.sbox = Listbox(root) 7 self.sbox.grid(row=0, column=0, sticky=N+S+E+W) 8 root.grid_rowconfigure(0, minsize=400, weight=1) 9 root.grid_columnconfigure(0, minsize=400, weight=1) 10 11 root = Tk() 12 TkSync(root) 13 root.mainloop() Works perfectly. -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re.format a file
A.T.Hofkamp wrote: prasad rao wrote: helloThank you Lie and Kent. I forgot about newline character and the fact that string can be sliced. Thanks for your timely help BTW I have gone through the Python library reference and find no examples in fileinput module. The fileinput module only deals with reading and writing data from/to files, it does not deal with manipulating that data. How to manipulate strings is in the 'strings' or 'text' section of a tutorial. z=fileinput.input(file,inplace=1) for line in z: ???if len(line)60:pass ???if len(line)60: ??line=line[:60]+'\n'+line[60:] Is it the right way to do? A nice step forward, I'd say. Did you consider what to do with ridiculous long lines, eg 200, 500, 1000 or 1 characters long? If you want to deal with them, you'd need to repeatedly split the line. You could use a while loop for it. Or if the lines resemble paragraphs, then one might use the textwrap module which breaks on word boundaries by default I think, and provides several options for tweaking -- perhaps not what the OP is looking for. # untested import textwrap for line in z: line = textwrap.fill(line, width=60) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] setting PYTHONPATH on mac
how does one go about setting a PYTHON path environment variable on Mac OS X 10.4? i set up my .profile in the Terminal.app (UNIX) with a text file with the following line: PATH=$PATH:/Applications/Autodesk/maya8.5/Maya.app/Contents/bin thanks, -b___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] re Format a file
HelloI don't know why, but this I think going into infinite loop. I cant see anything wrong in it. Please show me where the problem is. def myform(s): import os so=open(s) d=os.path.dirname(s)+os.sep+'temp.txt' de=open(d,'w') for line in so: while len(line)60: tem=line[60:] try: ??? a,b=tem.split(' ',1) ??? de.write(line[:60]+a+'\n') ??? line=b except ValueError:pass de.write(line+'\n') so.close() de.close() os.remove(s) os.rename(d,s) Thank you Prasad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re Format a file
2009/2/27 prasad rao prasadarao...@gmail.com: Hello I don't know why, but this I think going into infinite loop. I cant see anything wrong in it. Please show me where the problem is. [...] while len(line)60: tem=line[60:] try: ??? a,b=tem.split(' ',1) ??? de.write(line[:60]+a+'\n') ??? line=b except ValueError:pass So you have a while loop, whose condition depends on line. This will loop infinitely if the body of the loop does not change the value of line. Can you see how that could happen? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class instance understanding = None
Hi Everyone, I go through the archived [Tutor] mail list to find programs others have tried to do. I found one that would keep track of a petty cash fund. please point out my misunderstanding. Here is what I started with; code #!/usr/bin/python from reportlab.lib.normalDate import ND #import cPickle as p #import pprint today = ND() class Account: def __init__(self, initial): self.balance = initial def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def getbalance(self): return self.balance print 'The current date is: ', today.formatUS() data = float('100.00') a = Account(data) p = a.getbalance() print 'balance = ', p remove_data = float('50.00') w = a.withdraw(remove_data) print withdraw = , w add_data = float('50.00') add = a.deposit(add_data) print deposit = , add /code results; The current date is: 02/27/09 balance = 100.0 withdraw = None deposit = None expected results; The current date is: 02/27/09 balance = 100.0 withdraw = 50.0 deposit = 100.0 thanks, -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] setting PYTHONPATH on mac
how does one go about setting a PYTHON path environment variable on Mac OS X 10.4? i set up my .profile in the Terminal.app (UNIX) with a text file with the following line: PATH=$PATH:/Applications/Autodesk/maya8.5/Maya.app/Contents/bin You should do more or less the same -- edit .bash_profile file and add these lines (replace [...] with actual path): PATH=[/absolute/path/to/folder/you/want]:${PATH} export PATH Please note that if you add your new directory at the end of PATH (as in your example), then this new directory will be searched for executables last. In my example, when you add new directory at the beginning, this new new directory will be searched first. This could be useful if you have several version of Python on you computer. Good luck, Valdas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Extract strings from a text file
Hi all, I am new to Python and still trying to figure out some things. Here is the situation: There is a text file that looks like this: text text text IDJoseph/text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text Full name Joseph Smith/text text text text text text Rights 1/text text text text text text LDAP 0/text text text This text file is very long, however all the entries in it looks the same at the above. What I am trying to do is: 1. I need to extract the name and the full name from this text file. For example: ( ID is Joseph Full name is Joseph Smith). - I am thinking I need to write something that will check the whole text file line by line which I have done already. - Now what I am trying to figure out is : How can I write a function that will check to see if the line contains the word ID between then copy the letter after until and dump it to a text file. Can somebody help please. I know this might soudn easy for some people, but again I am new to Python and still figuring out things. Thank you ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class instance understanding = None
Le Fri, 27 Feb 2009 00:06:59 -0500, David da...@abbottdavid.com s'exprima ainsi: Hi Everyone, I go through the archived [Tutor] mail list to find programs others have tried to do. I found one that would keep track of a petty cash fund. please point out my misunderstanding. Here is what I started with; code [...] /code results; The current date is: 02/27/09 balance = 100.0 withdraw = None deposit = None expected results; The current date is: 02/27/09 balance = 100.0 withdraw = 50.0 deposit = 100.0 thanks, -david So, what is your question, actually? If you do not want None, replace it with 0.0. None is good as a flag value. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class instance understanding = None
Le Fri, 27 Feb 2009 00:06:59 -0500, David da...@abbottdavid.com s'exprima ainsi: Hi Everyone, I go through the archived [Tutor] mail list to find programs others have tried to do. I found one that would keep track of a petty cash fund. please point out my misunderstanding. Here is what I started with; code #!/usr/bin/python from reportlab.lib.normalDate import ND #import cPickle as p #import pprint today = ND() class Account: def __init__(self, initial): self.balance = initial def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def getbalance(self): return self.balance print 'The current date is: ', today.formatUS() data = float('100.00') a = Account(data) p = a.getbalance() print 'balance = ', p remove_data = float('50.00') w = a.withdraw(remove_data) print withdraw = , w add_data = float('50.00') add = a.deposit(add_data) print deposit = , add /code results; The current date is: 02/27/09 balance = 100.0 withdraw = None deposit = None expected results; The current date is: 02/27/09 balance = 100.0 withdraw = 50.0 deposit = 100.0 thanks, -david PS: I guess you misunderstand the methods withdraw and deposit: If you read the code (read it again), they *do* something, meaning they process an *action*. Understand they name as verbs. As a consequence they do not return anything result (they do not *make* anything). They are like Pascal procedures. So that you should state instead: w = float('50.00') ###w = a.withdraw(remove_data) print withdraw = , w denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Extract strings from a text file
Le Thu, 26 Feb 2009 21:53:43 -0800, Mohamed Hassan linuxlove...@gmail.com s'exprima ainsi: Hi all, I am new to Python and still trying to figure out some things. Here is the situation: There is a text file that looks like this: text text text IDJoseph/text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text Full name Joseph Smith/text text text text text text Rights 1/text text text text text text LDAP 0/text text text This text file is very long, however all the entries in it looks the same at the above. What I am trying to do is: 1. I need to extract the name and the full name from this text file. For example: ( ID is Joseph Full name is Joseph Smith). - I am thinking I need to write something that will check the whole text file line by line which I have done already. - Now what I am trying to figure out is : How can I write a function that will check to see if the line contains the word ID between then copy the letters after until and dump it to a text file. Can somebody help please. I know this might soudn easy for some people, but again I am new to Python and still figuring out things. Thank you This is a typical text parsing job. There are tools for that. However, probably we would need a bit more information about the real text structure, and first of all what you wish to do with it later, to point you to the most appropriate tool. I guess that there is a higher level structure that nests IDs, names, rights etc in a section and that you will need to keep them together for further process. Anyway for a startup exploration you can use regular expressions (regex) to extract individual data item. For instance: from re import compile as Pattern pattern = Pattern(r.*ID(.+).+.*) line = text text text IDJoseph/text text text print pattern.findall(line) text = \ text text text IDJoseph/text text text text text text IDJodia/text text text text text text IDJoobawap/text text text print pattern.findall(text) == ['Joseph'] ['Joseph', 'Jodia', 'Joobawap'] There is a nice tutorial on regexes somewhere (you will easily find). Key points on this example are: r.*ID(.+).+.* * the pattern between ... expresses the overall format to be matched * all what is between (...) will be extracted by findall * '.' mean 'any character'; '*' means zero or more of what is just before; '+' mean one or more of what is just before. So the pattern will look for chains that contains a sequence formed of: 1. possible start chars 2. ID literally 3. one or more chars -- to return 4. something between ... 5. possible end chars Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re Format a file
On Fri, 27 Feb 2009 09:59:40 +0530, prasad rao wrote: def myform(s): import os so=open(s) d=os.path.dirname(s)+os.sep+'temp.txt' de=open(d,'w') for line in so: while len(line)60: item=line[60:] try: a,b=tem.split(' ',1) what is tem here? It must be a global, since I can't see any other reference to tem in the function definition. If tem is global, then the value of b (a.k.a. line) will never change (except for the first iteration) de.write(line[:60]+a+'\n') line=b except ValueError: pass de.write(line+'\n') so.close() de.close() os.remove(s) os.rename(d,s) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor