Re: Catching a non-Exception object (KeyboardInterrupt)

2008-02-04 Thread Duncan Booth
Hrvoje Niksic [EMAIL PROTECTED] wrote: Michael Goerz [EMAIL PROTECTED] writes: when I try to catch ctrl+c with except KeyboardInterrupt: pychecker tells me Catching a non-Exception object (KeyboardInterrupt) Looks like a pychecker bug. It might be confused by KeyboardInterrupt

Re: refcount

2008-01-29 Thread Duncan Booth
Simon Pickles [EMAIL PROTECTED] wrote: Is is possible to access the refcount for an object? import sys sys.getrefcount(42) 6 Ideally, I am looking to see if I have a refcount of 1 before calling del That's a pointless exercise: you probably don't understand what del does. All that del does

Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-29 Thread Duncan Booth
Berteun Damman [EMAIL PROTECTED] wrote: On Tue, 29 Jan 2008 09:23:16 -0800 (PST), [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: If you're going to delete elements from a list while iterating over it, then do it in reverse order: Why so hard? Reversing it that way creates a copy, so you might

Re: is possible to get order of keyword parameters ?

2008-01-26 Thread Duncan Booth
rndblnch [EMAIL PROTECTED] wrote: On Jan 25, 9:01 pm, Duncan Booth [EMAIL PROTECTED] wrote: rndblnch [EMAIL PROTECTED] wrote: the following example should also work: size = Point(width=23, height=45) w, h = size So you want the unpacking to depend on how the Point was initialised

Re: is possible to get order of keyword parameters ?

2008-01-25 Thread Duncan Booth
rndblnch [EMAIL PROTECTED] wrote: (sorry, draft message gone to fast) i.e. is it possible to write such a function: def f(**kwargs): skipped return result such as : f(x=12, y=24) == ['x', 'y'] f(y=24, x=12) == ['y', 'x'] what i need is to get the order of the keyword

Re: is possible to get order of keyword parameters ?

2008-01-25 Thread Duncan Booth
rndblnch [EMAIL PROTECTED] wrote: the following example should also work: size = Point(width=23, height=45) w, h = size So you want the unpacking to depend on how the Point was initialised! Aaargh! -- http://mail.python.org/mailman/listinfo/python-list

Re: Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?

2008-01-23 Thread Duncan Booth
Kristian Domke [EMAIL PROTECTED] wrote: foo = (not f and 1) or 0 In this case f may be None or a string. If I am not wrong here, one could simply write foo = not f Yes, it sounds pretty silly, and not just on the level you spotted. The only difference between the two expressions is

Re: Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?

2008-01-23 Thread Duncan Booth
Steven D'Aprano [EMAIL PROTECTED] wrote: On Wed, 23 Jan 2008 09:30:28 +, Duncan Booth wrote: Kristian Domke [EMAIL PROTECTED] wrote: foo = (not f and 1) or 0 In this case f may be None or a string. If I am not wrong here, one could simply write foo = not f Yes, it sounds

Re: problem with 'global'

2008-01-21 Thread Duncan Booth
Mel [EMAIL PROTECTED] wrote: oyster wrote: why the following 2 prg give different results? a.py is ok, but b.py is 'undefiend a' I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 #a.py def run(): if 1==2:# note, it

Re: problem with 'global'

2008-01-21 Thread Duncan Booth
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote: The future statement is another example, even worse: if 0: from __future__ import with_statement with open(xxx) as f: print f In Python =2.5 it's a compile time

Re: Loop in a loop?

2008-01-17 Thread Duncan Booth
Chris [EMAIL PROTECTED] wrote: You could always pre-pad the lists you are using before using the zip function, kinda like def pad(*iterables): max_length = 0 for each_iterable in iterables: if len(each_iterable) max_length: max_length = len(each_iterable) for

Re: Interesting Thread Gotcha

2008-01-16 Thread Duncan Booth
Hendrik van Rooyen [EMAIL PROTECTED] wrote: It would have been nice, however, to have gotten something like: TypeError - This routine needs a tuple. instead of the silent in line calling of the routine in question, while failing actually to start a new thread. Given that the

Re: import from question

2008-01-15 Thread Duncan Booth
iu2 [EMAIL PROTECTED] wrote: file a3.py: from a1 import the_number import a2 ... Why doesn't it work in the first version of a3.py? Think of 'import a2' as being the same as: a2 = __import__('a2') and 'from a1 import the_number' as roughly the same as: the_number =

Re: NotImplimentedError

2008-01-14 Thread Duncan Booth
Gary Herron [EMAIL PROTECTED] wrote: hakim ouaras wrote: Hi, I am begining with python, I want to know what is the utility and how to use the expression NotImplementedError. Thak you for your answers Hakim It's meant to be used to mark a procedure that you intend to write, but have

Re: Analyzing Python GC output - what is a cell, and what information is available about it.

2008-01-11 Thread Duncan Booth
John Nagle [EMAIL PROTECTED] wrote: I'm printing out each entry in gc.garbage after a garbage collection in DEBUG_LEAK mode, and I'm seeing many entries like cell at 0x00F7C170: function object at 0x00FDD6B0 That's the output of repr. Are cell objects created only from external C

Re: HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS??

2008-01-11 Thread Duncan Booth
abhishek [EMAIL PROTECTED] wrote: Hi group any idea on HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS?? Yes, it indicates you haven't read http://catb.org/~esr/faqs/smart-questions.html -- http://mail.python.org/mailman/listinfo/python-list

Re: python recursive function

2008-01-11 Thread Duncan Booth
Bruno Desthuilliers [EMAIL PROTECTED] wrote: You want: return bears(n - 42) Actually, no he doesn't. He needs to explore all options when the first attempt fails. But I'm not going to write his homework for him. -- http://mail.python.org/mailman/listinfo/python-list

Re: for loop without variable

2008-01-10 Thread Duncan Booth
erik gartz [EMAIL PROTECTED] wrote: Hi. I'd like to be able to write a loop such as: for i in range(10): pass but without the i variable. The reason for this is I'm using pylint and it complains about the unused variable i. I can achieve the above with more lines of code like: i = 0

Re: stupid/style/list question

2008-01-09 Thread Duncan Booth
Fredrik Lundh [EMAIL PROTECTED] wrote: Giampaolo Rodola' wrote: To flush a list it is better doing del mylist[:] or mylist = []? Is there a preferred way? If yes, why? The latter creates a new list object, the former modifies an existing list in place. The latter is shorter, reads

Re: alternating string replace

2008-01-09 Thread Duncan Booth
cesco [EMAIL PROTECTED] wrote: Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 = 'hi:cat,bye:dog' Is there a common recipe to accomplish that? I can't

Re: stupid/style/list question

2008-01-09 Thread Duncan Booth
[EMAIL PROTECTED] wrote: Duncan Booth: I tried to measure this with timeit, and it looks like the 'del' is actually quite a bit faster (which I find suprising). Yes, it was usually faster in my benchmarks too. Something similar is true for dicts too. I think such timings are influenced

Re: Treating a unicode string as latin-1

2008-01-03 Thread Duncan Booth
Simon Willison [EMAIL PROTECTED] wrote: How can I tell Python I know this says it's a unicode string, but I need you to treat it like a bytestring? Can you not just fix your xml file so that it uses the same encoding as it claims to use? If the xml says it contains utf8 encoded data then it

Re: Treating a unicode string as latin-1

2008-01-03 Thread Duncan Booth
Fredrik Lundh [EMAIL PROTECTED] wrote: ET has already decoded the CP1252 data for you. If you want UTF-8, all you need to do is to encode it: u'Bob\x92s Breakfast'.encode('utf8') 'Bob\xc2\x92s Breakfast' I think he is claiming that the encoding information in the file is incorrect and

Re: Is there a string function to trim all non-ascii characters out of a string

2007-12-31 Thread Duncan Booth
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, Is there a string function to trim all non-ascii characters out of a string? Let say I have a string in python (which is utf8 encoded), is there a python function which I can convert that to a string which composed of only ascii characters?

Re: fiber(cooperative multi-threading)

2007-12-24 Thread Duncan Booth
Michael Sparks [EMAIL PROTECTED] wrote: To be clear - I REALLY didn't like the fact that generators were single layer when I first saw them - it seemed a huge limitation. (Indeed as huge a limitation as only having single level function calls, or only single layer of nesting for namespaces,

Re: fiber(cooperative multi-threading)

2007-12-23 Thread Duncan Booth
Akihiro KAYAMA [EMAIL PROTECTED] wrote: Thanks for your replies. But current Python generator specification requires me: def f1(): for x in foo(f2, foo): yield x for x in foo(f2, foo): yield x # XXX v = ... (I don't know how to do this) for x in foo(f2, v=%s world % v,

Re: fiber(cooperative multi-threading)

2007-12-23 Thread Duncan Booth
Michael Sparks [EMAIL PROTECTED] wrote: Duncan Booth wrote: Unfortunately generators only save a single level of stack-frame, so they are not really a replacement for fibers/coroutines. The OP should perhaps look at Stackless Python or Greenlets. See On the surface of things, the single

Re: Performance on local constants?

2007-12-22 Thread Duncan Booth
William McBrine [EMAIL PROTECTED] wrote: Hi all, I'm pretty new to Python (a little over a month). I was wondering -- is something like this: s = re.compile('whatever') def t(whatnot): return s.search(whatnot) for i in xrange(1000): print t(something[i]) significantly

Re: fiber(cooperative multi-threading)

2007-12-22 Thread Duncan Booth
Arnaud Delobelle [EMAIL PROTECTED] wrote: I am not really familiar with ruby but these fibers seem to be some sort of coroutines. Since python 2.5, generators can be sent values, this can be used to implement what you want. I have had a got at it for fun and this is what I came up with:

Re: Instrospection question

2007-12-21 Thread Duncan Booth
Matias Surdi [EMAIL PROTECTED] wrote: I have the following code: -- import new class A: def a(self): print Original def other(cad): return cad + modified def replace_method(method): def b(self,*args,**kwargs): result =

Re: How to memoize/cache property access?

2007-12-20 Thread Duncan Booth
thebjorn [EMAIL PROTECTED] wrote: It would have been nice to be able to write class Foo(object): @property def expensive(self): self.expensive = insert expensive db call here return self.expensive but apparently I can't set [that] attribute :-( You can set and

Re: Is this a bug in int()?

2007-12-20 Thread Duncan Booth
[EMAIL PROTECTED] wrote under the subject line Is this a bug in int()?: int('0x', 16) 0 I think it is a general problem in the tokenizer, not just the 'int' constructor. The syntax for integers says: hexinteger ::= 0 (x | X) hexdigit+ but 0x appears to be accepted in source code as an

Re: checking a string against multiple patterns

2007-12-18 Thread Duncan Booth
tomasz [EMAIL PROTECTED] wrote: Is there an alternative to it? Am I missing something? Python doesn't have special variables $1, $2 (right?) so you must assign the result of a match to a variable, to be able to access the groups. Look for repetition in your code and remove it. That will

Re: Best idiom to unpack a variable-size sequence

2007-12-18 Thread Duncan Booth
ram [EMAIL PROTECTED] wrote: Here's a little issue I run into more than I like: I often need to unpack a sequence that may be too short or too long into a fixed-size set of items: a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2) What I usually do is something like this:

Re: IDLE question

2007-12-16 Thread Duncan Booth
[EMAIL PROTECTED] wrote: I'm using IDLE for my Python programming. I can't seem to solve one issue though. Whenever I try to indent a region of code, I simply select it and hit the tab key, as I usually do in most editors, like GEdit or Geany on Linux, for instance, and it works fine. But,

Re: Question from a python newbie

2007-12-13 Thread Duncan Booth
Russell [EMAIL PROTECTED] wrote: I've searched the Language Reference and was not able to find any info regarding the structure of this code fragment: int(text) if text.isdigit() else text http://docs.python.org/whatsnew/pep-308.html -- http://mail.python.org/mailman/listinfo/python-list

Re: Is a real C-Python possible?

2007-12-13 Thread Duncan Booth
Christian Heimes [EMAIL PROTECTED] wrote: Python 2.6 and 3.0 have a more Pythonic way for the problem: class A(object): @property def foo(self): return self._foo @foo.setter def foo(self, value) self._foo = value @foo.deletter def foo(self)

Re: Help needed with python unicode cgi-bin script

2007-12-12 Thread Duncan Booth
weheh [EMAIL PROTECTED] wrote: John and Martin, Thanks for your help. However, I have identified the culprit to be with Apache and the command: AddDefaultCharset utf-8 which forces my browser to utf-8 encoding. It looks like your suggestions to change charset were incorrect. My

Re: Help needed with python unicode cgi-bin script

2007-12-12 Thread Duncan Booth
weheh [EMAIL PROTECTED] wrote: Hi Duncan, thanks for the reply. FWIW, the code you posted only ever attempted to set the character set encoding using an html meta tag which is the wrong place to set it. The encoding specified in the HTTP headers always takes precedence. This is why the

Re: sqlite weirdness

2007-12-12 Thread Duncan Booth
[EMAIL PROTECTED] wrote: So the data us there, but the sql only works part of the time. My SQL works if my database is in SQL Server, but not sqlite. Is my SQL malformed? Is it something about dates in sqlite? Or is it something else? Your dateworked field seems to have strings rather than

Re: Is a real C-Python possible?

2007-12-11 Thread Duncan Booth
sturlamolden [EMAIL PROTECTED] wrote: On 9 Des, 23:34, Christian Heimes [EMAIL PROTECTED] wrote: http://antoniocangiano.com/2007/11/28/holy-shmoly-ruby-19-smokes-pyth ... The Ruby developers are allowed to be proud. They were able to optimize some aspects of the implementation to get one

Re: Are Python deques linked lists?

2007-12-11 Thread Duncan Booth
Peter Otten [EMAIL PROTECTED] wrote: Only if you try to modify the list from both of them. One deletion is enough to trigger the assertion: Yes, but the assertion isn't intended to be the complete code. Or you just rule that delete(x) must occur immediately after x = next(). My

Re: Are Python deques linked lists?

2007-12-11 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: If you put an instrumented iterator through, say, reversed or sorted, you'd lose the ability to use it to modify the list I think that is kind of irrelevant. reversed doesn't take an iterator, it requires a sequence and returns an iterator. sorted will

Re: Is anyone happy with csv module?

2007-12-11 Thread Duncan Booth
massimo s. [EMAIL PROTECTED] wrote: 1) Is there a good tutorial, example collection etc. on the csv module that I'm missing? Yes, see http://docs.python.org/lib/csv-examples.html 2) Is there an alternative csv read/write module? No but feel free to write your own 3) In case anyone else is

