Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread David Perlman
Oh, except one problem: the csv.DictWriter lets you tell it what order you want the columns output in. With your version, they just show up in whatever order Python wants them. On May 28, 2010, at 2:33 AM, Peter Otten wrote: I think it's simpler and therefore more appropriate to use a norm

Re: [Tutor] list of dicts <-> dict of lists?

2010-05-28 Thread David Perlman
Aha, now this is the clever solution that I didn't find "outside the box". :) On May 28, 2010, at 2:33 AM, Peter Otten wrote: I think it's simpler and therefore more appropriate to use a normal csv.writer here: import csv import sys data = {'a': [1, 2, 3], 'c': [7, 8, 9], 'b': [4, 5, 6]} w

[Tutor] list of dicts <-> dict of lists?

2010-05-27 Thread David Perlman
Using the csv.DictReader and csv.DictWriter lets you read and write lists of dictionaries from files containing tabular data. I have a system that naturally generates tabular data in the form of a dictionary of lists: the dictionary key is the name of the column, and then the value is a li

Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread David Perlman
On Feb 17, 2010, at 4:17 PM, Sander Sweers wrote: On 17 February 2010 22:37, David Perlman wrote: As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? There is pytz [1] which should provide a simpler way to m

Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread David Perlman
*(t[4]-u[4]) + (t[5]-u[5]) return datetime.timedelta(seconds=osec) As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? On Feb 17, 2010, at 3:12 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:48 PM, David Perlman wrote: Surel

Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread David Perlman
ion already; it doesn't require any kind of fancy calculations about global politics or anything. On Feb 17, 2010, at 3:12 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:48 PM, David Perlman wrote: Surely there is a way to simply print out the local time, date and time zone with

Re: [Tutor] datetime, time zones, and ISO time

2010-02-17 Thread David Perlman
ithout subclassing tzinfo. This makes me feel like I MUST be missing something obvious, because it shouldn't require so much coding just to find out what the current local time and timezone is! On Feb 17, 2010, at 1:42 PM, William Witteman wrote: On Wed, Feb 17, 2010 at 12:44:02PM -0600

[Tutor] datetime, time zones, and ISO time

2010-02-17 Thread David Perlman
I have been really scratching my head over this, it seems like there *should* be a nice easy way to do what I want but I can't find it for the life of me. What I would like to do would be something like this: >>> datetime.datetime.now().isoformat() '2010-02-17T12:13:17.913260-06:00' But wha

[Tutor] subclass question

2009-12-19 Thread David Perlman
If I make a subclass of a built-in class, like this: class mylist(list): def __init__(self): list.__init__(self) Then it is valid for me to do this: >>> x=mylist() >>> x.hello=3 >>> But I can't do this: >>> y=list() >>> y.hello=3 Traceback (most recent call last): File "", line

Re: [Tutor] Methods that return instances of their own class?

2009-10-15 Thread David Perlman
Maybe there's a prettier way to do this, but I did it explicitly like this because the "transpose" I needed was specific to the structure of the game payoff matrix; it isn't a normal mathematical transpose. I agree that it's hard to read but I'm not sure the specific needs of this applicat

Re: [Tutor] Methods that return instances of their own class?

2009-10-15 Thread David Perlman
Oh my goodness, it really is that easy! I guess I assumed that was too simple to work. Silly me. Thanks Andre! On Oct 15, 2009, at 10:31 AM, Andre Engels wrote: On Thu, Oct 15, 2009 at 5:14 PM, David Perlman wrote: I'm trying to figure out how to define a class so that its inst

[Tutor] Methods that return instances of their own class?

2009-10-15 Thread David Perlman
I'm trying to figure out how to define a class so that its instances have a method that return a different object of the same class. In particular, I'm trying to run a simple prisoner's dilemma game, and I want to make a "game" object that has a method which returns the "game" object with t

Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread David Perlman
OK, I thought I had this one fixed but it was weirder than I thought. I think I understand what's going on, but I wanted to check with the experts here. I have the following class definition, which does not subclass anything: class oneStim: def __init__(self, time, mods=[], dur=None, fo

