Re: Extending dict (dict's) to allow for multidimensional dictionary

2011-03-07 Thread TomF
On 2011-03-05 12:05:43 -0800, Paul Rubin said: Ravi writes: I can extend dictionary to allow for the my own special look-up tables. However now I want to be able to define multidimensional dictionary which supports look-up like this: d[1]['abc'][40] = 'dummy' Why do that anyway? You can us

Simple way of handling errors

2009-05-06 Thread TomF
As a relative newcomer to Python, I like it a lot but I'm dismayed at the difficulty of handling simple errors. In Perl if you want to anticipate a file-not-found error you can simply do: open($file) or die("open($file): $!"); and you get an intelligible error message. In Python, to get the

Re: Simple way of handling errors

2009-05-06 Thread TomF
On 2009-05-06 19:41:29 -0700, Steven D'Aprano said: On Wed, 06 May 2009 16:40:19 -0700, TomF wrote: As a relative newcomer to Python, I like it a lot but I'm dismayed at the difficulty of handling simple errors. In Perl if you want to anticipate a file-not-found error you can

Re: Simple way of handling errors

2009-05-07 Thread TomF
On 2009-05-07 01:01:57 -0700, Peter Otten <__pete...@web.de> said: TomF wrote: As a relative newcomer to Python, I like it a lot but I'm dismayed at the difficulty of handling simple errors. In Perl if you want to anticipate a file-not-found error you can simply do: open($fi

Python system with exemplary organization/coding style

2009-05-14 Thread TomF
I'm looking for a medium-sized Python system with very good coding style and good code organization, so I can learn from it. I'm reading various books on Python with advice on such things but I'd prefer to see a real system. By medium-sized I mean 5-20 classes, 5-20 files, etc; a code base th

Re: Python system with exemplary organization/coding style

2009-05-15 Thread TomF
On 2009-05-14 16:18:07 -0700, CTO said: On May 14, 7:01 pm, TomF wrote: I'm looking for a medium-sized Python system with very good coding style and good code organization, so I can learn from it.  I'm reading various books on Python with advice on such things but I'd prefe

Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread TomF
On 2010-02-16 11:44:45 -0800, a...@pythoncraft.com (Aahz) said: In article <4b7a91b1.6030...@lonetwin.net>, steve wrote: On 02/16/2010 05:49 PM, W. eWatson wrote: See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2. The most obvious would be a.index(max(a)). Is that what you wante

Re: formatting a number as percentage

2010-02-21 Thread TomF
On 2010-02-21 09:53:45 -0800, vsoler said: I'm trying to print .7 as 70% I've tried: print format(.7,'%%') .7.format('%%') but neither works. I don't know what the syntax is... print "Grade is {0:%}".format(.87) Grade is 87.00% or if you want to suppress those trailing zeroes: print

Re: cpan for python?

2010-03-02 Thread TomF
On 2010-03-02 13:14:50 -0800, R Fritz said: On 2010-02-28 06:31:56 -0800, sstein...@gmail.com said: On Feb 28, 2010, at 9:28 AM, Someone Something wrote: Is there something like cpan for python? I like python's syntax, but Iuse perl because of cpan and the tremendous modules that it has. -

Re: cpan for python?

2010-03-03 Thread TomF
On 2010-03-02 19:59:01 -0800, Lie Ryan said: On 03/03/2010 09:47 AM, TomF wrote: On 2010-03-02 13:14:50 -0800, R Fritz said: On 2010-02-28 06:31:56 -0800, sstein...@gmail.com said: On Feb 28, 2010, at 9:28 AM, Someone Something wrote: Is there something like cpan for python? I like

Re: to pass self or not to pass self

2010-03-15 Thread TomF
On 2010-03-15 09:39:50 -0700, lallous said: Hello, Learning Python from the help file and online resources can leave one with many gaps. Can someone comment on the following: # - class X: T = 1 def f1(self, arg): print "f1, arg=%d" % arg def f2(self, arg):

Re: sum for sequences?

2010-03-24 Thread TomF
On 2010-03-24 14:07:24 -0700, Steven D'Aprano said: On Wed, 24 Mar 2010 15:29:07 +, kj wrote: Is there a sequence-oriented equivalent to the sum built-in? E.g.: seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6) ? Yes, sum. help(sum) is your friend. You might not want t

Re: (a==b) ? 'Yes' : 'No'

2010-03-31 Thread TomF
On 2010-03-31 00:57:51 -0700, Peter Otten <__pete...@web.de> said: Pierre Quentel wrote: I'm surprised nobody proposed a solution with itertools ;-) next(itertools.takewhile(lambda _: a == b, ["yes"]), "no") You spoke to soon :) I salute you, sir, for upholding the standards of this group.

Comparisons of incompatible types

2010-12-06 Thread TomF
I'm aggravated by this behavior in python: x = "4" print x < 7# prints False The issue, of course, is comparisons of incompatible types. In most languages this throws an error (in Perl the types are converted silently). In Python this comparison fails silently. The documentation says:

Re: Comparisons of incompatible types

2010-12-06 Thread TomF
On 2010-12-06 09:04:00 -0800, Peter Otten said: TomF wrote: I'm aggravated by this behavior in python: x = "4" print x < 7# prints False The issue, of course, is comparisons of incompatible types. In most languages this throws an error (in Perl the types are convert

Re: Comparisons of incompatible types

2010-12-07 Thread TomF
On 2010-12-07 16:09:17 -0800, Mark Wooding said: Carl Banks writes: I think that feeling the need to sort non-homogenous lists is indictative of bad design. Here's a reason you might want to. You're given an object, and you want to compute a hash of it. (Maybe you want to see whether some

Re: True lists in python?

2010-12-19 Thread TomF
On 2010-12-18 22:18:07 -0800, Dmitry Groshev said: Is there any way to use a true lists (with O(c) insertion/deletion and O(n) search) in python? For example, to make things like reversing part of the list with a constant time. I assume you mean a C extension that implements doubly linked list

examples of realistic multiprocessing usage?

2011-01-16 Thread TomF
I'm trying to multiprocess my python code to take advantage of multiple cores. I've read the module docs for threading and multiprocessing, and I've done some web searches. All the examples I've found are too simple: the processes take simple inputs and compute a simple value. My problem inv

Re: Not clear about the dot notation

2011-01-16 Thread TomF
On 2011-01-16 11:59:11 -0800, Zeynel said: What does vote.vote refer to in this snippet? def txn(): quote = Quote.get_by_id(quote_id) vote = Vote.get_by_key_name(key_names = user.email(), parent = quote) if vote is None: vote = Vote(key_name = user.email(

Re: Not clear about the dot notation

2011-01-16 Thread TomF
On 2011-01-16 12:44:35 -0800, Zeynel said: On Jan 16, 3:24 pm, TomF wrote: vote refers to the Vote instance. So he must have instatiated previously like vote = Vote() is this correct? Yes. So I have a model class Item(db.Model): title = db.StringProperty() url

Re: examples of realistic multiprocessing usage?

2011-01-16 Thread TomF
On 2011-01-16 19:16:15 -0800, Dan Stromberg said: On Sun, Jan 16, 2011 at 11:05 AM, TomF wrote: I'm trying to multiprocess my python code to take advantage of multiple cores.  I've read the module docs for threading and multiprocessing, and I've done some web searches.  All th

Re: examples of realistic multiprocessing usage?

2011-01-16 Thread TomF
On 2011-01-16 20:57:41 -0800, Adam Skutt said: On Jan 16, 11:39 pm, TomF wrote: One difficulty is that there is a queue of work to be done and a queue of results to be incorporated back into the parent; there is no one-to-one correspondence between the two.  It's not obvious to me h

distutils examples?

2010-04-16 Thread TomF
I'm packaging up a program with distutils and I've run into problems trying to get setup.py right. It's not a standalone package; it's a script plus modules, data files and documentation. I've been over the distutils documentation but I'm having trouble getting the package_data and data_files

Re: getting a string as the return value from a system command

2010-04-18 Thread TomF
On 2010-04-16 12:06:13 -0700, Catherine Moroney said: Hello, I want to call a system command (such as uname) that returns a string, and then store that output in a string variable in my python program. What is the recommended/most-concise way of doing this? I could always create a temporary f

Django as exemplary design

2010-05-03 Thread TomF
I'm interested in improving my python design by studying a large, well-designed codebase. Someone (not a python programmer) suggested Django. I realize that Django is popular, but can someone comment on whether its code is well-designed and worth studying? Thanks, -Tom -- http://mail.python

Re: Django as exemplary design

2010-05-04 Thread TomF
Thanks to everyone for their comments. On 2010-05-04 07:11:08 -0700, alex23 said: TomF wrote: I'm interested in improving my python design by studying a large, well-designed codebase.  Someone (not a python programmer) suggested Django.  I realize that Django is popular, but can so

Re: Django as exemplary design

2010-05-06 Thread TomF
On 2010-05-06 18:20:02 -0700, Trent Nelson said: I'm interested in improving my python design by studying a large, well-designed codebase. I'll tell you one of the best ways to improve your Python code: attend one of Raymond Hettinger's Code Clinic workshops at a Python conference and put some

Re: Global variables for python applications

2010-05-19 Thread TomF
On 2010-05-16 12:27:21 -0700, christian schulze said: On 16 Mai, 20:20, James Mills wrote: On Mon, May 17, 2010 at 4:00 AM, Krister Svanlund wrote: On Sun, May 16, 2010 at 7:50 PM, AON LAZIO wrote:    How can I set up global variables for the entire python applications? Like I can call an

Re: Global variables for python applications

2010-05-20 Thread TomF
On 2010-05-19 07:34:37 -0700, Steven D'Aprano said: # Untested. def verbose_print(arg, level, verbosity=1): if level <= verbosity: print arg def my_function(arg): my_print(arg, level=2) return arg.upper() if __name__ == '__main__': if '--verbose' in sys.argv:

Re: please help explain this result

2010-10-17 Thread TomF
On 2010-10-17 10:21:36 -0700, Paul Kölle said: Am 17.10.2010 13:48, schrieb Steven D'Aprano: On Sun, 17 Oct 2010 03:58:21 -0700, Yingjie Lan wrote: Hi, I played with an example related to namespaces/scoping. The result is a little confusing: [snip example of UnboundLocalError] Python's sco

Deferring a function call

2010-10-18 Thread TomF
I'm writing a simple simulator, and I want to schedule an action to occur at a later time. Basically, at some later point I want to call a function f(a, b, c). But the values of a, b and c are determined at the current time. One way way to do this is to keep a list of entries of the form [[T

Re: Deferring a function call

2010-10-19 Thread TomF
Thanks for the ideas, everyone. functools.partial and lambda expressions seem like a more pythonic way of doing what I want. I don't know whether they're actually more efficient or better, but at least they eliminate the need to carry args around separately. I'd forgotten Python has a sched

Request for comments on a design

2010-10-22 Thread TomF
I have a program that manipulates lots of very large indices, which I implement as bit vectors (via the bitarray module). These are too large to keep all of them in memory so I have to come up with a way to cache and load them from disk as necessary. I've been reading about weak references a

Re: Request for comments on a design

2010-10-23 Thread TomF
On 2010-10-23 01:50:53 -0700, Peter Otten said: TomF wrote: I have a program that manipulates lots of very large indices, which I implement as bit vectors (via the bitarray module). These are too large to keep all of them in memory so I have to come up with a way to cache and load them from