[Tutor] A simpler mousetrap

2004-12-16 Thread Liam Clarke
Hi all, I'm writing some code, and I want to take a given path + filename, and change the file extension to *.bak. In doing so, (entirely internal this function), I am assuming - That the file will always have an extension Thathe extension will vary But, it will follow the general DOS format of

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Brian van den Broek
Liam Clarke said unto the world upon 2004-12-16 02:05: Alright, so that was a quick example, but return not x % 2 A light dawns. On Thu, 16 Dec 2004 15:58:38 +0900, Guillermo Fernandez Castellanos <[EMAIL PROTECTED]> wrote: Well... I find multiple returns to be rather useful def isOdd(x):

Re: [Tutor] Re: A simpler mousetrap

2004-12-16 Thread Liam Clarke
x=os.path.splitext(a)[0]+'.bak' Ah, jolly good, looks a bit simpler. Thanks! Regards, Liam Clarke On Thu, 16 Dec 2004 09:44:03 +0100, Wolfram Kraus <[EMAIL PROTECTED]> wrote: > Liam Clarke wrote: > > Hi all, > > > > I'm writing some code, and I want to take a given path + filename, and > > chan

Re: [Tutor] A simpler mousetrap

2004-12-16 Thread Kent Johnson
As Wolfram pointed out, os.path.splitext() is a good way to do this. It is also more robust than the other solutions because it does the right thing if there is no extension on the original file name. I just want to say that your first solution can be written much more simply as x=a[:a.rfind('.')

[Tutor] Multiple returns in a function

2004-12-16 Thread Kent Johnson
Brian van den Broek wrote: Kent Johnson said unto the world upon 2004-12-15 20:16: > I would write > def is_leap_year(year): > try: > datetime.date(year, 2, 29) > return True > except ValueError: > return False Not an adherent of the "only one exit point" doct

[Tutor] sorting and editing large data files

2004-12-16 Thread Scott Melnyk
Hello! I recently suffered a loss of programming files (and I had been putting off my backups...) The project I am working on involved the use of programs which I had been assisted with by some of the group. I am rewritten the programs however my results are not what I expected. I would appreci

Re: [Tutor] AttributeError: instance has no __call__ method

2004-12-16 Thread Juan Shen
Marc Gartler wrote: Hi everybody, Prior to this chunk of code 'glass' has been chosen from a list of colors via user input, and I now want to have that choice connect to one of several possible classes: def glass_type(glasstype): if glasstype == 'Red': myglass = RedGlassCost() el

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Kent Johnson
It's probably worth pointing out that these two functions are not entirely equivalent: def t1(): if condition: return True return False def t2(): return condition because 'condition' does not have to evaluate to a boolean value, it can be any Python value. Here is a simple example where

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Loptr Chaote
On Wed, 15 Dec 2004 19:58:56 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: > As I mentioned, I feel as though I have a mental block getting in the > way of coming up with code in the smoother fashion of the second snippet > above. As I have been making a lot of use of a construct (pattern?)

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Juan Shen
Kent, Bingle! Python's 'and' and 'or' operation only returns the right value (any kind of value: integer, string, list, excuting a function and so on) not Boolean value (True or False). It's a real fuzzy tip for beginners. See Chapter 4.6 of Dive into Python for futher reading Juan Shen Ken

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Gregor Lingl
Kent Johnson schrieb: It's probably worth pointing out that these two functions are not entirely equivalent: def t1(): if condition: return True return False def t2(): return condition ... >>> if t1(100) == True: print '100 is True' ... 100 is True >>> if t2(100) == True: print '100

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Blake Winton
Juan Shen wrote: def is_leap_year(year): is_leap = True try: datetime.date(year, 2, 29) except ValueError: is_leap = False return is_leap I would write def is_leap_year(year): try: datetime.date(year, 2, 29) return True except ValueError:

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Gonçalo Rodrigues
Loptr Chaote wrote: On Wed, 15 Dec 2004 19:58:56 -0500, Brian van den Broek 1) Every operation has a return value [unless purposefully left out] This includes, but is not limited to; mathematical operations (of course), variable assignment, compare blocks, etc.. A small correction: every function h

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Brian van den Broek
Blake Winton said unto the world upon 2004-12-16 09:20: Juan Shen wrote: Yeah, I support Kent. Brian's code is obviously C style, define a variable and give it an origin value, then use it, modify its value and so on. If you choose Python, you should adapt to it that variable needn't to be