Re: [Tutor] Poorly understood error involving class inheritance

2009-09-10 Thread David Perlman
Yeah, this seems to be the best answer in this situation. :) On Sep 10, 2009, at 4:17 PM, Kent Johnson wrote: I would skip the cleverness of trying to subclass string. You can use str(z).rjust(20) as above, or use string formatting: '%20s' % z -- -dave---

Re: [Tutor] Poorly understood error involving class inheritance

2009-09-10 Thread David Perlman
Well, here's what I am really doing: class oneStim(str): def __init__(self, time, mods=[], dur=None, format='%1.2f'): self.time=time self.mods=mods self.dur=dur self.format=format def __cmp__(self,other): return cmp(self.time,other.time) def _

[Tutor] Poorly understood error involving class inheritance

2009-09-10 Thread David Perlman
I'm not sure why I'm getting an error at the end here: >>> class dummy: ... def __init__(self,dur=0): ... self.dur=dur ... >>> z=dummy(3) >>> z.dur 3 >>> z=dummy(dur=3) >>> z.dur 3 That worked fine, of course. >>> class dummy2(str): ... def __init__(self,dur=0): ...

[Tutor] Processing a string of bytes

2009-09-03 Thread David Perlman
I have successfully used the builtin "wave" module to read an audio file in as a string of bytes: >>> x=wave.open('DaCWL.wav','rb') >>> x.getparams() (1, 2, 44100, 15440, 'NONE', 'not compressed') >>> samp=x.readframes(15440) >>> type(samp) The x.getparams() indicates that there is one audio

[Tutor] Interactive plots...

2008-03-27 Thread David Perlman
I am thinking about writing a program which will involve, among other things, displaying a plot of a series of numbers. The idea is that you could click on the points and move them to change the numbers. Reverse-plotting, I suppose. It need not be complex; the numbers will all be zero or

Re: [Tutor] automate daily tasks

2007-09-11 Thread David Perlman
As far as making something run automatically at various times, if you're certain that you want to do it in a Mac-only way, Apple's recommended method for timing jobs is described here: http://developer.apple.com/macosx/launchd.html "Getting started with launchd" otherwise use cron or at, as Tom

Re: [Tutor] making math problems mmmm fun

2007-09-11 Thread David Perlman
Also keep in mind that if you want your code to work, your grammar and punctuation in code has to be absolutely flawless. Writing English well is excellent practice for writing code well; writing English poorly is excellent practice for writing code poorly. You get what you pay for. If you

Re: [Tutor] Regex Ordering

2007-07-23 Thread David Perlman
Then maybe you could write a regex that matches every regex that does not match itself. ha! sorry, couldn't resist. On Jul 23, 2007, at 11:29 AM, Bill Campbell wrote: > On Mon, Jul 23, 2007, James Hartley wrote: >> On 7/23/07, Shidan <[EMAIL PROTECTED]> wrote: >>> I'm looking for a Python mod

Re: [Tutor] Bundle help!

2007-07-09 Thread David Perlman
Maybe this reference will help: http://xahlee.org/perl-python/sort_list.html Just the first part of the discussion there. On Jul 9, 2007, at 9:54 AM, Sara Johnson wrote: > Sorry to be so confusing. Just realized a dumb mistake I made. It > doesn't need to be resorted alphabetically and numeri

Re: [Tutor] Bundle help!

2007-07-09 Thread David Perlman
I think what you want to do is start from the beginning with two separate lists, sort each one however you want, and then either join them with zip() or simply reference them as (list1[n], list2[n]). I believe there's also a way to use zip() to separate your list of tuples into separate list

Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-06 Thread David Perlman
I think it's a little strange and possibly problematic that type(1) is 'int' and type(True) is 'bool' but 1 == True specifically evaluates to True even though anything else, even if it evaluates to True when cast as a boolean, is not == True. >>> 1 == True True >>> 2 == True False >>> 0 ==

Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-06 Thread David Perlman
On Mar 6, 2007, at 4:28 PM, wesley chun wrote: >> >>> x=('i' in 'i') >> >>> x >> True >> >>> y='i' >> >>> x==y >> False > > you're right when you talk about "casting" altho that's not what > python does. it merely performs an object value comparison when you > use '=='. for example, change

Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-06 Thread David Perlman
On Mar 6, 2007, at 11:03 AM, Alan Gauld wrote: > It's doing the latter and since anything that's not 'empty' in > Python evaluates to true we wind up checking whether > true == (item in word) > > So if the item is in word we get true == true which is true. > > HTH, Sorry, but this still doesn't ma