Re: Are Python deques linked lists?

2007-12-10 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: Python's iterators are unsuitable for mutating the linked list while iterating--the only major application of linked lists. Wrapping in a generator won't allow you to use for loop syntax, unless I'm missing something, which has often happened. It is

Re: Are Python deques linked lists?

2007-12-10 Thread Duncan Booth
Peter Otten [EMAIL PROTECTED] wrote: Neil Cerutti wrote: On 2007-12-10, Duncan Booth [EMAIL PROTECTED] wrote: def test(): ll = LinkedList([random.randint(1,1000) for i in range(10)]) for el in ll: if el.value%2==0: ll.delete(el) print [el.value for el

Re: Minimalistic Software Transactional Memory

2007-12-09 Thread Duncan Booth
Michael Sparks [EMAIL PROTECTED] wrote: I'm interested in writing a simple, minimalistic, non persistent (at this stage) software transactional memory (STM) module. The idea being it should be possible to write such a beast in a way that can be made threadsafe fair easily. For those who

Re: File to dict

2007-12-07 Thread Duncan Booth
[EMAIL PROTECTED] wrote: def lookupdmo(domain): lines = open('/etc/virtual/domainowners','r').readlines() lines = [ [y.lstrip().rstrip() for y in x.split(':')] for x in lines] lines = [ x for x in lines if len(x) == 2 ] d = dict() for line in lines:

Re: File to dict

2007-12-07 Thread Duncan Booth
Matt Nordhoff [EMAIL PROTECTED] wrote: Using two list comprehensions mean you construct two lists, which sucks if it's a large file. Only if it is very large. You aren't duplicating the data except for entries with whitespace round them. If there isn't a lot of whitespace then the extra

Re: File to dict

2007-12-07 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-12-07, Duncan Booth [EMAIL PROTECTED] wrote: from __future__ import with_statement def loaddomainowners(domain): with open('/etc/virtual/domainowners','r') as infile: I've been thinking I have to use contextlib.closing for auto-closing

Re: Speed of Nested Functions Lambda Expressions

2007-12-07 Thread Duncan Booth
Terry Jones [EMAIL PROTECTED] wrote: Duncan Booth wrote: You can use Python's bytecode disassembler to see what actually gets executed here: def fn_outer(v): a=v*2 def fn_inner(): print V:%d,%d % (v,a) fn_inner() import dis dis.dis(fn_outer) 2

Re: Python surpasses Perl in TIOBE index

2007-12-04 Thread Duncan Booth
George Sakkis [EMAIL PROTECTED] wrote: Even more amazing is the rate C++ is losing ground: http://www.tiobe.com/tiobe_index/C__.html Given that the ratings are relative it may simply indicate that C++ is standing still while the others run ahead. --

Re: Overriding member methods in __init__

2007-12-03 Thread Duncan Booth
c james [EMAIL PROTECTED] wrote: Thanks, I was trying to eliminate another level of indirection with a test at each invocation of __call__ Try using different subclasses for each variant: class YesNo(object): def __new__(cls, which, *args, **kw): if cls is YesNo:

Re: How to read strings cantaining escape character from a file and use it as escape sequences?

2007-12-02 Thread Duncan Booth
John Machin [EMAIL PROTECTED] wrote: Hmmm ... the encode is documented as Produce a string that is suitable as Unicode literal in Python source code, but it *isn't* suitable. A Unicode literal is u'blah', this gives just blah. Worse, it leaves the caller to nut out how to escape apostrophes

Re: How to read strings cantaining escape character from a file and use it as escape sequences?

2007-12-01 Thread Duncan Booth
slomo [EMAIL PROTECTED] wrote: print line \u0050\u0079\u0074\u0068\u006f\u006e But I want to get a string: \u0050\u0079\u0074\u0068\u006f\u006e How do you make it? line.decode('unicode-escape') -- http://mail.python.org/mailman/listinfo/python-list

Re: Unexpected behavior when initializing class

2007-11-28 Thread Duncan Booth
Paul Rudin [EMAIL PROTECTED] wrote: You have to understand that the default value for v - an empty list - is made at compile time Not at compile time: the default value is created at runtime when the def statement is executed. -- http://mail.python.org/mailman/listinfo/python-list

Re: the annoying, verbose self

2007-11-27 Thread Duncan Booth
Iain King [EMAIL PROTECTED] wrote: FTR, I won't be using this :) I do like this syntax though: class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def abs(self): using self: return math.sqrt(.x*.x + .y*.y +

Re: the annoying, verbose self

2007-11-24 Thread Duncan Booth
Ton van Vliet [EMAIL PROTECTED] wrote: It would boil down to choice: explicit/speed vs implicit/readability No, it would boil down to explicit+speed+readability+maintainability vs implicit+error prone. It would mean that as well as the interpreter having to search the instance to work out

Re: Function stopping a function

2007-11-23 Thread Duncan Booth
Sorin Schwimmer [EMAIL PROTECTED] wrote: For instance, lenghty_function() executes, when an external event triggers cancel(), which is supposed to abruptly stop lengthy_function(), reset some variables and exit immediately. def lenghty_function(some, arguments, abort=lambda: False):

Re: eof

2007-11-22 Thread Duncan Booth
braver [EMAIL PROTECTED] wrote: In many cases, you want to do this: for line in f: do something with the line, setup counts and things if line % 1000 == 0 or f.eof(): # eof() doesn't exist in Python yet! use the setup variables and things to process the chunk My control

Re: eof

2007-11-22 Thread Duncan Booth
Boris Borcic [EMAIL PROTECTED] wrote: Duncan Booth wrote: import itertools def chunks(f, size): iterator = iter(f) def onechunk(line): yield line for line in itertools.islice(iterator, size-1): yield line for line in iterator: yield

Re: How to access C variables in Python code object generated by Py_C ompileString

2007-11-20 Thread Duncan Booth
Borse, Ganesh [EMAIL PROTECTED] wrote: 2) my this code got compiled but when running, I got an error from Py_CompileString, as below. Why is it so? File string, line 1 if ( (size 1000) (vol (0.001 * ADV)) (prod==Stock)): print OK ^ SyntaxError: invalid syntax But when I

