[RELEASED] Python 3.1.1

2009-08-16 Thread Benjamin Peterson
On behalf of the Python development team, I'm happy to announce the first bugfix
release of the Python 3.1 series, Python 3.1.1.

This bug fix release fixes many normal bugs and several critical ones including
potential data corruption in the io library.

Python 3.1 focuses on the stabilization and optimization of the features and
changes that Python 3.0 introduced.  For example, the new I/O system has been
rewritten in C for speed.  File system APIs that use unicode strings now handle
paths with undecodable bytes in them. Other features include an ordered
dictionary implementation, a condensed syntax for nested with statements, and
support for ttk Tile in Tkinter.  For a more extensive list of changes in 3.1,
see http://doc.python.org/3.1/whatsnew/3.1.html or Misc/NEWS in the Python
distribution.

Please note the Windows and Mac binaries are not available yet but
will be in the coming days.

To download Python 3.1.1 visit:

 http://www.python.org/download/releases/3.1.1/

The 3.1 documentation can be found at:

 http://docs.python.org/3.1

Bugs can always be reported to:

 http://bugs.python.org


Enjoy!

--
Benjamin Peterson
Release Manager
benjamin at python.org
(on behalf of the entire python-dev team and 3.1.1's contributors)
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/


Re: Pygresql, and query meta informations

2009-08-16 Thread Tim Roberts
durumdara durumd...@gmail.com wrote:

Pygresql, DB-API.

I search for a solution to get meta information about last query,
because I must export these infos to Delphi.

Delphi have TDataSet, and it have meta structure that must be defined
before I create it.
For char/varchar fields I must define their sizes!

Pygresql is not retreive the field sizes.

You should be able to get that information from the .description member of
the cursor object.  Have you checked it?

If not, you will have to use fetch the information from the
Postgres-specific tables, pg_class and pg_attribute.  pg_class contains
information about your tables; when you fetch the class number for your
table, you can look up the columns in pg_attribute.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-16 Thread John Nagle

Hendrik van Rooyen wrote:

On Saturday 15 August 2009 14:40:35 Michael Ströder wrote:

Hendrik van Rooyen wrote:

In the past, on this  group, I have made statements that said that on
Linux, the serial port handling somehow does not allow transmitting and
receiving at the same time, and nobody contradicted me.


Absolutely false.


Despite all the good comments here by other skilled people I'd recommend to
determine whether the transmission line to the devices accessed support
full duplex.


All standard PC serial ports are full-duplex devices.

Here's a program I wrote which uses pyserial to drive Baudot teletypes
as full-duplex devices.

https://sourceforge.net/projects/baudotrss/

This uses an input thread and an output thread.  It reads RSS feeds and
prints them on antique Teletype machines.  (Reuters RSS feeds produce
a classic news ticker.  Twitter RSS feeds work but look silly when
hammered out on yellow paper at 45.45 baud.)


You raise a good point, that is probably not well known amongst the youngsters 
here, as simple serial multidropping has gone out of fashion.


Actually, no.  Dynamixel servos as used on the latest Bioloid robots
are multidrop serial RS-485.  But outside the embedded world, nobody uses
that stuff any more.  (Embedded is going Ethernet; it's overkill but
works fine and is now cheap.)

John Nagle
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Emmanuel Surleau
 Dr. Phillip M. Feldman wrote:

[snip]

  def is_prime(n):
 for j in range(2,n):
if (n % j) == 0: return False
 return True
 
  It seems as though Python is actually expanding range(2,n) into a list of
  numbers, even though this is incredibly wasteful of memory. There should
  be a looping mechanism that generates the index variable values
  incrementally as they are needed.

 You already have an answer to the range issue, so I will only add that
 putting a loop inside another loop is a decent way to expand the time
 taken.

 I will also observe that if you were to stop programming whatever
 language you are more familiar with in Python, and start programming
 Python in Python, you'll have an easier time of it.

I don't see what's particularly un-Pythonic with this code. Not using xrange() 
is a mistake, certainly, but it remains clear, easily understandable code 
which correctly demonstrates the naive algorithm for detecting whether n is a 
prime. It doesn't call for condescension

 The Dive Into Python is an excellent start for that.

I never read it, but I was under the impression that the tutorial on 
python.org was geared toward programmers moving to Python from other 
languages. It was also my impression that Dive Into Python is rather outdated 
and occasionally inaccurate (based on comments on this mailing list).

Cheers,

Emm
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for cycle with assigning index

2009-08-16 Thread dmitrey
Thanks all, especially Dennis for your detailed answer.
left_arr_indexes is list of nonnegative integers, eg [0,0,0,1,1,4]
IndDict is a dict like {0: [1,2], 3: [0,1], 10:[0,2,3]}, so that's why
I don't use python list instead.
The code is taken from OpenOpt framework that I develop. Currently I
have implemented another workaround but the solution you proposed can
be used in other parts of code.
Regards, D.

On Aug 16, 4:23 am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
 On Sat, 15 Aug 2009 13:22:08 -0700, Dennis Lee Bieber
 wlfr...@ix.netcom.com declaimed the following in
 gmane.comp.python.general:

         Okay -- my lack of experience with lambda shows... forgot to guard
 the intermediates...

   for i in xrange(len(Funcs2)):
        Funcs.append(lambda i=i,*args, **kwargs: (Funcs2[i](*args,
   **kwargs)[IndDict[left_arr_indexes[i]]]))

   I get list indices must be integers, not dict (and i is equal to
   Python dictionary, that is Funcs2)

         So... with the following assumptions (and ignoring all the other
 respondents G)...

 1)      Funcs2 IS a dictionary keyed by sequential integers starting at 0
 2)      Order in Funcs matters; that is, the first element of the list must
 align with the element of the dictionary having key 0
 3)      IndDict and left_arr_indexes DO NOT CHANGE CONTENT after the loop
 runs
 4)      left_arr_indexes is at least as long as Funcs2

 assert (min(Funcs2.keys()) == 0
                         and
                 max(Funcs2.keys()) == (len(Funcs2) - 1))        #1
 assert len(left_arr_indexes) = len(Funcs2)          #4

 Funcs = [None] * len(Funcs2)                                            #2a
 for ki, fv in Funcs2.items():   #ki-key as index; fv-function value
         ritmp = IndDict[left_arr_indexes[ki]]                           #3
         Funcs[ki] = (lambda ri=ritmp, fn=fv, *args, **kwargs:
                                         (fn(*args, **kwargs)[ri])             
           #2b

 ri and fn used to protect the intermediates, ritmp and fv, from changes

 2a is needed as the order of .items() is undefined, so .append() can not
 be used. This presets the result list size permitting direct indexing
 (2b) to fill slots in order.

 #1 can still fail itself if a key is NOT an integer (what is the min()
 of 0 and a)

         I still don't know if IndDict is really a list or a dictionary, nor
 what left_array_indexes contains (by name, it is a list of values to
 index into another list -- but could be a list of keys to access a
 dictionary)

         And I'll reiterate: if you are using dictionaries in which the keys
 are sequential integers starting at 0... Replace them with simple
 lists...
 --
         Wulfraed         Dennis Lee Bieber               KD6MOG
         wlfr...@ix.netcom.com      HTTP://wlfraed.home.netcom.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Hendrik van Rooyen

Steven D'Aprano st...@remove-this-c...e.com.au wrote:

Now that I understand what the semantics of cout  Hello world are, I 
don't have any problem with it either. It is a bit weird, Hello world 
 cout would probably be better, but it's hardly the strangest design in 
any programming language, and it's probably influenced by input 
redirection using  in various shells.

I find it strange that you would prefer:

Hello world  cout 
over:
cout  Hello world 

The latter seems to me to be more in line with normal assignment: -
Take what is on the right and make the left the same.
I suppose it is because we read from left to right that the first one seems 
better to you.
Another instance of how different we all are.

It goes down to the assembler - there are two schools:

mova,b  - for Intel like languages, this means move b to a
mova,b  - for Motorola like languages, this means move a to b

Gets confusing sometimes.

- Hendrik



-- 
http://mail.python.org/mailman/listinfo/python-list


my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
I'm trying to write program to translate define macro in 'C'.
And start_parse has return condition that list's length is 0.
At this time return statement invoke start_parse() function.
I can't understand do that.

I'm using Python 2.6.2 in Windows XP

import re
import sys
comment = '''
#if defined (FEATURE_ONENESTED)
#define PYTHON_POWERED
#if defined(ANY_LANGUAGE)
#error
#endif
#else
#define FEATURE_NONE
#endif
'''

symbol_table = ['FEATURE_ONENESTED']

valid_area = False

p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]*')
p_if = re.compile('^[\t ]*#[\t ]*if[\t
]+defined[\s]*[\(]*([a-zA-Z0-9_]+)[\)]*[\t ]*')
p_elif = re.compile('^[\t ]*#[\t ]*elif[\t ]*')
p_else = re.compile('^[\t ]*#[\t ]*else[\t ]*')
p_endif = re.compile('^[\t ]*#[\t ]*endif[\t ]*')

def start_parse(macro):
global valid_area
if len(macro) == 0:
return

if valid_area == True:

if p_else.match(macro[0]):
valid_area = False
macro.pop(0)
start_parse(macro)

match = p_define.match(macro[0])
if match:
symbol_table.append(match.group(1))
macro.pop(0)
start_parse(macro)


match = p_if.match(macro[0])
if match:
for symbol in symbol_table:#print match.group(1)
if match.group(1) == symbol:
#print match.group(1)
valid_area = True
else:
valid_area = False

if p_else.match(macro[0]):
macro.pop(0)
start_parse(macro)

match = p_endif.match(macro[0])
if match:
valid_area = False

macro.pop(0)
start_parse(macro)

if __name__ == '__main__':
l = comment.splitlines()
start_parse(l)
print symbol_table
-- 
CashFlow
To be rich.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Chris Rebert
On Sat, Aug 15, 2009 at 8:09 AM, Xavier Hocont...@xavierho.com wrote:
 Hey all,

 I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in the
 documentation.

 Is there a substitution for this special method in 3.1, or do I really have
 to define all six rich comparison methods to work it out?

I don't think so. Quoting
http://docs.python.org/dev/py3k/reference/datamodel.html

There are no swapped-argument versions of [the comparison] methods
(to be used when the left argument does not support the operation but
the right argument does); rather, __lt__() and __gt__() are each
other’s reflection, __le__() and __ge__() are each other’s reflection,
and __eq__() and __ne__() are their own reflection.

I believe this means you only need to define one method from each of
the following pairs to get all the operators working:
* __eq__ or __ne__
* __lt__ or __gt__
* __ge__ or __le__

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Chris Rebert
On Sat, Aug 15, 2009 at 1:06 PM, Mark Lawrencebreamore...@yahoo.co.uk wrote:
 Xavier Ho wrote:

 Hey all,

 I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in
 the
 documentation.

 Is there a substitution for this special method in 3.1, or do I really
 have
 to define all six rich comparison methods to work it out?

 If this question has already been asked somewhere, I apologise in advance.
 Already googled around but I didn't find information on this.

 Any replies appreciated.

 http://bytes.com/topic/python/answers/844614-python-3-sorting-comparison-function

That seems to be about the built-in function cmp(). The OP was asking
about the special method __cmp__ and the related rich comparison
special methods (__eq__, __lt__, etc).

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: random.gauss vs. random.normalvariate

2009-08-16 Thread Chris Rebert
 On Sat, Aug 15, 2009 at 10:18 PM, Paul Rubin http://phr...@nospam.invalid
 wrote:

 Dennis Lee Bieber wlfr...@ix.netcom.com writes:
        No language can guard against independent access of a
  shared/global
  object by multiple threads...

 Erlang?

On Sun, Aug 16, 2009 at 12:23 AM, John Haggertybouncy...@gmail.com wrote:
 Erlang I assume is a computer programming language?

http://en.wikipedia.org/wiki/Erlang_(programming_language)

There's a reason Wikipedia has a search function... :)

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 09:24:36 +0200, Hendrik van Rooyen wrote:

Steven D'Aprano st...@remove-this-c...e.com.au wrote:
 
Now that I understand what the semantics of cout  Hello world are, I
don't have any problem with it either. It is a bit weird, Hello world
 cout would probably be better, but it's hardly the strangest design
 in
any programming language, and it's probably influenced by input
redirection using  in various shells.
 
 I find it strange that you would prefer:
 
 Hello world  cout
 over:
 cout  Hello world
 
 The latter seems to me to be more in line with normal assignment: - Take
 what is on the right and make the left the same. 

I don't like normal assignment. After nearly four decades of mathematics 
and programming, I'm used to it, but I don't think it is especially good. 
It confuses beginners to programming: they get one set of behaviour 
drilled into them in maths class, and then in programming class we use 
the same notation for something which is almost, but not quite, the same. 
Consider the difference between:

y = 3 + x
x = z

