Re: List mapping question

2005-02-03 Thread Steven Bethard
Marc Huffnagle wrote: Steve Holden wrote: >>> a,b,c = 1.1, 2.2, 3.3 >>> a,b,c = map(int, (a,b,c)) >>> a,b,c (1, 2, 3) >>> a,b,c = [int(x) for x in (a,b,c)] >>> a,b,c (1, 2, 3) regards Steve Thanks ... so there's no way to pass an actual variable into a list mapping, instead of its value? I

Re: About standard library improvement

2005-02-03 Thread Steven Bethard
Lee Harr wrote: On 2005-02-03, BJörn Lindqvist <[EMAIL PROTECTED]> wrote: The process seem slow. I've submitted two patches and haven't gotten any response so far, but it has only been three weeks. Other patches seem to be idling for months. I'm not complaining, just want to know why the process is

Re: dict indexed by lists - ?

2005-02-03 Thread Steven Bethard
Alexander Zatvornitskiy wrote: Hello All! I'am trying to make something like this: CPT={ ['b0','c0']:1, ['b0','c1']:0, ['b1','c0']:3, ['b1','c1']:1 } but python says "TypeError: list objects are unhashable" I can replace list with touple: CPT={ ('b0','c0'):1, ('b0','c1'):0, ...and so on. But, where

Re: Possible additions to the standard library? (WAS: About standard library improvement)

2005-02-04 Thread Steven Bethard
Daniel Bickett wrote: |def setreplace( self , set ): |""" |Do multiple replaces at once, using dictionary `set' as a legend, |where each instance of each key is to be replaced with that key's |value. |""" |if type( set ) == dict: |resu

Re: Computing class variable on demand?

2005-02-04 Thread Steven Bethard
fortepianissimo wrote: Thank you so much about this useful tip! I learned the new decorator feature of 2.4 simply because of your post. Unfortunately I don't have luxury right now to run Python 2.4 (for what I'm doing anyways). You mentioned the way to do decorator in 2.3. Still I have a question h

returning True, False or None

