Re: Pythonic search of list of dictionaries

2005-01-04 Thread Tim Hochberg
Bulba! wrote: Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary containing the row data looks like this: o={'TermID':'4', 'English':'System Administration'

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Tim Hochberg
Jordan Rastrick wrote: [CHOP] Behold: # An Acceptor/Generator!!! def combineIntoRecords(): optionalline = None # We may not get given a value for this line accept firstline accept secondline if condition(secondline): accept optionalline accept lastline

Re: numarray :: multiplying all the elements in 1d array

2005-12-21 Thread Tim Hochberg
Mandus wrote: > Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam: > >>Hi all, >> Lets say I have an array: >> from numarray import * >> a = array([ 6, 7, 8, 9, 10, 11, 12]) >> >> I want to multiply out all the elements and get the result. >> >> r = 1.0 >>

Re: python coding contest

2005-12-25 Thread Tim Hochberg
Christian Tismer wrote: > Simon Hengel wrote: > >>-BEGIN PGP SIGNED MESSAGE- >>Hash: SHA1 >> >> >>>I'm envisioning lots of convoluted one-liners which >>>are more suitable to a different P-language... :-) >> >>I feel that python is more beautiful and readable, even if you write >>short pro

Re: python coding contest

2005-12-25 Thread Tim Hochberg
Steven D'Aprano wrote: > On Sun, 25 Dec 2005 19:14:43 -0500, rbt wrote: > > >>Steven D'Aprano wrote: >> >>>On Sun, 25 Dec 2005 18:05:37 +0100, Simon Hengel wrote: >>> >>> -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 >I'm envisioning lots of convoluted one-liners which >

Re: python coding contest

2005-12-25 Thread Tim Hochberg
Is it necessary to keep the input parameter as 'input'? Reducing that to a single character drops the length of a program by at least 8 characters. Technically it changes the interface of the function, so it's a little bogus, but test.py doesn't check. (Personally I prefer that if be illegal,

Re: python coding contest

2005-12-25 Thread Tim Hochberg
Remi Villatel wrote: > André Malo wrote: > > >>>is two lines and 347 characters ugly enough to win? > > >>Nope. 3 lines / 179 chars here >:-> >>Yes, it's quite unreadable. > > > I'm in for the second place with 4 lines / 228 chars. > > >>(The problem is that I need to find an internet cafe

Re: python coding contest

2005-12-26 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > Currently I'm on 149 characters in one line - 128 without > spaces/newlines. (it'd be three characters shorter if it didn't have > to end with a "\n") It'll be interesting to see what the short 1-line answers look like. I have a hard time seeing how that's done. It'l

Re: python coding contest

2005-12-26 Thread Tim Hochberg
Claudio Grondi wrote: > Peter Otten wrote: [SNIP] >>How good is "good" for >> >>import test_vectors >>seven_seg = test_vectors.test_vectors.get >> >>or code using the test suite in general? >> >>Peter >> > > This started to remind myself about the story of the contest where the > shortest program

Re: python coding contest

2005-12-26 Thread Tim Hochberg
Steven D'Aprano wrote: > On Mon, 26 Dec 2005 13:33:42 -0700, Tim Hochberg wrote: > > >>Claudio Grondi wrote: > > >>>I am currently at 39 bytes following the requirements and the principle >>>given above (my module passes the test). Anyone able to

Re: python coding contest

2005-12-26 Thread Tim Hochberg
Tim Hochberg wrote: [CHOP] > > import test;seven_seg=test.test_vectors.get > > This works if you run test. It fails if you try to run it standalone > since the import order is wrong. [CHOP] Or maybe not. An earlier version did, but this one seems OK. -tim -- http://mail.pyt

Re: python coding contest

2005-12-27 Thread Tim Hochberg
Paul McGuire wrote: > "Shane Hathaway" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>I'm down to 133 characters (counted according to 'wc -c') on a single >>line. It contains about 11 whitespace characters (depending on what you >>consider whitespace.) It's way too tricky for

Re: python coding contest

2005-12-27 Thread Tim Hochberg
Shane Hathaway wrote: > Tim Hochberg wrote: > >>Paul McGuire wrote: >> >> >>>"Shane Hathaway" <[EMAIL PROTECTED]> wrote in message >>>news:[EMAIL PROTECTED] >>> >>> >>> >>>>I'm down to 133 chara

Re: python coding contest

2005-12-27 Thread Tim Hochberg
py pan wrote: > When you guys say 127~150 characters, did you guys mean > usinging test_vectors.py in some way? Or there's no import at all? > No import at all. The shortest solution reported so far is 131 characters. Getting down to 127 is just a guess as to where the lower bound is likely to

Re: python coding contest

2005-12-27 Thread Tim Hochberg
Shane Hathaway wrote: > Paul McGuire wrote: > >>"Paul McGuire" <[EMAIL PROTECTED]> wrote in message >>news:[EMAIL PROTECTED] >> >> >>>Well *I'm* certainly looking forward to learning some new tricks! My >>>(non-cheat) version is a comparatively-portly 245, and no alternatives are >>>popping into

Re: python coding contest

2005-12-27 Thread Tim Hochberg
Jean-Paul Calderone wrote: > On Tue, 27 Dec 2005 14:02:57 -0700, Tim Hochberg <[EMAIL PROTECTED]> wrote: > >>Shane Hathaway wrote: >> >>>Paul McGuire wrote: >>> >>> >>>Also, here's another cheat version. (No, 7seg.com does n

Re: python coding contest

2005-12-27 Thread Tim Hochberg
Christian Tismer wrote: [SNIP] > And then help me to setup a different contest about content -- chris As usual, I expect that actually having some working code measuring 'Pythonic' length (and I'm sure we could get into all sorts of fun arguments about the exact definition of that) would go a l

Re: query on python list

2005-12-27 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > hi all > am searching for a key in a list, am using > > Found = 0 > for item in list: > if not key == item: > Found = 0 > elif key == item: > Found =1 > > Now based on the Found value i ll manipulate the list. > but whenev

Re: python coding contest

2005-12-28 Thread Tim Hochberg
Shane Hathaway wrote: > I just found a 125 character solution. It's actually faster and more > readable than the 133 character solution (though it's still obscure.) > > It depends on Python 2.4. If Python 2.3 compatibility is required for > the contest, I have to add 4 characters. I asked, 2.

Re: python coding contest

2005-12-28 Thread Tim Hochberg
Marius Gedminas wrote: > Jean-Paul Calderone wrote: > >>On Tue, 27 Dec 2005 14:02:57 -0700, Tim Hochberg <[EMAIL PROTECTED]> wrote: >> >>>Shane Hathaway wrote: >>> >>>>Paul McGuire wrote: >>>> >>>> >>>>Also

Re: python coding contest

2005-12-30 Thread Tim Hochberg
Shane Hathaway wrote: > Andrew Durdin wrote: > >>On 12/28/05, Shane Hathaway <[EMAIL PROTECTED]> wrote: >> >> >>>I just found a 125 character solution. It's actually faster and more >>>readable than the 133 character solution (though it's still obscure.) >> >> >>Having spent a good deal of time a

Re: python coding contest

2005-12-30 Thread Tim Hochberg
Shane Hathaway wrote: > André wrote: > >>For the few that might be interested, I will be posting the details of >>a 117 character long solution to the challenge on my blog >>http://aroberge.blogspot.com/. >> >>Enjoy! > > > You took advantage of prime numbers, enabling you to extract encoded > i

Re: What is the slickest way to transpose a square list of lists (tuple of tuples)?

2006-01-08 Thread Tim Hochberg
Brian van den Broek wrote: > Gerard Brunick said unto the world upon 08/01/06 01:27 PM: > >>My way is ugly. These has to be a better way. >> >>Thanks, >>Gerard > > > If you'd posted your way, I might well have seen if I could do it in a > nicer fashion. But, since for all I know, my best effor

Re: flatten a level one list

2006-01-11 Thread Tim Hochberg
Michael Spencer wrote: > > Robin Becker schrieb: > >> Is there some smart/fast way to flatten a level one list using the > >> latest iterator/generator idioms. > ... > > David Murmann wrote: > > Some functions and timings > ... Here's one more that's quite fast using Psyco, but only average w

Re: numpy magic: cast scalar returns auto to python types float & int ?

2006-11-17 Thread Tim Hochberg
robert wrote: > Turning algs for old NumPy modules into numpy code I suffer from this: > Upon further processing of returns of numpy calculations, lots of data in an > apps object tree will become elementary numpy types. > First there is some inefficiency in calculations. And then you get data >

Re: How can I speed this function up?

2006-11-17 Thread Tim Hochberg
Chris wrote: > This is just some dummy code to mimic what's being done in the real > code. The actual code is python which is used as a scripting language in > a third party app. The data structure returned by the app is more or > less like the "data" list in the code below. The test for "ELEMEN

Re: numpy: frequencies

2006-11-18 Thread Tim Hochberg
Filip Wasilewski wrote: > robert wrote: >> I have an integer array with values limited to range(a,b) like: >> >> ia=array([1,2,3,3,3,4,...2,0,1]) >> >> and want to speedly count the frequencies of the integers into get a density >> matrix. >> Is this possible without looping? > > See numpy.bincou

Re: Simple text parsing gets difficult when line continues to next line

2006-11-28 Thread Tim Hochberg
John Machin wrote: > Jacob Rael wrote: >> Hello, >> >> I have a simple script to parse a text file (a visual basic program) >> and convert key parts to tcl. Since I am only working on specific >> sections and I need it quick, I decided not to learn/try a full blown >> parsing module. My simple scri

Re: Numarray, numeric, NumPy, scpy_core ??!!

2006-01-22 Thread Tim Hochberg
Szabolcs Nagy wrote: >>Basically all I need is vectors and 3x3 matrices. > > > hmm > is numpy really efficient for 3x3 (or 4x4) matrices and vectors? > > IMHO an optimized matrix4x4 class can be much faster (i'm just guessing > here) > > eg cgtypes is a simple c++ implementation with boost-pyth

Re: Scientific Computing with NumPy

2006-02-06 Thread Tim Hochberg
mclaugb wrote: > Is Scipy the same thing as ScientificPython? > I am confused if SciPy is just the new version. they appear to be separate > things. > Bryan No, Scientific Python is "a collection of Python modules that are useful for scientific computing" written by Konrad Hinsen. I'm not a use

Re: Replace a module variable with a function call

2006-02-06 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > I have a module that defines a variable with a constant value and now I > need to make that value dynamic, without affecting module clients. In > other words, I need to call a function witout using parenthesis. > Example: > > mymod.py-- > > def value

Re: Question about idioms for clearing a list

2006-02-07 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > Fredrik Lundh wrote: > >>>Python now has, what, three built-in mutable collections types: >>>lists, dictionaries, and sets. Dicts and sets both have a clear() >>>method and lists do not. >> >>dicts and sets are mappings, and lists are not. mappings don't >>support slici

Re: Shortest prime number program

2006-02-12 Thread Tim Hochberg
While not nearly the shortest proposed thus far, I'm fond of: from itertools import count, ifilter def sieve(s=count(2)): while 1:p=s.next();s=ifilter(p.__rmod__,s);yield p It will generate quite a large number of primes before blowing up (at least 50,000 primes, p=611,957) and it's much fast

Re: Shortest prime number program

2006-02-13 Thread Tim Hochberg
In the spirit of pointless pessimization and obfuscation I have crushed something very similar to Alex Martelli's eratosthenes function onto a single line. It's truly monstrous, but somewhat entertaining [Some preemptive linebreaks added]: def short():import itertools as it;D={};g=D.get;return

Re: trapping errors in function call syntax

2006-02-13 Thread Tim Hochberg
Avi Kak wrote: > Hello: > >Suppose I write a function that I want to be called >with ONLY keyword argumnts, how do I raise an > exception should the function get called with > what look like position-specfic arguments? > > Any help would be appreciated. Say you want the signature

Re: how do you pronounce 'tuple'?

2006-02-13 Thread Tim Hochberg
Erik Max Francis wrote: > Roy Smith wrote: > > >>A more interesting question is what do you call ()? A none-tuple? > > > Yeah, that's at the point where it _really_ departs from anything > remotely mathematical. Don't think I've ever heard the occasion to talk > about 0-tuples in any contex

Re: number ranges (was Re: Matlab page on scipy wiki)

2006-02-19 Thread Tim Hochberg
Colin J. Williams wrote: >> >>It would be good if the range and slice could be merged in some way, >>although the extended slice is rather complicated - I don't understand it. >> >> The semantics for an extended slicing are as follows. The primary >> must evaluate to a mapping object, and it i

Re: number ranges (was Re: Matlab page on scipy wiki)

2006-02-20 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > Tim Hochberg wrote: > >>Colin J. Williams wrote: >> >> >>>>It would be good if the range and slice could be merged in some way, >>>>although the extended slice is rather complicated - I don't understand it. >>&g

Re: Changing values of 1st column of a matrix using take(mymatrix, (0, ), axis=1)

2006-02-20 Thread Tim Hochberg
Anthony Liu wrote: > I am talking about the 'take' method of numarray. > > See here for more info: > > http://stsdas.stsci.edu/numarray/Doc/node33.html > > If I initialize a matrix 'mymatrix' by zeros((3,3), > type="Float64"), I get a 3*3 matrix of all zeros. > > Look: > > from numarray i

Re: number ranges

2006-02-21 Thread Tim Hochberg
[Lots of proposals snipped] 90% of my gripes with range disappeared with the addition of enumerate. However, if there's going to be another round of range literal proposals I might as well throw out what seems (to me anyway) like the only halfway obvious choice in the context of Python. 1. a:

Re: Incorrect Decimal-Float behaviour in comparison tests

2006-03-03 Thread Tim Hochberg
Fredrik Lundh wrote: > "Cassiano, Marco" wrote: > > >>I have difficulties with a weird Python 2.4.2 behaviour in comparing >>Decimal to Floats. >> >>For Example : >> >> >> >from decimal import Decimal >a=Decimal('3.7') >b=6.3 >> >if a > b :print a,b,'a is greater than b - NOT TRUE

Re: Python advocacy in scientific computation

2006-03-06 Thread Tim Hochberg
Magnus Lycka wrote: > Dennis Lee Bieber wrote: [SNIP] > >> For CPU-bound number-crunching, perhaps... For I/O-bound jobs, the >>GIL is(should be) released when ever a thread is blocked waiting for I/O >>to complete. > > > I think CPU-bound number-crunching was the big deal in this case. > S

Re: PEP 359: The "make" Statement

2006-04-17 Thread Tim Hochberg
Carl Banks wrote: > Mike Orr wrote: > >>>I think this PEP is going off the rails. It's primary virtue was that it >> >>was a simpler, clearer way to write: >> >> class Foo(args): >>__metaclass__ = some_metaclass >>#... >> >>And it doesn't even do that. What's wrong with "class

Re: PEP 359: The "make" Statement

2006-04-17 Thread Tim Hochberg
Carl Banks wrote: > Tim Hochberg wrote: > >>Carl Banks wrote: >> >>>Mike Orr wrote: >>> >>> >>>>>I think this PEP is going off the rails. It's primary virtue was that it >>>> >>>>was a simpler, clea

Re: Cryptographically random numbers

2006-03-07 Thread Tim Hochberg
Paul Rubin wrote: > "Tuvas" <[EMAIL PROTECTED]> writes: > >>I've actually done the tests on this one, it's actually faster to use >>the += than a list, odd as it may sound. > > > Frederik explained the reason; there's an optimization in Python 2.4 > that I'd forgotten about, for that specific c

Re: import and shared global variables

2006-03-10 Thread Tim Hochberg
Michael Brenner wrote: > Hi, > > I'm implementing a plugin-based program, structured like the example > below (where m1 in the main module, loading m2 as a plugin). I wanted > to use a single global variable (m1.glob in the example) to store some > config data that the plugins can access. How

Re: Cheese Shop: some history for the new-comers

2006-03-12 Thread Tim Hochberg
[EMAIL PROTECTED] wrote: > richard wrote: > [snip] > Should the "Python Cheeseshop" have anything in it, though? Having a > stocked cheese shop in relation to Python is just silly! Well, it shouldn't have any *cheese*, but that's probably OK for a software repository. -- http://mail.python.org

Re: unboundlocalerror with cgi module

2006-04-10 Thread Tim Hochberg
Kent Johnson wrote: > David Bear wrote: > >>I'm attempting to use the cgi module with code like this: >> >>import cgi >>fo = cgi.FieldStorage() >># form field names are in the form if 'name:part' >>keys = fo.keys() >>for i in keys: >>try: >>item,value=i.split(':') >>

Re: unboundlocalerror with cgi module

2006-04-10 Thread Tim Hochberg
Tim Hochberg wrote: > Kent Johnson wrote: > >>David Bear wrote: >> >> >>>I'm attempting to use the cgi module with code like this: >>> >>>import cgi >>>fo = cgi.FieldStorage() >>># form field names are in t

Re: unboundlocalerror with cgi module

2006-04-10 Thread Tim Hochberg
Kent Johnson wrote: > Tim Hochberg wrote: > >>Kent Johnson wrote: >> >>>David Bear wrote: >>> >>> >>>>I'm attempting to use the cgi module with code like this: >>>> >>>>import cgi >>>>fo = cgi.Fiel

Re: PEP 359: The "make" Statement

2006-04-14 Thread Tim Hochberg
Steven Bethard wrote: > Steven Bethard wrote: > >>Duncan Booth wrote: >> >>>Steven Bethard wrote: >>> >>> Should users of the make statement be able to determine in which dict object the code is executed? The make statement could look for a ``__make_dict__`` attribute and call it to a

Re: PEP 359: The "make" Statement

2006-04-14 Thread Tim Hochberg
Steven Bethard wrote: > Steven Bethard wrote: > >>Tim Hochberg wrote: >> >>>Steven Bethard wrote: >>> >>>>Steven Bethard wrote: >>>> >>>>>Duncan Booth wrote: >>>>> >>>>>>

Re: PEP 359: The "make" Statement

2006-04-15 Thread Tim Hochberg
Tim Hochberg wrote: > Steven Bethard wrote: > >> Steven Bethard wrote: >> >>> Tim Hochberg wrote: >>> >>>> Steven Bethard wrote: >>>> >>>>> Steven Bethard wrote: >>>>> >>>>>> Dun