Re: Some head clauses cases BeautifulSoup to choke?

2007-11-19 Thread Duncan Booth
Frank Stutzman [EMAIL PROTECTED] wrote: I did some poking and proding and it seems that there is something in the head clause that is causing the problem. Heck if I can see what it is. Maybe Beautifulsoup believes the incorrect encoding in the meta tags? --

Re: Interfaces.

2007-11-17 Thread Duncan Booth
Bjoern Schliessmann [EMAIL PROTECTED] wrote: Benjamin wrote: Python is has duck typing. If it quacks like a duke, it's duck. How do dukes quack, exactly? :) Regards, They quack with a North-eastern Scottish accent of course. The following excerpt from Scots: Practical Approaches

Re: Looking for docs

2007-11-16 Thread Duncan Booth
Donn Ingle [EMAIL PROTECTED] wrote: Hi, I have seen strange looking things in various Python code like: staticmethod and also lines starting with an @ sign, just before method defs - I can't find an example right now. I have Python 2.5 installed with it's docs, but I can't find any

Re: Resolving declaring class of a method at runtime

2007-11-15 Thread Duncan Booth
Janne Härkönen [EMAIL PROTECTED] wrote: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type help, copyright, credits or license for more information. class X: ... def x(self): ...pass ... class Y(X): ...