Re: [Tutor] Python structure advice ?

2004-12-16 Thread Dave S
Dave S wrote: Im sorry to bang on about Python structure, but I do struggle with it, having in the past got into very bad habits with loads of BASIC where everything was global, and Forth, and hand coded 8031, 8051, 6502 I cant get my head round how you guys handle a modern structured lang

Re: [Tutor] Python structure advice ?

2004-12-16 Thread Kent Johnson
Dave S wrote: Dave S wrote: The 'remembering where is was' seems a continuous stumbling block for me. I have though of coding each module as a class but this seems like a cheat. I could declare copious globals, this seems messy, I could define each module as a thread & get them talking via queue

Re: [Tutor] sorting and editing large data files

2004-12-16 Thread Danny Yoo
On Thu, 16 Dec 2004, Scott Melnyk wrote: > I recently suffered a loss of programming files (and I had been > putting off my backups...) Hi Scott, [Side note that's not really related to Python: if you don't use a version control system to manage your software yet, please learn to use one. The

RE: [Tutor] hello i need help

2004-12-16 Thread Robert, Andrew
I recommend you use vim. You can get it at http://www.vim.org/download.php Thank you, Andrew Robert Systems Architect Information Technology - OpenVMS Massachusetts Financial Services Phone: 617-954-5882 Pager: 781-764-7321 E-mail: [EMAIL PROTECTED] Linux User Number: #201204 -Original

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Jeff Shannon
Blake Winton wrote: def is_leap_year(year): is_leap = True try: datetime.date(year, 2, 29) except ValueError: is_leap = False return is_leap I would write def is_leap_year(year): try: datetime.date(year, 2, 29) return True except ValueError:

[Tutor] hello i need help

2004-12-16 Thread alex biggerstaff
is it possible 2 write a script for wordpad or something, i only started so i dont know much i do know a little about if ($1 == hi)  && (£2 == m8) but im not sure how 2 make this apply to other programs. i can only write scripts on a thing called mIRC. ne help would b great thnxs ALL-NEW Yahoo!

Re: [Tutor] removedirs ?

2004-12-16 Thread Jason Child
is this what you want to do? import os from os.path import join # Delete everything reachable from the directory named in 'top'. # CAUTION: This is dangerous! For example, if top == '/', it # could delete all your disk files. for root, dirs, files in os.walk(top, topdown=False): for name in file

Re: [Tutor] least squares

2004-12-16 Thread Danny Yoo
On Thu, 16 Dec 2004, mdcooper wrote: > I am trying to run a least squares program (written by Konrad Hinsen) Hi Matthew, You're assuming that we know who Konrad Hinsen is. *grin* Ok, when you're referring to the least-squared code, are you referring to a module in Scientific Python? Please p

Re: [Tutor] removedirs ?

2004-12-16 Thread Jason Child
Ertl, John wrote: I am trying to remove a directory that has other directories and files in it. I thought removedirs was supposed to do a recursive remove of files and directories. When I try it I get os.removedirs("DAF") Traceback (most recent call last): File "", line 1, in -tople

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-16 Thread Jacob S.
Ha! That's what I was looking for! The builtin apply function! The only way I could send the *args to the function was through a list, and function calls see a list as one argument. The apply argument doesn't! Thanks Bob. Jacob Schmidt > At 12:39 PM 12/8/2004, Bob Gailer wrote: > >At 11:27 AM 12/

Re: [Tutor] Address book sort of

2004-12-16 Thread Jacob S.
Hey, I made some changes to my address book, with help from Oh darn, well whoever you are, I haven't forgotten the help you gave me in changing my if/if/if/if/if/else things into mapping objects Anyway, if anybody wants a look, here it is. P.S. I made some changes to the file reading part t

Re: [Tutor] Complex roots

2004-12-16 Thread Jacob S.
Finding the all the roots of a complex number shouldn't be too difficult. I tend to do it on paper sometimes. Maybe I can write a script to do it for me instead. I stongly caution you though. The methods that I show below are unstable and should be verified by a math web site as it has been quite

Re: [Tutor] Address book sort of

2004-12-16 Thread Jacob S.
You know, instead of that long or thing you've set up, you could do this. if select in [ 'l', 'v', 'V' ]: > [quote] > if select == '1' or select == 'v' or select == 'V': > if file_in_disk in os.listdir('/home/jerimed'): # change??? > fhandle = open(file_in_disk, 'r

Re: [Tutor] Problem with python2.4.

2004-12-16 Thread Jacob S.
If you mean the "Internet Connection Firewall" thingy that you access from the network connection options, then Nope, that's not the problem, because it's disabled. Thanks for your help, Jacob > > Seeing this comment reminded me of some conversations I've seen in > comp.lang.python recently. App

Re: [Tutor] Python structure advice ?

2004-12-16 Thread Alan Gauld
> everything was global, how you guys handle a modern structured > language Don't worry this is one of the hardest bad habits to break. You are not alone. The easiest way is to just pass the data from function to function in the function parameters. Its not at all unusual for functions to have

Re: [Tutor] dbcp module

2004-12-16 Thread Rene Bourgoin
Yah i came across that site and was trying to learn from that code. I just cant seem to find a download for a python version. i came across this site while searching. http://jakarta.apache.org/ --- On Wed 12/15, Kent Johnson < [EMAIL PROTECTED] > wrote: From: Kent Johnson [mailto: [EMAIL

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Alan Gauld
> I far prefer the Brian's version, because it lets me set a single > breakpoint while I'm debugging, and I can look at the return value > before returning it, In most debiggers(including Pythons) you can still do that with a boolean condition provided the condition does not itself contain a

Re: [Tutor] Python structure advice ?

2004-12-16 Thread Alan Gauld
> Having written this email, it has put my thoughts in order, though it > seems a bit cheaty, wouldn't defining all modules that have to remember > their internal state as classes be the best bet ? Its one solution certainly, creeate objects and the objects carry their state with them. But the pro

Re: [Tutor] Complex roots

2004-12-16 Thread Dick Moores
Thanks. Tim Peters helped me out with his answer of 12/9. Dick Moores Jacob S. wrote at 19:27 12/15/2004: Finding the all the roots of a complex number shouldn't be too difficult. I tend to do it on paper sometimes. Maybe I can wr

Re: [Tutor] removedirs ?

2004-12-16 Thread Juan Shen
Of course, shutil.rmtree() is a fast and direct method, but still a little warning, it could be dangerous. No warning and ensure will be raised while removing a tree of files. Juan Shen Kent Johnson wrote: You misunderstand what removedirs does. It removes an empty directory; then, if the r

Re: [Tutor] removedirs ?

2004-12-16 Thread Juan Shen
This is an iterative method, although much slower than shutil.rmtree(), which can be useful sometimes, while some stdout/in is added: #!/usr/bin/python #Filename: myrmtree.py import os,sys,shutil def rmtree(top): uncleandir=[] for (root,dirs,files) in os.walk(top,topdown=False): for

[Tutor] Re: A simpler mousetrap

2004-12-16 Thread Wolfram Kraus
Liam Clarke wrote: Hi all, I'm writing some code, and I want to take a given path + filename, and change the file extension to *.bak. In doing so, (entirely internal this function), I am assuming - That the file will always have an extension Thathe extension will vary But, it will follow the gene

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Gregor Lingl
Brian van den Broek schrieb: If my original bit of code, the structure was like: output_value = False if condition: output_value = True return output_value Mine would be like yours if transformed to: if condition: return True return False Hi Brian! Do you mean, that condition is something

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Juan Shen
Kent Johnson wrote: Brian van den Broek wrote: I've begun to wonder if I am overlooking a improvement similar to that in DogWalker's suggestion. As an example of the sort of thing I have been doing: import datetime def is_leap_year(year): '''-> boolean Returns True or False as year is, o

Re: [Tutor] AttributeError: instance has no __call__ method

2004-12-16 Thread Juan Shen
Juan Shen wrote: Marc Gartler wrote: Hi everybody, Prior to this chunk of code 'glass' has been chosen from a list of colors via user input, and I now want to have that choice connect to one of several possible classes: def glass_type(glasstype): if glasstype == 'Red': myglass = RedG

Re: [Tutor] sorting and editing large data files

2004-12-16 Thread Rich Krauter
Scott Melnyk wrote: Hello! I recently suffered a loss of programming files (and I had been putting off my backups...) [snip] #regular expression to pull out gene, transcript and exon ids info=re.compile('^(ENSG\d+\.\d).+(ENST\d+\.\d).+(ENSE\d+\.\d)+') #above is match gene, transcript, then one or m

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Brian van den Broek
Gregor Lingl said unto the world upon 2004-12-16 04:14: Brian van den Broek schrieb: If my original bit of code, the structure was like: output_value = False if condition: output_value = True return output_value Mine would be like yours if transformed to: if condition: return True return Fa

Re: [Tutor] hello i need help

2004-12-16 Thread Jason Child
alex biggerstaff wrote: is it possible 2 write a script for wordpad or something, i only started so i dont know much i do know a little about if ($1 == hi) && (£2 == m8) but im not sure how 2 make this apply to other programs. i can only write scripts on a thing called mIRC. ne help would b grea

[Tutor] removedirs ?

2004-12-16 Thread Ertl, John
I am trying to remove a directory that has other directories and files in it. I thought removedirs was supposed to do a recursive remove of files and directories. When I try it I get >>> os.removedirs("DAF") Traceback (most recent call last): File "", line 1, in -toplevel- os.removedirs(

RE: [Tutor] removedirs ?

2004-12-16 Thread Ertl, John
Jason, I could...That is the exact feature I am trying to replicate, but I would just like to do it in Python if I can (in a simple way). I am writing this code in Python to avoid some funny scripting that I would need to do. To go back to combing shell and Python again would be a bit deflating..

Re: [Tutor] removedirs ?

2004-12-16 Thread Kent Johnson
You misunderstand what removedirs does. It removes an empty directory; then, if the removed directory's parent is now empty, it removes that, too, and so on until it finds a directory with something in it or gets up to the root. Try shutil.rmtree() or this recipe: http://aspn.activestate.com/ASP

[Tutor] least squares

2004-12-16 Thread mdcooper
Hi there, I am trying to run a least squares program (written by Konrad Hinsen) but I would like to only have positive values returned. Can anyone help is altering the code to do this? Thanks, matthew ___ Tutor maillist - [EMAIL PROTECTED] http:/

Re: [Tutor] least squares

2004-12-16 Thread Jason Child
well, not sure if there is a module with the function you are looking for, but... #sloppy as hell method: #... if str(my_returned_value).find("-") != -1: return my_returned_value #or whatever you want to do with it #... #slightly less sloppy as hell method: if my_returned_value < 0: return

RE: [Tutor] least squares

2004-12-16 Thread mdcooper
Hi Danny, Thanks for the reply - I was purposely vague just to see what people would ask for. The code uses LinearAlgebra.py from Numeric and LeastSquares.py from Scientific. I am trying to get a corrolation between a large number of variables and for many similar equations : (Ca1 * xa^2)

RE: [Tutor] least squares

2004-12-16 Thread mdcooper
Hi Danny, Thanks for the reply - I was purposely vague just to see what people would ask for. The code uses LinearAlgebra.py from Numeric and LeastSquares.py from Scientific. I am trying to get a corrolation between a large number of variables and for many similar equations : (Ca1 * xa^2)

Re: [Tutor] hello i need help

2004-12-16 Thread Alan Gauld
> is it possible 2 write a script for wordpad or something, Yes it is, using the COM interface. But frankly the interface to Wordpad is pretty basic. Word has much more powerful COM facilities. But... > i only started so i dont know much You probably need to do a bit of reading on the fundament

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Alan Gauld
> What makes me lean toward mine still? I'd encountered a general > injunction to avoid multiple exit point somewhere on the wild web This goes back to Djikstra's original paper on structured programming (and he was really thinking about assembler!) Multiple returns are a source of bugs, especi