2005-02-04 Thread Steven Bethard
I have lists containing values that are all either True, False or None, e.g.: [True, None, None, False] [None, False, False, None ] [False, True, True, True ] etc. For a given list: * If all values are None, the function should return None. * If at least one value is True, t

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Alan McIntyre wrote: Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my original list is [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Alan McIntyre wrote: Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my original list is [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2

Re: returning True, False or None

2005-02-04 Thread Steven Bethard
Raymond Hettinger wrote: "Steven Bethard" For a given list: * If all values are None, the function should return None. * If at least one value is True, the function should return True. * Otherwise, the function should return False. . . . Right now, my code looks like: if T

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Mike C. Fletcher wrote: Alan McIntyre wrote: ... I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my original list is [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want t

Re: empty classes as c structs?

2005-02-04 Thread Steven Bethard
ow (instead of waiting for a future version of Python, assuming it's accepted), the current code for the Bunch class follows. STeVe -- # Copyright (c) 2004 Python Software Foundation. # All rights reserved. # Written by Steven

Re: string issue

2005-02-04 Thread Steven Bethard
rbt wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.

Re: string issue

2005-02-04 Thread Steven Bethard
Steve Holden wrote: You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Also worth noting, you can write this like: for ip in list(ips): ... if you're afraid of smiley-faces [:] in your code. ;) Steve -- http://mail.python.

Re: returning True, False or None

2005-02-04 Thread Steven Bethard
Fredrik Lundh wrote: Steven Bethard wrote: Raymond Hettinger wrote: return max(lst) Very clever! Thanks! too clever. boolean > None isn't guaranteed by the language specification: Yup. I thought about mentioning that for anyone who wasn't involved in the previous thread di

Re: string issue

2005-02-04 Thread Steven Bethard
rbt wrote: John J. Lee wrote: Steve Holden <[EMAIL PROTECTED]> writes: [...] You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Just to help popularise the alternative idiom, which IMO is significantly less cryptic (sane constructors of

Re: empty classes as c structs?

2005-02-05 Thread Steven Bethard
Nick Coghlan wrote: Steven Bethard wrote: Yes -- help me rally behind my generic object PEP which proposes a Bunch type (probably to be renamed) for the Python standard lib. =) Did you see the suggestion of 'namespace' as a name? Yup, it's in the current PEP draft. See the "

Re: empty classes as c structs?

2005-02-05 Thread Steven Bethard
Nick Coghlan wrote: Alex Martelli wrote: Nick Coghlan <[EMAIL PROTECTED]> wrote: ... Michael Spencer also posted an interesting idea recently about setting up a view of an existing dictionary, rather than as a separate object: class attr_view(object): def __init__(self, data): object.__s

Re: Computing class variable on demand?

2005-02-05 Thread Steven Bethard
fortepianissimo wrote: Thanks Steve - actually my question was simpler than that. I just wanted to use Daniels' recipe of lazy initialization on objects with __slots__: class Lazy(object): def __init__(self, calculate_function): self._calculate = calculate_function def __get__(self,

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Steven Bethard
Alex Martelli wrote: Steven Bethard <[EMAIL PROTECTED]> wrote: Here's a solution that works for iterables other than lists: py> def collapse(iterable): ... enumeration = enumerate(iterable) ... _, lastitem = enumeration.next() ... yield lastitem ... for i, item

Re: extreme newbie

2005-06-18 Thread Steven D'Aprano
nition among software purchasers and IT departments; and it was (usually) a future sale to Microsoft. It was only as Microsoft approached monopoly status that copyright infringement began to hurt them more than it gained them. With few if any competitors, the loss of revenue from unauthorized copie

Re: extreme newbie

2005-06-18 Thread Steven D'Aprano
On Sat, 18 Jun 2005 15:43:24 -0400, Chinook wrote: > Steven, > > Your weigh-in on semantics is misleading, How is it misleading? > but your elaboration of the aspect is very well put. > > As to semantics, piracy is to the originator what freedom fighter is to thos

Re: calling subclass constructor question

2005-06-19 Thread Steven Bethard
In Han Kang wrote: > Anyway, I was wondering...I have an super class which is the superclass > for 5 other classes. However, I want to be able to call the subclass > constructors from the super class. Is this possible? Are you trying to use your superclass as a factory? If so, one option is s

Re: calling subclass constructor question

2005-06-19 Thread Steven Bethard
In Han Kang wrote: > So each of the sub classes plots a different type of graph. The > superclass defines methods that are the same for all the plots. I want > to be able to pick some points and be able to generate a more plots. > What I was wondering if I could define in a method in the supe

Re: import via pathname

2005-06-20 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > I know this is wrong syntax, but I think it demonstrates what I'm > trying to do: > > import myModule path = /modules/myModule > import myModule2 path = /modules/myModule2 > > Something like that. Is it possible? I would put your additional modules into a 'modules' di

Re: Couple functions I need, assuming they exist?

2005-06-20 Thread Steven Bethard
Charles Krug wrote: > myWords = split(aString, aChar) > > is depreciated but > > myWords = aString.split(aChgar) > > is not? Yes, that's basically correct. What's deprecated are the functions in the string module. So string.split(a_str, b_str) is deprecated in favor of a_str.split

Re: import via pathname

2005-06-20 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > AhhI see. I played around with the sys.path function...and it > looks like python automatically looks in the same directory as my > script first. Then is searches to all the other pre-defined paths. So > it works for me to just keep my main script in the same direc

Re: Python and encodings drives me crazy

2005-06-20 Thread Steven Bethard
Oliver Andrich wrote: > def remove_html_entities(data): > for html, char in html2text: > data = apply(string.replace, [data, html, char]) > return data I know this isn't your question, but why write: > data = apply(string.replace, [data, html, char]) when you could write data

Re: Tuple Unpacking in raise

2005-06-20 Thread Steven Bethard
James Stroud wrote: > Hello All, > > Is this a bug? Why is this tuple getting unpacked by raise? Am I missing some > subtle logic? Why does print not work the same way as raise? Both are > statements. Why does raise need to be so special? > > py> sometup = 1,2 > py> print sometup > (1, 2) > py>

Re: Want to learn a language - is Python right?

2005-06-21 Thread Steven D'Aprano
tter way of putting what James tried to get across is, don't be too concerned about writing fast code until you have learnt the basics of the language. Get the code WORKING first, and FAST second. (And frequently, the best way to get it working is also the best way to have it run fast.) -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Nested lists [was Re: tree functions daily exercise: Table]

2005-06-21 Thread Steven D'Aprano
i in dummies: funString += i + ',' > funString = funString[0:len(funString)-1] > funString += ')' > loopStr= '[ ' + funString > for i in range(0,dim): > loopStr += ' for ' + dummies[i] + ' in Range(' + > r

Re: utf8 silly question

2005-06-21 Thread Steven Bethard
Grig Gheorghiu wrote: import codecs print codecs.encode(c, 'utf-8') > > © some text Or simply: py> print c.encode('utf-8') © some text -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop until condition is true

2005-06-21 Thread Steven D'Aprano
h risk. It also relies on an implementation detail, that True and False are singletons (doubletons?), and will break if that changes. And finally, since True is just an object, and not a constant like None, it will break if True gets set to some other value. In other words, it is not only superfluous but also fragile. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Loop until condition is true

2005-06-22 Thread Steven D'Aprano
On Wed, 22 Jun 2005 09:54:44 +0200, Fredrik Lundh wrote: >> [CENSORED] I keep for myself how stupid I found your post. > > so let's see if everyone who understands how embarrassingly stupid > your post is will keep that to themselves... Damn, did I fail some test? -

Re: how to use more than 1 __init__ constructor in a class ?

2005-06-22 Thread Steven D'Aprano
I thought I was just calling other functions :-) That's the joys of a mostly self-taught programming knowledge: you miss out on all the buzzwords. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Does a function like isset() exist in Python?

2005-06-23 Thread Steven D'Aprano
which act in a similar way, you would say try: False except NameError: False = 0 True = not False Note that there is no need to do something with the name "False" in the try clause -- just use it. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: pickle broken: can't handle NaN or Infinity under win32

2005-06-23 Thread Steven D'Aprano
because the test did atan2 on the > components of an all-zero complex raised to an integer power, and I > had written one of the few 754-conforming libms at the time. They > wanted 0, while my atan2 dutifully returned -pi. I haven't had much > personal love for 75

Re: Hardening enviroment by overloading __import__?

2005-06-23 Thread Steven Bethard
Steve Juranich wrote: > I have in some code an 'eval', which I hate, but it's the shortest > path to where I need to get at this point. What's this code trying to do? If you care about malicious code at all, you'll avoid 'eval' completely. A couple reasons why: With only a little trouble, I ca

Re: trouble subclassing str

2005-06-23 Thread Steven D'Aprano
return self.value (only with error checking etc for production code). Then you use it like this: py> myprintablestr = MyClass("Lovely Spam!") py> print myprintablestr *** Lovely Spam!!! *** Am I close? -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: pickle broken: can't handle NaN or Infinity under win32

2005-06-23 Thread Steven D'Aprano
Tim Peters wrote: > [Steven D'Aprano] >>It isn't necessary to look at complex numbers to see the difference >>between positive and negative zero. Just look at a graph of y=1/x. In >>particular, look at the behaviour of the graph around x=0. Now tell me >>

Re: Annoying behaviour of the != operator

2005-06-23 Thread Steven D'Aprano
ount of your target; - try to avoid adding or subtracting numbers of wildly differing magnitudes; and - be aware of the risk of errors blowing out and have strategies in place to manage the size of the error. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-24 Thread Steven D'Aprano
.blue := 255; colour.green := 0; Okay, so maybe it is more of a feature than a trick, but I miss it and it would be nice to have in Python. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-24 Thread Steven D'Aprano
from Pascal with self: blue = blue green = green red = red And now you can see why Python doesn't support this idiom. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Is there something similar to ?: operator (C/C++) in Python?

2005-06-24 Thread Steven D'Aprano
There's an easy way to settle this once and for all. If somebody can come up with a good empirical test for "easier" (easier what?), we can run some tests, get some hard figures for, eg, how many calories are expended in typing one form over the other, and find out which is easier. Until then, the question is at least partly a matter of personal taste, and as such, there is no justification for such sweeping generalities as "the answer is simply no, just use an if statement instead". -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: formatted xml output from ElementTree inconsistency

2005-06-24 Thread Steven Bethard
Matthew Thorley wrote: > from elementtree import ElementTree as et > > xmla = et.ElementTree('some_file.xml') > xmlb = et.Element('parent') > et.SubElement(xmlb, 'child1') > et.SubElement(xmlb, 'child2') > > root = et.Element('root') > root.append(xmla.getroot()) > root.append(xmlb) > > print et

Re: How does one write a function that increments a number?

2005-06-24 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > Apologies if this question seems stupid: How does one write a > function that increments a value in Python? When I tried, the variable > never changed. > The session went like this: > def incr(counter): > counter = int(counter) > counter += 1 > cou

Re: what list comprehension can't

2005-06-25 Thread Steven D'Aprano
doesn't exactly fit the specification but may > be easier to read. Or, more generally, [0 for x in L if x is None] which doesn't solve the asked-for question, but is a useful feature. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
old the entire list in memory. > 2.) Contracts Explain please. > 3.) With Explain please. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
On Fri, 24 Jun 2005 15:47:45 -0700, James Stroud wrote: > On Friday 24 June 2005 05:58 am, Steven D'Aprano wrote: >> with colour do begin >> red := 0; blue := 255; green := 0; >> end; >> >> instead of: >> >> colour.red := 0; colour.blue := 255;

Re: How does one write a function that increments a number?

2005-06-25 Thread Steven D'Aprano
): > counter = int(counter) > counter += 1 > >>>> counter = 1 >>>> incr(counter) >>>> print counter > 1 Why does it need to be a function? Why complicate matters? Isn't it simpler to just do this: counter = 1 counter += 1 print counter -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: a dictionary from a list

2005-06-25 Thread Steven D'Aprano
x for initialising dicts conflicts with the use of keyword arguments for non-existent, hypothetical and/or user-defined classes. That's okay. I'm perfectly comfortable with the fact that the syntax for initialising a dict conflicts with the syntax for initialising a list, a Decimal, a MutableString, and a ConfigParser object. So why should I be distressed that it conflicts with the syntax for initialising MyDictWithDefaultValue objects? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
On Sat, 25 Jun 2005 17:41:58 +0200, Konstantin Veretennicov wrote: > On 6/25/05, Mandus <[EMAIL PROTECTED]> wrote: >> It is really a consensus on this; that >> removing map, filter, reduce is a good thing? It will render a whole lot >> of my software unusable :( > > I think you'll be able to use

python-dev Summary for 2005-05-01 through 2005-05-15

2005-06-25 Thread Steven Bethard
[The HTML version of this Summary is available at http://www.python.org/dev/summary/2005-05-01_2005-05-15.html] = Summary Announcements = -- PEP 340 Episode 2: Revenge of the With (Block) -

python-dev Summary for 2005-05-16 through 2005-05-31

2005-06-25 Thread Steven Bethard
[The HTML version of this Summary is available at http://www.python.org/dev/summary/2005-05-16_2005-05-31.html] = Summary Announcements = QOTF We have our first ever Quote of the Fortnight (QOTF), thanks to the wave of discussion over `PEP 343`_

python-dev Summary for 2005-06-01 through 2005-06-15

2005-06-25 Thread Steven Bethard
[The HTML version of this Summary is available at http://www.python.org/dev/summary/2005-06-01_2005-06-15.html] = Summary Announcements = - Bug Day: Saturday, June 25th 2005 - AMK organized an

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
>>>> list comprehensions a bit better. >>>> >> >> >> Couldnt there just be a "functional" module ?... >> >> from functional import map, filter, reduce > > but lambda is grammar, so probably not so easy to import? "from __future

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
On Sat, 25 Jun 2005 15:44:14 -0400, Nicolas Fleury wrote: > Steven D'Aprano wrote: >> One of the things I liked in Pascal was the "with" keyword. You could >> write something like this: >> >> with colour do begin >> red := 0; blue := 255; green :=

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
ile our code every single time to know this? Isn't it reasonable to just say, "I use join because it is faster than adding strings" without being abused for invalid optimization? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
omments suggest to me that they are safe until list comps are significantly faster. But reduce needs to be re-factored as a for loop, with all the disadvantages that implies. I think that if people like Mandus have good real-world usage cases for reduce, it can be saved, at least as part of

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
hold the intermediate results of from the for loop. Yes, that seems to be the case." Funny how people differ in what they find readable :-) -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
On Sat, 25 Jun 2005 18:44:12 -0700, James Stroud wrote: > On Saturday 25 June 2005 11:08 am, Steven D'Aprano wrote: >> The problem is, you have made colour (returning to English spelling >> instead of foreign) into a class. If you need two colour variables, you >> have

Re: Favorite non-python language trick?

2005-06-25 Thread Steven D'Aprano
On Sat, 25 Jun 2005 23:08:10 +, Bengt Richter wrote: > On Sun, 26 Jun 2005 04:08:31 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >>On Fri, 24 Jun 2005 15:47:45 -0700, James Stroud wrote: >> >>> On Friday 24 June 2005 05:58 am, Steven D'Apran

Rebindings [was Re: Favorite non-python language trick?]

2005-06-25 Thread Steven D'Aprano
here, ;-/ Given how much the use of global variables are discouraged, is it a good idea to allow even more inter-namespace interactions? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Thoughts on Guido's ITC audio interview

2005-06-25 Thread Steven D'Aprano
ever throws an exception > (unless the parameter list has the wrong types). Its an example of a > method that can be used cleanly with a type inferencer, while the index > and find methods cannot. Er, how does that last one work? How can findall be used cleanly? Why can't find? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: noob question

2005-06-26 Thread Steven D'Aprano
, easy to understand examples of where Python's name/object model differs from the variable/value model? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Thoughts on Guido's ITC audio interview

2005-06-26 Thread Steven D'Aprano
exchanging one special case for another special case. > The result of find() does > not have this property: it can be an integer that is an index into the > string, > or it can be a -1. Both of which are ints. What you are talking about isn't really TYPE checking, but more like

Re: Favorite non-python language trick?

2005-06-27 Thread Steven D'Aprano
do assignment like this: > > a, b = (1,)*2 > > But I guess that's not exactly elegant. ;-) In general that is not the same thing as a = b = obj. py> a, b = ([], []) py> a.append(1) py> b [] py> a = b = [] py> a.append(1) py> b [1] -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: noob question

2005-06-27 Thread Steven D'Aprano
The history and justification for Hungarian notation is explained here: http://www.joelonsoftware.com/articles/Wrong.html -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Modules for inclusion in standard library?

2005-06-27 Thread Steven Bethard
Reinhold Birkenfeld wrote: > For my part, ctypes seems like a suggestion to start with. I believe this has been discussed somewhere before and the conclusion was that ctypes should not be a candidate for inclusion in the Python stdlib because people don't want things in the stdlib that can make

Re: Modules for inclusion in standard library?

2005-06-27 Thread Steven Bethard
Fredrik Johansson wrote: > On 6/27/05, Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: > >>Do you have any other good and valued Python modules that you would think are >>bug-free, mature (that includes a long release distance) and useful enough to >>be granted a place in the stdlib? > > First of

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > if type(input) == str: You might consider writing this as: if isinstance(input, basestring): I don't know if pyparsing ever produces unicode objects, but in case it does (or it starts to in the future), isinstance is a better call here. STeVe -- http://mail

Re: When someone from Britain speaks, Americans hear a "British accent"...

2005-06-29 Thread Steven D'Aprano
in a la-bor-atory, not a lab-rat-ory, even if they have lab rats in the laboratory. Fans of the X-Men movies and comics will remember Professor Charles Xavier. Unless you are Spanish (Kh-avier), the X sounds like a Z: Zaviour. But never never never Xecks-Aviour or Eggs-Savior. Nuclear. Say no more. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: strange __call__

2005-06-29 Thread Steven Bethard
Rahul wrote: > def wrapper(obj): >g = obj.__call__ >def f(*args,**kwargs): > for arg in args:print arg > return g(*args,**kwargs) >obj.__call__=f > but it seems this will not work for functions :( def wrap(obj): def f(*args, **kwargs): for arg in args:

Re: Modules for inclusion in standard library?

2005-06-29 Thread Steven Bethard
Noah wrote: > def unzip(list): > if len(list) == 0: return () > l = [] > for t in range(len(list[0])): > l.append(map( lambda x,t=t: x[t], list )) > return tuple(l) The simplest solution to this problem that I know of: def unzip(iterable): return zip(*iterabl

Re: strange __call__

2005-06-29 Thread Steven Bethard
Steven Bethard wrote: > > def wrap(obj): > def f(*args, **kwargs): > for arg in args: > print arg > return obj(*args, **kwargs) > return f > > @wrap > def func(a, b, c): > ... > > class C(object): >

Re: Inheriting from object

2005-06-29 Thread Steven Bethard
Fuzzyman wrote: > Surely when they are removed : > > class foo: > pass > > won't become invalid syntax, it will just automatically inherit from > object ? Well, Guido views this particular syntax as an "oopsie"[1]. It should actually look like: class C(): pass Guido also su

aligning text with space-normalized text

2005-06-29 Thread Steven Bethard
I have a string with a bunch of whitespace in it, and a series of chunks of that string whose indices I need to find. However, the chunks have been whitespace-normalized, so that multiple spaces and newlines have been converted to single spaces as if by ' '.join(chunk.split()). Some example d

Re: aligning text with space-normalized text

2005-06-29 Thread Steven Bethard
John Machin wrote: > If "work" is meant to detect *all* possibilities of 'chunks' not having > been derived from 'text' in the described manner, then it doesn't work > -- all information about the positions of the whitespace is thrown away > by your code. > > For example, text = 'foo bar', chun

Re: Help please: How to assign an object name at runtime

2005-06-29 Thread Steven D'Aprano
from the context: def stepsum(n, step=1): total = 0 for i in range(0, n, step): total + i return total def add_colon_str(s, t): return s + ": " + t It doesn't need much explanation to understand what s and t are. But for anything more complex, I always use descriptive names. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: aligning text with space-normalized text

2005-06-30 Thread Steven Bethard
John Machin wrote: > Steven Bethard wrote: > >> John Machin wrote: >> >>> For example, text = 'foo bar', chunks = ['foobar'] >> >> This doesn't match the (admittedly vague) spec > > That is *exactly* my point -- it is not val

Re: Speaking of list-comprehension?

2005-06-30 Thread Steven Bethard
Chinook wrote: > >>> ta = [5, 15, 12, 10, 9] > >>> for i in range(len(ta)): > ... if ta[i] >= 10: > ... ta[i] -= 10 > ... else: > ... ta[i] += 10 > ... > >>> ta > [15, 5, 2, 0, 19] One possibility: py> [(tai + 10, tai - 10)[tai >= 10] for tai in ta] [15, 5, 2, 0, 19] But see: htt

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-01 Thread Steven Bethard
[EMAIL PROTECTED] wrote: > I avoid map sometimes, because I find its syntax less readable > than list (and expression) comprehensions. But occasionally it > is the most readable way to do something, and I wouldn't want to lose > it. I tend to avoid map as much as possible. The only places I'm sti

Re: aligning text with space-normalized text

2005-07-01 Thread Steven Bethard
Peter Otten wrote: > import re > _reLump = re.compile(r"\S+") > > def indices(text, chunks): > lumps = _reLump.finditer(text) > for chunk in chunks: > lump = [lumps.next() for _ in chunk.split()] > yield lump[0].start(), lump[-1].end() Thanks, that's a really nice, clean s

Re: Inheriting from object

2005-07-02 Thread Steven Bethard
Bengt Richter wrote: > I wonder if the above common use of super could be implemented as a property > of object, > so you'd normally inherit it and be able to write > self.super.__init__(*args, **kwargs) # (maybe spell it > self.__super__.__init__(...) I suppose) > > I.e., self.__super__ wo

Re: Favorite non-python language trick?

2005-07-02 Thread Steven D'Aprano
o know. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-02 Thread Steven D'Aprano
On Fri, 01 Jul 2005 09:13:58 -0700, mcherm wrote: > Lambda serves a very specific purpose: declaring small, in-place > functions which are no bigger than a single expression. I do this often > enough that I DO want special syntax for it. But I'll admit that I > wish "lambda" were about 5 or 6 cha

Re: Assigning to None (was Re: Question about Python)

2005-07-02 Thread Steven D'Aprano
that demonstrated that this made a significant -- or even measurable -- speed-up in anything but the most unusual cases. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-02 Thread Steven D'Aprano
to be moved into a module rather than dropped altogether. Provided list comps are made as fast as map and filter, then at the cost of readability they can be replaced by list comps. But reduce can't be written as a list comp, only as a relatively complex for loop at a HUGE loss of rea

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-02 Thread Steven D'Aprano
Guido seems to like list comprehensions. Unless I'm mistaken (not for the first time) I think he actually introduced them to the language. They won't be going anywhere anytime soon. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Another newbie question from Nathan.

2005-07-02 Thread Steven D'Aprano
e it this way: rand_nums = four_random() # rand_nums is a list of four numbers print rand_nums[0] # prints the first random number print rand_nums[3] # prints the last one or like this: alpha, beta, gamma, delta = four_random() # four names for four separate numbers -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

math.nroot [was Re: A brief question.]

2005-07-02 Thread Steven D'Aprano
0 for any non-zero N return 0.0 else: return math.exp(math.log(x)/y) Note how much simpler this would be if we could guarantee proper infinities and NaNs in the code. We could turn a 23-line block to a one-liner. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-02 Thread Steven D'Aprano
of reduce. > Also, map is easily replaced. > map(f1, sequence) == [f1(element) for element in sequence] Three mental tokens ( map, f1, sequence ) versus seven ( [], f1, element, for, element, in, sequence ). Also, map can take any number of sequences: map(f1, seq1, seq2, seq3, seq4, ...

Re: Favorite non-python language trick?

2005-07-03 Thread Steven D'Aprano
common ones. Er, excuse me, but that is EXACTLY what Devan claimed. Quote: "With the exception of reduce(lambda x,y:x*y, sequence), reduce can be replaced with sum, and Guido wants to add a product function." -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: a question

2005-07-03 Thread Steven D'Aprano
ps2' > why does it happen? It works for me. >>> import sys >>> sys.ps2 '... ' Can you cut and past the relevant lines from your interactive session? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: math.nroot [was Re: A brief question.]

2005-07-03 Thread Steven D'Aprano
On Sun, 03 Jul 2005 10:56:42 +0100, Tom Anderson wrote: > On Sun, 3 Jul 2005, Steven D'Aprano wrote: > >> On Sun, 03 Jul 2005 02:22:23 +0200, Fredrik Johansson wrote: >> >>> On 7/3/05, Tom Anderson <[EMAIL PROTECTED]> wrote: >>>> Tha

Re: math.nroot [was Re: A brief question.]

2005-07-03 Thread Steven D'Aprano
od reason for it, though. Yes. You generally want exponentiation to have the highest precedence. 2*3**4 should give 162, not 1296. Think about how you would write that mathematically, with pencil and paper: the 4 is written as a superscript over the 3, and is applied to that before multiplying by the 2. Unary minus is equivalent to multiplying by -1, so -3**4 is equivalent to -1*3**4. These are the rules of precedence mathematicians have worked out over many centuries. Yes, some of them are arbitrary choices, but they do work, and changing them for another arbitrary choice doesn't give us any benefit. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientificmini-survey

2005-07-03 Thread Steven D'Aprano
urreal. Reduce etc *work*, right now. They have worked for years. If people don't like them, nobody is forcing them to use them. Python is being pushed into directions which are *far* harder to understand than map and reduce (currying, decorators, etc) and people don't complain about those. And yet something as simple and basic as map is supposed to give them trouble? These are the same people who clamoured for zip, which is just a special case of map? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Assigning to None (was Re: Question about Python)

2005-07-03 Thread Steven D'Aprano
On Sun, 03 Jul 2005 19:19:05 +, Bengt Richter wrote: > On Sun, 03 Jul 2005 11:47:07 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > >>On Fri, 01 Jul 2005 12:59:20 -0400, François Pinard wrote: >> >>> [Peter Hansen] >>>> Mike Meyer wrote:

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-03 Thread Steven D'Aprano
Because that is far less readable, and you take a performance hit. > But I suspect that most people would just do what I currently do and > write the for-loop to do what they want directly instead of using lambda > in reduce. That's your choice. I'm not suggesting we remove for loops and force you to use reduce. Or even list comps. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-03 Thread Steven Bethard
Christopher Subich wrote: > One caevat that I just noticed, though -- with the for-solution, you do > need to be careful about whether you're using a generator or list if you > do not set an explicit initial value (and instead use the first value of > 'sequence' as the start). The difference is

Re: math.nroot [was Re: A brief question.]

2005-07-03 Thread Steven D'Aprano
ill say this. Practicality beats purity. If you can demonstrate a good usage case for testing equality with NaNs, I will accept that it is a good thing to do. if x in (SOME_NAN, SOME_OTHER_NAN, ANOTHER_NAN, \ YET_ANOTHER_NAN, AND_ANOTHER_NAN, ..., \ # 240+ more NaNs listed ALMOST_FINISHED_NOW, LAST_NAN): print "Calculation failed!" is _not_ a good usage case, since it is best handled with: if isNan(x): # handled by the floating point package print "Calculation failed!" -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

<    40   41   42   43   44   45   46   47   48   49   >