[issue1445] SystemError accessing uninitialised cell contents

2007-11-15 Thread Duncan Booth
New submission from Duncan Booth: The following code throws a SystemError exception. cell_get_contents in Objects\cellobject.c should check for a null op-ob_ref value and throw an appropriate exception. def oops(): def f(): cell f.func_closure[0].cell_contents cell

Re: Define key in nlargest() of heapq?

2007-11-13 Thread Duncan Booth
posted for you yesterday. See message http://groups.google.co.uk/group/comp.lang.python/browse_frm/thread/83c10e0c67e566d8/a605259c41c78fa0?lnk=gstq=duncan+booth#a605259c41c78fa0 -- http://mail.python.org/mailman/listinfo/python-list

Re: How to get a set of keys with largest values?

2007-11-12 Thread Duncan Booth
Davy [EMAIL PROTECTED] wrote: For example, I have dic that includes n=4 elements, I want m=2 keys have the largest values) dic = {0:4,3:1,5:2,7:8} So, the the largest values are [8,4], so the keys are [7,0]. Is there any fast way to implement this algorithm? Any suggestions are welcome!

Re: How to get a set of keys with largest values?

2007-11-12 Thread Duncan Booth
Jeff [EMAIL PROTECTED] wrote: Why are you doing that with key-value pairs? Why not with the array module or lists? The original poster asked about a problem with key-value pairs. I just answered his question. -- http://mail.python.org/mailman/listinfo/python-list