as a pair of mathematics expressions versus as a pair of assignments. 
What conclusion can you draw about y and z?

Even though it looks funny due to unfamiliarity, I'd love to see the 
results of a teaching language that used notation like:

3 + x - y
len(alist) - n
Widget(1, 2, 3).magic - obj
etc.

for assignment. My prediction is that it would be easier to learn, and 
just as good for experienced coders. The only downside (apart from 
unfamiliarity) is that it would be a little bit harder to find the 
definition of a variable by visually skimming lines of code: your eyes 
have to zig-zag back and forth to find the end of the line, instead of 
running straight down the left margin looking for myvar =  But it 
should be easy enough to search for - myvar.


 I suppose it is because
 we read from left to right that the first one seems better to you.

Probably.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 08:30:54 +0200, Emmanuel Surleau wrote:

[...]
 I will also observe that if you were to stop programming whatever
 language you are more familiar with in Python, and start programming
 Python in Python, you'll have an easier time of it.
 
 I don't see what's particularly un-Pythonic with this code. Not using
 xrange() is a mistake, certainly, but it remains clear, easily
 understandable code which correctly demonstrates the naive algorithm for
 detecting whether n is a prime. It doesn't call for condescension

It's a particular unfair criticism because the critic (Ethan Furman) 
appears to have made a knee-jerk reaction. The some language in Python 
behaviour he's reacting to is the common idiom:

for i in range(len(seq)):
do_something_with(seq[i])


instead of the Python in Python idiom:

for obj in seq:
do_something_with(obj)


That's a reasonable criticism, but *not in the case*. Ethan appears to 
have made the knee-jerk reaction for i in range() is Bad without 
stopping to think about what this specific piece of code is actually 
doing.

(Replace 'obj' with 'j', 'seq' with 'range(2, n)', and 
'do_something_with' with 'if (n % j) == 0: return False', and you have 
the exact same code pattern.)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Douglas Alan
On Aug 16, 4:22 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:

 I don't like normal assignment. After nearly four decades of mathematics
 and programming, I'm used to it, but I don't think it is especially good.
 It confuses beginners to programming: they get one set of behaviour
 drilled into them in maths class, and then in programming class we use
 the same notation for something which is almost, but not quite, the same.
 Consider the difference between:

 y = 3 + x
 x = z

 as a pair of mathematics expressions versus as a pair of assignments.
 What conclusion can you draw about y and z?

Yeah, the syntax most commonly used for assignment today sucks. In the
past, it was common to see languages with syntaxes like

   y - y + 1

or

   y := y + 1

or

   let y = y + 1

But these languages have mostly fallen out of favor. The popular
statistical programming language R still uses the

   y - y + 1

syntax, though.

Personally, my favorite is Lisp, which looks like

   (set! y (+ y 1))

or

   (let ((x 3)
 (y 4))
 (foo x y))

I like to be able to read everything from left to right, and Lisp does
that more than any other programming language.

I would definitely not like a language that obscures assignment by
moving it over to the right side of lines.

|ouglas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Erik Max Francis

Steven D'Aprano wrote:
I don't like normal assignment. After nearly four decades of mathematics 
and programming, I'm used to it, but I don't think it is especially good. 
It confuses beginners to programming: they get one set of behaviour 
drilled into them in maths class, and then in programming class we use 
the same notation for something which is almost, but not quite, the same. 
Consider the difference between:


y = 3 + x
x = z

as a pair of mathematics expressions versus as a pair of assignments. 
What conclusion can you draw about y and z?


What you're saying is true, but it's still a matter of terminology.  The 
symbol = means different things in different contexts, and mathematics 
and programming are very different ones indeed.  The problem is 
compounded with early languages which lazily confused the two in 
different context, such as (but not exclusive to) BASIC using = for both 
assignment and equality testing in what are in esssence totally 
unrelated contexts.


Even though it looks funny due to unfamiliarity, I'd love to see the 
results of a teaching language that used notation like:


3 + x - y
len(alist) - n
Widget(1, 2, 3).magic - obj
etc.

for assignment. My prediction is that it would be easier to learn, and 
just as good for experienced coders.


This really isn't new at all.  Reverse the arrow and the relationship to 
get::


y - x + 3

(and use a real arrow rather than ASCII) and that's assignment in APL 
and a common representation in pseudocode ever since.  Change it to := 
and that's what Pascal used, as well as quite a few mathematical papers 
dealing with iterative computations, I might add.


Once you get past the point of realizing that you really need to make a 
distinction between assignment and equality testing, then it's just a 
matter of choosing two different operators for the job.  Whether it's 
-/= or :=/= or =/== or -/= (with reversed behavior for assignment) is 
really academic and a matter of taste at that point.


Given the history of programming languages, it doesn't really look like 
the to-be-assigned variable being at the end of expression is going to 
get much play, since not a single major one I'm familiar with does it 
that way, and a lot of them have come up with the same convention 
independently and haven't seen a need to change.


--
Erik Max Francis  m...@alcyone.com  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM/Y!M/Skype erikmaxfrancis
  Get there first with the most men.
   -- Gen. Nathan Bedford Forrest, 1821-1877
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Erik Max Francis

Douglas Alan wrote:

Personally, my favorite is Lisp, which looks like

   (set! y (+ y 1))


For varying values of Lisp.  `set!` is Scheme.

--
Erik Max Francis  m...@alcyone.com  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM/Y!M/Skype erikmaxfrancis
  Get there first with the most men.
   -- Gen. Nathan Bedford Forrest, 1821-1877
--
http://mail.python.org/mailman/listinfo/python-list


Re: ignored test cases in unittest

2009-08-16 Thread Duncan Booth
Ben Finney ben+pyt...@benfinney.id.au wrote:

 Terry terry.yin...@gmail.com writes:
 
 It seemed the to me that python unittest module does not support the
 counting of ignored test cases directly. Is there any ready solution
 for this?
 
 One solution I've seen involves:
 
 * a custom exception class, ‘TestSkipped’
 
 * raising that exception at the top of test cases you want to
   temporarily skip
 
 * a custom ‘TestResult’ class that knows about a “skipped” result
 
 * a custom reporter class that knows how you want to report that result
 

I'd add to that a decorator so you can quickly mark a test case as ignored 
without editing the test itself. Also you could include a reason why it is 
ignored:

 @ignore(This test takes too long to run)
 def test_foo(self):
...

That also means you can redefine the decorator easily if you want to try 
running all the ignored tests.

Another decorator useful here is one that asserts that the test will fail. 
If the test passes then maybe someone fixed whatever was making it fail and 
if so you want to consider re-enabling it.

 @fails(Needs the frobnozz module to be updated)
 def test_whotsit(self):
...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Douglas Alan
On Aug 16, 4:48 am, Erik Max Francis m...@alcyone.com wrote:
 Douglas Alan wrote:
  Personally, my favorite is Lisp, which looks like

     (set! y (+ y 1))

 For varying values of Lisp.  `set!` is Scheme.

Yes, I'm well aware!

There are probably as many different dialects of Lisp as all other
programming languages put together.

|ouglas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Xavier Ho
On Sun, Aug 16, 2009 at 7:38 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:


 Blast, I posted the wrong flaming link, sorry everybody.


No, don't be sorry. I found your link very informative, and while it's a
little mixed, it could be useful.

I'm really looking for a way to set up Python classes' natural ordering for
sorting purposes. For example, every object of a class could own an
attribute called 'Value'. If I could get Python to sort() a list of classes
with that value, would the key=value parameter work, and is there a better
way than to redefine 3 or more of the rich comparison special methods, etc.

Also, I noticed heapq (the priority queue/heap queue module) doesn't use the
natural sorting order like sorted() does. Just saying. Could someone give it
a try?

Regards,

Ching-Yun Xavier Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a string into substrings of equal size

2009-08-16 Thread Gregor Lingl

Mark Tolonen schrieb:


Gregor Lingl gregor.li...@aon.at wrote in message 
news:4a87036a$0$2292$91cee...@newsreader02.highway.telekom.at...

Emile van Sebille schrieb:

On 8/14/2009 5:22 PM candide said...

...

What is the pythonic way to do this ?


I like list comps...

  jj = '1234567890123456789'
  ,.join([jj[ii:ii+3] for ii in range(0,len(jj),3)])
'123,456,789,012,345,678,9'
 

Emile



Less beautiful but more correct:

 ,.join([jj[max(ii-3,0):ii] for ii in
 range(len(jj)%3,len(jj)+3,3)])
'1,234,567,890,123,456,789'

Gregor


Is it?


jj = '234567890123456789'
,.join([jj[max(ii-3,0):ii] for ii in range(len(jj)%3,len(jj)+3,3)])

',234,567,890,123,456,789'


Schluck!

Even more ugly:

 ,.join([jj[max(ii-3,0):ii] for ii in
   range(len(jj)%3,len(jj)+3,3)]).strip(,)
'234,567,890,123,456,789'

Gregor



At least one other solution in this thread had the same problem.

-Mark



--
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 5:47 AM, Terryterry.yin...@gmail.com wrote:
 Hi,

 Is there a simple way (the pythonic way) to flatten a list of list?
 rather than my current solution:

 new_list=[]
 for l in list_of_list:
    new_list.extend(l)

 or,

 new_list=reduce(lambda x,y:x.extend(y), list_of_list)

#only marginally better:
from operator import add
new_list = reduce(add, list_of_list)

#from the itertools recipes:
from itertools import chain
def flatten(listOfLists):
return list(chain.from_iterable(listOfLists))

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


flatten a list of list

2009-08-16 Thread Terry
Hi,

Is there a simple way (the pythonic way) to flatten a list of list?
rather than my current solution:

new_list=[]
for l in list_of_list:
new_list.extend(l)

or,

new_list=reduce(lambda x,y:x.extend(y), list_of_list)

br, Terry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Mark Lawrence

Chris Rebert wrote:

On Sat, Aug 15, 2009 at 1:06 PM, Mark Lawrencebreamore...@yahoo.co.uk wrote:

Xavier Ho wrote:

Hey all,

I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in
the
documentation.

Is there a substitution for this special method in 3.1, or do I really
have
to define all six rich comparison methods to work it out?

If this question has already been asked somewhere, I apologise in advance.
Already googled around but I didn't find information on this.

Any replies appreciated.


http://bytes.com/topic/python/answers/844614-python-3-sorting-comparison-function


That seems to be about the built-in function cmp(). The OP was asking
about the special method __cmp__ and the related rich comparison
special methods (__eq__, __lt__, etc).

Cheers,
Chris

Blast, I posted the wrong flaming link, sorry everybody.

--
Kindest regards.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: ignored test cases in unittest

2009-08-16 Thread Terry
On Aug 16, 5:25 pm, Duncan Booth duncan.bo...@invalid.invalid wrote:
 Ben Finney ben+pyt...@benfinney.id.au wrote:
  Terry terry.yin...@gmail.com writes:

  It seemed the to me that python unittest module does not support the
  counting of ignored test cases directly. Is there any ready solution
  for this?

  One solution I've seen involves:

  * a custom exception class, ‘TestSkipped’

  * raising that exception at the top of test cases you want to
    temporarily skip

  * a custom ‘TestResult’ class that knows about a “skipped” result

  * a custom reporter class that knows how you want to report that result

 I'd add to that a decorator so you can quickly mark a test case as ignored
 without editing the test itself. Also you could include a reason why it is
 ignored:

 �...@ignore(This test takes too long to run)
  def test_foo(self):
     ...

 That also means you can redefine the decorator easily if you want to try
 running all the ignored tests.

 Another decorator useful here is one that asserts that the test will fail.
 If the test passes then maybe someone fixed whatever was making it fail and
 if so you want to consider re-enabling it.

 �...@fails(Needs the frobnozz module to be updated)
  def test_whotsit(self):
     ...

Thanks for the solutions. I think the decorator idea is what I'm look
for:-)



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Michael Fötsch

Terry wrote:

Is there a simple way (the pythonic way) to flatten a list of list?


This is probably the shortest it can get:

sum(list_of_lists, [])


Kind Regards,
M.F.
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 01:41:41 -0700, Douglas Alan wrote:

 I like to be able to read everything from left to right, and Lisp does
 that more than any other programming language.
 
 I would definitely not like a language that obscures assignment by
 moving it over to the right side of lines.

One could argue that left-assigned-from-right assignment obscures the 
most important part of the assignment, namely *what* you're assigning, in 
favour of what you're assigning *to*.

In any case, after half a century of left-from-right assignment, I think 
it's worth the experiment in a teaching language or three to try it the 
other way. The closest to this I know of is the family of languages 
derived from Apple's Hypertalk, where you do assignment with:

put somevalue into name