Re: [Tutor] trouble with function-- trying to check differences btwn 2 strings

2007-03-06 Thread David Perlman
OK, I'm new to python too so I don't assume I know what I'm talking about yet, but this looks like a mess to me. What exactly does "item == item in word2" evaluate to? Does "in" or "==" have higher precedence? I can't figure out how this would ever work at all. It seems like it's either

Re: [Tutor] Yet another list comprehension question

2007-03-02 Thread David Perlman
On Mar 2, 2007, at 9:56 AM, Alan Gauld wrote: > Why not use a Set? > > s = Set([somefun(i) for i in some-iterator]) > > Might be slow for big lists though... I'm curious why using a Set would be slower than doing it in a loop? In either case, the processor has to scan through all the data l

Re: [Tutor] Two sys.exit questions

2007-02-28 Thread David Perlman
If you want to play around with this stuff, you can first import sys, and then insert this line in the except clause: print repr(sys.exc_info()) (or some other way of getting the details out of the returned tuple.) That will tell you exactly what brought you to the except clause. On Feb 28, 200

Re: [Tutor] Telnet and special characters

2007-02-28 Thread David Perlman
This question isn't well posed. There is no such thing as an F1 "character". Data is sent over telnet connections as 8-bit bytes. You can send any combination of 8-bit bytes you want by coding them in a number of different ways, such as chr(xx) like you wrote below, or '\xnn' or whatever

Re: [Tutor] Group sequence pairwise

2007-02-25 Thread David Perlman
I found this by "using Google". You should be able to make a simple modification (I can think of a couple of ways to do it) to have it pad the end with "None". It is 100% iterator input and output. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 On Feb 25, 2007, at 7:06 AM, C

Re: [Tutor] geeks like us and the rest of THEM

2007-02-25 Thread David Perlman
Keep in mind that things generally become extremely reliable only after extensive real-world testing. TANSTAAFL On Feb 25, 2007, at 1:14 AM, Kirk Bailey wrote: > This has to be baby carriage reliable and simple for the business road > warrior who has not a geekified bone in their body. --

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-21 Thread David Perlman
234567890) > _t1 = _timer() > return _t1 - _t0 > """ > I get these results: > > for computeBin(12345678901234567890) > 1000 loops, best of 3: 448 usec per loop > > for computeBin(1234567890) > 1 loops, best of 3: 59.7 usec per loop > >

Re: [Tutor] Function for converting ints from base10 to base2?

2007-02-20 Thread David Perlman
I suspect the function I sent out earlier, using octal conversion and a lookup table, will be faster. But it would be very interesting to see some simple benchmarks. On Feb 20, 2007, at 10:47 PM, Dick Moores wrote: > I was surprised to be unable to find a function in Python for > converting

Re: [Tutor] Struct the solution for Hex translation

2007-02-19 Thread David Perlman
You're way off base... :) On Feb 19, 2007, at 9:25 AM, Johan Geldenhuys wrote: > Here is what I have: > data > '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00 > \x00\x17\x > 01C\xc7' data[0] > '\xa5' len(data[0]) > 1 > > You see that data[0] is only one by

Re: [Tutor] control multiple FTP sessions using multiple ipconnectionsvia different com ports

2007-02-19 Thread David Perlman
This kind of thing is usually handled at the level of the OS's routing table. Routing tables have an entry called "metric" that is used to weight the different routes, so that when there are multiple possible links available, the one with the lowest metric is used first. In Unix at least,