Re: Global variables within classes.

2007-11-10 Thread Duncan Booth
Donn Ingle [EMAIL PROTECTED] wrote: class Stack: list = [] Okay, this has me a little weirded-out. How is this different from putting it in: def __init__(self): self.list = [] ? I see from tests that it is different, but I don't quite grok it. Who owns the list ref? The

Re: Creating a cell 'by hand'

2007-11-09 Thread Duncan Booth
Prepscius, Colin \(IT\) [EMAIL PROTECTED] wrote: The last argument to new.function takes a closure, which is a tuple of cell objects. Does anybody know how to create those cell objects 'by hand'? def newcell(): def f(): cell return f.func_closure[0] cell = None

Re: Some pythonic suggestions for Python

2007-11-09 Thread Duncan Booth
Scott David Daniels [EMAIL PROTECTED] wrote: To quote a poster at http://www.thescripts.com/forum/thread22741.html, While we are at it, I also don't understand why sequences can't be used as indices. Why not, say, l[[2,3]] or l[(2, 3)]? Why a special slice concept? Isn't that unpythonic?

Re: Some pythonic suggestions for Python

2007-11-09 Thread Duncan Booth
Steven D'Aprano [EMAIL PROTECTED] wrote: Besides, if you want this behaviour, you can add it yourself: class mylist(list): # Untested! def __getitem__(self, index): if type(index) is list: return [self[i] for i in index] return super(mylist,

