Re: [Tutor] Should this be a list comprehension or something?

2005-01-27 Thread Alan Gauld
> Oh wait, I know: let's all start writing code in MS Word .doc format! > Arial for functions, Times New Roman for classes. Who's with me? ;-) So you've been looking at Eiffel then? :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.pytho

Re: [Tutor] Re: Syntax Check

2005-01-27 Thread Chad Crabtree
I'm going to look more in to import hooks. I am only looking for syntactic sugar, at least at this point. It's not really my ambition to make more efficient code. To Ryan. I looked at COG before (not in this case but general looking at python packages) it doesn't really do what I was thinki

[Tutor] Re: Syntax Check

2005-01-27 Thread Javier Ruere
Chad Crabtree wrote: > Ok I'll explain what I've done so far. [...] > I hope I made myself clearer. Yes. To add new stuff to the language you would have to work at the interpreter level. Probably you can avoid going too deep using import hooks, as it has already been suggested, to modify what's

Re: [Tutor] Should this be a list comprehension or something?

2005-01-27 Thread Brian van den Broek
Tony Meyer said unto the world upon 2005-01-27 21:46: [Sean Perry] And now, for the pedant in me. I would recommend against naming functions with initial capital letters. In many languages, this implies a new type (like your Water class). so CombineWater should be combineWater. [Brian van den B

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-01-27 16:08: Brian van den Broek wrote: Finally, in the first instance, I was aiming for the OP's stated end. To make this more general and reusable, I think I'd do: def get_list_dup_dict(a_list, threshold=1): items_dict, dup_dict = {}, {} # Ques

RE: [Tutor] Should this be a list comprehension or something?

2005-01-27 Thread Tony Meyer
[Sean Perry] >> And now, for the pedant in me. I would recommend against naming >> functions with initial capital letters. In many languages, this >> implies a new type (like your Water class). so >> CombineWater should be combineWater. [Brian van den Broek] > Do you mean implies by the dominan

Re: [Tutor] Preffered way to search posix filesystem

2005-01-27 Thread Jacob S.
Try this def findfile(thefile, toplevel="C:\\windows",findcount=-1,subdirectories=True,returnlist=False): results = [] t = 0 if subdirectories: for root,dirs,files in os.walk(toplevel): for x in files: fullname = os.path.join(root,x) if x.lo

Re: [Tutor] Convert string to variable name

2005-01-27 Thread Liam Clarke
I've used something along the lines for Pythoncard, but the names are generated within strict rules and expectations. i.e. first off รข #Some code that uses regExs, finds all headings, and # a asterisk to indicate a date value, returns a iterable object reHead using finditer() #which contai

RE: [Tutor] Syntax Check

2005-01-27 Thread Ryan Davis
Check out COG (http://www.nedbatchelder.com/code/cog/), a macro system for any language created by Ned Batchelder (http://www.nedbatchelder.com/). I'm not sure if that's what you're looking for, but allows some good macro capabilities. Thanks, Ryan -Original Message- From: [EMAIL PROT

Re: [Tutor] Syntax Check

2005-01-27 Thread Chad Crabtree
Well I don't think that it would really require that. I could just define macro's in a module and just do it like so import macro import defined_macros as m macro.expand(m.with(),m.assert()) I just thought it would be best to have definitions at the head of a script, or at least to have the o

Re: [Tutor] Syntax Check

2005-01-27 Thread Jeff Shannon
Chad Crabtree wrote: I'm trying to make a macro system that's work by just doing this import macro # [...] Perhaps you could turn things around, and make your macro preprocessor into an import hook? I.E., you'd use it like -- import macro module = macro.import("module_with_macros"[, macr

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Kent Johnson
Brian van den Broek wrote: incorporating some of Wolfram's and Kent's (hope I've missed no one) suggestions: def dups_in_list_report(a_list): '''Prints a duplication report for a list.''' items_dict = {} for i in a_list: items_dict[i] = items_dict.get(i, 0) + 1 for k, v i

Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Chad Crabtree
Try a simple bubble sort. This will not have great performance on big files at a worst case scenario it will take n-1 loops to sort. This is not very general and would require a substantially different implementation. def bublesort(l1,l2,l3): modified=True while modified: modif

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-01-27 05:57: Brian van den Broek wrote: Wolfram Kraus said unto the world upon 2005-01-27 03:24: This whole part can be rewritten (without sorting, but in Py2.4 you can use sorted() for this) with a list comprehension (Old Python2.1 style, with a newer

Re: [Tutor] Syntax Check

2005-01-27 Thread Chad Crabtree
Ok I'll explain what I've done so far. I'm using tokenize to take the file that imports macro; found by using stack inspection and then tokenize it. I look at all the string tokens to see if they match they pattern a macro should have. Then I pass the macro string through the tokenizer agai

Re: [Tutor] Safely buffering user input

2005-01-27 Thread Danny Yoo
On Thu, 27 Jan 2005, Miles Stevenson wrote: > I'm trying to practice safe coding techniques. I just want to make sure > that a user can't supply a massive argument to my script and cause > trouble. I'm just trying only accept about 256 bytes: > > buffer(sys.argv[1], 0, 256) ^^ Hi Miles,

Re: [Tutor] Safely buffering user input

2005-01-27 Thread Max Noel
On Jan 27, 2005, at 18:17, Bill Mill wrote: I've never used buffer(); in fact, I didn't even know it existed, and I've been using python for a while now. Instead of using buffer, just do: sys.argv[1] = sys.argv[1][:255] This says "Set the second element of sys.argv equal to its first 256 characters

Re: [Tutor] Safely buffering user input

2005-01-27 Thread Bill Mill
Miles, On Thu, 27 Jan 2005 13:08:05 -0500, Miles Stevenson <[EMAIL PROTECTED]> wrote: > Newbie question. > > I'm trying to practice safe coding techniques. I just want to make sure that a > user can't supply a massive argument to my script and cause trouble. I'm just > trying only accept about 2

[Tutor] Safely buffering user input

2005-01-27 Thread Miles Stevenson
Newbie question. I'm trying to practice safe coding techniques. I just want to make sure that a user can't supply a massive argument to my script and cause trouble. I'm just trying only accept about 256 bytes: buffer(sys.argv[1], 0, 256) searchpath = sys.argv[1] The script runs successfully, b

Re: [Tutor] Syntax Check

2005-01-27 Thread Alan Gauld
> I'm trying to make a macro system that's work by just doing this OK, But to get your acros to use a language extension to python (the with syntax) you are going to have to write your own language interpreter/parser. Even if that just turns the 'with' stuff into Python. So why is the """ ma

Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Bill Mill
Srinivas, You can't sort a string, since it's immutable. You can, however, sort a list. To sort your table by the third element, you can do something like this: >>> table = (("apple", "fruit", "denmark"), ... ("mummy", "antique", "egypt"), ... ("taj", "wonder", "india"), ... ("f-16", "fighter", "

Re: [Tutor] Syntax Check

2005-01-27 Thread Chad Crabtree
I'm trying to make a macro system that's work by just doing this import macro class withMacro(prefixMacro): pass mac=withMacro() mac.pattern("item key") macro.expand() g=[] """ with g: .append('a') .append('b') .append(123) """ I would like to not have to use the comment strings

Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Srinivas Iyyer
Dear Danny, thank you for ur help. But a basic question ? In a table, more specifically a matrix 3X3, AppleFruitDenmark F-16 Fighter USA Taj Wonder India MummyAntique Egypt IF I have to sort on country, it should be AppleFruitDenmark MummyAntique Egypt Taj

Re: [Tutor] Cluster algorithms

2005-01-27 Thread Kim Branson
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Are there any example programs depicting Clustering algorithms such as agglomerative, complete link, partional , squared error clustering, k-means or clustering algos based on Neural networks or genetic algorithm. although I just learned python, (to m

Re: [Tutor] Cluster algorithms

2005-01-27 Thread Bill Mill
Kumar, On Wed, 26 Jan 2005 22:35:59 -0800 (PST), kumar s <[EMAIL PROTECTED]> wrote: > Hi: > > I am still trying to learn the OOPs side of python. > however, things/circumstances dont seems to stop until > I finish my practise and attaing higher understanding. > may be, i am being pushed by circum

Re: [Tutor] Compile Only

2005-01-27 Thread Kent Johnson
If your program is written so it doesn't do anything when imported, you could use > python -c "import myprogram" myprogram.py should have a structure like this: def main(): # do something... if __name__ == '__main__': main() The "if __name__ ..." prevents the module from doing anything when i

[Tutor] Compile Only

2005-01-27 Thread jhomme
-Original message- From: "Alan Gauld" [EMAIL PROTECTED] Date: Thu, 27 Jan 2005 05:08:07 -0500 To: "Chad Crabtree" [EMAIL PROTECTED] Subject: Re: [Tutor] Syntax Check > > Does anyone happen to know how to turn of the syntax checking in > > python? I've been working on a module driven prep

[Tutor] Re: Syntax Check

2005-01-27 Thread Javier Ruere
Chad Crabtree wrote: > Does anyone happen to know how to turn of the syntax checking in > python? I've been working on a module driven preprocessor but I'd > like to not have to use comment strings. I don't think that's possible. What would the compiler do with code with an invalid syntax? W

Re: [Tutor] Cluster algorithms

2005-01-27 Thread Kent Johnson
Google is your friend: Googling 'python clustering algorithm' gives many hits that seem to have what you are looking for. Kent kumar s wrote: Hi: I am still trying to learn the OOPs side of python. however, things/circumstances dont seems to stop until I finish my practise and attaing higher und

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Kent Johnson
Brian van den Broek wrote: Wolfram Kraus said unto the world upon 2005-01-27 03:24: Brian van den Broek wrote: for key in items_dict.copy(): # Try it without the .copy() if items_dict[key] == 1:# and see what happens. del items_dict[key] dict_keys = items_dict.key

Re: [Tutor] Syntax Check

2005-01-27 Thread Alan Gauld
> Does anyone happen to know how to turn of the syntax checking in > python? I've been working on a module driven preprocessor but I'd > like to not have to use comment strings. So don't use them! They aren't mandatory. I'm not sure I understand youir problem? Why would turning off syntax check

Re: [Tutor] Should this be a list comprehension or something?

2005-01-27 Thread Alan Gauld
> > functions with initial capital letters. In many languages, this implies > > a new type (like your Water class). so CombineWater should be combineWater. > > Do you mean implies by the dominant coding conventions, or by language > syntax? (Indulging the curious pedant in me.) Coding convention.

Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Alan Gauld
> for i in range(len(a)): > for k in range(len(a)): for k in range(i,len(a)): is faster, and if you calculate len before starting the loops that will speed it up too. (You calculate len for each iteration of each loop!) > if i != k: > if a[i] == a[k]: > print a[i] > break HTH Alan G. ___

Re: [Tutor] New to Python

2005-01-27 Thread Alan Gauld
> I hear that Alan Gauld's tutorial is also very good, but geared more > towards people new to programming. Yes, I try to take folks to the point where they can understand the official tutor (well maybe a wee bit further than that, but that was the original target...) > (which itself should be in

Re: [Tutor] New to Python

2005-01-27 Thread Alan Gauld
> Greetings all, I'm new to python and thought I'd pop in here for advice. Good decisions both :-) > I've done object oriented design and programmed in perl, java, c++, basic, etc. > ... > I'm curious about good tutorial websites and books to buy. With your background the standard Python tutoria

Re: [Tutor] Convert string to variable name

2005-01-27 Thread Alan Gauld
> This is something I've been trying to figure out for some time. Is > there a way in Python to take a string [say something from a > raw_input] and make that string a variable name? I want to to this so > that I can create class instances on-the-fly, using a user-entered > string as the instance

[Tutor] Syntax Check

2005-01-27 Thread Chad Crabtree
Does anyone happen to know how to turn of the syntax checking in python? I've been working on a module driven preprocessor but I'd like to not have to use comment strings. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Brian van den Broek
Wolfram Kraus said unto the world upon 2005-01-27 03:24: Brian van den Broek wrote: Thanks Wolfram, I knew someone would improve what I posted. (It can always be done ;-) for i in a_list: if i in items_dict: items_dict[i] = items_dict[i] + 1 else: items_

[Tutor] Re: Unique Items in Lists

2005-01-27 Thread Wolfram Kraus
Chad Crabtree wrote: Ok you got me thinking. I used the same thing I posted before but created a one liner that as a side affect makes adict like before. [adict.__setitem__(x,adict.get(x,0)+1) for x in l] I think it's kinda funny and ugly, ohh and not particuarly clear about what it does. Uhh

Re: [Tutor] Re: Unique Items in Lists

2005-01-27 Thread Chad Crabtree
Ok you got me thinking. I used the same thing I posted before but created a one liner that as a side affect makes adict like before. [adict.__setitem__(x,adict.get(x,0)+1) for x in l] I think it's kinda funny and ugly, ohh and not particuarly clear about what it does. Wolfram Kraus wrote: > g

[Tutor] Re: Unique Items in Lists

2005-01-27 Thread Wolfram Kraus
Brian van den Broek wrote: [...] Hi Srini, for the task of finding out which items are repeated and how many times, I'd do this: def dups_in_list_report(a_list): '''Prints a duplication report for a list.''' items_dict = {} for i in a_list: if i in items_dict: ite

Re: [Tutor] Should this be a list comprehension or something?

2005-01-27 Thread Sean Perry
Brian van den Broek wrote: Sean Perry said unto the world upon 2005-01-27 02:13: And now, for the pedant in me. I would recommend against naming functions with initial capital letters. In many languages, this implies a new type (like your Water class). so CombineWater should be combineWater. Do