(Doesn't COBOL do something similar?)

Beginners found that *very* easy to understand, and it didn't seem to 
make coding harder for experienced Hypercard developers.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


wxFormBuilder finally supports wxPython

2009-08-16 Thread sturlamolden
Version 3.1 of wxFormBuilder can generate wxPython code. I have
previously used wxFormBuilder to generate XRC files for my wxPython
projects. Though still in beta, this might be even better. :-)


http://wxformbuilder.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 6:25 AM, Johannes
Janssenm...@johannes-janssen.de wrote:
 Gabriel Genellina schrieb:

 The try/except around sys._getframe(1) is because that function is not
 mandatory/available on all Python implementations (that's the case for
 jython which doesn't provide it).

 Thanks, shouldn't such information be part of the python documentation of
 sys._getframe()
 (http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?

The leading underscore kinda indirectly implies it, but yeah, it's
worth mentioning.
File a bug in the docs: http://bugs.python.org/

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Johannes Janssen

Gabriel Genellina schrieb:
The try/except around sys._getframe(1) is because that function is not 
mandatory/available on all Python implementations (that's the case for 
jython which doesn't provide it).


Thanks, shouldn't such information be part of the python documentation 
of sys._getframe() 
(http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?


Johannes
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Johannes Janssen

Chris Rebert schrieb:

On Sun, Aug 16, 2009 at 6:25 AM, Johannes
Janssenm...@johannes-janssen.de wrote:
  

Gabriel Genellina schrieb:


The try/except around sys._getframe(1) is because that function is not
mandatory/available on all Python implementations (that's the case for
jython which doesn't provide it).
  

Thanks, shouldn't such information be part of the python documentation of
sys._getframe()
(http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?



The leading underscore kinda indirectly implies it, but yeah, it's
worth mentioning.
File a bug in the docs: http://bugs.python.org/

Cheers,
Chris
  

I filed a bug: http://bugs.python.org/issue6712 .

Johannes
--
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote:

 On Sun, Aug 16, 2009 at 5:47 AM, Terryterry.yin...@gmail.com wrote:
 Hi,

 Is there a simple way (the pythonic way) to flatten a list of list?
 rather than my current solution:

 new_list=[]
 for l in list_of_list:
    new_list.extend(l)

 or,

 new_list=reduce(lambda x,y:x.extend(y), list_of_list)
 
 #only marginally better:
 from operator import add
 new_list = reduce(add, list_of_list)

Surely that's going to be O(N**2)?

I'd predict that would perform even worse than O(N**2) string 
concatenation, on account that a string of size N has to allocate space 
for N bytes, but a list of size N allocates space for N pointers (each 4 
bytes, or 8 depending on the system), rounded up to the nearest power of 
two. Also CPython can optimize string concatenation to O(N) under some 
circumstances, but not lists.


 from timeit import Timer
 setup = \\
... L = [ ([None]*5000) for x in xrange(%d) ]
... from operator import add
... 
 Timer(reduce(add, L), setup % 4).repeat(number=1000)
[0.53808808326721191, 0.51404905319213867, 0.51157188415527344]
 Timer(reduce(add, L), setup % 8).repeat(number=1000)
[2.1178171634674072, 3.8830602169036865, 4.72245192527771]
 Timer(reduce(add, L), setup % 16).repeat(number=1000)
[17.190337896347046, 21.572744131088257, 21.584265947341919]


Looks like O(N**2) behaviour to me.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 6:49 AM, Steven
D'Apranost...@remove-this-cybersource.com.au wrote:
 On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote:
 On Sun, Aug 16, 2009 at 5:47 AM, Terryterry.yin...@gmail.com wrote:
 Hi,

 Is there a simple way (the pythonic way) to flatten a list of list?
 rather than my current solution:

 new_list=[]
 for l in list_of_list:
    new_list.extend(l)

 or,

 new_list=reduce(lambda x,y:x.extend(y), list_of_list)

 #only marginally better:
 from operator import add
 new_list = reduce(add, list_of_list)

 Surely that's going to be O(N**2)?

The OP asked for simple, not best, most proper, or fastest. My
comment was intended to mean that the code was marginally *simpler*,
not faster.

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 12:03:53 +0200, Michael Fötsch wrote:

 Terry wrote:
 Is there a simple way (the pythonic way) to flatten a list of list?
 
 This is probably the shortest it can get:
 
 sum(list_of_lists, [])


That's also O(N**2).


 from timeit import Timer
 setup = L = [ ([None]*5000) for x in xrange(%d) ]
 Timer(sum(L, []), setup % 4).repeat(number=1000)
[0.6070549488067627, 0.54354715347290039, 0.54686999320983887]
 Timer(sum(L, []), setup % 8).repeat(number=1000)
[2.1285719871520996, 3.6722278594970703, 4.0785009860992432]
 Timer(sum(L, []), setup % 16).repeat(number=1000)
[18.370341062545776, 20.40509295463562, 21.871708869934082]



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port? - conclusions

2009-08-16 Thread Hendrik van Rooyen
On Sunday 16 August 2009 08:20:34 John Nagle wrote:
 Hendrik van Rooyen wrote:
  On Saturday 15 August 2009 14:40:35 Michael Ströder wrote:
  Hendrik van Rooyen wrote:
  In the past, on this  group, I have made statements that said that on
  Linux, the serial port handling somehow does not allow transmitting and
  receiving at the same time, and nobody contradicted me.

  Absolutely false.

No its true, if you open the serial port  with the standard open() instead of 
os.open().  And it is also true that I was not contradicted in the past. :-)


  Despite all the good comments here by other skilled people I'd recommend
  to determine whether the transmission line to the devices accessed
  support full duplex.

  All standard PC serial ports are full-duplex devices.

I know this, and I started the thread because they would not work full duplex 
for me.

8  pyserial baudot program  link ---

  You raise a good point, that is probably not well known amongst the
  youngsters here, as simple serial multidropping has gone out of fashion.

  Actually, no.  Dynamixel servos as used on the latest Bioloid robots
 are multidrop serial RS-485.  But outside the embedded world, nobody uses
 that stuff any more.  (Embedded is going Ethernet; it's overkill but
 works fine and is now cheap.)

Exactly - it is no longer part of mainstream fashion. We also still use RS-485 
because it is cheaper, and we have better control over timing.

I enclose two naive test implementations.
To run them you will need a loopback connector.
One can be made by shorting pin 2 and 3 together on the standard DB9.

The results are illuminating - on my development machine, (dual 64 bit, some 
gigs), the last lines look like this:

test.py:
The quick brown fox jumps over the lazy dog 1023
That took 3.91238284111 seconds - 3.82068636827 Millisecs per record, 
261.733077152 recs per sec
h...@linuxbox:~/dos/JOBS/sms/lib
   
test1.py:
The quick brown fox jumps over the lazy dog 1023
That took 3.90402388573 seconds - 3.81252332591 Millisecs per record, 
262.293477185 recs per sec
h...@linuxbox:~/dos/JOBS/sms/lib   

Almost no difference between the two implementations.
This is basically because there is enough processing power to keep the link 
running at full speed in both instances.

On the eBox, (486, 400MHz, 128Mb, no FP), the difference is startling:

test.py:
The quick brown fox jumps over the lazy dog 1023
That took 69.2863521576 seconds - 67.6624532789 Millisecs per record, 
14.7792453797 recs per sec
r...@ebox:/home/user/sms/lib# 

About eighteen times slower than the development machine.

test1.py:
The quick brown fox jumps over the lazy dog 1023
That took 10.391780138 seconds - 10.148222791 Millisecs per record, 
98.5394211964 recs per sec
r...@ebox:/home/user/sms/lib# 

Less than three times slower than the development machine.

The little processor + Slackware + python cannot keep the link busy.
Python, as a character handler, well let us say that it is suboptimal,
because saying that something sucks, sucks.
An almost seven times ratio between the implementations is not to be sneezed 
at.

So the conclusions I have come to are the following:

1)  Thou shalt not use ordinary python files for serial ports, on pain of 
death.
2)  Thou shalt strive mightily to minimize python calls, doing all in thy   
 
power to move away from character input to string input.
3)  Thou shalt expend real treasure for real processing power as there is no 
such thing as a free lunch.

I would like to thank everybody who took the trouble to respond to teach me 
the error of my ways.

- Hendrik



test1.py
Description: application/python


test.py
Description: application/python
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem Regarding Handling of Unicode string

2009-08-16 Thread joy99
On Aug 11, 1:17 pm, John Machin sjmac...@lexicon.net wrote:
 On Aug 10, 9:26 pm, joy99 subhakolkata1...@gmail.com wrote:

  Dear Group,

  I am using Python26 on WindowsXP with service pack2. My GUI is IDLE.
  I am using Hindi resources and get nice output like:
  एक
  where I can use all the re functions and other functions without doing
  any transliteration,etc.
  I was trying to use Bengali but it is giving me output like:

 WHAT is giving you this output?

  '\xef\xbb\xbf\xe0\xa6\x85\xe0\xa6\xa8\xe0\xa7\x87\xe0\xa6\x95'

 In a very ordinary IDLE session (Win XP SP3, Python 2.6.2, locale:
 Australia/English, no Hindi resources):

  x = '\xef\xbb\xbf\xe0\xa6\x85\xe0\xa6\xa8\xe0\xa7\x87\xe0\xa6\x95'
  ux = x.decode('utf-8')
  ux

 u'\ufeff\u0985\u09a8\u09c7\u0995' print ux

 অনেক # looks like what you wanted; please confirm import unicodedata
  for c in ux:

         print unicodedata.name(c)

 ZERO WIDTH NO-BREAK SPACE # this is a BOM
 BENGALI LETTER A
 BENGALI LETTER NA
 BENGALI VOWEL SIGN E
 BENGALI LETTER KA



  I wanted to see Bengali output as
  অনেক
  and I like to use all functions including re.
  If any one can help me on that.

 I am using Hindi resources doesn't tell us much ... except to prompt
 the comment that perhaps if you want to display Bengali script, you
 may need Bengali resources. However it looks like I can display your
 Bengali data without any special resources.

 It seems like you are not doing the same with Bengali as you are doing
 with Hindi. We can't help you very much if you don't show exactly what
 you are doing.

 Have you considered asking in an Indian Python forum? Note: you will
 still need to say what you are doing that works with Hindi but not
 with Bengali.

 Cheers,
 John

Dear Group,
I have already worked out my solution but everyone of yours' answers
helped me to see different solutions from different angles. Thank you
for the same. I am building some social network program in Bengali. I
just gave the transliteration problem which was giving me problem, I
thought as you are very expert pythoners so it would be minutes'
matter. By your answers I saw I was not wrong. But as I solved the
problem so I checked it bit late. Sorry for the same.
Best Regards,
Subhabrata.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Terry
On Aug 16, 6:59 pm, Chris Rebert c...@rebertia.com wrote:
 On Sun, Aug 16, 2009 at 6:49 AM, Steven





 D'Apranost...@remove-this-cybersource.com.au wrote:
  On Sun, 16 Aug 2009 05:55:48 -0400, Chris Rebert wrote:
  On Sun, Aug 16, 2009 at 5:47 AM, Terryterry.yin...@gmail.com wrote:
  Hi,

  Is there a simple way (the pythonic way) to flatten a list of list?
  rather than my current solution:

  new_list=[]
  for l in list_of_list:
     new_list.extend(l)

  or,

  new_list=reduce(lambda x,y:x.extend(y), list_of_list)

  #only marginally better:
  from operator import add
  new_list = reduce(add, list_of_list)

  Surely that's going to be O(N**2)?

 The OP asked for simple, not best, most proper, or fastest. My
 comment was intended to mean that the code was marginally *simpler*,
 not faster.

 Cheers,
 Chris
 --http://blog.rebertia.com

Well, if possible, I'd like not only to know a simple solution, but
also the 'best', the 'most proper' and the 'fastest':-)

If they are not the same.

br, Terry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 02:47:42 -0700, Terry wrote:

 Hi,
 
 Is there a simple way (the pythonic way) to flatten a list of list?
 rather than my current solution:
 
 new_list=[]
 for l in list_of_list:
 new_list.extend(l)

I don't think that scales terribly well. In my testing, it performs about 
as badly as the O(N**2) behaviours others have suggested (using sum or 
reduce with add) -- perhaps a bit worse for small N, but not quite as 
badly for large N.


 new_list=reduce(lambda x,y:x.extend(y), list_of_list)

That doesn't even work.

 list_of_list = [ [1,2,3], [2, 4, 8] ]
 new_list=reduce(lambda x,y:x.extend(y), list_of_list)
 new_list is None
True



Chris' suggestion using itertools seems pretty good:

 from timeit import Timer
 setup = \\
... L = [ [None]*5000 for _ in xrange(%d) ]
... from itertools import chain
... 
 Timer(list(chain.from_iterable(L)), setup % 4).repeat(number=1000)
[0.61839914321899414, 0.61799716949462891, 0.62065696716308594]
 Timer(list(chain.from_iterable(L)), setup % 8).repeat(number=1000)
[1.2618398666381836, 1.3385050296783447, 3.9113419055938721]
 Timer(list(chain.from_iterable(L)), setup % 16).repeat(number=1000)
[3.1349358558654785, 4.8554730415344238, 5.431217987061]


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Steven D'Aprano
On Sun, 16 Aug 2009 06:59:52 -0400, Chris Rebert wrote:

 Surely that's going to be O(N**2)?
 
 The OP asked for simple, not best, most proper, or fastest. My
 comment was intended to mean that the code was marginally *simpler*, not
 faster.

Fair enough, but he also asked for Pythonic, and while some people might 
argue that terrible performance is Pythonic, I hope you wouldn't be one 
of them! :)