Re: Some pythonic suggestions for Python

2007-11-09 Thread Duncan Booth
Frank Samuelson [EMAIL PROTECTED] wrote: It's also not clear how you expect this to work with anything more complex than a single expression. How do you handle statements and multiple returns? def foo(x, y): L = [] try: if x[y] % 2: print x, y

Re: How to use list as key of dictionary?

2007-11-06 Thread Duncan Booth
Wildemar Wildenburger [EMAIL PROTECTED] wrote: maybe something like this could help: def tupleize(non_tuple): try: return tuple(tupleize(thing) for thing in non_tuple) except TypeError: # non_tuple is not iterable return non_tuple Just don't try

Re: How to use list as key of dictionary?

2007-11-06 Thread Duncan Booth
Paul McGuire [EMAIL PROTECTED] wrote: On Nov 6, 4:08 am, Dustan [EMAIL PROTECTED] wrote: On Nov 6, 3:58 am, Duncan Booth [EMAIL PROTECTED] wrote: Wildemar Wildenburger [EMAIL PROTECTED] wrote: maybe something like this could help: def tupleize(non_tuple): try

Re: How to use list as key of dictionary?

2007-11-06 Thread Duncan Booth
Steven D'Aprano [EMAIL PROTECTED] wrote: Not quite, because that will also convert strings to tuples, which may not be what you want for a general solution. I take it you didn't actually try the original code then. Converting strings to tuples is not something it did. That works for all

