Re: Can I run an operation on an object's attribute when reading?

2009-01-19 Thread Michael Hartl
Phillip B Oldham schrieb: On Mon, Jan 19, 2009 at 12:15 PM, Chris Rebert wrote: Assuming I'm interpreting you correctly (you're going to have to use something like a getter): Thanks, but I'm looking for a way to do it *without* using a getter as I don't have easy access to the class (

Re: lazy evaluation is sometimes too lazy... help please.

2009-01-16 Thread Michael Hartl
James Stroud schrieb: Ken Pu wrote: Hi, below is the code I thought should create two generates, it[0] = 0,1,2,3,4,5, and it[1] = 0,10,20,30,..., but they turn out to be the same!!! from itertools import * itlist = [0,0] for i in range(2): itlist[i] = (x+(i*10) for x in count()) print "what

Re: can someone tell me why this doesn't work please python 3

2009-01-14 Thread Michael Hartl
garywood schrieb: def ask_ok(prompt, retries=4, complaint="Yes or no, please!"): while True: password = input("enter something") if password in ('y', 'ye', 'yes'): return True if password in ('n', 'no', 'nope'): return False retries = retries - 1 if ret

Re: Need a little parse help

2005-05-10 Thread Michael Hartl
Mike mentions an important point, and I've been bitten by the phenomenon Mike mentions---but only when *writing* to files. They should always be closed explicitly, as in f = file(filename, 'w') f.write(somestring) f.close() On the other hand, I've never encountered a problem with the "for line

Re: Need a little parse help

2005-05-10 Thread Michael Hartl
I'd also like to note that both the inputfiles variable and the readlines() method are superfluous; to iterate through the file line by line, use either for line in open(inputfilename): # do something with line or (my personal preference, since I like to think of the file as a thing rather th

Re: Whats the best Python Book for me

2005-05-01 Thread Michael Hartl
I'd like to second the recommendation for the Python Cookbook. As an experienced programmer, you'll learn a lot by osmosis. Michael -- http://mail.python.org/mailman/listinfo/python-list

Re: Calling external text-files (newb)

2005-02-28 Thread Michael Hartl
There are two problems: (1) by quoting 'source', you refer to a string literal, not the variable you defined via raw_input; (2) you have not defined the variable 'input', so there's no way to call the 'read' method on it. Try this instead: source_path = raw_input('file path: ') s = open(source_pa

Re: Calling external text-files (newb)

2005-02-28 Thread Michael Hartl
There are two problems: (1) by quoting 'source', you refer to a string literal, not the variable you defined via raw_input; (2) you have not defined the variable 'input', so there's no way to call the 'read' method on it. Try this instead: source_path = raw_input('file path: ') s = open(source_pa

Re: Which module is "set " in?

2005-02-25 Thread Michael Hartl
It's good that you're using Python 2.3, which does have sets available, as a previous poster mentioned. Users of Python 2.2 or earlier can get most of the Set functionality using the following class (from Peter Norvig's utils.py file): # class Set: # """This implements the Set class from PEP

Re: rounding problem

2005-02-23 Thread Michael Hartl
> Is it normal? Yes. The interpreter prints back the repr of a, which reflects the imprecision inherent in floats. If you want '1.1', use the string returned by the str function. >>> a = 1.1 >>> a 1.1001 >>> repr(a) '1.1001' >>> str(a) '1.1' Michael -- Michael D. Hartl

Re: Tuple index

2005-02-21 Thread Michael Hartl
I actually find it strange that tuples don't have an index function, since finding the index doesn't involve any mutation. Anyone know why Python doesn't allow a statement like t.index('foo')? In any case, you can use the index method of list objects if you convert your tuple to a list first: >>

Re: Test for structure

2005-02-16 Thread Michael Hartl
I use a function isListLike in cases such as this one: # def isListLike(L): # """Return True if L is list-like, False otherwise.""" # try: # L + [] # return True # except: # return False Then you can use a standard if-else construct: # if isListLike(myvar): #

Re: Newbie help

2005-02-13 Thread Michael Hartl
Sorry about the code: Google Groups screwed up the formatting, but I hope you get the picture. -- http://mail.python.org/mailman/listinfo/python-list

Re: Newbie help

2005-02-13 Thread Michael Hartl
You need to put the "You guessed it..." block inside a conditional (if guess == number) so that it only executes if the guess is right. (You also need to break out of the loop in that case.) As written, the "You guessed it..." block executes the first time through the loop regardless of the guess

Re: builtin functions for and and or?

2005-02-13 Thread Michael Hartl
I warmly recommend downloading Peter Norvig's Python utilities file (http://aima.cs.berkeley.edu/python/utils.py) and putting it on your Python path. (E.g., in bash, put a line like export PYTHONPATH="/path/to/utilities_directory" in your .bashrc file.) The utils.py file defines many useful fun

Re: check if object is number

2005-02-12 Thread Michael Hartl
Oops, my bad. The utilities file I use gets loaded automatically when I start my interpreter, so I mistook isnumber for a built-in function. A battle-tested isnumber function is defined in Peter Norvig's utils.py (http://aima.cs.berkeley.edu/python/utils.py): #def isnumber(x): #"Is x a number

Re: check if object is number

2005-02-12 Thread Michael Hartl
As I mention below, I mistook the function from my utilities file for a Python built-in; here's the implementation: #def isnumber(x): #"Is x a number? We say it is if it has an __int__ method." #return hasattr(x, '__int__') -- http://mail.python.org/mailman/listinfo/python-list

Re: check if object is number

2005-02-11 Thread Michael Hartl
As luck would have it, isnumber is a built-in Python function: >>> isnumber(1) True >>> isnumber(1.0) True >>> isnumber('1') False Michael -- Michael D. Hartl, Ph.D. Chief Technology Officer http://quarksports.com/ -- http://mail.python.org/mailman/listinfo/python-list

Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Michael Hartl
Adam brings up a good point: eval is a very general function which evaluates an arbitrary Python expression. As a result, it (and its close cousin exec) should be used with caution if security is an issue. Michael -- http://mail.python.org/mailman/listinfo/python-list

Re: How do I convert arithemtic string (like "2+2") to a number?

2005-02-05 Thread Michael Hartl
Use the eval function: >>> eval("30/(6+9)") 2 Michael -- http://mail.python.org/mailman/listinfo/python-list

Re: naive doc question

2005-01-29 Thread Michael Hartl
i.e., http://docs.python.org/lib/typesmapping.html -- http://mail.python.org/mailman/listinfo/python-list

Re: a sequence question

2005-01-28 Thread Michael Hartl
For problems like this I use a partition function defined in a utils.py file that I use (based on Peter Norvig's utils file at http://aima.cs.berkeley.edu/python/utils.py). Using partition, the problem you posed can be solved by writing #for a, b in partition([1, 2, 3, 4], 2): #print a, b Th

Re: counting items

2005-01-12 Thread Michael Hartl
That's cool! Of course, walk returns a generator, so using a list comprehension to turn it into a list seems natural, but I didn't realize that list() does the same thing (and neither, apparently, did the original implementor) -- although, with a little reflection, it obviously must! Michael --

Re: counting items

2005-01-12 Thread Michael Hartl
There's a great function called "walk" at that iterates over arbitrary data (and is recursion-proof) at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/118845 It also supplies a recursion-proof way to flatten a list. (Once you can iterate over an arbitrary sequence, the flattened version