If it soothes your ruffled sense of honour *wink*, I think your solution 
with itertools.chain is probably the best so far.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Hendrik van Rooyen
On Sunday 16 August 2009 12:18:11 Steven D'Aprano wrote:

 In any case, after half a century of left-from-right assignment, I think
 it's worth the experiment in a teaching language or three to try it the
 other way. The closest to this I know of is the family of languages
 derived from Apple's Hypertalk, where you do assignment with:

 put somevalue into name

 (Doesn't COBOL do something similar?)

Yup.

move banana to pineapple.

move accountnum in inrec to accountnum in outrec.

move corresponding inrec to outrec.

It should all be upper case of course...

I cannot quite recall, but I have the feeling that in the second  form, of 
was also allowed instead of in, but it has been a while now so I am 
probably wrong.

The move was powerful - it would do conversions for you based on the types of 
the operands - it all just worked.

- Hendrik
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Terry Reedy

Xavier Ho wrote:

I'm really looking for a way to set up Python classes' natural ordering 
for sorting purposes.


I believe __lt__ () is the only method (operator) used by both .sort 
and heap module.


--
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 7:31 AM, Steven
D'Apranost...@remove-this-cybersource.com.au wrote:
 On Sun, 16 Aug 2009 06:59:52 -0400, Chris Rebert wrote:
 Surely that's going to be O(N**2)?

 The OP asked for simple, not best, most proper, or fastest. My
 comment was intended to mean that the code was marginally *simpler*, not
 faster.

 Fair enough, but he also asked for Pythonic, and while some people might
 argue that terrible performance is Pythonic, I hope you wouldn't be one
 of them! :)

Indeed not. :) I expected it would be worse performance-wise than the
OP's original due to all the intermediate lists that get produced; it
shouldn't be used in optimized production code.

 If it soothes your ruffled sense of honour *wink*, I think your solution
 with itertools.chain is probably the best so far.

Except it's not really my solution, it's whoever put it in the
itertools docs's. :(
But I'm glad to been able to help by pointing the recipe out. :-)

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Mark Lawrence

Chris Rebert wrote:

On Sat, Aug 15, 2009 at 8:09 AM, Xavier Hocont...@xavierho.com wrote:

Hey all,

I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in the
documentation.

Is there a substitution for this special method in 3.1, or do I really have
to define all six rich comparison methods to work it out?


I don't think so. Quoting
http://docs.python.org/dev/py3k/reference/datamodel.html

There are no swapped-argument versions of [the comparison] methods
(to be used when the left argument does not support the operation but
the right argument does); rather, __lt__() and __gt__() are each
other’s reflection, __le__() and __ge__() are each other’s reflection,
and __eq__() and __ne__() are their own reflection.

I believe this means you only need to define one method from each of
the following pairs to get all the operators working:
* __eq__ or __ne__
* __lt__ or __gt__
* __ge__ or __le__

Cheers,
Chris

Unfortunately I don't think it's that easy, see.
http://mail.python.org/pipermail/python-list/2008-November/688761.html
The issue referenced is still open.  This of course assumes that I've 
posted the correct link this time!


--
Kindest regards.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Xavier Ho
On Sun, Aug 16, 2009 at 9:49 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:


 Unfortunately I don't think it's that easy, see.
 http://mail.python.org/pipermail/python-list/2008-November/688761.html
 The issue referenced is still open.  This of course assumes that I've
 posted the correct link this time!


I'm not sure what you're referring to here. The link brought me to about
__ne__ being automatically determined when __eq__ is defined. Although it is
a rich comparison special method, it doesn't entirely cover the usefulness
of __cmp__().

Terry: I'll give that a test tomorrow and see what I can come up with.
Thanks for the quick info.

Regards,

Ching-Yun Xavier Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


platform-specific overrides of functions and class methods (expanding on imputils demo code)

2009-08-16 Thread lkcl
i've just had to put something together for pyjamas-desktop which may
prove to be useful to other people, so i'm pointing people in its
general direction, for archive purposes.

the purpose behind the platform override system is to allow
implementations of a common API, in python, to share the majority of
their codebase (modules, classes, functions etc.) _but_, for _very_
specific platform implementation purposes, allow for overrides.

example:

test.py
def test():
   print hello

platform/testOverridingPlatformName.py:
def test():
   print this is not kansas any more

code is here:
http://pyjamas.svn.sourceforge.net/viewvc/pyjamas/trunk/pyjd/

modules to pay attention to: importers.py (a modified version of Demos/
imputils/importers.py) and modcompile.py.  setup/init function is very
simple - see __init__py.in last few lines.

on reading modcompile.py you may be forgiven for going wtf but
basically PlatformParser loads the python source; turns it into an
AST; then also the platform-specific version is loaded and turned
into an AST; then, the two ASTs are handed to the merge function
which does a top-level replace of functions and a top-level
replace of class methods.

_theen_ the resultant merged AST is handed to a module which was
derived from compiler/pycodegen.py - none of the code in there can
cope with being handed an already-compiled AST so it was necessary to
make a class that did.  looking around on the internet i find quite a
few people asking about how to do this, so ... take a look at
modcompile.Module, and how it's used.  very simple.

i've disabled saving and detection of .pyc files for now, because the
job of loading code from .pyc should really be done by PlatformParser
(which understands the concept of... duh, a platform).   the _last_
thing that you want to happen is to run one platform's code, generate
some .pyc files, then change a config file to run a different back-end
platform, for example (which is possible with pyjamas-desktop) and end
up running the _wrong_ platform-specific code.

for those people wondering, why in god's green earth would anyone
want to _do_ such a thing??? it's quite simple: the alternative is a
complete dog's dinner, in pyjamas:

def do_some_DOM_manipulation(document):
# hre we go...
if platform == 'hulahop' # this is for XULrunner
   do_some_stuff()
elif platform == 'pywebkitgtk':
   do_something_different()
elif platform == 'mshtml' # for pywin32 and comtypes
   do_something_dreadful()
else:
   do_the_default_here()

now imagine that across an API with ... four hundred functions.

i just... couldn't bring myself to do that.  not when the pyjs (python-
to-javascript) compiler _already_ solved this problem (thanks to james
tauber) by deploying the AST merge concept.

all i did was merge that with the imputils demo code so that, rather
than at compile-time the pyjs compiler goes and generates five
platform-specific versions of the same application, pyjamas-desktop at
_run_ time can do exactly the same thing.

but - it's a generic enough idea to be of value elsewhere - for
example, all the lvely code in e.g. the setup distutils?  all
those looovely if os.platform == 'win32', and if sys.this = 'posix'
 could be replaced with platform-specific overrides that got merged
at run-time, thus _dramatically_ simplifying the look and the
useability of the code.

enjoy.

l.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Bearophile
Chris Rebert:
 The OP asked for simple, not best, most proper, or fastest. My
 comment was intended to mean that the code was marginally *simpler*,
 not faster.

Yep, the OP has asked for simple code. But often this is not the right
way to solve this situation. A better way is to create (or copy) a
flatten that's efficient and well tested  debugged, put it into some
library of utilities, and then use importuse it when it's needed.

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Mark Lawrence

Xavier Ho wrote:

On Sun, Aug 16, 2009 at 9:49 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:


Unfortunately I don't think it's that easy, see.
http://mail.python.org/pipermail/python-list/2008-November/688761.html
The issue referenced is still open.  This of course assumes that I've
posted the correct link this time!



I'm not sure what you're referring to here. The link brought me to about
__ne__ being automatically determined when __eq__ is defined. Although it is
a rich comparison special method, it doesn't entirely cover the usefulness
of __cmp__().

Terry: I'll give that a test tomorrow and see what I can come up with.
Thanks for the quick info.

Regards,

Ching-Yun Xavier Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/


I wasn't discussing __cmp__, I was referring to the quote by Chris 
Rebert from the Python docs regarding the rich comparison methods, a 
discrepancy between the documentation and the implementation as noted in 
the link that I gave, and an indication that the issue raised on this 
discrepancy is still open.  As Terry Reedy has mentioned __lt__ 
elsewhere, you should be aware of the discrepancy, otherwise you could 
go charging off down the wrong track.  FWIW it was Terry who raised the 
issue 4395, I'm sure that he could explain the ramifications of it all 
far better than I ever could, so I'll sign out.


--
Kindest regards.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread MRAB

Douglas Alan wrote:
[snip]

C++ also allows for reading from stdin like so:

   cin  myVar;

I think the direction of the arrows probably derives from languages
like APL, which had notation something like so:

 myVar - 3
 [] - myVar

- was really a little arrow symbol (APL didn't use ascii), and the
first line above would assign the value 3 to myVar. In the second
line, the [] was really a little box symbol and represented the
terminal.  Assigning to the box would cause the output to be printed
on the terminal, so the above would output 3.  If you did this:

 [] - myVar

It would read a value into myVar from the terminal.

APL predates Unix by quite a few years.


No, APL is strictly right-to-left.

- x

means goto x.

Writing to the console is:

[] - myVar

Reading from the console is:

myVar - []
--
http://mail.python.org/mailman/listinfo/python-list


Re: unittest

2009-08-16 Thread Mag Gam
John:

Well, this is actually a script which wraps around another application. :-)
My goal is when I introduce a new feature I don't want to break old
stuff so instead of me testing manually I want to build a framework of
tests.



On Sat, Aug 15, 2009 at 11:37 PM, John Haggertybouncy...@gmail.com wrote:
 This is an interesting question. I am just wondering: do you really have
 that many features that it would be impossible to just have a shell script
 run specific types of input or tests?

 When I did programming in the past for education they just had lists of
 input data and we ran the program against the test data.

 I just get slightly confused when test suites start to have to apply?

 On Fri, Aug 14, 2009 at 9:28 PM, Mag Gam magaw...@gmail.com wrote:

 I am writing an application which has many command line arguments.
 For example: foo.py -args bar bee

 I would like to create a test suit using unittest so when I add
 features to foo.py I don't want to break other things. I just heard
 about unittest and would love to use it for this type of thing.

 so my question is, when I do these tests do I have to code them into
 foo.py? I prefer having a footest.py which will run the regression
 tests. Any thoughts about this?

 TIA
 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: my recursive function call is wrong?

2009-08-16 Thread Kev Dwyer
On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote:


Hello,

You have placed recursive calls to the function in a number of different 
locations; when len(macro) becomes zero control will return to the 
calling function, but this calling function may have more code to 
execute, including further calls to start_parse(), and further attempts 
to index macro.

I like to keep recursive calls at the end of a function, so that there is 
a clean path back to the top level caller once the terminal condition is 
reached.  You can do it differently, but you need to bear in mind the 
execution paths through your code.

Cheers,

Kev

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Emmanuel Surleau
 It's a particular unfair criticism because the critic (Ethan Furman)
 appears to have made a knee-jerk reaction. The some language in Python
 behaviour he's reacting to is the common idiom:

 for i in range(len(seq)):
 do_something_with(seq[i])


 instead of the Python in Python idiom:

 for obj in seq:
 do_something_with(obj)


 That's a reasonable criticism, but *not in the case*. Ethan appears to
 have made the knee-jerk reaction for i in range() is Bad without
 stopping to think about what this specific piece of code is actually
 doing.

 (Replace 'obj' with 'j', 'seq' with 'range(2, n)', and
 'do_something_with' with 'if (n % j) == 0: return False', and you have
 the exact same code pattern.)

Fair enough. But as far as I know, for i in (x)range(num) is the canonical way 
to iterate over numbers in Python.

Another case of lack of RTFM* before answering, I suppose.

Cheers,

Emm

*Read The Fine Mail
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-16 Thread Jean-Michel Pichavant

Scott David Daniels wrote:

Jean-Michel Pichavant wrote:

Steven D'Aprano wrote:

On Fri, 14 Aug 2009 18:49:26 +0200, Jean-Michel Pichavant wrote:

 

Sorry guys (means guys *and* gals :op ), I realized I've not been able
to describe precisely what I want to do. I'd like the base class to be
virtual (aka abstract). However it may be abstract but it does not 
mean

it cannot do some usefull stuff.


Here is the schema of my abstract methods :