Re: Please explain collections.defaultdict(lambda: 1)

2007-11-06 Thread Duncan Booth
metaperl.com [EMAIL PROTECTED] wrote: Per http://docs.python.org/lib/defaultdict-examples.html It seems that there is a default factory which initializes each key to 1. So by the end of train(), each member of the dictionary model will have value = 1 But why wouldnt he set the value to

Re: python newbie

2007-11-05 Thread Duncan Booth
Steven D'Aprano [EMAIL PROTECTED] wrote: As far as I know, all it would take to allow modules to be callable would be a single change to the module type, the equivalent of: def __call__(self, *args, **kwargs): try: # Special methods are retrieved from the class, not #

Re: python newbie

2007-11-03 Thread Duncan Booth
Steven D'Aprano [EMAIL PROTECTED] wrote: Then if foo is a class, we probably all agree that bar is a method of foo. There are funny edge cases (e.g. bar might be an attribute with a __call__ method) but in general, yes, bar would be a method of foo. But that 'funny edge case' is exactly

Re: python newbie

2007-11-03 Thread Duncan Booth
Paul Rubin http://[EMAIL PROTECTED] wrote: Duncan Booth [EMAIL PROTECTED] writes: modules are not special in any way, except that you cannot subclass them. Oops, sorry I got that wrong. Modules are not special in any way, they can have methods as well as functions: I've felt for a long

Re: Poor python and/or Zope performance on Sparc

2007-11-03 Thread Duncan Booth
Jean-Paul Calderone [EMAIL PROTECTED] wrote: The T1000 isn't a very good machine for general server purposes. It has advantages when running software with a lot of hardware-level parallelism, but Zope isn't such a piece of software. Zope can scale well on multi-processor machines, but you

Re: python newbie

