Re: Wrap a function

2010-01-28 Thread John Posner
On 1/28/2010 2:24 PM, Joan Miller wrote: On 28 ene, 19:16, Josh Holland wrote: On 2010-01-28, Joan Miller wrote: I've to call to many functions with the format: run("cmd") Check the docs on os.system(). No. I've a function that uses subprocess to run commands on the same shell and so s

Re: Wrap a function

2010-01-28 Thread John Posner
On 1/28/2010 3:45 PM, Joan Miller wrote: On 28 ene, 20:34, Joan Miller wrote: On 28 ene, 20:20, Peter wrote: On Jan 29, 6:58 am, John Posner wrote: On 1/28/2010 2:24 PM, Joan Miller wrote: On 28 ene, 19:16, Josh Hollandwrote: On 2010-01-28, Joan Millerwrote: I've to

Re: For loop searching takes too long!

2010-01-28 Thread John Posner
On 1/28/2010 6:52 PM, elsa wrote: Hi guys, I've got a problem with my program, in that the code just takes too long to run. Here's what I'm doing. If anyone has any tips, they'd be much appreciated! So, say I have a list of lists that looks something like this (I'm using a list of lists, rather

Re: Still too slow

2010-01-30 Thread John Posner
On 1/30/2010 6:08 PM, elsa wrote: Hello again, Thanks for the tips r.e random.ranint(). This improved matters somewhat, however my program is still too slow. If anyone has any further tips on how to speed it up, they would be much appreciated! So, I'm calling evolve(L,limit) from the interactiv

[off-topic] Pessimal (was: Detecting changes to a dict)

2009-09-29 Thread John Posner
If you can enumerate the language of possible inputs you could generate a unique binary representation. Against a language of size l that would only take you O(l*n) to build the repr for a dict and for certain repr sizes the comparison could be O(1), making the entire operation O(l*n+l*m) vs O(n

Re: UnboundLocalError with extra code after return

2009-09-30 Thread John Posner
Duncan Booth wrote: / class CallableOnlyOnce(object): /def __init__(self, func): self.func = func def __call__(self): f = self.func if f: self.func = None return f() / def callonce(func): / return CallableOnlyOnce(func) / @callonce /

Re: a splitting headache

2009-10-16 Thread John Posner
Mensenator said: c = '001110' c.split('0') ['', '', '1', '', '', '', '11', ''] Ok, the consecutive delimiters appear as empty strings for reasons unknown (except for the first one). Except when they start or end the string in which case the first one is included. Maybe there's a r

Re: a splitting headache

2009-10-22 Thread John Posner
Carl Banks wrote: s.split() and s.split(sep) do different things, and there is no string sep that can make s.split(sep) behave like s.split(). That's not unheard of but it does go against our typical expectations. It would have been a better library design if s.split() and s.split(sep) were d

Re:

2009-10-22 Thread John Posner
Mensanator wrote: That's interesting. If string.splitfields(delim) was equivalent to str.split(sep), it would have been useful to add the phrase "str.split(sep) is equivalent to the old string.splitfields(delim) which no longer exists." to the docs. That way, a search on "splitfields" would direc

Re: a splitting headache

2009-10-22 Thread John Posner
[resend, with Subject line corrected and formatting crud deleted] Mensanator wrote: That's interesting. If string.splitfields(delim) was equivalent to str.split(sep), it would have been useful to add the phrase "str.split(sep) is equivalent to the old string.splitfields(delim) which no longer ex

Re: quit button

2009-10-25 Thread John Posner
linda.s wrote: When I click "quit" button, why the following code has problem? from Tkinter import * colors = ['red', 'green', 'yellow', 'orange', 'blue', 'navy'] def gridbox(parent): r = 0 for c in colors: l = Label(parent, text=c, relief=RIDGE, width=25) e = Entry(pa

Re: Adding a menu to Tkinter

2009-10-26 Thread John Posner
Ronn Ross wrote: I'm attempting to add a menu bar to my Tkinter app. I can't figure out the correct syntax. Can someone help? I get this error when I run the app: Traceback (most recent call last): File "tkgrid.py", line 26, in app = App(root) File "tkgrid.py", line 10, in __init__

Re: Program to compute and print 1000th prime number

2009-11-07 Thread John Posner
Robert P. J. Day said: the ubiquitous sieve of eratosthenes requires you to pre-specify your maximum value, after which -- once the sieve completes -- all you know is that you have all of the prime numbers up to n. whether you'll have 1000 of them isn't clear, which means that you might have t

Re: object indexing and item assignment

2009-11-13 Thread John Posner
King wrote: class MyFloat(object): def __init__(self, value=0.): self.value = value def set(self, value): self.value = value def get(self): return self.value class MyColor(object): def __init__(self, value=(0,0,0)): self.value = (MyFloat(value[0

Re: odd drawing problem with turtle.py

2010-01-31 Thread John Posner
> I'm on Python 2.5, but using the updated turtle.py Version 1.0.1 - 24. 9. 2009. > The following script draws 5 circles, which it is supposed to, but then > doesn't draw the second turtle which is supposed to simply move forward. > Any ideas? Try commenting out this statement: self.turtle.t

Re: which

2010-02-05 Thread John Posner
On 2/5/2010 9:21 AM, mk wrote: if isinstance(cmd, str): self.cmd = cmd.replace(r'${ADDR}',ip) else: self.cmd = cmd or self.cmd = cmd if isinstance(cmd, str): self.cmd = cmd.replace(r'${ADDR}',ip) (lunatic fringe?) Last August [1], I offered this alternative: self.cmd = (cmd

Re: which

2010-02-05 Thread John Posner
On 2/5/2010 11:06 AM, Gerald Britton wrote: [snip] Last August [1], I offered this alternative: self.cmd = (cmd.replace(r'${ADDR}',ip) if isinstance(cmd, str) else cmd) But it didn't get much love in this forum! I'd probably go for that one as well though I mig

Re: which

2010-02-05 Thread John Posner
On 2/5/2010 11:26 AM, Gerald Britton wrote: sure, but it will fit nicely on one line if you like On Fri, Feb 5, 2010 at 11:22 AM, John Posner wrote: On 2/5/2010 11:06 AM, Gerald Britton wrote: [snip] Last August [1], I offered this alternative: self.cmd = (cmd.replace

Re: which

2010-02-05 Thread John Posner
On 2/5/2010 11:53 AM, Gerald Britton wrote: Also, I'm contractually obligated to admonish you not to "top post". Contract? Joke. (I know it's hard to tell.) At any rate, I proposed the 3-line format specifically because it separates the data values from the if-then-else machinery, mak

Re: Doctests and decorated methods don't get along

2010-02-06 Thread John Posner
On 2/6/2010 6:48 AM, Steven D'Aprano wrote: class MyStaticMethod(object): """Emulate built-in staticmethod descriptor.""" def __init__(self, f): self.f = f def __get__(self, obj, objtype=None): return self.f How about using a function, instead of a class, to imp

Re: Function attributes

2010-02-10 Thread John Posner
On 2/10/2010 9:36 AM, Steven D'Aprano wrote: On Wed, 10 Feb 2010 05:59:41 -0800, Muhammad Alkarouri wrote: Hi everyone, What is the simplest way to access the attributes of a function from inside it, other than using its explicit name? In a function like f below: def f(*args): f.args = a

Re: Function attributes

2010-02-10 Thread John Posner
On 2/10/2010 10:24 AM, Bruno Desthuilliers wrote: They didn't break immediately for me -- what am I missing?: The fact that in the OP's snippet, code inside f's body refers to f by its name. Of course! Tx. -John -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating formatted output using picture strings

2010-02-10 Thread John Posner
On 2/10/2010 2:57 PM, Grant Edwards wrote: On 2010-02-10, [email protected] wrote: [regardning "picture" output format specifiers] I was thinking that there was a built-in function for this common(?) use case I haven't seen that paradigm since my one-and-only exposure to COBOL in a class I

Re: Modifying Class Object

2010-02-10 Thread John Posner
On 2/10/2010 1:38 PM, Ethan Furman wrote: After all is said and done - if you had a truly good grasp of Python, I might buy your book even if you still had -- ummm -- a less than winning presence on the mailing list; but right now your understanding is not worth paying for. Alf, here's my sug

Re: Sorting a list of lists

2010-02-12 Thread John Posner
On 2/12/2010 12:45 PM, R (Chandra) Chandrasekhar wrote: Dear Folks, I have lines of values like so: 14, [25, 105, 104] 10, [107, 106, 162] 21, [26, 116, 165] I need to sort them in two ways: (a) By the numeric value of the first column; and (b) by the sum of the elements of the second item i

Re: Please help with MemoryError

2010-02-12 Thread John Posner
On 2/12/2010 12:14 PM, Steven D'Aprano wrote: On Fri, 12 Feb 2010 06:45:31 -0800, Jeremy wrote: You also confirmed what I thought was true that all variables are passed "by reference" so I don't need to worry about the data being copied (unless I do that explicitly). No, but yes. No, variabl

Re: Executing a command from within python using the subprocess module

2010-02-15 Thread John Posner
On 2/15/2010 7:35 AM, R (Chandra) Chandrasekhar wrote: Dear Folks, I want to execute a command from within python using the subprocess module. Coming from a Perl background, I thought I could use variable interpolation in strings, but found that this is neither supported Yes, it is: see the u

Re: Modifying Class Object

2010-02-15 Thread John Posner
Alf said (2/13/2010 8:34 PM): Names in Python refer to objects. Those references can be copied via assignment. That's (almost) all. And it provides a very short and neat way to describe pass by sharing. Alf also said (2/13/2010 8:43 PM): * Steve Howell: > This thread is interesting on man

Re: Modifying Class Object

2010-02-15 Thread John Posner
On 2/15/2010 6:09 PM, Steven D'Aprano wrote: On Mon, 15 Feb 2010 21:25:23 +, Arnaud Delobelle wrote: John Posner writes: [...] x = s[0] [...] assigns the name *x* to the object that *s[0]* refers to s[0] does not refer to an object, it *is* an object (once evaluated of c

Re: Traversing through variable-sized lists

2010-02-17 Thread John Posner
On 2/17/2010 1:10 PM, Andrej Mitrovic wrote: Hi, I couldn't figure out a better description for the Subject line, but anyway, I have the following: _num_frames = 32 _frames = range(0, _num_frames) # This is a list of actual objects, I'm just pseudocoding here. _values = [0, 1, 2, 3, 4] I want

Re: Referring to class methods in class attributes

2010-02-17 Thread John Posner
On 2/17/2010 2:44 PM, Bruno Desthuilliers wrote: Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO beautifully) simple once you get it, but I agree it's a bit peculiar when compared to most mainstream OO languages. Very nice writeup, Bruno -- thanks! class method(ob

Re: Why this doesn't work?

2010-02-18 Thread John Posner
On 2/18/2010 12:28 PM, mk wrote: Sorry to bother everyone again, but I have this problem bugging me: #!/usr/bin/python -i class Foo(object): def nostat(self,val): print val nostat.__orig_get__ = nostat.__get__ @staticmethod def nostatget(*args, **kwargs): print 'args:', args, 'kwargs:', kwa

Re: How to make an empty generator?

2010-02-19 Thread John Posner
On 2/19/2010 2:25 PM, Terry Reedy wrote: On 2/19/2010 12:44 PM, Stephen Hansen wrote: Much to my embarrassment, sometime last night I realized I was being a complete idiot, and the 'correct' way to handle this in my scenario is really just: def initialize(): # do one time processing here retu

Re: speed question, reading csv using takewhile() and dropwhile()

2010-02-19 Thread John Posner
On 2/19/2010 3:02 PM, MRAB wrote: Is this any better? def read_data_file(filename): reader = csv.reader(open(filename, "U"),delimiter='\t') data = [] for row in reader: if '[MASKS]' in row: break data.append(row) As noted in another thread recently, you

Re: Efficiently building ordered dict

2010-02-22 Thread John Posner
On 2/22/2010 4:29 PM, Bryan wrote: Sorry about the sorted != ordered mix up. I want to end up with a *sorted* dict from an unordered list. *Sorting the list is not practical in this case.* I am using python 2.5, with an ActiveState recipe for an OrderedDict. Have you looked at this: htt

Re: Signature-based Function Overloading in Python

2010-02-23 Thread John Posner
On 2/23/2010 1:25 PM, Michael Rudolf wrote: Just a quick question about what would be the most pythonic approach in this. In Java, Method Overloading is my best friend, but this won't work in Python: >>> def a(): pass >>> def a(x): pass >>> a() Traceback (most recent call last): File "", lin

Re: How to make an empty generator?

2010-02-24 Thread John Posner
On 2/24/2010 9:07 AM, Steve Holden wrote: John Posner wrote: Note that the Py2.6.4 documentation is inconsistent. AFAICT, it conforms to Terry's definitions above in most places. But the Glossary says: generator A function which returns an iterator.<... more ...>

Re: How to make an empty generator?

2010-02-24 Thread John Posner
generator An iterator produced by a generator function or a generator expression. -John +1. Can someone submit a documentation patch, please? Will do. -John [sorry if this is a dup] Done: #8012 "Revise generator-related Glossary entries" -John -- http://mail.python.org/mailman/listinfo/

Re: Noob raw_input question

2010-02-24 Thread John Posner
On 2/24/2010 12:39 PM, Abigail wrote: Yesterday I downloaded and installed Python 3.1 and working through some examples but I have hit a problem a = raw_input("Enter a number" ) Traceback (most recent call last): File "", line 1, in a = raw_input("Enter a number" ) NameError: name 'raw

Re: Docstrings considered too complicated

2010-02-24 Thread John Posner
On 2/24/2010 4:54 PM, Jonathan Gardner wrote: On Wed, Feb 24, 2010 at 12:23 PM, Andreas Waldenburger wrote: Hi all, a company that works with my company writes a lot of of their code in Python (lucky jerks). I've seen their code and it basically looks like this: """Function that does stuff""

Re: loop through each line in a text file

2010-02-26 Thread John Posner
On 2/26/2010 4:21 PM, qtrimble wrote: fileIN = open(r"C:\testing.txt", "r") for line in fileIN: year = line[3:7] day = line[7:10] print year, day This is good since i can get the year and day of year into a variable but I haven't gotten any further. That's an excellent start.

Re: Variable definition

2010-02-26 Thread John Posner
On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iteritems(): 'myPrefix' + key = value No trick, just swap a new ke

Re: Variable definition

2010-02-26 Thread John Posner
On 2/26/2010 10:20 PM, Steven D'Aprano wrote: On Fri, 26 Feb 2010 20:15:16 -0500, John Posner wrote: On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red

Re: Variable definition

2010-03-01 Thread John Posner
On 3/1/2010 1:07 PM, Raphael Mayoraz wrote: John Posner wrote: On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green':

Re: Method / Functions - What are the differences?

2010-03-01 Thread John Posner
On 3/1/2010 2:59 PM, Bruno Desthuilliers wrote: Answer here: http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/bd71264b6022765c/3a77541bf9d6617d#doc_89d608d0854dada0 I really have to put this in the wiki :-/ Bruno, I performed a light copy-edit of your writeup and put i

Re: Method / Functions - What are the differences?

2010-03-02 Thread John Posner
thod object: in the call method, it should inject self.im_self as first arg, not self.im_func. This had been spotted by someone named John Posner, IIRC !-) Fixed (oops!). I've updated the text at this location: > http://cl1p.net/bruno_0301.rst/ I think the ball is back in your court,

Re: Adding to a module's __dict__?

2010-03-02 Thread John Posner
On 3/2/2010 10:19 AM, Roy Smith wrote: Somewhat sadly, in my case, I can't even machine process the header file. I don't, strictly speaking, have a header file. What I have is a PDF which documents what's in the header file, and I'm manually re- typing the data out of that. Sigh. Here's an

Re: Method / Functions - What are the differences?

2010-03-03 Thread John Posner
On 3/3/2010 5:56 AM, Bruno Desthuilliers wrote: Eike Welk a écrit : John Posner wrote: I've updated the text at this location: > http://cl1p.net/bruno_0301.rst/ I think this is a very useful writeup! It would be perfect with a little bit of introduction that says: 1. - What it

Re: Method / Functions - What are the differences?

2010-03-03 Thread John Posner
On 3/3/2010 9:58 AM, John Posner wrote: Film at 11, John Done -- see http://wiki.python.org/moin/FromFunctionToMethod -John -- http://mail.python.org/mailman/listinfo/python-list

Re: Method / Functions - What are the differences?

2010-03-03 Thread John Posner
On 3/3/2010 10:48 AM, Bruno Desthuilliers wrote: I spotted this: http://www.python.org/doc/faq/programming/#what-is-a-method http://www.python.org/doc/faq/general/#why-must-self-be-used-explicitly-in-method-definitions-and-calls Our text is probably a bit too long for a direct inclusion in t

Re: Method / Functions - What are the differences?

2010-03-03 Thread John Posner
On 3/3/2010 6:33 PM, Eike Welk wrote: I have two small ideas for improvement: - Swap the first two paragraphs. First say what it is, and then give the motivation. No problem -- since this is a Wiki, you can perform the swap yourself! (If you haven't done it in a day or so, I'll do the deed.)

Re: Method / Functions - What are the differences?

2010-03-04 Thread John Posner
On 3/4/2010 5:59 AM, Bruno Desthuilliers wrote: I have two small ideas for improvement: - Swap the first two paragraphs. First say what it is, and then give the motivation. Mmm... As far as I'm concerned, I like it the way its. John ? I think it doesn't make very much difference. But in the

Re: Method / Functions - What are the differences?

2010-03-04 Thread John Posner
On 3/3/2010 6:56 PM, John Posner wrote: ... I was thinking today about "doing a Bruno", and producing similar pieces on: * properties created with the @property decorator * the descriptor protocol I'll try to produce something over the next couple of days. Starting t

Re: Method / Functions - What are the differences?

2010-03-05 Thread John Posner
On 3/5/2010 7:15 AM, Bruno Desthuilliers wrote: John Posner a écrit : On 3/3/2010 6:56 PM, John Posner wrote: ... I was thinking today about "doing a Bruno", and producing similar pieces on: * properties created with the @property decorator * the descriptor protocol I'll

Re: a simple def how-to

2010-03-07 Thread John Posner
On 3/7/2010 10:05 AM, vsoler wrote: Hello, My script starts like this: book=readFromExcelRange('book') house=readFromExcelRange('house') table=readFromExcelRange('table') read=readFromExcelRange('read') ... But I would like to have something equivalent, like... ranges=['book','house','table',

Re: a simple def how-to

2010-03-07 Thread John Posner
On 3/7/2010 10:59 AM, vsoler wrote: Thank you for your help. Perhaps the solution you are suggesting is not exactly what I was looking for, but helped anyway. Oops, I was thinking list, not dict. Too fast, and not enough coffee! -John -- http://mail.python.org/mailman/listinfo/python-list

Re: related lists mean value

2010-03-08 Thread John Posner
On 3/8/2010 5:34 PM, dimitri pater - serpia wrote: Hi, I have two related lists: x = [1 ,2, 8, 5, 0, 7] y = ['a', 'a', 'b', 'c', 'c', 'c' ] what I need is a list representing the mean value of 'a', 'b' and 'c' while maintaining the number of items (len): w = [1.5, 1.5, 8, 4, 4, 4] I have looke

Re: related lists mean value

2010-03-08 Thread John Posner
On 3/8/2010 9:39 PM, John Posner wrote: # gather data tally_dict = defaultdict(Tally) for i in range(len(x)): obj = tally_dict[y[i]] obj.id = y[i] <--- statement redundant, remove it obj.total += x[i] obj.count += 1 -John -- http://mail.python.org/mailman/listinfo/pyt

Re: related lists mean value

2010-03-08 Thread John Posner
On 3/8/2010 9:43 PM, John Posner wrote: On 3/8/2010 9:39 PM, John Posner wrote: obj.id = y[i] <--- statement redundant, remove it Sorry for the thrashing! It's more correct to say that the Tally class doesn't require an "id" attribute at all. So the code bec

Re: imported var not being updated

2010-03-09 Thread John Posner
On 3/8/2010 11:55 PM, Gary Herron wrote: The form of import you are using from helpers import mostRecent makes a *new* binding to the value in the module that's doing the import. What you can do, is not make a separate binding, but reach into the helpers module to get the value there. Like

Re: imported var not being updated

2010-03-09 Thread John Posner
On 3/9/2010 9:48 AM, Bruno Desthuilliers wrote: John Posner a écrit : On 3/8/2010 11:55 PM, Gary Herron wrote: The form of import you are using from helpers import mostRecent makes a *new* binding to the value in the module that's doing the import. What you can do, is not m

Re: Method / Functions - What are the differences?

2010-03-10 Thread John Posner
[ cross-posting to edu-sig ] Bruno (and anyone else interested) -- As I promised/threatened, here's the *start* of a write-up on properties, aimed at non-advanced Python programmers: http://www.jjposner.net/media/python-properties-0310.pdf I'm interested in corrections, of course. But I'm

Re: pivot() equivalent

2010-03-11 Thread John Posner
On 3/11/2010 6:16 PM, gundlach wrote: I *know* this already exists, but I can't remember where: def pivot(func, seq): # I know, a good implementation shouldn't call func() twice per item return ( (x for x in seq if func(x)), (x for x in seq if not func(x)) ) I feel like I read a thread in

Re: Method / Functions - What are the differences?

2010-03-18 Thread John Posner
On 3/10/2010 8:37 PM, Gabriel Genellina wrote: En Wed, 10 Mar 2010 11:45:38 -0300, John Posner escribió: As I promised/threatened, here's the *start* of a write-up on properties, aimed at non-advanced Python programmers: http://www.jjposner.net/media/python-properties-0310.pdf I&

Re: Method / Functions - What are the differences?

2010-03-21 Thread John Posner
On 3/21/2010 5:34 PM, Aahz wrote: In article, John Posner wrote: Bruno (and anyone else interested) -- As I promised/threatened, here's the *start* of a write-up on properties, aimed at non-advanced Python programmers: http://www.jjposner.net/media/python-properties-0310.pdf

Re: How to automate accessor definition?

2010-03-22 Thread John Posner
On 3/22/2010 11:44 AM, Bruno Desthuilliers wrote: Another (better IMHO) solution is to use a plain property, and store the computed value as an implementation attribute : @property def foo(self): cached = self.__dict__.get('_foo_cache') if cached is None: self._foo_cache = cached = self._some

Re: setting variables in pdb

2010-05-18 Thread John Posner
On 5/18/2010 4:15 PM, Art wrote: If I am in Pdb, I would like to set a temporary variable, for example: (Pdb) r = 1 The 'r' gets interpreted as 'return' by Pdb. Is there a Pdb instruction that guarantees the intended effect, like: (Pdb) let r = 1 I can usually avoid using such variable names

Re: how to cause a request for a missing class attribute cause its calculation

2010-05-19 Thread John Posner
On 5/18/2010 4:54 PM, Chris Rebert wrote: Suggested reading: http://docs.python.org/library/functions.html#property I've placed a revision to this official *property* documentation at: http://wiki.python.org/moin/AlternativeDescriptionOfProperty There's also a gentle (I hope) intro to t

Re: function that counts...

2010-05-19 Thread John Posner
On 5/19/2010 5:51 PM, Steven D'Aprano wrote: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: Rather than iterating over an index j = 0, 1, 2, ... and then fetching the jth character of the string, you can iterate over the characters directly. So the inner loop is better written: for c in

Re: Minor annoyances with properties

2010-05-27 Thread John Posner
On 5/27/2010 9:14 AM, Neil Cerutti wrote: On 2010-05-27, eb303 wrote: I've been using Python properties quite a lot lately and I've found a few things that are a bit annoying about them in some cases. I wondered if I missed something or if anybody else has this kind of problems too, and if ther

Re: Infinite prime number generator

2010-06-29 Thread John Posner
On 6/29/2010 12:51 PM, Thomas Jollans wrote: def rprimes(): def elim_mult(n): yield n for p in filter((lambda x:x%n != 0), elim_mult(n+1)): yield p yield 1 for p in elim_mult(2): yield p Thomas, take a look at the thread "Generators/iterators, Pythonicity, an

Re: drag & drop in a python GUI application

2010-07-03 Thread John Posner
On 7/2/2010 11:20 AM, Michael Torrie wrote: On 07/01/2010 08:57 AM, Alan wrote: I know drag& drop is not possible with TK. Is this a Python Tk limitation or a Tk limitation in general? Google suggests that Tk itself supports some form of dnd. Which widget could I use for my python applicat

Re: Easy questions from a python beginner

2010-07-14 Thread John Posner
On 7/14/2010 12:06 PM, Ethan Furman wrote: ... Have you tried this? --> def foo(): ... print locals() ... blah = 'interesting' ... print locals() ... --> foo() {} {'blah': 'interesting'} As can be clearly seen, blah does not exist before the assignment -- the *name* blah has not been *bound* t

Re: default behavior

2010-07-31 Thread John Posner
On 7/31/2010 11:08 AM, Christian Heimes wrote: ... All you have to do is subclass dict and implement a __missing__ method. See http://docs.python.org/library/stdtypes.html?highlight=__missing__#mapping-types-dict Caveat -- there's another description of defaultdict here: http://docs.python.

Re: default behavior

2010-08-01 Thread John Posner
On 7/31/2010 2:00 PM, Christian Heimes wrote: Your answer is confusing even me. ;) Yeah, I get that a lot. :-) Let me try an easier to understand explanation. defaultdict *implements* __missing__() to provide the default dict behavior. In my experience, the word *implements* is commonly u

Re: default behavior

2010-08-02 Thread John Posner
On 7/31/2010 1:31 PM, John Posner wrote: Caveat -- there's another description of defaultdict here: http://docs.python.org/library/collections.html#collections.defaultdict ... and it's bogus. This other description claims that __missing__ is a method of defaultdict, not of dict.

Re: default behavior

2010-08-03 Thread John Posner
On 8/3/2010 12:54 PM, Ethan Furman wrote: I think mentioning how __missing__ plays into all this would be helpful. Perhaps in the first paragraph, after the colon: if a key does not currently exist in a defaultdict object, __missing__ will be called with that key, which in turn will call a "d

Re: default behavior

2010-08-03 Thread John Posner
On 8/3/2010 5:47 PM, Christian Heimes wrote: So I'd rather not mention __missing__ in the first paragraph, which describes the functionality provided *by* the defaultdict class. How about adding this para at the end: defaultdict is defined using functionality that is available to *any* sub

Re: default behavior

2010-08-03 Thread John Posner
On 8/3/2010 6:48 PM, Ethan Furman wrote: Christian Heimes wrote: I just went and read the entry that had the bogus claim -- personally, I didn't see any confusion. I would like to point out the __missing__ is *not* part of dicts (tested on 2.5 and 2.6 -- don't have 2.7 installed yet). I beg yo

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread John Posner
On 8/5/2010 12:33 AM, John Nagle wrote: There's got to be a better way to do this: def editmoney(n) : return((",".join(reduce(lambda lst, item : (lst + [item]) if item else lst, re.split(r'(\d\d\d)',str(n)[::-1]),[])))[::-1]) Here's a more elegant variant, using regexp lookahead: def thous_

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread John Posner
On 8/5/2010 12:36 PM, MRAB wrote: You don't need to reverse the string: def thous_format(integer_string): """ add comma thousands separator(s) to an integer-valued string """ return re.sub(r"(?<=\d)(?=(?:\d\d\d)+$)", ",", integer_string) Nice! My first encounter with a look-behind

Re: default behavior

2010-08-06 Thread John Posner
On 8/2/2010 11:00 PM, John Posner wrote: On 7/31/2010 1:31 PM, John Posner wrote: Caveat -- there's another description of defaultdict here: http://docs.python.org/library/collections.html#collections.defaultdict ... and it's bogus. This other description claims that __missing__ i

Re: default behavior

2010-08-06 Thread John Posner
On 8/6/2010 6:24 PM, Wolfram Hinderer wrote: This is probably nitpicking, but the patch calls __missing__ a special method. However, unlike special methods, it is not invoked by "special syntax" but by the dict's __getitem__ method. (len() invokes __len__ on any object - you can't do something s

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-12 Thread John Posner
On 8/12/2010 9:22 AM, Dave Angel wrote: Now you have to find the largest number below 120, which you can easily do with brute force Dept of overkill, iterators/generators division ... -John #-- from itertools import imap, product, ifilter from operator import mul box_sizes =

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-12 Thread John Posner
On 8/12/2010 6:31 PM, News123 wrote: candidate_box_counts = product( xrange(target/box_sizes[0] + 1), xrange(target/box_sizes[1] + 1), xrange(target/box_sizes[2] + 1), ) Couldn't this be rewritten as: candidate_box_counts = product( * [ xrange

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-13 Thread John Posner
On 8/13/2010 6:25 AM, Roald de Vries wrote: On Aug 12, 2010, at 10:51 PM, John Posner wrote: On 8/12/2010 9:22 AM, Dave Angel wrote: Now you have to find the largest number below 120, which you can easily do with brute force tgt = 120 # thanks, Dave Angel Anytime, but I'm not Dave

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread John Posner
On 8/14/2010 10:52 AM, Baba wrote: for n_nuggets in range(50): result1 = can_buy(n_nuggets) result2 = can_buy(n_nuggets+1) result3 = can_buy(n_nuggets+2) result4 = can_buy(n_nuggets+3) result5 = can_buy(n_nuggets+4) result6 = can_buy(n_nuggets+5) if result1!=[]

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-15 Thread John Posner
On 8/15/2010 11:38 AM, Baba wrote: In addition to the points that Emile and Ian made ... def diophantine_nuggets(x,y,z): cbc=0 #cbc=can_buy counter packages =[x,y,z] You can take advantage of a nifty "syntax convenience feature" here. Instead of loading all of the function's argumen

Re: Opposite of split

2010-08-16 Thread John Posner
On 8/16/2010 12:44 PM, Alex van der Spek wrote: Anybody catches any other ways to improve my program (attached), you are most welcome. 1. You don't need to separate out special characters (TABs, NEWLINEs, etc.) in a string. So: bt='-999.25'+'\t''-999.25'+'\t''-999.25'+'\t''-999.25'+'\t'+'

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-16 Thread John Posner
On 8/16/2010 4:18 PM, Baba wrote: packages=[2,103,105] min_size=min(packages[0],packages[1],packages[2]) or: min_size = min(packages) -John -- http://mail.python.org/mailman/listinfo/python-list

Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-18 Thread John Posner
On 8/18/2010 1:38 PM, [email protected] wrote: To go the other way, if d = 1, then there exists integers (not neccessarily positive) such that a*x + b*y + c*z = 1 That fact is non-trivial, although the proof isn't *too* hard [1]. I found it interesting to demonstrate the simpler cas

Re: Tkinter only: table widget with canvas...

2009-07-10 Thread John Posner
Hi -- a) Assume I would have some different widgets to add per row. How do I get the maximum row height? If you are placing widgets in a table like this: w.grid(row=a, column=b) ... where the *master* of widget "w" is a Tkinter.Frame named "table_frm", then you can determine the height

Re: Tkinter only: table widget with canvas...

2009-07-11 Thread John Posner
There's a working app at http://cl1p.net/tkinter_table_headers/ Thank you for this example. However, one issue to that... When resizing the window (vertical) then the header moves away from the table. How can I avoid this with the grid? With "pack" I now this... Oops ... packing can be tricky

Re: Tkinter / Entry widget problem

2009-07-14 Thread John Posner
Andras Szabo wrote: Hello. I searched the archives but couldn't find a solution to a problem related to the Entry widget in Tkinter. When creating a pop-up window in an app, which contains an Entry widget, I want this widget to contain some default string, to have all this default string s

Re: Announcing PythonTurtle

2009-08-03 Thread John Posner
... I would also venture to say a key-map of sorts that is available thru the help menu where one could push an "Up" button, or a "rotate" button, and have the proper command inserted in the prompt, and then have the command execute, may also help make the connections here, a sort of *real* Visu

Re: Announcing PythonTurtle

2009-08-04 Thread John Posner
Certainly John- although I have not embedded the turtle module at all, I just wrote my own. OK, then why the statements "from turtle import *" in the modules turtleprocess.py and turtlewidget.py? Tx, John -- http://mail.python.org/mailman/listinfo/python-list

Re: Announcing PythonTurtle

2009-08-05 Thread John Posner
OK, then why the statements "from turtle import *" in the modules turtleprocess.py and turtlewidget.py? Yes, I see that now (he said meekly, too sheepish to complain about being misled by an unfortunate naming choice). Tx, John -- http://mail.python.org/mailman/listinfo/python-list

Re: XML parsing with python

2009-08-17 Thread John Posner
Use the iterparse() function of the xml.etree.ElementTree package. http://effbot.org/zone/element-iterparse.htm http://codespeak.net/lxml/parsing.html#iterparse-and-iterwalk Stefan iterparse() is too big a hammer for this purpose, IMO. How about this: from xml.etree.ElementTree import E

Code formatting question: conditional expression

2009-08-18 Thread John Posner
While refactoring some code, I ran across an opportunity to use a conditional expression. Original: if total > P.BASE: excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True) else: excessblk = None Is there any consensus on how to format a conditional expression that i

Re: Code formatting question: conditional expression

2009-08-18 Thread John Posner
My choice would be excessblk = None if total > P.BASE: excessblk = ... Diez and Jean-Michel, Ha! Your suggestion above was my *original* coding. It looks like I'm evolving backwards! But doesn't it violate the DRY principle? The token "excessblk" appears twice instead of once. Than

<    1   2   3   >