class Interface(object):
def method(self):
# -
# some common stuff executed here
# -
print 'hello world'
# -
# here shall stand child specific stuff (empty in the 
interface

method)
# -
if self.__class__.method == Interface.method:
raise NotImplementedError('You should have read the 
f**

manual ! You must override this method.')




Okay, so I want to sub-class your Interface class. As you said, the 
methods in the abstract class are still useful, so in my class, I 
don't need any extra functionality for some methods -- I'm happy 
with just the common stuff. So I use normal OO techniques and 
over-ride just the methods I need to over-ride:


  
Sometimes the base is doing cool stuff but incomplete stuff which 
requires knowledge only hold by the sub class. In my case the 
interface is a high level interface for a software that can run on 
multiple hardware platforms. Only the sub class has knowledge on how 
to operate the hardware, but no matter the hardware it still produces 
the same effect.


Let's say I have 50 different hardwares, I'll have 50 sub classes of 
Interface with the 'start' method to define. It wouldn't be 
appropriate (OO programming)to write 50 times '_log.debug('Starting 
%s' % self)' in each child start method when the simple task of 
logging the call can be nicely handled by the base class.


In the meantime, I must make sure  the user, who is not a python guru 
in this case, has implemented the start method for his hardware, 
because only him knows how to effectively start this hardware. I 
don't want him to come to me saying, I got no error, still my 
hardware does not start. You can then blame him for not reading the 
docs, but it will still be less expensive to throw a nice exception 
with an accurate feedback.


[snip]

class VerboseGoodChild(Interface):
# forced to over-ride methods for no good reason
  


Definitely no !! This is the purpose of an interface class: to force 
people to write these methods. They *are* required, if they were not, 
they would not belong to the Interface.


JM


But there _is_ one moment when you can check those things, then avoid
checking thereafter: object creation.  So you can complicate your
__init__ (or __new__) with those checks that make sure you instantiate
only fully defined subclasses:

# obviously not tested except in concept:

class Base(object_or_whatever):
 def __init__(self, ...):
 class_ = self.__class__
 if class_ is Base:
 raise TypeError('Attempt to instantiate Base class')
 for name in 'one two three four':
 if getattr(Base, name) is not getattr(Base, name):
 raise NotImplementedError(
 '%s implementation missing' % name)
 ...

--Scott David Daniels
scott.dani...@acm.org
That could do the trick, sparing me from writing additional code in each 
methods. Thanks.


JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to use python to get True Full Duplex on a Serial port?

2009-08-16 Thread Grant Edwards
On 2009-08-15, Hendrik van Rooyen hend...@microcorp.co.za wrote:
 On Saturday 15 August 2009 16:25:03 Grant Edwards wrote:

 Are you using python file operations open/read/write or OS
 file-descriptor operations os.open/os.read/os.write?

 The former - that seems to be the source of my trouble.

 I have now written a little test that uses serial.Serial and
 it works a treat.

Good to hear.

 I am still confused about pyserial and serial - I found serial
 in my distribution library, (on the SuSe machine, not on the
 2.5 in Slackware) but I had to download pyserial.

That's very interesting.  Is the pre-existing serial a
version of pyserial that the packager had pre-installed or is
it something else?  I didn't know any distributions shipped
Python with pyserial installed.  In either case, serial isn't
something that ships with the standard Python library.

 I see that you were the the original author.  Thank you for
 letting this stuff loose in the wild.

My pleasure.

-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-16 Thread Christian Heimes

Jean-Michel Pichavant wrote:

scott.dani...@acm.org
That could do the trick, sparing me from writing additional code in each 
methods. Thanks.


Why are you trying to reinvent the wheel? Python's abc module already 
takes care of these details.


Christian
--
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-16 Thread Jean-Michel Pichavant

Christian Heimes wrote:

Jean-Michel Pichavant wrote:

talking about approaches:

1/
class Interface:
def foo(self):
   if self.__class__.foo == Interface.foo:
  raise NotImplementedError

2/
class Interface:
def foo(self):
   self._foo()

def _foo(sef):
   raise NotImplementedError


Please don't call it an interface when it's really an abstract base 
class. And abstract base classes are probably the solution the OP is 
looking for, http://docs.python.org/library/abc.html


Christian

Sadly I'm working with python 2.4. Anyway it's good to hear they've 
added ABC support to python, didn't know that. I didn't know there was a 
difference between interface and abstract classes as well. With a little 
bit of googling, I found out that actually Interface classes do not 
provide any implementation.

What I'm looking for is definitely an abstract class.

JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: callable virtual method

2009-08-16 Thread jean-michel Pichavant

Christian Heimes a écrit :

Jean-Michel Pichavant wrote:

scott.dani...@acm.org
That could do the trick, sparing me from writing additional code in 
each methods. Thanks.


Why are you trying to reinvent the wheel? Python's abc module already 
takes care of these details.


Christian
I'm working with python 2.4 and don't plan to change. The abc module 
would have been exactly what I was looking for. I'll keep it mind if I 
ever upgrade to python 2.6+


JM
--
http://mail.python.org/mailman/listinfo/python-list


Re: Surpressing Warnings

2009-08-16 Thread Victor Subervi
This is strange because I actually had if exists in my code:

sqlKWDrop = 'DROP TABLE IF EXISTS ' + kwTable + ';'

where kwTable, in the instance cited below, becomes judaism_128. What
gives?
Victor

On Sun, Aug 9, 2009 at 7:57 PM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote:

 On Sun, 9 Aug 2009 15:21:43 -0500, Victor Subervi
 victorsube...@gmail.com declaimed the following in
 gmane.comp.python.general:

  Hi:
  I get a ton of warnings like this from a program I run:
 
  Warning (from warnings module):
File C:\Python25\read.py, line 67
  cursor.execute(sqlKWDrop)
  Warning: Unknown table 'judaism_128'
 
 Personally -- I'd suggest fixing the database so that the table
 does
 exist...

Ah, but then -- it appears from the name, that the purpose of this
 SQL statement is to delete the table... In which case use a database
 that supports (for example, current versions of MySQL, SQLite, and
 PostgreSQL) drop table IF EXISTS tbl_name which suppress the
 warning/error message and update the SQL as appropriate.

Appears that M$ SQL Server does NOT implement that syntax... Nor
 could I find it for Firebird.

If the application is specific for one database engine, it might be
 worth it to find out what features permit discovering if a table exists
 and bypassing the drop if it doesn't...


 --
Wulfraed Dennis Lee Bieber   KD6MOG
wlfr...@ix.netcom.com   HTTP://wlfraed.home.netcom.com/

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Benjamin Kaplan
On Sun, Aug 16, 2009 at 2:30 AM, Emmanuel Surleau
emmanuel.surl...@gmail.com wrote:

 I don't see what's particularly un-Pythonic with this code. Not using xrange()
 is a mistake, certainly, but it remains clear, easily understandable code
 which correctly demonstrates the naive algorithm for detecting whether n is a
 prime. It doesn't call for condescension

It's not that the code is bad, but too many people coming from Java
and C keep thinking of for loops like they're using Java or C and
therefore that for i in range(a,b) is identical to for(int i = a; i
 b; i++). It's not and, for the most part, you shouldn't code like
that. Since you're using numbers, range/xrange is the appropriate
idiom but you still have to remember that a for loop in python doesn't
loop until a condition is met, it loops through an iterator until the
interator says it's done.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax notations.

2009-08-16 Thread Peter Keller
In comp.lang.scheme Xah Lee xah...@gmail.com wrote:
 Xah's Edu Corner: The importance of syntax  notations.
 
 http://www.stephenwolfram.com/publications/recent/mathml/mathml_abstract.html
 
 this article should teach the coding sophomorons and computer
 ?science? idiotic authors who harbor the notion that syntax is not
 important, picked up by all the elite i-reddit  twittering   hacker
 news am-hip dunces.

I must have really tweaked you with my Syntax is not important, ideas are.
statement.

I read Wolfram's article carefully. He applies an intuitive sense onto
why he does or doesn't like a particular notation, but yet can't really
elucidate his feelings. I'm surprised that didn't tweak you worse. He
also goes so far as to mention that:

But actually we still don't know a clean simple way to represent things
like geometrical diagrams in a kind of language-like notation. And
my guess is that actually of all the math-like stuff out there, only
a comparatively small fraction can actually be represented well with
language-like notation.

It is simply that the method by which the right brain categorizes
and processes visual information is not observable by the left
brain. Therefore no language can EVER be constructed by the left brain to
represent why the right brain prefers some visual layouts for languages
over others.  I've done enough classical art training in my life to
understand the conflict between the powerful spatial/visual processor
the right brain has and the (in the context of drawing) meaningless
linguistics of trying to describe the process.

Only when we as human beings build the observation channels needed (with
physical connection between certain areas of the left and right sides of
the brain) will any sort of meaningful left brain language be created
for the visual understanding/gradation of the spatial relationship and
the method by which our right brain performs its processing.

If you want to design a better computer language, hire an artist.

Most semantic objects in programs stand in some spatio-temporal
relation to each other. If you deny that fact, then simply look at the
directed acyclic form/SSA form/CPS transform of any of your favorite
languages. Compiler go through *great* pains to transform left brain
scribblings into large spatio-temporal 2d images where lots of algorithms
are done before converting them into assembly. This is because it is simply
easier to visually understand how to do the processing of those elements
than not.

Thank you.

-pete
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
Dear Kev

Thank you very much.
I got it.:)

2009/8/16 Kev Dwyer kevin.p.dw...@gmail.com

 On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote:


 Hello,

 You have placed recursive calls to the function in a number of different
 locations; when len(macro) becomes zero control will return to the
 calling function, but this calling function may have more code to
 execute, including further calls to start_parse(), and further attempts
 to index macro.

 I like to keep recursive calls at the end of a function, so that there is
 a clean path back to the top level caller once the terminal condition is
 reached.  You can do it differently, but you need to bear in mind the
 execution paths through your code.

 Cheers,

 Kev

 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
CashFlow
To be rich.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python or ActionScript 3.0

2009-08-16 Thread paul

Jaseem schrieb:

Hi,

Is python similar to actionscript 3.0

Not really.


Which is better to create a rich gui internet application?
Is it AS 3.0 with flex or python with its GUI libs?