2007-11-02 Thread Duncan Booth
Bjoern Schliessmann [EMAIL PROTECTED] wrote: Jim Hendricks wrote: I see the global keyword that allows access to global vars in a function, what I'm not clear on is does that global need to be declared in the global scope, You can't just declare in Python, you always define objects (and

Re: setting variables in outer functions

2007-11-01 Thread Duncan Booth
Hrvoje Niksic [EMAIL PROTECTED] wrote: In real-life code, closures are used to implement callbacks with automatic access to their lexical environment without the need for the bogus additional void * argument one so often sees in C callbacks, and without communication through global variables.

Re: urllib.getproxies and PAC

2007-11-01 Thread Duncan Booth
gooli [EMAIL PROTECTED] wrote: Hi, I'm using urllib to get html pages from the web but my computer is behind a proxy. The proxy is automatically configured in Internet Explorer via a proxy.pac file (http://en.wikipedia.org/wiki/Proxy_auto-config). From what I can see in the urllib

Re: setting variables in outer functions

2007-10-31 Thread Duncan Booth
Dustan [EMAIL PROTECTED] wrote: On Oct 30, 11:29 am, Duncan Booth [EMAIL PROTECTED] wrote: Neil Cerutti [EMAIL PROTECTED] wrote: It's allows a standard programming idiom which provides a primitive form of object oriented programming using closures to represent state. def account

Re: A Python 3000 Question

2007-10-31 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: But if I'm wrong about the performance benefits then I guess I'm still in the dark about why len is a builtin. The only compelling thing in the linked explation was the signatures of the guys who wrote the artible. (Guido does admit he would, hate to lose

Re: A Python 3000 Question

2007-10-31 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-10-31, Duncan Booth [EMAIL PROTECTED] wrote: Obviously it isn't an absolute thing: lists and dictionaries do have other methods in the user namespace, so the decision to keep len out of that namespace is partly a judgement call, and partly

Re: setting variables in outer functions

2007-10-30 Thread Duncan Booth
Neil Cerutti [EMAIL PROTECTED] wrote: It's allows a standard programming idiom which provides a primitive form of object oriented programming using closures to represent state. def account(opening_balance): balance = opening_balance def get_balance(): nonlocal balance return

Re: Built-in functions and keyword arguments

2007-10-30 Thread Duncan Booth
J. Clifford Dyer [EMAIL PROTECTED] wrote: How do you interpret: help(__import__) Help on built-in function __import__ in module __builtin__: __import__(...) __import__(name, globals={}, locals={}, fromlist=[], level=-1) - module ... help(int) Help on class int in module

Re: SQLite3; weird error

2007-10-29 Thread Duncan Booth
TYR [EMAIL PROTECTED] wrote: To do anything with it, you then need to create a cursor object by calling foo's method cursor (bar = foo.cursor). Perhaps this would work better if you actually try calling foo's method? bar = foo.cursor() Without the parentheses all you are doing is

Re: Built-in functions and keyword arguments

2007-10-29 Thread Duncan Booth
Armando Serrano Lombillo [EMAIL PROTECTED] wrote: Why does Python give an error when I try to do this: len(object=[1,2]) Traceback (most recent call last): File pyshell#40, line 1, in module len(object=[1,2]) TypeError: len() takes no keyword arguments but not when I use a normal

Re: Built-in functions and keyword arguments

2007-10-29 Thread Duncan Booth
Bruno Desthuilliers [EMAIL PROTECTED] wrote: In the second case, the name of the argument *is* 'object'. Which is not the case for the builtin len (which, fwiw, has type 'builtin_function_or_method', not 'function', so inspect.getargspec couldn't tell me more). ot While we're at it,

Re: Built-in functions and keyword arguments

2007-10-29 Thread Duncan Booth
J. Clifford Dyer [EMAIL PROTECTED] wrote: I think you are being a little bit unfair here: help(len) says: len(...) len(object) - integer Return the number of items of a sequence or mapping. which implies that the argument to len has the name 'object' (although in fact it

Re: how to convert tuple to a list of single values ?

2007-10-28 Thread Duncan Booth
stef mientki [EMAIL PROTECTED] wrote: hello, The next piece of code bothers me: ptx, pty, rectWidth, rectHeight = self._point2ClientCoord (p1, p2 ) dc.SetClippingRegion ( ptx, pty, rectWidth, rectHeight ) Because I want to write it in 1 line, and without the use of

Re: object inheritance

2007-10-26 Thread Duncan Booth
Anand [EMAIL PROTECTED] wrote: On Oct 26, 5:31 pm, Pradeep Jindal [EMAIL PROTECTED] wrote: Can you tell any specific use case for doing this? I have many implementaions of a db interface. SimpleDB - simple implementation BetterDB - optimized implementation CachedDB - an implementation

Re: tuples within tuples

2007-10-26 Thread Duncan Booth
[EMAIL PROTECTED] wrote: [cut] Without a better example or explanation of what you are trying to do it is difficult You're right. Actually i'm parsing an xml file using pyrxp, which returns something like this: (tagName, attributes, list_of_children, spare) Where list_of_children

Re: Speed of Nested Functions Lambda Expressions

2007-10-24 Thread Duncan Booth
beginner [EMAIL PROTECTED] wrote: It is really convenient to use nested functions and lambda expressions. What I'd like to know is if Python compiles fn_inner() only once and change the binding of v every time fn_outer() is called or if Python compile and generate a new function object every

Re: Better writing in python

2007-10-24 Thread Duncan Booth
Alexandre Badez [EMAIL PROTECTED] wrote: Thanks for your try Cliff, I was very confused :P More over I made some mistake when I post (to make it easiest). Here is my real code: with dArguments = { 'argName' : { 'mandatory' : bool, # True or False [...], # other field we do

<    3   4   5   6   7   8   9   10   11   12   >