Flex+AS3 definitely! (it's been designed for that, no surprise here)



Is python in demand?
Depends. It seems quite popular in the scientific community and for 
system administration these days.



Heard that python is similar to lisp. But both python and AS 3.0 is
almost identical. Which is more similar to lisp are powerful?

If python is 100m away from lisp, than it's 102.32m for AS3.

cheers
 Paul


Thank You.


--
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Scott David Daniels

Steven D'Aprano wrote:

On Sun, 16 Aug 2009 02:47:42 -0700, Terry wrote:

Is there a simple way (the pythonic way) to flatten a list of list?

Chris' suggestion using itertools seems pretty good:


from timeit import Timer
setup = \\

... L = [ [None]*5000 for _ in xrange(%d) ]
... from itertools import chain
... 

Timer(list(chain.from_iterable(L)), setup % 4).repeat(number=1000)

[0.61839914321899414, 0.61799716949462891, 0.62065696716308594]

Timer(list(chain.from_iterable(L)), setup % 8).repeat(number=1000)

[1.2618398666381836, 1.3385050296783447, 3.9113419055938721]

Timer(list(chain.from_iterable(L)), setup % 16).repeat(number=1000)

[3.1349358558654785, 4.8554730415344238, 5.431217987061]


OK, it definitely helps to get a size estimate before building:

 setup = \\
L = [ [None]*5000 for _ in xrange(%d) ]
import itertools

class Holder(object):
def __init__(self, list_of_lists):
self._list = list_of_lists
def __iter__(self):
return itertools.chain.from_iterable(self._list)
def __len__(self):
return sum(len(x) for x in self._list)


 timeit.Timer(list(Holder(L)), setup % 4).repeat(number=1000)
[0.59912279353940789, 0.59505886921382967, 0.59474989139681611]
 timeit.Timer(list(Holder(L)), setup % 8).repeat(number=1000)
[1.1898235669617208, 1.194797383466323, 1.1945367358141823]
 timeit.Timer(list(Holder(L)), setup % 16).repeat(number=1000)
[2.4244464031043123, 2.4261885239604482, 2.4050011942858589]

vs straight chain.from_iterable (on my machine):

[0.7828263089303249, 0.79326171343005925, 0.80967664884783019]
[1.499510971366476, 1.5263249938190455, 1.5599706107899181]
[3.4427520816193109, 3.632409426337702, 3.5290488036887382]

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: What happened to __cmp__() in Python 3.x?

2009-08-16 Thread Raymond Hettinger
[Xavier Ho]
  I've recently made my way to Python 3.1 and I'm not seeing __cmp__() in the
  documentation.

  Is there a substitution for this special method in 3.1, or do I really have
  to define all six rich comparison methods to work it out?

FWIW, there is a recipe for expanding the comparison operators:

http://code.activestate.com/recipes/576685/


Raymond
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python or ActionScript 3.0

2009-08-16 Thread Jaseem
On Aug 16, 9:30 pm, paul p...@subsignal.org wrote:
 Jaseem schrieb: Hi,

  Is python similar to actionscript 3.0

 Not really.

  Which is better to create a rich gui internet application?
  Is it AS 3.0 with flex or python with its GUI libs?

 Flex+AS3 definitely! (it's been designed for that, no surprise here)



  Is python in demand?

 Depends. It seems quite popular in the scientific community and for
 system administration these days.

  Heard that python is similar to lisp. But both python and AS 3.0 is
  almost identical. Which is more similar to lisp are powerful?

 If python is 100m away from lisp, than it's 102.32m for AS3.

 cheers
   Paul



  Thank You.


Alright!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting on '^' ?

2009-08-16 Thread kj
In ad212ad9-2cac-4a03-a25d-1d46dd527...@w41g2000yqb.googlegroups.com 
ru...@yahoo.com writes:

On Aug 14, 2:23=A0pm, kj no.em...@please.post wrote:
 Sometimes I want to split a string into lines, preserving the
 end-of-line markers. =A0In Perl this is really easy to do, by splitting
 on the beginning-of-line anchor:

 =A0 @lines =3D split /^/, $string;

 But I can't figure out how to do the same thing with Python. =A0E.g.:

Why not this?

 lines =3D 'spam\nham\neggs\n'.splitlines (True)
 lines
['spam\n', 'ham\n', 'eggs\n']

That's perfect.

And .splitlines seems to be able to handle all standard end-of-line
markers without any special direction (which, ironically, strikes
me as a *little* Perlish, somehow):

 spam\015\012ham\015eggs\012.splitlines(True)
['spam\r\n', 'ham\r', 'eggs\n']

Amazing.  I'm not sure this is the *best* way to do this in general
(I would have preferred it, and IMHO it would have been more
Pythonic, if .splitlines accepted an additional optional argument
where one could specify the end-of-line sequence to be used for
the splitting, defaulting to the OS's conventional sequence, and
then it split *strictly* on that sequence).

But for now this .splitlines will do nicely.

Thanks!

kynn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Komodo(!)

2009-08-16 Thread Lorenzo Bettini

Kee Nethery wrote:
I've heard there is a nice add-on to Eclipse but Eclipse has even more 
setup variables than Wings and I've avoided it for that reason.




Hi

I've just started using python and since I've been an eclipse user for 
many years I tried http://pydev.sourceforge.net/ and I really enjoyed 
that! :-)


cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Francesco Bochicchio
On Aug 16, 1:25 pm, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:

...

 Chris' suggestion using itertools seems pretty good:

  from timeit import Timer
  setup = \\

 ... L = [ [None]*5000 for _ in xrange(%d) ]
 ... from itertools import chain
 ...  Timer(list(chain.from_iterable(L)), setup % 
 4).repeat(number=1000)

 [0.61839914321899414, 0.61799716949462891, 0.62065696716308594] 
 Timer(list(chain.from_iterable(L)), setup % 8).repeat(number=1000)

 [1.2618398666381836, 1.3385050296783447, 3.9113419055938721] 
 Timer(list(chain.from_iterable(L)), setup % 16).repeat(number=1000)

 [3.1349358558654785, 4.8554730415344238, 5.431217987061]

 --
 Steven

I had a peek at itertools ( which is a C module btw) and realized that
chain solves the problem by creating a chain object,
which is a sort of generator. Both creating the chain object and
converting the chain object to a list seem to be O(N), so
the whole is O(N) too ...

Then I tried this pure python version:

# - CODE
from timeit import Timer
setup = \\
L = [ [None]*5000 for _ in range(%d) ]
def pychain( list_of_list ):
for l in list_of_list:
for elem in l:
yield elem


print( Timer(list(pychain(L)), setup % 4).repeat(number=1000))
print( Timer(list(pychain(L)), setup % 8).repeat(number=1000))
print( Timer(list(pychain(L)), setup % 16).repeat(number=1000))
# - END CODE


and got times that seem to confirm it :

[2.818755865097046, 2.7880589962005615, 2.79232120513916]
[5.588631868362427, 5.588244915008545, 5.587780952453613]
[11.620548009872437, 11.39465618133545, 11.40834903717041]

For reference, here are the times of the itertools.chain solution on
my laptop:

[0.6518809795379639, 0.6491332054138184, 0.6483590602874756]
[1.3188841342926025, 1.3173959255218506, 1.3207998275756836]
[2.7200729846954346, 2.5402050018310547, 2.543621063232422]

All this with Python 3.1 compiled from source on Xubuntu 8.10.

Ciao
-
FB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 still not giving memory back to the OS...

2009-08-16 Thread Martin v. Löwis
 As far as releasing memory back to the OS is concerned, I have dim
 memories of *x systems where free() would return space to the OS if
 the block was large and it was next to the break point ... this
 effect could be what you are seeing.

Today, there are two cases when malloc returns memory on a typical
Unix system (in particular, in Linux malloc):
a) if the malloc block block is small (below page size), it is allocated
   from the brk heap, where it can only be returned if the last page of
   that heap is completely free, and
b) if the block is large (multiple pages), it gets returned to the
   system right away.

Case b) can only happen if the C malloc uses mmap to allocate large
blocks. For Python, case b) is realistic, in the sense that most
allocations go to Python's obmalloc, and those allocate from C malloc
in chunks of 256kiB (IIRC). So when such an arena is completely free
(not a single Python object allocated on it anymore), it can get
returned to the system.

In some case, Python also allocates smaller blocks from C malloc; in
this case, a) will trigger. So as long as something at the end of the
heap stays allocated, C malloc will not return anything from the brk
heap to the system.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Alan G Isaac
On 8/16/2009 5:47 AM Terry apparently wrote:
 Is there a simple way (the pythonic way) to flatten a list of list? 
 rather than my current solution:

 new_list=[] 
 for l in list_of_list:
 new_list.extend(l)


new_list = list(xi for lst in list_of_list for xi in lst)

hth,
Alan Isaac
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: flatten a list of list

2009-08-16 Thread Paul Rubin
Terry terry.yin...@gmail.com writes:
 Is there a simple way (the pythonic way) to flatten a list of list?
 rather than my current solution:
 
 new_list=[]
 for l in list_of_list:
 new_list.extend(l)

from itertools import chain
new_list = list(chain(list_of_list))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Douglas Alan
On Aug 16, 8:45 am, MRAB pyt...@mrabarnett.plus.com wrote:

 No, APL is strictly right-to-left.

      - x

 means goto x.

 Writing to the console is:

      [] - myVar

 Reading from the console is:

      myVar - []

Ah, thanks for the correction. It's been 5,000 years since I used APL!

|ouglas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax notations.

2009-08-16 Thread w_a_x_man
On Aug 16, 11:05 am, Petey Keller psil...@merlin.cs.wisc.edu wrote:
  Compiler go through *great* pains

Compiler work real hard.
Compiler have heap big trouble.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with interface of operator.itemgetter

2009-08-16 Thread Simon Forman
On Thu, Aug 13, 2009 at 11:16 PM, dou dounirvana...@gmail.com wrote:
 I have a function to do some thing like LEFT JOIN in SQL, the function use
 the itemgetter to get the ON and SELECT parameters of the two table(list
 of list), the problem is that itemgetter may return a value or a tuple of
 values, because of the inconsistent return type of itemgetter, I have to do
 2^3 IF check,
 the function like this:

 def left_join(table1, on_index1, table2, on_index2, getter1, getter2):
     do thing like:
     SELECT on1(table1), g1(table1), g2(table2) FROM table1
     LEFT JOIN table2
     ON on1(table1) == on2(table2)
     
     on1 = itemgetter(*on_index1)
     on2 = itemgetter(*on_index2)
     g1 = itemgetter(*getter1)
     g2 = itemgetter(*getter2)

     d2 = {}
     d2 = dict((on2(r2), r2) for r2 in table2)

     #if itemgetter always return tuple, below could simple as one line
     #return [ list(on1(r1) + g1(r1) + g2(d2.get(on1(r1 for r1 in table1
 ]

     len_on = len(on_index1)
     len_g1 = len(getter1)
     len_g2 = len(getter2)

     if len_on == 1:
     if len_g1 == 1 and len_g2 == 1:
     return [ [on1(r1), g1(r1), g2(d2.get(on1(r1)))] for r1 in table1
 ]
     elif len_g1 == 1 and len_g2  1:
     return [ list((on1(r1),g1(r1))+g2(d2.get(on1(r1 for r1 in
 table1 ]
     elif len_g1  1 and len_g2 == 1:
     return [ list((on1(r1),)+g1(r1)+(g2(d2.get(on1(r1))),))  for r1
 in table1 ]
     else: #len_g1  1 and len_g2  1:
     return [ list((on1(r1),)+g1(r1)+g2(d2.get(on1(r1 for r1 in
 table1 ]
     else: # len_on  1
     if len_g1 == 1 and len_g2 == 1:
     return [ list(on1(r1))+[g1(r1),g2(d2.get(on1(r1)))] for r1 in
 table1 ]
     elif len_g1 == 1 and len_g2  1:
     return [ list(on1(r1)+(g1(r1),)+g2(d2.get(on1(r1 for r1 in
 table1 ]
     elif len_g1  1 and len_g2 == 1:
     return [ list(on1(r1)+g1(r1)+(g2(d2.get(on1(r1))),))  for r1 in
 table1 ]
     else: #len_g1  1 and len_g2  1:
     return [ list(on1(r1)+g1(r1)+g2(d2.get(on1(r1 for r1 in
 table1 ]

 so is there a way to force itemgetter to return tuple even when
 itemgetter(only_have_one_argument)? or some other function to do this?

 Thanks.



You can use a little helper function to create your itemgetter like this:

def makeItemGetter(indexes):
   I = itemgetter(*indexes)
   if len(indexes)  1:
   return I
   return lambda thing: (I(thing),)

If indexes contains only one index the itemgetter is wrapped in a
lambda that turns its output into a tuple.

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting on '^' ?

2009-08-16 Thread John Yeung
On Aug 16, 1:09 pm, kj no.em...@please.post wrote:
 And .splitlines seems to be able to handle all
 standard end-of-line markers without any special
 direction (which, ironically, strikes
 me as a *little* Perlish, somehow):

It's Pythonic.  Universal newline-handling for text has been a staple
of Python for as long as I can remember (very possibly since the very
beginning).

  spam\015\012ham\015eggs\012.splitlines(True)

 ['spam\r\n', 'ham\r', 'eggs\n']

 Amazing.  I'm not sure this is the *best* way to do
 this in general (I would have preferred it, and IMHO
 it would have been more Pythonic, if .splitlines
 accepted an additional optional argument [...]).

I believe it's the best way.  When you can use a string method instead
of a regex, it's definitely most Pythonic to use the string method.

I would argue that this particular string method is Pythonic in
design.  Remember, Python strives not only for explicitness, but
simplicity and ease of use.  When dealing with text, universal
newlines are much more often than not simpler and easier for the
programmer.

John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread bartc


Steven D'Aprano st...@remove-this-cybersource.com.au wrote in message 
news:02969972$0$20647$c3e8...@news.astraweb.com...

On Fri, 14 Aug 2009 18:25:45 -0700, Dr. Phillip M. Feldman wrote:


It seems as though Python is actually expanding range(2,n) into a list
of numbers, even though this is incredibly wasteful of memory. There
should be a looping mechanism that generates the index variable values
incrementally as they are needed.



Others have already pointed out to you that xrange() (for Python 2.x)
does what you want. In Python 3.x, the old range() is gone for good, and
xrange() renamed to just range().


It does seem rather worrying that whoever originally thought up Python 
decided it would be a good idea to implement a simple 1 to N (or 0 to N-1) 
for-loop by first creating an array of N consecutive integers!


They must have known Python was going to be slow anyway, but this wouldn't 
have helped the speed any. Nor the memory consumption.


A for-loop, for iterating over a simple sequence, should be one of the 
fastest things in the language.


[Presumably the internal code that created those consecutive integers used a 
more conventional looping method...]


--
Bartc 


--
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting on '^' ?

2009-08-16 Thread Stephen Hansen
And .splitlines seems to be able to handle all standard end-of-line
 markers without any special direction (which, ironically, strikes
 me as a *little* Perlish, somehow):

  spam\015\012ham\015eggs\012.splitlines(True)
 ['spam\r\n', 'ham\r', 'eggs\n']


... actually working correctly and robustly is perlish? :)

The only reason I've ever actually used this method is this very feature of
it, that you can't readily reproduce with other methods unless you start
getting into regular expressions (and I really believe regular expressions
should not be the default place one looks to solve a problem in Python)

Then again, as soon as Python started allowing you to open files with mode
rU, I gleefully ran through my codebase and changed every operation to
that and made sure to write out with platform-local newlines exclusively,
thus finally flipping off those darn files that users kept producing with
mixed line endings.


 Amazing.  I'm not sure this is the *best* way to do this in general
 (I would have preferred it, and IMHO it would have been more
 Pythonic, if .splitlines accepted an additional optional argument
 where one could specify the end-of-line sequence to be used for
 the splitting, defaulting to the OS's conventional sequence, and
 then it split *strictly* on that sequence).


If you want strict and absolute splitting, you don't need another method;
just do mystring.split(os.linesep); I mean sure, it doesn't have the
'keepends' feature -- but I don't actually understand why you want keepends
with a strict definition of endings... If you /only/ want to split on \n,
you know there's an \n on the end of each line in the returned list and can
easily be sure to write it out (for example) :)

In the modern world of mixed systems and the internet, and files being flung
around willy-nilly, and editors being configured to varying degrees of
correctness, and such It's Pythonic to be able to handle all these files
that anyone made on any system and treat them as they are clearly *meant* to
be treated. Since the intention *is* clear that these are all *end of line*
markers-- it's explicitly stated, just slightly differently depending on the
OS-- Python treats all of the line-endings as equal on read if you want it
to. By using either str.splitlines() or opening a text file as rU. Thank
goodness for that :)

In some cases you may need a more pedantic approach to line endings. In that
case, just use str.split() :)

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: random.gauss vs. random.normalvariate

2009-08-16 Thread Dave Angel

John Haggerty wrote:

On Sat, Aug 15, 2009 at 7:23 PM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote:

  

On Sat, 15 Aug 2009 14:34:36 -0600, John Haggerty bouncy...@gmail.com
declaimed the following in gmane.comp.python.general:



What does the term thread safe mean exactly. I never had to program
  

with


threads before
  

That, part way through the logic of the function, control could be
switched to a different thread which call the same function... This
second call would change some of the internal values and may then be
preempted and control returned to the first thread, which continues the
rest of the function with different values then it had when first
preempted.

   A very contrived example, untested of course, consider it
pseudo-code...

startt = None

def atimer():
   global startt
   startt = time.time()
   time.sleep(5)
   print time.time() - startt

t1 = threading.thread(atimer)
t2 = threading.thread(atimer)
t1.start()
t2.start()

Say t1 gets all the way up to the sleep call, and (since sleep is a
releasing call), t2 then starts. t2 changes the value of startt; and
sleeps... both sleep and presuming the resolution is fine enough, t1
resumes, and  prints a delta time that is incorrect -- it is printing
the time difference from when t2 started to sleep, not from when t1
started to sleep.
--
   Wulfraed Dennis Lee Bieber   KD6MOG
   wlfr...@ix.netcom.com   HTTP://wlfraed.home.netcom.com/

--
http://mail.python.org/mailman/listinfo/python-list




Interesting so it seems that the compiler(c/c++)interpreter(perl,
python)/vm(java) doesn't do this?

  
It is impossible for a language, vm, or operating system to avoid 
threading problems without the programmer's help, except by trivial 
means (eg. preventing you from having them at all).


The power of threading is entirely tied up with the features the 
environment gives to the developer, and those features come with a risk.


At one extreme is the CP/M model.  You start a new program only when you 
finish the previous one.  So the only communication between them is a 
file the first one leaves behind, that the second can look at.


Next is separate processes.  If you launch a second process, by default, 
they're independent, and not likely to get into trouble.  But you can 
build pipes or shared memory, or sockets between them, and then you have 
to worry about race conditions.


Next is threads, within a single process.  At this point, you can share 
(global) variables between them, or you can have objects known to both 
when the thread is launched.  The system cannot tell which ones are 
deliberate and which ones are accidental.  So a language might give 
extra keywords to tell the compiler that certain things should be 
protected in certain ways.  Or it might give a way to declare a 
per-thread global that acts like a global to each thread, but is 
actually two independent variables from the process point of view.


The only real reason threads are singled out is it's easier to collide 
by mistake.  But that's also what makes it efficient to collide on 
purpose.


DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT Signature quote [was Re: Unrecognized escape sequences in string literals]

2009-08-16 Thread Douglas Alan
On Aug 16, 6:18 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:

 On Sun, 16 Aug 2009 01:41:41 -0700, Douglas Alan wrote:

  I would definitely not like a language that obscures assignment by
  moving it over to the right side of lines.

 One could argue that left-assigned-from-right assignment obscures the
 most important part of the assignment, namely *what* you're assigning, in
 favour of what you're assigning *to*.

The most important things are always the side-effects and the name-
bindings.

In a large program, it can be difficult to figure out where a name is
defined, or which version of a name a particular line of code is
seeing. Consequently languages should always go out of their way to
make tracking this as easy as possible.

Side effects are also a huge issue, and a source of many bugs. This is
one of the reasons that that are many functional languages that
prohibit or discourage side-effects. Side effects should be made as
obvious as is feasible.

This is why, for instance, in Scheme, variable assignment as an
exclamation mark in it. E.g.,

   (set! x (+ x 1))

The exclamation mark is to make the fact that a side effect is
happening there stand out and be immediately apparent. And C++
provides the const declaration for similar reasons.

 In any case, after half a century of left-from-right assignment, I think
 it's worth the experiment in a teaching language or three to try it the
 other way. The closest to this I know of is the family of languages
 derived from Apple's Hypertalk, where you do assignment with:

 put somevalue into name

That's okay with me, but only because the statement begins with put,
which lets you know at the very beginning of the line that something
very important is happening. You don't have to scan all the way to the
right before you notice.

Still, I would prefer

   let name = somevalue

as the let gives me the heads up right away, and then immediately
after the let is the name that I might want to be able to scan for
quickly.

|ouglas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax notations.

2009-08-16 Thread Peter Keller
In comp.lang.scheme w_a_x_man w_a_x_...@yahoo.com wrote:
 On Aug 16, 11:05?am, Petey Keller psil...@merlin.cs.wisc.edu wrote:
  Compiler go through *great* pains
 
 Compiler work real hard.
 Compiler have heap big trouble.

That's a funny observation in the context of this thread--which I
appreciate, since syntax really is the cornerstone of meaning transferal
between people. The unintended connotation brought in by what I mistakenly
wrote underscores the value of syntax.

However, what we don't have is a means of measuring the effectiveness
and/or efficiency of expressing meaning for an arbitrary set of syntax
rules. Computer Scientists can do this somewhat in that the expressive
power of parsing is greater than regular expressions and both can use a
syntax to represent them. But in a single complexity class, the black
art of how to place a metric on a syntax is, at least at this time,
relegated to the right brain and how it visually sees (and visually
parses) the syntax and how our emotions relate to the syntax.

The wolfram article, in fact, never does mention any metric other than
this is hard to understand, this is less hard to understand. In a sense,
how is that useful at all?  Instead of really trying to find a method
by which understanding can be placed upon a metric (or discovering a
method *can not* be found, he seems to anecdotally ascribe understanding
difficulty upon various syntaxs.

The real frustrations of Xah Lee might be explained by his denial of the
right brain processing of syntax information. It is to be expected since
most industrial cultures suppress right brain advancement (emotional
understanding/social interaction, drawing, music, spatial relations) in
lieu of left brain processing (language and syntax, symbolic manipulation
(part, though not all of the skill set of math), object naming). In
fact, his skills of communicating his ideas in a social setting which,
in my opinion, are poor and stunted, is a red flag and the epitome of
this type of cultural viewpoint.

Thank you.

-pete
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread MRAB

bartc wrote:


Steven D'Aprano st...@remove-this-cybersource.com.au wrote in 
message news:02969972$0$20647$c3e8...@news.astraweb.com...

On Fri, 14 Aug 2009 18:25:45 -0700, Dr. Phillip M. Feldman wrote:


It seems as though Python is actually expanding range(2,n) into a list
of numbers, even though this is incredibly wasteful of memory. There
should be a looping mechanism that generates the index variable values
incrementally as they are needed.



Others have already pointed out to you that xrange() (for Python 2.x)
does what you want. In Python 3.x, the old range() is gone for good, and
xrange() renamed to just range().


It does seem rather worrying that whoever originally thought up Python 
decided it would be a good idea to implement a simple 1 to N (or 0 to 
N-1) for-loop by first creating an array of N consecutive integers!



Whoever? I am shocked! ;-)

They must have known Python was going to be slow anyway, but this 
wouldn't have helped the speed any. Nor the memory consumption.


A for-loop, for iterating over a simple sequence, should be one of the 
fastest things in the language.


[Presumably the internal code that created those consecutive integers 
used a more conventional looping method...]




--
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a string into substrings of equal size

2009-08-16 Thread Simon Forman
On Aug 14, 8:22 pm, candide cand...@free.invalid wrote:
 Suppose you need to split a string into substrings of a given size (except
 possibly the last substring). I make the hypothesis the first slice is at the
 end of the string.
 A typical example is provided by formatting a decimal string with thousands
 separator.

 What is the pythonic way to do this ?

 For my part, i reach to this rather complicated code:

 # --

 def comaSep(z,k=3, sep=','):
     z=z[::-1]
     x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1]
     return sep.join(x)

 # Test
 for z in [75096042068045, 509, 12024, 7, 2009]:
     print z+ -- , comaSep(z)

 # --

 outputting :

 75096042068045 --  75,096,042,068,045
 509 --  509
 12024 --  12,024
 7 --  7
 2009 --  2,009

 Thanks

FWIW:

def chunks(s, length=3):
stop = len(s)
start = stop - length
while start  0:
yield s[start:stop]
stop, start = start, start - length
yield s[:stop]


s = '1234567890'
print ','.join(reversed(list(chunks(s
# prints '1,234,567,890'
-- 
http://mail.python.org/mailman/listinfo/python-list


XPath support?

2009-08-16 Thread kj


I'm looking for a XML parser that produces an object with full
XPath support.  What I've been using up to now, xml.etree.ElementTree,
fails to support Xpath predicates, as in sp...@eggs='3']/ham.

What I'm trying to do is to read-in a large XML string, and parse
it into an object from which I can extract nodes matching selectors
that include such predicates.

Any suggestions would be greatly appreciated.

TIA!

kynn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XPath support?

2009-08-16 Thread Diez B. Roggisch

kj schrieb:

I'm looking for a XML parser that produces an object with full
XPath support.  What I've been using up to now, xml.etree.ElementTree,
fails to support Xpath predicates, as in sp...@eggs='3']/ham.

What I'm trying to do is to read-in a large XML string, and parse
it into an object from which I can extract nodes matching selectors
that include such predicates.

Any suggestions would be greatly appreciated.


Try lxml. It's element-tree compatible, and supports xpath.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: XPath support?

2009-08-16 Thread Kev Dwyer
On Sun, 16 Aug 2009 20:29:15 +, kj wrote:

 I'm looking for a XML parser that produces an object with full XPath
 support.  What I've been using up to now, xml.etree.ElementTree, fails
 to support Xpath predicates, as in sp...@eggs='3']/ham.
 
 What I'm trying to do is to read-in a large XML string, and parse it
 into an object from which I can extract nodes matching selectors that
 include such predicates.
 
 Any suggestions would be greatly appreciated.
 
 TIA!
 
 kynn


Have you tried lxml (http://codespeak.net/lxml/)?

Kev

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread sturlamolden
On 16 Aug, 11:45, bartc ba...@freeuk.com wrote:

 A for-loop, for iterating over a simple sequence, should be one of the
 fastest things in the language.

Anyone experienced with interpreted high-level languages knows this is
not true. Not because iterating a sequence is expensive, but because
the interpreter is constantly executing the body of the loop. There is
a reason why Matlab and Python/NumPy programmers rely heavily on
vectorized array expressions for efficient numerics.

The only thing more evil than a for-loop is recursive function calls.

This does not mean Python is slow. It just means you have to program
differently.







-- 
http://mail.python.org/mailman/listinfo/python-list


Re: random.gauss vs. random.normalvariate

2009-08-16 Thread Paul Rubin
Dennis Lee Bieber wlfr...@ix.netcom.com writes:
 No language can guard against independent access of a shared/global
   object by multiple threads...
 http://en.wikipedia.org/wiki/Erlang_(programming_language)
 
 Like operating system processes (and unlike green threads and operating
 system threads) they have NO SHARED STATE between them.
 
 which seems to reinforce my statement, not refute it.

Id say Erlang guards against independent access of shared/global
objects by not allowing them.

 (The Amiga was heavily dependent upon message ports... Having to
 copy data buffers between processes would have slowed the system
 down drastically [8MHz processor, in those days],

It's perfectly ok for Erlang implementation to just pass pointers
around, when the processes are on the same machine.  Erlang prohibits
data mutation so there isn't an issue of a process modifying some
structure while another process is using it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax notations.

2009-08-16 Thread toby
On Aug 16, 12:05 pm, Peter Keller psil...@merlin.cs.wisc.edu wrote:
 In comp.lang.scheme Xah Lee xah...@gmail.com wrote:

  Xah's Edu Corner: The importance of syntax  notations.

 http://www.stephenwolfram.com/publications/recent/mathml/mathml_abstr...

  this article should teach the coding sophomorons and computer
  ?science? idiotic authors who harbor the notion that syntax is not
  important, picked up by all the elite i-reddit  twittering   hacker
  news am-hip dunces.

 I must have really tweaked you with my Syntax is not important, ideas are.
 statement.

 I read Wolfram's article carefully. He applies an intuitive sense onto
 why he does or doesn't like a particular notation, but yet can't really
 elucidate his feelings.

Exactly; and as far as I can determine, Knuth does the same. He
applies standards of *good taste* to his notation. (No surprise that
he's also singled out for vituperation by the OP.)

In my opinion Knuth believed in the value of literate programming for
similar reasons: To try to exploit existing cognitive training. If
your user base is familiar with English, or mathematical notation, or
some other lexicography, try to exploit the pre-wired associations.
Clearly this involves some intuition.

 ...
 Thank you.

 -pete

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax notations.

2009-08-16 Thread rjf
On Aug 16, 9:05 am, Peter Keller psil...@merlin.cs.wisc.edu wrote:
 In comp.lang.scheme Xah Lee xah...@gmail.com wrote:

  Xah's Edu Corner: The importance of syntax  notations.

 http://www.stephenwolfram.com/publications/recent/mathml/mathml_abstr...

  this article should teach the coding sophomorons and computer
  ?science? idiotic authors who harbor the notion that syntax is not
  important, picked up by all the elite i-reddit  twittering   hacker
  news am-hip dunces.

 I must have really tweaked you with my Syntax is not important, ideas are.
 statement.

 I read Wolfram's article carefully. He applies an intuitive sense onto
 why he does or doesn't like a particular notation, but yet can't really
 elucidate his feelings. I'm surprised that didn't tweak you worse. He
 also goes so far as to mention that:

 But actually we still don't know a clean simple way to represent things
 like geometrical diagrams in a kind of language-like notation. And
 my guess is that actually of all the math-like stuff out there, only
 a comparatively small fraction can actually be represented well with
 language-like notation.



For someone talking about notation and language, it is amusing that
Wolfram refuses to write grammatically correct sentences.  Each of the
pseudo-sentences quoted above is a dependent clause.  Wolfram writes
like that. He typically begins sentences with but or and.

In the article cited, he also claims to be more-or-less the only
person alive to have thought about these issues, and that Mathematica
is the solution.  This is not surprising,and it is nevertheless clear
that Wolfram has spent some time thinking about mathematical notation,
and some money developing fonts and such.  His self-praise is perhaps
not deserved.

It is unfortunate that most users of Mathematica labor under a
misunderstanding of the meaning of the fundamental algorithmic
notation of that system, namely function definition.

f[x_]:=x+1is not a function definition, although most users think
so.

In fact, it is a pattern/replacement rule.

If you want to write the equivalent of the lisp (defun f(x)(+ x 1))
you can do so in mathematica, but it looks like this:

f=#1+1

or alternatively,

f=Function[Plus[Slot[1],1]]

or alternatively,

f= Function[{x},x+1]


How do those choices grab you for the Emmy in Excellence in
Notation?

By the way, what is wrong with f[x_]:=x+1?

While you might think it maps  x-x+1 for all x,  it does so only in
the absence of other rules involving f.  Thus the presence of another
definition (actually another rule) ... f[z_]:=z^2/;OddQ[z]  changes
the definition when the argument is an odd integer.
An equivalent definition is
f[z_?OddQ]:=z^2

now you could have 2 rules
f[x_?Predicate1]:= 123
f[x_?Predicate2]:= 456

and it might not be clear which Predicate is a subset of the other.
(It is clear that the blank pattern _ is a superset of _?OddQ ).

So how does Mathematica deal with this situation?
The predicates are tested in some order until one returns True.
What order is that?

 Whenever the appropriate ordering is not clear, Mathematica stores
rules in the order you give them. 
Oh, when is the ordering not clear?

Uh, you should realize that this [ordering] is not always possible.
(quotes from mathematica documentation , TheOrderingOfDefinitions.)

While Wolfram has provided an admirable summary of the history of
notation, his solution is less so.

RJF

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 still not giving memory back to the OS...

2009-08-16 Thread ryles
On Aug 15, 7:55 am, Chris Withers ch...@simplistix.co.uk wrote:
 Hi All,

 I thought this was fixed back in Python 2.5, but I guess not?

 So, I'm playing in an interactive session:

   from xlrd import open_workbook
   b = open_workbook('some.xls',pickleable=0,formatting_info=1)

 At this point, top shows the process usage for python to be about 500Mb.
 That's okay, I'd expect that, b is big ;-)

   del b

 However, it still does now, maybe the garbage collector needs a kick?

   import gc
   gc.collect()
 702614

 Nope, still 500Mb. What gives? How can I make Python give the memory its
 no longer using back to the OS?

 Okay, so maybe this is something to do with it being an interactive
 session? So I wrote this script:

 from xlrd import open_workbook
 import gc
 b = open_workbook('some.xls',pickleable=0,formatting_info=1)
 print 'opened'
 raw_input()
 del b
 print 'deleted'
 raw_input()
 gc.collect()
 print 'gc'
 raw_input()

 The raw inputs are there so I can check the memory usage in top.
 Even after the gc, Python still hasn't given the memory back to the OS :-(

 What am I doing wrong?

 Chris

 --
 Simplistix - Content Management, Batch Processing  Python Consulting
             -http://www.simplistix.co.uk

Repeat the 'b = open_workbook ...' line in the interpreter several
times and track the memory usage after each execution. I noticed this
behavior on linux, but saw that after repeating it a few times memory
usage finally stop going up. Probably just the behavior of the
allocator.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nltk with python

2009-08-16 Thread Aahz
In article 82c9f923-1098-4b7e-8f9d-9504c1a89...@12g2000pri.googlegroups.com,
ArshaKrishna  arshakrishnamt...@gmail.com wrote:

How can I resolve scope ambiguity using nltk toolkit with python

Question not clear, please provide more explanation
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

I saw `cout' being shifted Hello world times to the left and stopped
right there.  --Steve Gonedes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: redoing libgmail interface to smtplib blah?

2009-08-16 Thread Dave Angel

Dennis Lee Bieber wrote:

On Sat, 15 Aug 2009 23:14:39 -0600, John Haggerty bouncy...@gmail.com
declaimed the following in gmane.comp.python.general:

  

I did detect one problem thus far

 File test.py, line 152
if len(args) == 1 and args[0] = -c:



Should have been fine, unless my memory of sys.argv contents was
wrong (as I recall, sys.argv, if it behaves like other languages, would
  
snip
You missed the actual problem:   there's a single '=' in the second part 
of the if expression.  Should be ==


DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: zip codes

2009-08-16 Thread Shailen
Thanks Martin and Aahz. Anyone know if zip code information is
copyrighted for the US? Anyone can look up zip codes on usps.gov (and
other locations),so the information is readily available. I need zip
codes for a handful of cities and could map those myself (or write a
script to scrape them for me), but am not sure if they *MUST* be
purchased for copyright reasons. Anyone know?

- Shailen Tuli
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread sturlamolden
On 16 Aug, 14:57, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:

         Well, the alternative would be to have two keywords for looping: one
 for your simple incrementing integer loop, and another for a loop that
 operates over the elements of some collection type.

A compiler could easily recognise a statement like

   for i in range(n):

as a simple integer loop. In fact, Cython is able to do this.






-- 
http://mail.python.org/mailman/listinfo/python-list


Re: zip codes

2009-08-16 Thread Paul Rubin
Shailen shailen.t...@gmail.com writes:
 Thanks Martin and Aahz. Anyone know if zip code information is
 copyrighted for the US? Anyone can look up zip codes on usps.gov (and
 other locations),so the information is readily available. I need zip
 codes for a handful of cities and could map those myself (or write a
 script to scrape them for me), but am not sure if they *MUST* be
 purchased for copyright reasons. Anyone know?

I don't think US zip code info is copyrighted.  There is a lot of it
in the TIGER database:

  http://www.census.gov/geo/www/tiger/tigerua/ua_tgr2k.html

There are also some API's operated by usps.com if you want just a few
lookups now and then.

It was a long time ago, I don't remember specifics, and the contents
are surely out of date by now, but I extracted a bunch of the TIGER
geographic coordinates for zip codes here:

  http://www.nightsong.com/phr/chess/zipcodes.zip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: zip codes

2009-08-16 Thread Paul Rubin
 It was a long time ago, I don't remember specifics, and the contents
 are surely out of date by now, but I extracted a bunch of the TIGER
 geographic coordinates for zip codes here:
 
   http://www.nightsong.com/phr/chess/zipcodes.zip

That file may have actually come from here:

  http://www.census.gov/geo/www/tiger/zip1999.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: zip codes

2009-08-16 Thread Grant Edwards
On 2009-08-16, Shailen shailen.t...@gmail.com wrote:

 Thanks Martin and Aahz. Anyone know if zip code information is
 copyrighted for the US?

You can't copyright information as such.  Only concrete
expressions of information.  A particular publication
containing zip code information can be copyrighted.  The
underlying facts themselve cannot be.

-- 
Grant


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Benjamin Kaplan
On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden sturlamol...@yahoo.no wrote:

 A compiler could easily recognise a statement like

   for i in range(n):

 as a simple integer loop. In fact, Cython is able to do this.

but special cases aren't special enough to break the rules.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread John Machin
On Aug 17, 8:35 am, sturlamolden sturlamol...@yahoo.no wrote:

 A compiler could easily recognise a statement like
    for i in range(n):
 as a simple integer loop. In fact, Cython is able to do this.

Extremely easy, once users relinquish the right to replace built-in
range with their own concoctions ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread exarkun

On 01:23 am, benjamin.kap...@case.edu wrote:
On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden sturlamol...@yahoo.no 
wrote:


A compiler could easily recognise a statement like

� for i in range(n):

as a simple integer loop. In fact, Cython is able to do this.


but special cases aren't special enough to break the rules.


Although I think PyPy also recognizes this case and makes it as 
efficient as using xrange, and does so without breaking any rules.


There *are* _some_ legitimate complaints to be made about the CPython 
runtime. :)


Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Paul Rubin
exar...@twistedmatrix.com writes:
 Although I think PyPy also recognizes this case and makes it as
 efficient as using xrange, and does so without breaking any rules.

How can pypy possibly know that the user hasn't assigned some other
value to range?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Carl Banks
On Aug 16, 6:28 pm, exar...@twistedmatrix.com wrote:
 On 01:23 am, benjamin.kap...@case.edu wrote:

 On Sun, Aug 16, 2009 at 6:35 PM, sturlamolden sturlamol...@yahoo.no
 wrote:

 A compiler could easily recognise a statement like

   for i in range(n):

 as a simple integer loop. In fact, Cython is able to do this.

 but special cases aren't special enough to break the rules.

 Although I think PyPy also recognizes this case and makes it as
 efficient as using xrange, and does so without breaking any rules.

PyPy uses a JIT compiler (which is still slower than CPython,
suggesting that perhaps they should have spent more time optimizing
the general case than optimizing for an easily avoided special case).


 There *are* _some_ legitimate complaints to be made about the CPython
 runtime. :)

This isn't one of them.

xrange has been part of Python for 10 years.

If there are any complaints to be made about this situation it's that
there are any 2.x learning materials anythere that continue to use
range() and not xrange() in this context.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 'for' loop is memory inefficient

2009-08-16 Thread Carl Banks
On Aug 16, 3:35 pm, sturlamolden sturlamol...@yahoo.no wrote:
 On 16 Aug, 14:57, Dennis Lee Bieber wlfr...@ix.netcom.com wrote:

          Well, the alternative would be to have two keywords for looping: one
  for your simple incrementing integer loop, and another for a loop that
  operates over the elements of some collection type.

 A compiler could easily recognise a statement like

    for i in range(n):

 as a simple integer loop.

It would be a simple to do if you were writing it for a different
langauge was a lot less dynamic than Python is.  It'd be quite a
complex hack to add it to CPython's compiler while maintaing the
current highly dynamic runtime semantics and backwards compatibility,
which is a design constraint of Python whether you like it or not.

And all this complaining about an issue that can be worked around by
xrange instead of range.  Sheesh.


 In fact, Cython is able to do this.

Cython can do this easily because it is a different language that is a
lot less dynamic than Python.

If you don't care about the dynamic stuff why don't you just use
Cython?  Or quit complaining and just use xrange.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >