Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 01:39 schrieb Ian Kelly:

On Wed, Oct 24, 2012 at 5:08 PM, Paul Rubin  wrote:

from itertools import dropwhile

j = dropwhile(lambda j: j in selected,
  iter(lambda: int(random() * n), object()))
  .next()

kind of ugly, makes me wish for a few more itertools primitives, but I
think it expresses reasonably directly what you are trying to do.


Nice, although a bit opaque.  I think I prefer it as a generator expression:

j = next(j for j in iter(partial(randrange, n), None) if j not in selected)


This generator never ends. If it meets a non-matching value, it just 
skips it and goes on.


The dropwhile expression, however, stops as soon as the value is found.

I think

# iterate ad inf., because partial never returns None:
i1 = iter(partial(randrange, n), None)
# take the next value, make it None for breaking:
i2 = (j if j in selected else None for j in i1)
# and now, break on None:
i3 = iter(lambda: next(i2), None)

would do the job.


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


Re: [OT] Re: turn list of letters into an array of integers

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 07:47:48 +0200, Peter Otten wrote:

> Wasn't there a Monty Python sketch where a man carrying a parrot in a
> cage comes into a shop full of stuffed animals and complains: No, I
> don't admire the taxidermist for making that parrot look like it were
> alive -- that beast bit me!

I don't think so. Are you thinking of the famous Monty Python "Dead 
Parrot Sketch"? Here's one of many versions:

http://www.youtube.com/watch?v=4vuW6tQ0218



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


Re: [OT] Re: turn list of letters into an array of integers

2012-10-25 Thread Peter Otten
Steven D'Aprano wrote:

> On Thu, 25 Oct 2012 07:47:48 +0200, Peter Otten wrote:
> 
>> Wasn't there a Monty Python sketch where a man carrying a parrot in a
>> cage comes into a shop full of stuffed animals and complains: No, I
>> don't admire the taxidermist for making that parrot look like it were
>> alive -- that beast bit me!
> 
> I don't think so. Are you thinking of the famous Monty Python "Dead
> Parrot Sketch"? Here's one of many versions:
> 
> http://www.youtube.com/watch?v=4vuW6tQ0218

My rendition was meant to be a travesty of that one, an "undead" parrot as a 
follow-up to Dennis' "lich" post. I'm sorry I forgot the smiley ;)

- He didn't move, that was just the wind stirring his plumage.
- No, that parrot is alive and kicking, fresh as a daisy, full of beans...
- Aren't the glass eyes beautiful?
- Glass eyes -- he just blinked!

And so on. I'm off to work on my laden swallow branch of Python. It's going 
to be a real heavy-weight...

-- 
Always look on the dark side of death

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


Re: [OT] Re: turn list of letters into an array of integers

2012-10-25 Thread Mark Lawrence

On 25/10/2012 09:25, Peter Otten wrote:

Steven D'Aprano wrote:


On Thu, 25 Oct 2012 07:47:48 +0200, Peter Otten wrote:


Wasn't there a Monty Python sketch where a man carrying a parrot in a
cage comes into a shop full of stuffed animals and complains: No, I
don't admire the taxidermist for making that parrot look like it were
alive -- that beast bit me!


I don't think so. Are you thinking of the famous Monty Python "Dead
Parrot Sketch"? Here's one of many versions:

http://www.youtube.com/watch?v=4vuW6tQ0218


My rendition was meant to be a travesty of that one, an "undead" parrot as a
follow-up to Dennis' "lich" post. I'm sorry I forgot the smiley ;)

- He didn't move, that was just the wind stirring his plumage.
- No, that parrot is alive and kicking, fresh as a daisy, full of beans...
- Aren't the glass eyes beautiful?
- Glass eyes -- he just blinked!

And so on. I'm off to work on my laden swallow branch of Python. It's going
to be a real heavy-weight...



I just hope you get the technicalities correct.  "That parrot wouldn't 
move if you put 4 million volts through it".  What rubbish.  It should 
either have been 4 million amps through it or 4 million volts across it. 
 I'm +1 for the former, although possibly biased by history.


--
Cheers.

Mark Lawrence.

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


Re: Fast forward-backward (write-read)

2012-10-25 Thread Virgil Stokes

On 24-Oct-2012 17:11, rusi wrote:

On Oct 23, 7:52 pm, Virgil Stokes  wrote:

I am working with some rather large data files (>100GB) that contain time series
data. The data (t_k,y(t_k)), k = 0,1,...,N are stored in ASCII format. I perform
various types of processing on these data (e.g. moving median, moving average,
and Kalman-filter, Kalman-smoother) in a sequential manner and only a small
number of these data need be stored in RAM when being processed. When performing
Kalman-filtering (forward in time pass, k = 0,1,...,N) I need to save to an
external file several variables (e.g. 11*32 bytes) for each (t_k, y(t_k)). These
are inputs to the Kalman-smoother (backward in time pass, k = N,N-1,...,0).
Thus, I will need to input these variables saved to an external file from the
forward pass, in reverse order --- from last written to first written.

Finally, to my question --- What is a fast way to write these variables to an
external file and then read them in backwards?

Have you tried gdbm/bsddbm? They are meant for such (I believe).
Probably needs to be installed for windows; works for linux.
If I were you I'd try out with the giant data on linux and see if the
problem is solved, then see how to install for windows

Thanks Rusi :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
Actually what I wanted to do is :

suppose there are 3 lists :

like for xample :

clist = [] an empty list , alist = [1,2,3], blist = [4,5,6]

and the I want the result is clist = [ [1,2,3], [4,5,6] ..]

then I want to read all the lists in clist one by one

like :

 for item in clist:
 print item

which should print:

[1,2,3]
[4,5,6]




On Tue, Oct 23, 2012 at 10:10 PM, inshu chauhan wrote:

> ok I got it guys...
>
>
> On Tue, Oct 23, 2012 at 10:08 PM, Joshua Landau <
> joshua.landau...@gmail.com> wrote:
>
>>  On 23 October 2012 21:06, Joshua Landau wrote:
>>
>>>  On 23 October 2012 21:03, Joshua Landau wrote:
>>>
 On 23 October 2012 12:07, Jean-Michel Pichavant >>> > wrote:

> - Original Message -
>
> > Thankyou.. but my problem is different than simply joining 2 lists
> > and it is done now :)
>
>
> A lot of people though you were asking for joining lists, you
> description was misleading.
>
> I'll take a guess: you want to flatten a list of list.
> "Nested" list comprehensions can do the trick.
>
> aList =[[1,5], [2,'a']]
> [item for sublist in aList for item in sublist]
>
> ...
> [1, 5, 2, 'a']
>
> I find it rather difficult to read though.


 We have a library function for this, in the one-and-only itertools.

 >>> listoflists = [list(range(x, 2*x)) for x in range(5)]
> >>> listoflists
> [[], [1], [2, 3], [3, 4, 5], [4, 5, 6, 7]]
> >>> from itertools import chain
>  >>> list(chain.from_iterable(listoflists))
> [1, 2, 3, 3, 4, 5, 4, 5, 6, 7]


 It does exactly what it says... fast and easy-to-read.
>>>
>>>
>>> Note that I think what he really wanted is to go from
>>>
>>> a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]
>>>
>>> to
>>>
 list(range(30))
>>>
>>>
>> UNDO! UNDO! UNDO!
>>
>> I *meant *to say:
>>
>>  Note that I think what he really wanted is to go from
>>
>> a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]
>>
>> to
>>
>>> [a, b, c]
>>
>>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread Dave Angel
On 10/25/2012 05:04 AM, inshu chauhan wrote:
> Actually what I wanted to do is :
>
> suppose there are 3 lists :
>
> like for xample :
>
> clist = [] an empty list , alist = [1,2,3], blist = [4,5,6]
>
> and the I want the result is clist = [ [1,2,3], [4,5,6] ..]
>
> then I want to read all the lists in clist one by one
>
> like :
>
>  for item in clist:
>  print item
>
> which should print:
>
> [1,2,3]
> [4,5,6]
>
>

clist = alist, blist

or if the clist initial contents were relevant, perhaps you mean

clist += alist, blist



-- 

DaveA

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
> > clist = alist, blist
>
This may work i guess becuase i want list within list [ [1,2,3] , [4,5,6]]


>
> or if the clist initial contents were relevant, perhaps you mean
>
> > clist += alist, blist
>

But not this because it will simply concatenate the  list like
[1,2,3,4,5,6] .. I dont want this actaully...

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread Dave Angel
On 10/25/2012 05:16 AM, Dave Angel wrote:
> On 10/25/2012 05:04 AM, inshu chauhan wrote:
>> Actually what I wanted to do is :
>>
>> suppose there are 3 lists :
>>
>> like for xample :
>>
>> clist = [] an empty list , alist = [1,2,3], blist = [4,5,6]
>>
>> and the I want the result is clist = [ [1,2,3], [4,5,6] ..]
>>
>> then I want to read all the lists in clist one by one
>>
>> like :
>>
>>  for item in clist:
>>  print item
>>
>> which should print:
>>
>> [1,2,3]
>> [4,5,6]
>>
>>
> clist = alist, blist
>
> or if the clist initial contents were relevant, perhaps you mean
>
> clist += alist, blist
>
>
>

Sorry, i was too quick off the mark.  I meant

clist = [alist, blist]

my first response would work with the loop, but it would have been a
tuple of lists



-- 

DaveA

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread Dave Angel
On 10/25/2012 05:21 AM, inshu chauhan wrote:
>>
>> or if the clist initial contents were relevant, perhaps you mean
>>
>>> clist += alist, blist
>>
> 
> But not this because it will simply concatenate the  list like
> [1,2,3,4,5,6] .. I dont want this actaully...
> 
>>

No, it won't.  Try it to see.



-- 

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


Re: while expression feature proposal

2012-10-25 Thread Paul Rudin
Paul Rubin  writes:

> kind of ugly, makes me wish for a few more itertools primitives

JOOI, do you have specific primitives in mind?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
>>
> >> or if the clist initial contents were relevant, perhaps you mean
> >>
> >>> clist += alist, blist
> >>
> >
> > But not this because it will simply concatenate the  list like
> > [1,2,3,4,5,6] .. I dont want this actaully...
> >
> >>
>
> No, it won't.  Try it to see


Ok but it should be clist + = [alist, blist ] 

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 5:31 AM, Dave Angel  wrote:
> On 10/25/2012 05:21 AM, inshu chauhan wrote:
>>>
>>> or if the clist initial contents were relevant, perhaps you mean
>>>
 clist += alist, blist
>>>
>>
>> But not this because it will simply concatenate the  list like
>> [1,2,3,4,5,6] .. I dont want this actaully...
>>
>>>
>
> No, it won't.  Try it to see.
>

Is this what you mean:

>>> list_1 = [i for i in range(0,5)]
>>> list_2 = [i for i in range(5,11)]
>>> for i in list_2:
... list_1.append(i)
...
>>> list_1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


or would another example help you out more?

-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 5:44 AM, inshu chauhan  wrote:
>
>
>> >>
>> >> or if the clist initial contents were relevant, perhaps you mean
>> >>
>> >>> clist += alist, blist
>> >>
>> >
>> > But not this because it will simply concatenate the  list like
>> > [1,2,3,4,5,6] .. I dont want this actaully...
>> >
>> >>
>>
>> No, it won't.  Try it to see
>
>
> Ok but it should be clist + = [alist, blist ] 
>>
>>
>>
>> --
Also, try help(list) in the interpreter for functions utilizable by list.


Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
> Is this what you mean:
>
> >>> list_1 = [i for i in range(0,5)]
> >>> list_2 = [i for i in range(5,11)]
> >>> for i in list_2:
> ... list_1.append(i)
> ...
> >>> list_1
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
>
> or would another example help you out more?
>

No but really sorry this is what I DONT WANT...

The output I want to have is :

[ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].

>
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread Dave Angel
On 10/25/2012 05:44 AM, inshu chauhan wrote:
>>>
 or if the clist initial contents were relevant, perhaps you mean

> clist += alist, blist

>>>
>>> But not this because it will simply concatenate the  list like
>>> [1,2,3,4,5,6] .. I dont want this actaully...
>>>

>>
>> No, it won't.  Try it to see
> 
> 
> Ok but it should be clist + = [alist, blist ] 
> 
>>
>>
>> --
>>
>> DaveA
>>
> 

No, try it and see.  a,b is a tuple, and [a,b] is a list.  But if you're
doing the +=, and the LHS is a list, then it'll work with any iterable.

BTW, the extra space in your new line will also be a problem.  The plus
and the equals must be adjacent, without an intervening space.


-- 

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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 00:26 schrieb Cameron Simpson:


If I could write this as:

   if re_FUNKYPATTERN.match(test_string) as m:
 do stuff with the results of the match, using "m"

then some cascading parse decisions would feel a bit cleaner. Where I
current have this:

   m = re_CONSTRUCT1.match(line)
   if m:
 ... handle construct 1 ...
   else:
 m = re_CONSTRUCT2.match(line)
 if m:
   ... handle construct 2 ...
 else:
   m = re_CONSTRUCT3.match(line)

I could have this:

   if re_CONSTRUCT1.match(line) as m:
 ... handle construct 1 ...
   elif re_CONSTRUCT2.match(line) as m:
 ... handle construct 2 ...
   elif re_CONSTRUCT3.match(line) as m:


I would do

for r in re_CONSTRUCT1, re_CONSTRUCT2, re_CONSTRUCT3:
m = r.match(line)
if m: handle_construct

or maybe

actions = {re_CONSTRUCT1: action1, ...}

def matching(line, *rr):
for r in rr:
m = r.match(line)
if m: yield r; return

for r in matching(line, *actions.keys()):
actions[r]()
break
else:
raise NoActionMatched() # or something like that

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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 06:50 schrieb Terry Reedy:


Keep in mind that any new syntax has to be a substantial improvement in
some sense or make something new possible. There was no new syntax in
3.2 and very little in 3.3.


I would consinder this at least as new substantial than

yield_from it

as opposed to

for i in it: yield i

- although I think that was a good idea as well.

Although there are quite easy ways to do so, I would appreciate 
something like the proposed


   while EXPR as VAR: use VAR
   if EXPR as VAR: use VAR

Of course it is possible to construct a respective workaround such as

def maybe_do_that():
if moon == full:
with something as val:
yield val

for val in maybe_do_that():
bla

but I would consider this as an abuse of the generator concept.

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan  wrote:
>
>
>>
>> Is this what you mean:
>>
list_0 = []
list_1 = [i for i in range(0,5)]
list_2 = [i for i in range(5,11)]
list_0.append(list_1)
list_0.append(list_2)
>> >>> for i in list_2:
>> ... list_1.append(i)
>> ...
>> >>> list_1
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>
>>
>> or would another example help you out more?
>
>
> No but really sorry this is what I DONT WANT...
>
> The output I want to have is :
>
> [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].

Then, this is what you want :

>>david@david-desktop:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list_0 = []
>>> list_1 = [i for i in range(0,5)]
>>> list_2 = [i for i in range(5,11)]
>>> list_0.append(list_1)
>>> list_0.append(list_2)
>>> list_0
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
>>>




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
Yes Dave ..You are right and my problem is solved now..  Thanx to all...

On Thu, Oct 25, 2012 at 11:55 AM, David Hutto wrote:

> On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan 
> wrote:
> >
> >
> >>
> >> Is this what you mean:
> >>
> list_0 = []
> list_1 = [i for i in range(0,5)]
> list_2 = [i for i in range(5,11)]
> list_0.append(list_1)
> list_0.append(list_2)
> >> >>> for i in list_2:
> >> ... list_1.append(i)
> >> ...
> >> >>> list_1
> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >>
> >>
> >> or would another example help you out more?
> >
> >
> > No but really sorry this is what I DONT WANT...
> >
> > The output I want to have is :
> >
> > [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
>
> Then, this is what you want :
>
> >>david@david-desktop:~$ python
> Python 2.7.3 (default, Aug  1 2012, 05:16:07)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> list_0 = []
> >>> list_1 = [i for i in range(0,5)]
> >>> list_2 = [i for i in range(5,11)]
> >>> list_0.append(list_1)
> >>> list_0.append(list_2)
> >>> list_0
> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
> >>>
>
>
>
>
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
Also,

>>> list_1 = [i for i in range(0,5)]
>>> list_2 = [i for i in range(5,11)]
>>> list_0 = [list_1,list_2]
>>> list_0
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan  wrote:
> Yes Dave ..You are right and my problem is solved now..  Thanx to all...
>
>
> On Thu, Oct 25, 2012 at 11:55 AM, David Hutto 
> wrote:
>>
>> On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan 
>> wrote:
>> >
>> >
>> >>
>> >> Is this what you mean:
>> >>
>> list_0 = []
>> list_1 = [i for i in range(0,5)]
>> list_2 = [i for i in range(5,11)]
>> list_0.append(list_1)
>> list_0.append(list_2)
>> >> >>> for i in list_2:
>> >> ... list_1.append(i)
>> >> ...
>> >> >>> list_1
>> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> >>
>> >>
>> >> or would another example help you out more?
>> >
>> >
>> > No but really sorry this is what I DONT WANT...
>> >
>> > The output I want to have is :
>> >
>> > [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
>>
>> Then, this is what you want :
>>
>> >>david@david-desktop:~$ python
>> Python 2.7.3 (default, Aug  1 2012, 05:16:07)
>> [GCC 4.6.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> list_0 = []
>> >>> list_1 = [i for i in range(0,5)]
>> >>> list_2 = [i for i in range(5,11)]
>> >>> list_0.append(list_1)
>> >>> list_0.append(list_2)
>> >>> list_0
>> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]

if you have a format for the lists, there might be an easier way, or
simpler. How are the lists coming in?



-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
On Thu, Oct 25, 2012 at 12:04 PM, David Hutto wrote:

> On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan 
> wrote:
> > Yes Dave ..You are right and my problem is solved now..  Thanx to all...
> >
> >
> > On Thu, Oct 25, 2012 at 11:55 AM, David Hutto 
> > wrote:
> >>
> >> On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan 
> >> wrote:
> >> >
> >> >
> >> >>
> >> >> Is this what you mean:
> >> >>
> >> list_0 = []
> >> list_1 = [i for i in range(0,5)]
> >> list_2 = [i for i in range(5,11)]
> >> list_0.append(list_1)
> >> list_0.append(list_2)
> >> >> >>> for i in list_2:
> >> >> ... list_1.append(i)
> >> >> ...
> >> >> >>> list_1
> >> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >> >>
> >> >>
> >> >> or would another example help you out more?
> >> >
> >> >
> >> > No but really sorry this is what I DONT WANT...
> >> >
> >> > The output I want to have is :
> >> >
> >> > [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
> >>
> >> Then, this is what you want :
> >>
> >> >>david@david-desktop:~$ python
> >> Python 2.7.3 (default, Aug  1 2012, 05:16:07)
> >> [GCC 4.6.3] on linux2
> >> Type "help", "copyright", "credits" or "license" for more information.
> >> >>> list_0 = []
> >> >>> list_1 = [i for i in range(0,5)]
> >> >>> list_2 = [i for i in range(5,11)]
> >> >>> list_0.append(list_1)
> >> >>> list_0.append(list_2)
> >> >>> list_0
> >> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
>
> if you have a format for the lists, there might be an easier way, or
> simpler. How are the lists coming in?
>
> The list are in form of
>
>  first list like [(x,y,z) ]
>
> and another as [(a,b,c), (a+i, b+i, c+i).]
>
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


SSH Connection with Python

2012-10-25 Thread Schneider

Hi Folkz,
how can i create a SSH-Connection with python? I have to send some 
commands to the remote host and parse their answers.

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


Re: can we append a list with another list in Python ?

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 6:12 AM, inshu chauhan  wrote:
>
>
> On Thu, Oct 25, 2012 at 12:04 PM, David Hutto 
> wrote:
>>
>> On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan 
>> wrote:
>> > Yes Dave ..You are right and my problem is solved now..  Thanx to all...
>> >
>> >
>> > On Thu, Oct 25, 2012 at 11:55 AM, David Hutto 
>> > wrote:
>> >>
>> >> On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan 
>> >> wrote:
>> >> >
>> >> >
>> >> >>
>> >> >> Is this what you mean:
>> >> >>
>> >> list_0 = []
>> >> list_1 = [i for i in range(0,5)]
>> >> list_2 = [i for i in range(5,11)]
>> >> list_0.append(list_1)
>> >> list_0.append(list_2)
>> >> >> >>> for i in list_2:
>> >> >> ... list_1.append(i)
>> >> >> ...
>> >> >> >>> list_1
>> >> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> >> >>
>> >> >>
>> >> >> or would another example help you out more?
>> >> >
>> >> >
>> >> > No but really sorry this is what I DONT WANT...
>> >> >
>> >> > The output I want to have is :
>> >> >
>> >> > [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
>> >>
>> >> Then, this is what you want :
>> >>
>> >> >>david@david-desktop:~$ python
>> >> Python 2.7.3 (default, Aug  1 2012, 05:16:07)
>> >> [GCC 4.6.3] on linux2
>> >> Type "help", "copyright", "credits" or "license" for more information.
>> >> >>> list_0 = []
>> >> >>> list_1 = [i for i in range(0,5)]
>> >> >>> list_2 = [i for i in range(5,11)]
>> >> >>> list_0.append(list_1) list_0 = [list_1,list_2]
>> >> >>> list_0.append(list_2)
>> >> >>> list_0
>> >> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
>>
>> if you have a format for the lists, there might be an easier way, or
>> simpler. How are the lists coming in?
>>
>> The list are in form of
>>
>>  first list like [(x,y,z) ]
>>
>> and another as [(a,b,c), (a+i, b+i, c+i).]
>>
This shows tuples though, not appended lists, which are immutable.


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
On Thu, Oct 25, 2012 at 12:17 PM, David Hutto wrote:

> On Thu, Oct 25, 2012 at 6:12 AM, inshu chauhan 
> wrote:
> >
> >
> > On Thu, Oct 25, 2012 at 12:04 PM, David Hutto 
> > wrote:
> >>
> >> On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan 
> >> wrote:
> >> > Yes Dave ..You are right and my problem is solved now..  Thanx to
> all...
> >> >
> >> >
> >> > On Thu, Oct 25, 2012 at 11:55 AM, David Hutto  >
> >> > wrote:
> >> >>
> >> >> On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan <
> insidesh...@gmail.com>
> >> >> wrote:
> >> >> >
> >> >> >
> >> >> >>
> >> >> >> Is this what you mean:
> >> >> >>
> >> >> list_0 = []
> >> >> list_1 = [i for i in range(0,5)]
> >> >> list_2 = [i for i in range(5,11)]
> >> >> list_0.append(list_1)
> >> >> list_0.append(list_2)
> >> >> >> >>> for i in list_2:
> >> >> >> ... list_1.append(i)
> >> >> >> ...
> >> >> >> >>> list_1
> >> >> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >> >> >>
> >> >> >>
> >> >> >> or would another example help you out more?
> >> >> >
> >> >> >
> >> >> > No but really sorry this is what I DONT WANT...
> >> >> >
> >> >> > The output I want to have is :
> >> >> >
> >> >> > [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
> >> >>
> >> >> Then, this is what you want :
> >> >>
> >> >> >>david@david-desktop:~$ python
> >> >> Python 2.7.3 (default, Aug  1 2012, 05:16:07)
> >> >> [GCC 4.6.3] on linux2
> >> >> Type "help", "copyright", "credits" or "license" for more
> information.
> >> >> >>> list_0 = []
> >> >> >>> list_1 = [i for i in range(0,5)]
> >> >> >>> list_2 = [i for i in range(5,11)]
> >> >> >>> list_0.append(list_1) list_0 = [list_1,list_2]
> >> >> >>> list_0.append(list_2)
> >> >> >>> list_0
> >> >> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
> >>
> >> if you have a format for the lists, there might be an easier way, or
> >> simpler. How are the lists coming in?
> >>
> >> The list are in form of
> >>
> >>  first list like [(x,y,z) ]
> >>
> >> and another as [(a,b,c), (a+i, b+i, c+i).]
> >>
> This shows tuples though, not appended lists, which are immutable.
>
> My mistake it is :
>
> The list are in form of
> >>
> >>  first list like [ [x,y,z] ]
> >>
> >> and another as  [  [a,b,c], [a+i, b+i, c+i] and so on .]
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SSH Connection with Python

2012-10-25 Thread Laszlo Nagy

On 2012-10-25 12:16, Schneider wrote:

Hi Folkz,
how can i create a SSH-Connection with python? I have to send some 
commands to the remote host and parse their answers.

greatz Johannes

http://www.lag.net/paramiko/

Another solution would be to use subprocess and/or pexpect


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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 09:21 schrieb Thomas Rachel:


I think

# iterate ad inf., because partial never returns None:
i1 = iter(partial(randrange, n), None)
# take the next value, make it None for breaking:
i2 = (j if j in selected else None for j in i1)
# and now, break on None:
i3 = iter(lambda: next(i2), None)

would do the job.


But, as I read it now again, it might be cleaner to create an own 
generator function, such as


def rand_values(randrange, n, selected):
# maybe: selected = set(selected) for the "not in"
while True:
val = partial(randrange, n)
if val not in selected: break
yield val

for value in rand_values(...):

or, for the general case proposed some posings ago:

def while_values(func, *a, **k):
while True:
val = func(*a, **k):
if not val: break
yield val

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


Re: SSH Connection with Python

2012-10-25 Thread Kamlesh Mutha
You can use paramiko module. Very easy to use.



On Thu, Oct 25, 2012 at 4:04 PM, Laszlo Nagy  wrote:

> On 2012-10-25 12:16, Schneider wrote:
>
>> Hi Folkz,
>> how can i create a SSH-Connection with python? I have to send some
>> commands to the remote host and parse their answers.
>> greatz Johannes
>>
> http://www.lag.net/paramiko/
>
> Another solution would be to use subprocess and/or pexpect
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>



-- 
Faith waiting in the heart of a seed promises a miracle of life which it
can not prove!
-Ravindranath Tagore
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 11:52:31 +0200, Thomas Rachel wrote:

> Am 25.10.2012 06:50 schrieb Terry Reedy:
> 
>> Keep in mind that any new syntax has to be a substantial improvement in
>> some sense or make something new possible. There was no new syntax in
>> 3.2 and very little in 3.3.
> 
> I would consinder this at least as new substantial than
> 
>  yield_from it
> 
> as opposed to
> 
>  for i in it: yield i
> 
> - although I think that was a good idea as well.

Then I think you have misunderstood the purpose of "yield from". The fact 
that you can replace the two lines:

for value in another_iterator:
yield iterator

with a one-liner "yield from another_iterator" is the least important use-
case for yield-from. If that was the only use-case, it probably would not 
have been allowed, because it adds complication to the language for a 
trivial gain.

The purpose of yield-from is to transfer control to another coroutine, 
not to save one trivial line of code.

[quote]
However, if the subgenerator is to interact properly with the caller in 
the case of calls to send(), throw() and close(), things become 
considerably more difficult. As will be seen later, the necessary code is 
very complicated, and it is tricky to handle all the corner cases 
correctly.

A new syntax will be proposed to address this issue. In the simplest use 
cases, it will be equivalent to the above for-loop, but it will also 
handle the full range of generator behaviour, and allow generator code to 
be refactored in a simple and straightforward way.
[end quote]

http://www.python.org/dev/peps/pep-0380/


"yield from" is a *huge* win in terms of correctness and power, not just 
a trivial saving in lines of code. "while expr as var" is not.


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


How to set 250000 baud rate in pyserial ?

2012-10-25 Thread kurabas
I use Arduino 1280 and Arduino 2560 under Fedora 15. 
1280 creates ttyUSB0 port  and can be set at 250 successfully.
2560 creates ttyACM0 port and can be only set at speeds from list (no 25) 
in pyserial. How to set 25 to ttyACM0 port?? Need I patch kernel or python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Antoon Pardon
On 25-10-12 06:50, Terry Reedy wrote:
> On 10/24/2012 7:19 PM, Evan Driscoll wrote:
>> On 10/24/2012 05:26 PM, Cameron Simpson wrote:
>>> But I'm still -0 on it, because it supplants the glaringly obvious:
>>>
>>>m = ...
>>>
>>> assignment with the far less in your face:
>>>
>>>possibly-long-expr as m
>>>
>>> and I think it would get quite heavily used, to the detriment of
>>> assignment readability in general. At present the nature of most
>>> effects
>>> is at the left. An assignment is obvious on the left, an
>>> if/with/while/etc
>>> is visible at the left.
>>
>> In the interest of brainstorming, what about
>>
>> while VAR from EXPR:
>>
>> or something like that? I don't think I like 'from' on a couple counts,
>> but there's probably some word that fits.
>
> The op wondered if these proposals have been made before. They have
> been, and have been rejected. Some of the discussion has been on
> python-ideas list. But go ahead and brainstorm and discuss.
>
> Keep in mind that any new syntax has to be a substantial improvement
> in some sense or make something new possible. There was no new syntax
> in 3.2 and very little in 3.3.

If I recal correctly at one point the following was accepted:

do:
suite
while expr:
suite

But it was later discarded because of lack of a champion or something
like that.

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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 12:50 schrieb Steven D'Aprano:


Then I think you have misunderstood the purpose of "yield from".


Seems so. As I have not yet switched to 3.x, I haven't used it till now.


[quote]
However, if the subgenerator is to interact properly with the caller in
the case of calls to send(), throw() and close(), things become
considerably more difficult. As will be seen later, the necessary code is
very complicated, and it is tricky to handle all the corner cases
correctly.


Ok, thanks.


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


Question about long-running web scripts

2012-10-25 Thread Gilles
Hello

I'd like to check something about running Python web applications.

Generally speaking, the reason scripts run faster when called through
FastCGI or the mod_* modules, is because the interpreter is already up
and running.
But when running PHP scripts, this does nothing about fetching the
file from disk, recompiling, rerunning it, and usually reconnecting to
the database.

OTOH, Python web scripts can be written as long-running scripts: In
this case, what is the added-value of using FastCGI? Why can't the web
server simply call the Python script directly, just like CGI?

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


Re: Question about long-running web scripts

2012-10-25 Thread Tim Golden
On 25/10/2012 12:45, Gilles wrote:
> I'd like to check something about running Python web applications.
> 
> Generally speaking, the reason scripts run faster when called
> through FastCGI or the mod_* modules, is because the interpreter is
> already up and running. But when running PHP scripts, this does
> nothing about fetching the file from disk, recompiling, rerunning it,
> and usually reconnecting to the database.
> 
> OTOH, Python web scripts can be written as long-running scripts: In 
> this case, what is the added-value of using FastCGI? Why can't the
> web server simply call the Python script directly, just like CGI?

(Your question is a little confused at the end. I'm choosing to
understand: why can't we just run Python one-shot, like CGI? The likely
alternative meaning is: why can't the incoming request be routed to an
already-running Python program -- which is not, of course, what CGI
generally does. Hence my confusion).

The answer is: it can. CGI is a protocol rather than anything else. In
front of a CGI exchange is the browser (or some other web client).
Behind it is some program which is capable of producing a valid HTTP
response, including a Python program.

It's perfectly possible to run a usable website against Python running
one-shot. You won't get terrific performance out of it, but for a
website which doesn't expect humungous amounts of traffic, it'll work fine.

The amount of time it takes a half-decent, even shared, server to start
up a Python process, connect to a database, pull stuff together, and
send a response will likely not impact on an average user's experience.
As long as too many of them don't try to do that at the same time.
Exactly where the line is drawn will depend on your particular hosting
solution, your assumed traffic, and your users' expectations as to
responsiveness.

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


Re: SSH Connection with Python

2012-10-25 Thread Roy Smith
In article ,
 Schneider  wrote:

> Hi Folkz,
> how can i create a SSH-Connection with python? I have to send some 
> commands to the remote host and parse their answers.
> greatz Johannes

At a low level, you want to look at the paramiko library.  Built on top 
of that, and adding hoards of neat functionality, is fabric.  One of 
these is likely to be what you're looking for.

http://www.lag.net/paramiko/

https://github.com/fabric/fabric
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about long-running web scripts

2012-10-25 Thread Gilles
On Thu, 25 Oct 2012 13:03:14 +0100, Tim Golden 
wrote:
>(Your question is a little confused at the end. I'm choosing to
>understand: why can't we just run Python one-shot, like CGI? The likely
>alternative meaning is: why can't the incoming request be routed to an
>already-running Python program -- which is not, of course, what CGI
>generally does. Hence my confusion).

Yes indeed. Sorry about the confusion.

But actually, I didn't mean one-shot scripts, where the Python
interpreter + script must be loaded each time, but rather: If I leave
a Python running in an endless loop, why not just use either CGI or
some other basic way to call the script instead of FastCGI?

In the case of PHP, FastCGI makes sense, but I don't see the benefit
for long-running Python scripts.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string format question

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Piet van Oostrum  wrote:
> Adrien  writes:
>
>> print "{:.3g}".format(2.356)  # this rounds up
>
> But:
>
 print "{:.3g}".format(12.356) 
> 12.4
 print "{:.3g}".format(123.356) 
> 123


  The precision is a decimal number indicating how many digits
  should be displayed after the decimal point for a floating
  point value formatted with 'f' and 'F', or before and after the
  decimal point for a floating point value formatted with 'g' or
  'G'. For non-number types the field indicates the maximum field
  size - in other words, how many characters will be used from
  the field content. The precision is not allowed for integer
  values.

So g will print a specific number of significant digits, so it
won't do what Adrien wants.

And f will print a fixed number of digits after the decimal
point, so it won't do want Adrien wants.

Adrien, you will need to do some post-processing on fixed point
output to remove trailing zeroes.

>>> print("{:.2f}".format(2.1).rstrip('0'))
2.1
>>> print("{:.2f}".format(2.127).rstrip('0'))
2.13

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


Re: Question about long-running web scripts

2012-10-25 Thread David Hutto
On Thu, Oct 25, 2012 at 8:03 AM, Tim Golden  wrote:
> On 25/10/2012 12:45, Gilles wrote:
>> I'd like to check something about running Python web applications.
>>
>> Generally speaking, the reason scripts run faster when called
>> through FastCGI or the mod_* modules, is because the interpreter is
>> already up and running. But when running PHP scripts, this does
>> nothing about fetching the file from disk, recompiling, rerunning it,
>> and usually reconnecting to the database.
>>

I'd say that is the same as py, unless it's a cron job to limit script
iterations

>> OTOH, Python web scripts can be written as long-running scripts: In
>> this case, what is the added-value of using FastCGI? Why can't the
>> web server simply call the Python script directly, just like CGI?

The server should call a the script, or script.sleep()

There are also server options to setup when a script is run, other
than a cron jo for php.

>
> (Your question is a little confused at the end. I'm choosing to
> understand: why can't we just run Python one-shot, like CGI? The likely
> alternative meaning is: why can't the incoming request be routed to an
> already-running Python program -- which is not, of course, what CGI
> generally does. Hence my confusion).
>
> The answer is: it can. CGI is a protocol rather than anything else. In
> front of a CGI exchange is the browser (or some other web client).
> Behind it is some program which is capable of producing a valid HTTP
> response, including a Python program.
>
> It's perfectly possible to run a usable website against Python running
> one-shot. You won't get terrific performance out of it, but for a
> website which doesn't expect humungous amounts of traffic, it'll work fine.
>
> The amount of time it takes a half-decent, even shared, server to start
> up a Python process, connect to a database, pull stuff together, and
> send a response will likely not impact on an average user's experience.
> As long as too many of them don't try to do that at the same time.
> Exactly where the line is drawn will depend on your particular hosting
> solution, your assumed traffic, and your users' expectations as to
> responsiveness.
>
> TJG
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about long-running web scripts

2012-10-25 Thread Tim Golden
On 25/10/2012 13:40, Gilles wrote:
> On Thu, 25 Oct 2012 13:03:14 +0100, Tim Golden 
> wrote:
>> (Your question is a little confused at the end. I'm choosing to
>> understand: why can't we just run Python one-shot, like CGI? The likely
>> alternative meaning is: why can't the incoming request be routed to an
>> already-running Python program -- which is not, of course, what CGI
>> generally does. Hence my confusion).
> 
> Yes indeed. Sorry about the confusion.
> 
> But actually, I didn't mean one-shot scripts, where the Python
> interpreter + script must be loaded each time, but rather: If I leave
> a Python running in an endless loop, why not just use either CGI or
> some other basic way to call the script instead of FastCGI?

In essence, you're describing FastCGI. A Python program (or, indeed, any
program) which uses FastCGI runs continuously and waits for the incoming
request on a TCP socket (instead of as a sys.stdin stream + env vars
immediately after process startup).

The key description is here:

  http://www.fastcgi.com/drupal/node/6?q=node/15

(The sections have no anchors; you're looking for the section titled "2.
FastCGI Interface")

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


RE: Fastest web framework

2012-10-25 Thread Andriy Kornatskyy

Web content caching is the most effective type of cache. This way your python 
handler is not executed to determine a valid response to user, instead one 
returned from cache. Since the operation is that simple, it should be the 
maximum possible speed your `real world` application capable to provide.

The web content caching benchmark is provided for two types of caching: memory 
and distributed. There is payed attention how gzip transform impact throughput 
of cached content. Read more here:

http://mindref.blogspot.com/2012/10/python-web-caching-benchmark.html

Your ability to utilize managed (semi-real time) caching is essential to be 
able run your `real world` at the speed of `hello world`. Read more here:

http://packages.python.org/wheezy.http/userguide.html#content-cache
http://packages.python.org/wheezy.web/tutorial.html

Compare throughput with numbers from the other post:

http://mindref.blogspot.com/2012/09/python-fastest-web-framework.html

Comments or suggestions are welcome.

Thanks.

Andriy Kornatskyy


> From: andriy.kornats...@live.com
> To: python-list@python.org
> Subject: Fastest web framework
> Date: Sun, 23 Sep 2012 12:19:16 +0300
>
>
> I have run recently a benchmark of a trivial 'hello world' application for 
> various python web frameworks (bottle, django, flask, pyramid, web.py, 
> wheezy.web) hosted in uWSGI/cpython2.7 and gunicorn/pypy1.9... you might find 
> it interesting:
>
> http://mindref.blogspot.com/2012/09/python-fastest-web-framework.html
>
> Comments or suggestions are welcome.
>
> Thanks.
>
> Andriy Kornatskyy
>
> --
> http://mail.python.org/mailman/listinfo/python-list
  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SSH Connection with Python

2012-10-25 Thread Demian Brecht

On 2012-10-25, at 3:16 AM, Schneider  wrote:

> how can i create a SSH-Connection with python? I have to send some commands 
> to the remote host and parse their answers.


I have yet to use it, but Fabric (http://docs.fabfile.org/en/1.4.3/) should 
have everything you're looking for. I've heard nothing but good things about it.

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




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


Re: while expression feature proposal

2012-10-25 Thread Grant Edwards
On 2012-10-24, Cameron Simpson  wrote:

>| I must say I really like the parity of Dan's
>|   while EXPR as VAR:
>|  BLOCK
>| proposal with the "with" statement.
>
> Well, it's nice. But usually EXPR will be a boolean.

I guess that depends on what sort of programs you write.  In my
experience, EXPR is usually a read from a file/socket/pipe that
returns '' on EOF. If VAR is not '', then you process, then you
process it inside the loop.

-- 
Grant Edwards   grant.b.edwardsYow! We're going to a
  at   new disco!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Grant Edwards
On 2012-10-25, Terry Reedy  wrote:

> The op wondered if these proposals have been made before. They have 
> been, and have been rejected. Some of the discussion has been on 
> python-ideas list. But go ahead and brainstorm and discuss.
>
> Keep in mind that any new syntax has to be a substantial improvement in 
> some sense or make something new possible. There was no new syntax in 
> 3.2 and very little in 3.3.

I think the new syntax should be introduced in 2.00.  There were a
number of other big changes between 1.52 and 2.00, so that seems like
a good spot to put this change.

-- 
Grant Edwards   grant.b.edwardsYow! !!  I am having fun!!!
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: resume execution after catching with an excepthook?

2012-10-25 Thread andrea crotti
2012/10/25 Steven D'Aprano :
> On Wed, 24 Oct 2012 13:51:30 +0100, andrea crotti wrote:
>
>> So I would like to be able to ask for confirmation when I receive a C-c,
>> and continue if the answer is "N/n".
>
> I don't think there is any way to do this directly.
>
> Without a try...except block, execution will cease after an exception is
> caught, even when using sys.excepthook. I don't believe that there is any
> way to jump back to the line of code that just failed (and why would you,
> it will just fail again) or the next line (which will likely fail because
> the previous line failed).
>
> I think the only way you can do this is to write your own execution loop:
>
> while True:
> try:
> run(next_command())
> except KeyboardInterrupt:
> if confirm_quit():
> break
>
>
> Of course you need to make run() atomic, or use transactions that can be
> reverted or backed out of. How plausible this is depends on what you are
> trying to do -- Python's Ctrl-C is not really designed to be ignored.
>
> Perhaps a better approach would be to treat Ctrl-C as an unconditional
> exit, and periodically poll the keyboard for another key press to use as
> a conditional exit. Here's a snippet of platform-specific code to get a
> key press:
>
> http://code.activestate.com/recipes/577977
>
> Note however that it blocks if there is no key press waiting.
>
> I suspect that you may need a proper event loop, as provided by GUI
> frameworks, or curses.
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list



Ok thanks, but here the point is not to resume something that is going
to fail again, just to avoid accidental kill of processes that take a
long time.  Probably needed only by me in debugging mode, but anyway I
can do the simple try/except then, thanks..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: resume execution after catching with an excepthook?

2012-10-25 Thread Chris Angelico
On Thu, Oct 25, 2012 at 12:15 PM, Steven D'Aprano
 wrote:
> I don't believe that there is any
> way to jump back to the line of code that just failed (and why would you,
> it will just fail again)

There are several reasons to retry something after an exception,
mainly if some external state gets changed. Virtual memory is usually
implemented using traps, so the OS handles an interrupt by paging
something in from the disk, then retrying the "failing" instruction.
The old-favorite "Abort, retry, ignore[, fail]?" prompt from DOS has
the same notion; you tried to save a file onto a write-protected
floppy disk, an exception is thrown, you handle the exception by
getting the user to unprotect or change disks, and you resume where
you left off. CPU-level interrupts always have a return address for
that exact reason.

Handling Ctrl-C in this way makes a lot of sense. Give the user the
option to try to abort, but then to optionally change his/her mind and
keep going. Or possibly have a third option: break out of the current
operation and go back to some primary loop (throw the exception and
let it be caught at the main loop).

Arguably the problem here is that KeyboardInterrupt is an exception.
Perhaps a more classic signal handling structure should be used: when
signal received, call function. That function then has the power to
raise an exception, which will propagate through whatever code is
currently executing. This sort of thing would have all the usual
dangers of signal handlers, though; you have NO IDEA what state the
program's in, so you have to be ubercareful of what globals you use or
change.

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


bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an 
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit 
set?  I'd prefer to use integers, as I'm probably going to need 
thousands of these, if the tests work out.  But before I can test, I 
need a decent bit counter.  (shift, xor, &, and | are already present 
for integer values, but I also need to count the number of "true" items 
after the logical operation.  So if a bitset is the correct approach, 
I'll need it to implement those operations, or their equivalents in 
terms of union and intersection.)


Or do I need to drop into C for this?

--
Charles Hixson

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


Re: bit count or bit set && Python3

2012-10-25 Thread MRAB

On 2012-10-25 15:47, Charles Hixson wrote:

In Python3 is there any good way to count the number of on bits in an
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit
set?  I'd prefer to use integers, as I'm probably going to need
thousands of these, if the tests work out.  But before I can test, I
need a decent bit counter.  (shift, xor, &, and | are already present
for integer values, but I also need to count the number of "true" items
after the logical operation.  So if a bitset is the correct approach,
I'll need it to implement those operations, or their equivalents in
terms of union and intersection.)

Or do I need to drop into C for this?


There's a nice algorithm for counting the ones. It takes one iteration
per set bit:

def count_set_bits(number):
count = 0
while number:
count += 1
number &= number - 1
return count

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


Re: bit count or bit set && Python3

2012-10-25 Thread Christian Heimes
Am 25.10.2012 16:47, schrieb Charles Hixson:
> In Python3 is there any good way to count the number of on bits in an
> integer (after an & operation)?


Simple, easy, faster than a Python loop but not very elegant:

   bin(number).count("1")

Christian


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


Re: bit count or bit set && Python3

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes  wrote:
> Simple, easy, faster than a Python loop but not very elegant:
>
>bin(number).count("1")

Unlikely to be fast.

What you may want is some sort of hybrid loop/lookup approach. Do you
know what your highest bit number is going to be? For instance, are
all your integers 32-bit? You could use something like this:

c = bitcount[n&255] + bitcount[n>>8&255] + bitcount[n>>16&255] + bitcount[n>>24]

where bitcount is a list of 256 values, giving the counts for each
value from 0 to 255.

Profile and test. :)

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


Re: resume execution after catching with an excepthook?

2012-10-25 Thread Hans Mulder
On 24/10/12 14:51:30, andrea crotti wrote:
> So I would like to be able to ask for confirmation when I receive a C-c,
> and continue if the answer is "N/n".
> 
> I'm already using an exception handler set with sys.excepthook, but I
> can't make it work with the confirm_exit, because it's going to quit in
> any case..
> 
> A possible solution would be to do a global "try/except
> KeyboardInterrupt", but since I already have an excepthook I wanted to
> use this.  Any way to make it continue where it was running after the
> exception is handled?
> 
> 
> def confirm_exit():
> while True:
> q = raw_input("This will quit the program, are you sure? [y/N]")
> if q in ('y', 'Y'):
> sys.exit(0)
> elif q in ('n', 'N'):
> print("Continuing execution")
> # just go back to normal execution, is it possible??
> break
> 
> 
> def _exception_handler(etype, value, tb):
> if etype == KeyboardInterrupt:
> confirm_exit()
> else:
> sys.exit(1)
> 
> 
> def set_exception_handler():
> sys.excepthook = _exception_handler

I think the trick is to not use an except hook, but trap the
interrupt on a lower level.

This seems to work; I'm not sure how robust it is:

import signal

def handler(signum, frame):
while True:
q = raw_input("This will quit the program, are you sure? [y/N]")
if q[:1] in "yY":
raise KeyboardInterrupt
elif q[:1] in "nN":
print("Continuing execution")
# just go back to normal execution
return

signal.signal(signal.SIGINT, handler)


If you're debugging this on a Unix platform, it may help to know
that you can also kill a process with control-\

Hope this helps,

-- HansM


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


Re: resume execution after catching with an excepthook?

2012-10-25 Thread Steven D'Aprano
On Fri, 26 Oct 2012 01:51:43 +1100, Chris Angelico wrote:

> On Thu, Oct 25, 2012 at 12:15 PM, Steven D'Aprano
>  wrote:
>> I don't believe that there is any
>> way to jump back to the line of code that just failed (and why would
>> you, it will just fail again)
> 
> There are several reasons to retry something after an exception, 

I'm sure there are, but you're taking my point out of context. 

Andrea described his problem as *continuing*, not *re-trying*. I 
understand that re-trying operations is useful:

while True:
try:
some_operation()
except SomeException:
if not retry():
break  # or raise an exception, or return


but I wouldn't describe that as "continuing", as Andrea did. I understand 
that as:

try:
operation(1)
operation(2)
operation(3)
operation(4)
# and so forth...
except SomeException:
if retry():
# Magically jump back to the operation that was
# active when the exception occurred.
magic_happens_here()


If you could guarantee that each operation(N) was atomic ("all or 
nothing" -- it either succeeds, or has no effect) then such a feature 
would be useful. But as far as I know, you can't jump back into a try 
block from the except block, and even if you could, what's to stop the 
operation from failing again and again and again?

In Andrea's case, the failure he is worried about is "oops, I hit Ctrl-C 
when I actually wanted to not hit Ctrl-C", so presumably the failure 
wouldn't reoccur if you could jump backwards. But in any case, I can see 
no obvious way to make it work.

The python debugger pdb has a "jump" command that allows you to step 
backwards and re-execute code under certain conditions, so perhaps it is 
not quite impossible.

(I'm tempted to reply that the actual solution to this problem of 
accidentally hitting Ctrl-C is "well don't do that then".)



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


Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:

> On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes 
> wrote:
>> Simple, easy, faster than a Python loop but not very elegant:
>>
>>bin(number).count("1")
> 
> Unlikely to be fast.

Oh I don't know about that. Here's some timing results using Python 2.7:

py> from timeit import Timer
py> t = Timer('bin(number).count("1")', setup='number=2**10001-1')
py> min(t.repeat(number=1, repeat=7))
0.6819710731506348

Compare to MRAB's suggestion:

def count_set_bits(number):
 count = 0
 while number:
 count += 1
 number &= number - 1
 return count

py> t = Timer('count_set_bits(number)', 
... setup='from __main__ import count_set_bits; number=2**10001-1')
py> min(t.repeat(number=100, repeat=7))
4.141788959503174


That makes the "inelegant" solution using bin() and count() about 600 
times faster than the mathematically clever solution using bitwise 
operations.

On the other hand, I'm guessing that PyPy would speed up MRAB's version 
significantly.



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


Re: resume execution after catching with an excepthook?

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 2:31 AM, Hans Mulder  wrote:
> This seems to work; I'm not sure how robust it is:
>
> import signal
>
> def handler(signum, frame):
> while True:
> q = raw_input("This will quit the program, are you sure? [y/N]")
> if q[:1] in "yY":
> raise KeyboardInterrupt
> elif q[:1] in "nN":
> print("Continuing execution")
> # just go back to normal execution
> return
>
> signal.signal(signal.SIGINT, handler)
>

Yes, that's what I was talking about. You do have to be fairly careful
what you do (for instance, what should happen if the user hits Ctrl-C
during handler()? Default is that it'll raise KeyboardInterrupt
unconditionally), but you have perfect flexibility.

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


Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 7:56 pm, Charles Hixson  wrote:
> In Python3 is there any good way to count the number of on bits in an
> integer (after an & operation)?
> Alternatively, is there any VERY light-weight implementation of a bit
> set?  I'd prefer to use integers, as I'm probably going to need
> thousands of these, if the tests work out.  But before I can test, I
> need a decent bit counter.  (shift, xor, &, and | are already present
> for integer values, but I also need to count the number of "true" items
> after the logical operation.  So if a bitset is the correct approach,
> I'll need it to implement those operations, or their equivalents in
> terms of union and intersection.)
>
> Or do I need to drop into C for this?
>
> --
> Charles Hixson

You may have already checked it out and find it unsuitable...
I guess you know that python has good implementation of sets
http://docs.python.org/library/sets.html ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 8:57 pm, Steven D'Aprano  wrote:
> On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:
> > On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes 
> > wrote:
> >> Simple, easy, faster than a Python loop but not very elegant:
>
> >>    bin(number).count("1")
>
> > Unlikely to be fast.
>
> Oh I don't know about that. Here's some timing results using Python 2.7:
>
> py> from timeit import Timer
> py> t = Timer('bin(number).count("1")', setup='number=2**10001-1')
> py> min(t.repeat(number=1, repeat=7))
> 0.6819710731506348
>
> Compare to MRAB's suggestion:
>
> def count_set_bits(number):
>      count = 0
>      while number:
>          count += 1
>          number &= number - 1
>      return count
>
> py> t = Timer('count_set_bits(number)',
> ...     setup='from __main__ import count_set_bits; number=2**10001-1')
> py> min(t.repeat(number=100, repeat=7))
> 4.141788959503174
>
> That makes the "inelegant" solution using bin() and count() about 600
> times faster than the mathematically clever solution using bitwise
> operations.

You meant 600% I think?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence

On 25/10/2012 15:47, Charles Hixson wrote:

In Python3 is there any good way to count the number of on bits in an
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit
set?  I'd prefer to use integers, as I'm probably going to need
thousands of these, if the tests work out.  But before I can test, I
need a decent bit counter.  (shift, xor, &, and | are already present
for integer values, but I also need to count the number of "true" items
after the logical operation.  So if a bitset is the correct approach,
I'll need it to implement those operations, or their equivalents in
terms of union and intersection.)

Or do I need to drop into C for this?



If needed bitarray and bitstring are available on pypi.

--
Cheers.

Mark Lawrence.

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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 16:15 schrieb Grant Edwards:


I guess that depends on what sort of programs you write.  In my
experience, EXPR is usually a read from a file/socket/pipe that
returns '' on EOF. If VAR is not '', then you process, then you
process it inside the loop.


Right. The same as in

if regex.search(string) as match:
process it

But with

def if_true(expr):
if expr: yield expr

you can do

for match in if_true(regex.search(string)):
process it

But the proposed if ... as ...: statment woulkd be more beautiful by far.

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


Re: bit count or bit set && Python3

2012-10-25 Thread Chris Angelico
On Fri, Oct 26, 2012 at 3:17 AM, rusi  wrote:
> On Oct 25, 8:57 pm, Steven D'Aprano  +comp.lang.pyt...@pearwood.info> wrote:
>> py> min(t.repeat(number=1, repeat=7))
>> 0.6819710731506348
>> py> min(t.repeat(number=100, repeat=7))
>> 4.141788959503174
>>
>> That makes the "inelegant" solution using bin() and count() about 600
>> times faster than the mathematically clever solution using bitwise
>> operations.
>
> You meant 600% I think?

It took six times longer to do one hundredth the iterations.

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


Re: bit count or bit set && Python3

2012-10-25 Thread Dan Stromberg
On Thu, Oct 25, 2012 at 9:24 AM, Mark Lawrence wrote:

> On 25/10/2012 15:47, Charles Hixson wrote:
>
>> In Python3 is there any good way to count the number of on bits in an
>> integer (after an & operation)?
>> Alternatively, is there any VERY light-weight implementation of a bit
>> set?  I'd prefer to use integers, as I'm probably going to need
>> thousands of these, if the tests work out.  But before I can test, I
>> need a decent bit counter.  (shift, xor, &, and | are already present
>> for integer values, but I also need to count the number of "true" items
>> after the logical operation.  So if a bitset is the correct approach,
>> I'll need it to implement those operations, or their equivalents in
>> terms of union and intersection.)
>>
>> Or do I need to drop into C for this?
>>
>>
> If needed bitarray and bitstring are available on pypi.
>

There's also my bits_mod.py:  http://stromberg.dnsalias.org/svn/bits/trunk/

I separated it out of my bloom filter code recently, to facilitate a prime
number sieve.

Behind the scenes it's an array of int's.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 1:21 AM, Thomas Rachel

wrote:
>> j = next(j for j in iter(partial(randrange, n), None) if j not in
>> selected)
>
>
> This generator never ends. If it meets a non-matching value, it just skips
> it and goes on.

next() only returns one value.  After it is returned, the generator is
discarded, whether it has ended or not.  If there were no valid values
for randrange to select, then it would descend into an infinite loop.
But then, so would the dropwhile and the original while loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence

On 25/10/2012 17:29, Chris Angelico wrote:

On Fri, Oct 26, 2012 at 3:17 AM, rusi  wrote:

On Oct 25, 8:57 pm, Steven D'Aprano  wrote:

py> min(t.repeat(number=1, repeat=7))
0.6819710731506348
py> min(t.repeat(number=100, repeat=7))
4.141788959503174

That makes the "inelegant" solution using bin() and count() about 600
times faster than the mathematically clever solution using bitwise
operations.


You meant 600% I think?


It took six times longer to do one hundredth the iterations.

ChrisA



Oh no, not another PEP 393 foul up :)

--
Cheers.

Mark Lawrence.

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


Re: bit count or bit set && Python3

2012-10-25 Thread rusi
On Oct 25, 9:30 pm, Chris Angelico  wrote:
> On Fri, Oct 26, 2012 at 3:17 AM, rusi  wrote:
> > On Oct 25, 8:57 pm, Steven D'Aprano  > +comp.lang.pyt...@pearwood.info> wrote:
> >> py> min(t.repeat(number=1, repeat=7))
> >> 0.6819710731506348
> >> py> min(t.repeat(number=100, repeat=7))
> >> 4.141788959503174
>
> >> That makes the "inelegant" solution using bin() and count() about 600
> >> times faster than the mathematically clever solution using bitwise
> >> operations.
>
> > You meant 600% I think?
>
> It took six times longer to do one hundredth the iterations.
>
> ChrisA

Oh! Missed the number=
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 3:52 AM, Thomas Rachel

wrote:
> Am 25.10.2012 06:50 schrieb Terry Reedy:
>
>
>> Keep in mind that any new syntax has to be a substantial improvement in
>> some sense or make something new possible. There was no new syntax in
>> 3.2 and very little in 3.3.
>
>
> I would consinder this at least as new substantial than
>
> yield_from it
>
> as opposed to
>
> for i in it: yield i
>
> - although I think that was a good idea as well.

Except that those two are not exactly identical, because "yield from"
also properly delegates sent data and exceptions to the sub-generator.
 The actual equivalent code for "yield from expr()", as given in the
PEP, is 39 lines long.  This is a substantial feature, not just a
little syntactic sugar.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while expression feature proposal

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 10:36 AM, Ian Kelly  wrote:
> On Thu, Oct 25, 2012 at 1:21 AM, Thomas Rachel
> 
> wrote:
>>> j = next(j for j in iter(partial(randrange, n), None) if j not in
>>> selected)
>>
>>
>> This generator never ends. If it meets a non-matching value, it just skips
>> it and goes on.
>
> next() only returns one value.  After it is returned, the generator is
> discarded, whether it has ended or not.  If there were no valid values
> for randrange to select, then it would descend into an infinite loop.
> But then, so would the dropwhile and the original while loop.

To demonstrate that the code does in fact return:

>>> selected = set(range(5))
>>> n = 10
>>> from functools import partial
>>> from random import randrange
>>> j = next(j for j in iter(partial(randrange, n), None) if j not in selected)
>>> j
5
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SSH Connection with Python

2012-10-25 Thread Peter Pearson
On Thu, 25 Oct 2012 12:16:58 +0200, Schneider  wrote:
> how can i create a SSH-Connection with python? I have to send some 
> commands to the remote host and parse their answers.
> greatz Johannes

I've been using Twisted (twistedmatrix.com).  It is especially
convenient for the server end.  Its organization is "event-driven",
which you may or may not find convenient.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 09:17:40 -0700, rusi wrote:

> On Oct 25, 8:57 pm, Steven D'Aprano  +comp.lang.pyt...@pearwood.info> wrote:
>> On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:
>> > On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes
>> >  wrote:
>> >> Simple, easy, faster than a Python loop but not very elegant:
>>
>> >>    bin(number).count("1")
>>
>> > Unlikely to be fast.
>>
>> Oh I don't know about that. Here's some timing results using Python
>> 2.7:
>>
>> py> from timeit import Timer
>> py> t = Timer('bin(number).count("1")', setup='number=2**10001-1') 
>> py> min(t.repeat(number=1, repeat=7)) 
>> 0.6819710731506348
>>
>> Compare to MRAB's suggestion:
>>
>> def count_set_bits(number):
>>      count = 0
>>      while number:
>>          count += 1
>>          number &= number - 1
>>      return count
>>
>> py> t = Timer('count_set_bits(number)', 
>> ...     setup='from __main__ import count_set_bits;
>> ... number=2**10001-1') 
>> py> min(t.repeat(number=100, repeat=7)) 
>> 4.141788959503174
>>
>> That makes the "inelegant" solution using bin() and count() about 600
>> times faster than the mathematically clever solution using bitwise
>> operations.
> 
> You meant 600% I think?

No, I mean a factor of 600 times faster. Look at the number of iterations 
used in each test: 1 versus 100.

Using bin() and count() takes 0.6819710731506348/1 = 6.8e-5 seconds 
on my computer; using MRAB's neat trick takes 4.141788959503174/100 = 
0.041 seconds. 0.041/6.8e-5 is slightly over 600.


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


Re: bit count or bit set && Python3

2012-10-25 Thread Christian Gollwitzer

Am 25.10.12 16:47, schrieb Charles Hixson:

In Python3 is there any good way to count the number of on bits in an
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit
set?  I'd prefer to use integers, as I'm probably going to need
thousands of these, if the tests work out.  But before I can test, I
need a decent bit counter.  (shift, xor, &, and | are already present
for integer values, but I also need to count the number of "true" items
after the logical operation.  So if a bitset is the correct approach,
I'll need it to implement those operations, or their equivalents in
terms of union and intersection.)

Or do I need to drop into C for this?



There is a very geeky algorithm with only a few integer operations.

Checkout
http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64

for a C version. Maybe the same thing is equally fast when ported to 
Python.


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


Re: bit count or bit set && Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 11:25 AM, Christian Gollwitzer  wrote:
> There is a very geeky algorithm with only a few integer operations.
>
> Checkout
> http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSet64
>
> for a C version. Maybe the same thing is equally fast when ported to Python.

It does seem to be about five times faster than the loop, but still
only half as fast as the string-counting approach.  I'm using a 64-bit
CPU but only a 32-bit OS, but it probably doesn't make much difference
anyway for Python, which is going to do the computations with Python
longs rather than with 64-bit C integers.

Also, this approach is a bit limited in that it only works for 32-bit
ints, so you can't pass it an aribtrary precision int and expect
correct results.

By the way, I expect the reason that Steve saw a 600x difference and
I'm only getting a 10x difference is because he was testing 1-bit
ints with expensive "long" operations, whereas I'm using 32-bit ints
that aren't too far removed from the C level.


>>> from timeit import Timer
>>> t = Timer("bin(number).count('1')", setup="number=2**31-1")
>>> min(t.repeat(number=100, repeat=7))
0.634620810749
>>> def count_set_bits(number):
...   count = 0
...   while number:
... count += 1
... number &= number - 1
...   return count
...
>>> t = Timer("count_set_bits(number)", setup="from __main__ import count_set_bi
ts; number = 2 ** 31-1")
>>> min(t.repeat(number=10, repeat=7))
0.5898140685478239
>>> def count_set_bits_geeky(number):
... c = ((number & 0xfff) * 0x1001001001001 & 0x84210842108421) % 0x1f
... c += (((number & 0xfff000) >> 12) * 0x1001001001001 & 0x84210842108421)
% 0x1f
... c += ((number >> 24) * 0x1001001001001 & 0x84210842108421) % 0x1f
... return c
...
>>> t = Timer("count_set_bits_geeky(number)", setup="from __main__ import count_
set_bits_geeky; number = 2 ** 31-1")
>>> min(t.repeat(number=10, repeat=7))
0.1247119396459766
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Appending a list using list obtained from a class

2012-10-25 Thread Prasad, Ramit
Demian Brecht wrote:
> On 2012-10-24, at 8:00 AM, inshu chauhan  wrote:
> 
> > Yes, a Class method returns a list. I am trying to append this in main() to 
> > make another list.
> > But the list i am getting after appending i showing addresses like this 
> > '<__main__.Point object at
> 0x0254FAB0>' but if i print the same in the same loop its showing me numbers 
> which i want. Why I dont know ??
> 
> If you can, please post the relevant blocks of code. That'll be a tremendous 
> help in figuring out your problem.
> 

I would like to re-iterate that posting the relevant code is very
helpful and the best way for us to help you. Everything below is an
educated guess based on the information you provided.


Based on the text '<__main__.Point object at 0x0254FAB0>', it seems
like the object being returned is most likely NOT a list but instead a 
Point class that is iterable. Either that or it is a list of Point 
objects. To find out exactly, try something like this:

lst = # whatever your class is returning.
print 'LIST - type : ', type(lst) '\trepr: ', repr(lst), '\tstr :', str(lst)
for p in lst[-1:]:
print 'POINT - type: ', type(p) '\trepr: ', repr(p), '\tstr :', str(p)

My suspicion is that when you print the list containing
points/addresses it prints the repr version. When you print the 
points/addresses themselves it prints the str version.

>>> class t(object):
... def __str__(self):
... return 'str'
... def __repr__(self):
... return 'repr'
... 
>>> a = t()
>>> print [a] 
[repr]
>>> print a
str
>>> print a.__class__.__base__.__repr__(a)
<__pieshell__.t object at 0x11B28F10>

Hope that helps you to understand what is going on.

Ramit Prasad

P.S. Usually it is best to specify, Python version, OS version,
expected input/output, and the smallest code sample you can provide
that shows the problem (that any reader of your message can run).
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Serhiy Storchaka

Chris Angelico's suggestion:

>>> bitcount = bytes(bin(i).count("1") for i in range(256))
>>> t = Timer('sum(number.to_bytes((number.bit_length() + 7) // 8,'
... '"little").translate(bitcount))',
... setup='from __main__ import bitcount; number=2**10001-1')
>>> min(t.repeat(number=1, repeat=7))


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


Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Steven D'Aprano  wrote:
> On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:
>> On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes
>> 
>> wrote:
>>> Simple, easy, faster than a Python loop but not very elegant:
>>>
>>>bin(number).count("1")
>> 
>> Unlikely to be fast.
>
> Oh I don't know about that.

Yes indeed! Python string operations are fast enough and its
arithmetic slow enough that I no longer assume I can beat a neat
lexicographical solution. Try defeating the following with
arithmetic:

def is_palindrom(n):
   s = str(n)
   return s = s[::-1]

> Here's some timing results using Python 2.7:

Excellent work.

You can of course drop to C for arithmetic and likely triumph
over Python strings. That's never been applicable for me, though.

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


Re: bit count or bit set && Python3

2012-10-25 Thread Neil Cerutti
On 2012-10-25, Neil Cerutti  wrote:
> Try defeating the following with arithmetic:
>
> def is_palindrom(n):
>s = str(n)
>return s = s[::-1]

Sorry for the typos. It should've been:

def is_palindrome(n):
   s = str(n)
   return s == s[::-1]

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


Re: while expression feature proposal

2012-10-25 Thread Terry Reedy

On 10/25/2012 6:50 AM, Steven D'Aprano wrote:

On Thu, 25 Oct 2012 11:52:31 +0200, Thomas Rachel wrote:


Am 25.10.2012 06:50 schrieb Terry Reedy:


Keep in mind that any new syntax has to be a substantial improvement in
some sense or make something new possible. There was no new syntax in
3.2 and very little in 3.3.


I would consinder this at least as new substantial than

  yield_from it

as opposed to

  for i in it: yield i

- although I think that was a good idea as well.


Then I think you have misunderstood the purpose of "yield from". The fact
that you can replace the two lines:

for value in another_iterator:
 yield iterator

with a one-liner "yield from another_iterator" is the least important use-
case for yield-from. If that was the only use-case, it probably would not
have been allowed, because it adds complication to the language for a
trivial gain.

The purpose of yield-from is to transfer control to another coroutine,
not to save one trivial line of code.

[quote]
However, if the subgenerator is to interact properly with the caller in
the case of calls to send(), throw() and close(), things become
considerably more difficult. As will be seen later, the necessary code is
very complicated, and it is tricky to handle all the corner cases
correctly.

A new syntax will be proposed to address this issue. In the simplest use
cases, it will be equivalent to the above for-loop, but it will also
handle the full range of generator behaviour, and allow generator code to
be refactored in a simple and straightforward way.
[end quote]

http://www.python.org/dev/peps/pep-0380/


"yield from" is a *huge* win in terms of correctness and power, not just
a trivial saving in lines of code. "while expr as var" is not.


r = yield from g

is equivalent to about 40 lines of code as given here
http://www.python.org/dev/peps/pep-0380/#formal-semantics

It took the developers several tries to first get a version that worked 
and then to work out the exact details.


--
Terry Jan Reedy

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


Re: bit count or bit set && Python3

2012-10-25 Thread Terry Reedy

On 10/25/2012 12:13 PM, rusi wrote:

On Oct 25, 7:56 pm, Charles Hixson  wrote:

In Python3 is there any good way to count the number of on bits in an
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit
set?  I'd prefer to use integers, as I'm probably going to need
thousands of these, if the tests work out.  But before I can test, I
need a decent bit counter.  (shift, xor, &, and | are already present
for integer values, but I also need to count the number of "true" items
after the logical operation.  So if a bitset is the correct approach,
I'll need it to implement those operations, or their equivalents in
terms of union and intersection.)

Or do I need to drop into C for this?


If I wanted 1000s of fast limited-length bitsets, I would consider using 
Cython to make an extension class.


--
Terry Jan Reedy

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


Re: bit count or bit set && Python3

2012-10-25 Thread Ian Kelly
On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti  wrote:
> Yes indeed! Python string operations are fast enough and its
> arithmetic slow enough that I no longer assume I can beat a neat
> lexicographical solution. Try defeating the following with
> arithmetic:
>
> def is_palindrom(n):
>s = str(n)
>return s = s[::-1]

Problems like these are fundamentally string problems, not math
problems.  The question being asked isn't about some essential
property of the number,  but about its digital representation.
Certainly they can be reasoned about mathematically, but the fact
remains that the math being done is about the properties of strings.
-- 
http://mail.python.org/mailman/listinfo/python-list


bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an 
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit 
set?  I'd prefer to use integers, as I'm probably going to need 
thousands of these, if the tests work out.  But before I can test, I 
need a decent bit counter.  (shift, xor, &, and | are already present 
for integer values, but I also need to count the number of "true" items 
after the logical operation.  So if a bitset is the correct approach, 
I'll need it to implement those operations, or their equivalents in 
terms of union and intersection.)


Or do I need to drop into C for this?

--
Charles Hixson

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


Re: while expression feature proposal

2012-10-25 Thread Thomas Rachel

Am 25.10.2012 18:36 schrieb Ian Kelly:

On Thu, Oct 25, 2012 at 1:21 AM, Thomas Rachel

wrote:

j = next(j for j in iter(partial(randrange, n), None) if j not in
selected)



This generator never ends. If it meets a non-matching value, it just skips
it and goes on.


next() only returns one value.  After it is returned, the generator is
discarded, whether it has ended or not.  If there were no valid values
for randrange to select, then it would descend into an infinite loop.
But then, so would the dropwhile and the original while loop.


You are completely right. My solution was right as well, but didn't 
match the problem...


Yours does indeed return one random value which is guaranteed not to be 
in selected.


Mine returns random values until the value is not in selected. I just 
misread the intention behind the while loop...



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


RE: resume execution after catching with an excepthook?

2012-10-25 Thread Prasad, Ramit
andrea crotti wrote:
> 2012/10/25 Steven D'Aprano :
> > On Wed, 24 Oct 2012 13:51:30 +0100, andrea crotti wrote:
> >
[snip]
> > Without a try...except block, execution will cease after an exception is
> > caught, even when using sys.excepthook. I don't believe that there is any
> > way to jump back to the line of code that just failed (and why would you,
> > it will just fail again) or the next line (which will likely fail because
> > the previous line failed).
> >
> > I think the only way you can do this is to write your own execution loop:
> >
> > while True:
> > try:
> > run(next_command())
> > except KeyboardInterrupt:
> > if confirm_quit():
> > break
> >
> >
> > Of course you need to make run() atomic, or use transactions that can be
> > reverted or backed out of. How plausible this is depends on what you are
> > trying to do -- Python's Ctrl-C is not really designed to be ignored.
> >
> > Perhaps a better approach would be to treat Ctrl-C as an unconditional
> > exit, and periodically poll the keyboard for another key press to use as
> > a conditional exit. Here's a snippet of platform-specific code to get a
> > key press:
> >
> > http://code.activestate.com/recipes/577977
> >
> > Note however that it blocks if there is no key press waiting.
> >
> > I suspect that you may need a proper event loop, as provided by GUI
> > frameworks, or curses.
> >
> 
> Ok thanks, but here the point is not to resume something that is going
> to fail again, just to avoid accidental kill of processes that take a
> long time.  Probably needed only by me in debugging mode, but anyway I
> can do the simple try/except then, thanks..

On the other hand, if you store state externally (pickle?) maybe 
you can just restart at the last "check point". That way even if
the program dies you can recover on the next run.

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Rivka Miller
Hello Programmers,

I am looking for a regexp for a string not at the beginning of the
line.

For example, I want to find $hello$ that does not occur at the
beginning of the string, ie all $hello$ that exclude ^$hello$.

In addition, if you have a more difficult problem along the same
lines, I would appreciate it. For a single character, eg < not at the
beginning of the line, it is easier, ie

^[^<]+<

but I cant use the same method for more than one character string as
permutation is present and probably for more than one occurrence,
greedy or non-greedy version of [^<]+ would pick first or last but not
the middle ones, unless I break the line as I go and use the non-
greedy version of +. I do have the non-greedy version available, but
what if I didnt?

If you cannot solve the problem completely, just give me a quick
solution with the first non beginning of the line and I will go from
there as I need it in a hurry.

Thanks


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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Janis Papanagnou
On 25.10.2012 22:53, Rivka Miller wrote:
> Hello Programmers,
> 
> I am looking for a regexp for a string not at the beginning of the
> line.
> 
> For example, I want to find $hello$ that does not occur at the
> beginning of the string, ie all $hello$ that exclude ^$hello$.

   .hello

The dot represents any character. But for specific strings that
needs adjustments (e.g. looking for hh not at the beginning of a
line would require something like ^[^h]+hh - ah, well, you wrote
something similar below).

Janis

> 
> In addition, if you have a more difficult problem along the same
> lines, I would appreciate it. For a single character, eg < not at the
> beginning of the line, it is easier, ie
> 
> ^[^<]+<
> 
> but I cant use the same method for more than one character string as
> permutation is present and probably for more than one occurrence,
> greedy or non-greedy version of [^<]+ would pick first or last but not
> the middle ones, unless I break the line as I go and use the non-
> greedy version of +. I do have the non-greedy version available, but
> what if I didnt?
> 
> If you cannot solve the problem completely, just give me a quick
> solution with the first non beginning of the line and I will go from
> there as I need it in a hurry.
> 
> Thanks
> 
> 

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


Re: bit count or bit set && Python3

2012-10-25 Thread Charles Hixson

On 10/25/2012 08:57 AM, Steven D'Aprano wrote:

On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:


On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes
wrote:

Simple, easy, faster than a Python loop but not very elegant:

bin(number).count("1")

Unlikely to be fast.

Oh I don't know about that. Here's some timing results using Python 2.7:

py>  from timeit import Timer
py>  t = Timer('bin(number).count("1")', setup='number=2**10001-1')
py>  min(t.repeat(number=1, repeat=7))
0.6819710731506348

Compare to MRAB's suggestion:

def count_set_bits(number):
  count = 0
  while number:
  count += 1
  number&= number - 1
  return count

py>  t = Timer('count_set_bits(number)',
... setup='from __main__ import count_set_bits; number=2**10001-1')
py>  min(t.repeat(number=100, repeat=7))
4.141788959503174


That makes the "inelegant" solution using bin() and count() about 600
times faster than the mathematically clever solution using bitwise
operations.

On the other hand, I'm guessing that PyPy would speed up MRAB's version
significantly.



Really nice and good to know.  I had guessed the other way.   (As you 
point out this is compiler dependent, and I'll be using Python3, 
but...conversion from an int to a bit string must be a *lot* faster than 
I had thought.)


--
Charles Hixson

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


Re: How to set 250000 baud rate in pyserial ?

2012-10-25 Thread Grant Edwards
On 2012-10-25, Dennis Lee Bieber  wrote:
> On Thu, 25 Oct 2012 04:09:56 -0700 (PDT), kura...@gmail.com wrote:
>
>> I use Arduino 1280 and Arduino 2560 under Fedora 15.  1280 creates
>> ttyUSB0 port and can be set at 250 successfully. 2560 creates
>> ttyACM0 port and can be only set at speeds from list (no 25) in
>> pyserial. How to set 25 to ttyACM0 port?? Need I patch kernel or
>> python?
>
> You don't say what error you are receiving but looking at the source
> (serialposix.py) implies that it accepts nearly anything on Linux, and
> relies on the OS returning a success/failure if the value is allowed or
> not. 

1) Are you sure it matters?  I've never played with an Arduino board,
   but other stuff I've used that implements a "virtual serial port"
   using a ttyUSB or ttyACM device (e.g. oscilloscope, various Atmel
   eval boards, JTAG interfaces, etc.) didn't have actual UARTs in
   them with real baud rate generators.  You got the same high-speed
   transfers no matter what baud rate you told the tty driver.

2) If you want a non-standard baud rate, there is a termios2 API on
   Linux that allows that (assuming the low-level driver and hardwdare
   support it).  The last time I looked, it wasn't supported by
   pyserial, but you can ask pyserial for the underlying file
   descriptor and do the ioctl manually.  The slightly ugly bit is
   that you'll have to use struct (or maybe ctypes) to handle the
   termios2 "C" structure.

   The behavior of baud rate requests that can't be met exactly is
   probably not very consistent.  IIRC, the recommended approach is
   for the low level driver to pick the closest supported baud, and
   then report the actual baud rate back when you subsequently read
   the termios2 structure. However, I do know of some devices that
   will always report the requested baud rate even if the physical
   baud rate that was selected wasn't exactly the same as the request
   rate.

Here's how you set an arbitrary baud rate in C:
   
-arbitrary-baud.c--
#include 
#include 
#include 
#include 
#include 
#include 

int ioctl(int d, int request, ...);

int main(int argc, char *argv[])
{
  struct termios2 t;
  int fd,baud;

  if (argc != 3)
{
  fprintf(stderr,"usage: %s  \n", argv[0]);
  return 1;
}

  fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)
  {
fprintf(stderr, "error opening %s: %s", argv[1], strerror(errno));
return 2;
  }

  baud = atoi(argv[2]);

  if (ioctl(fd, TCGETS2, &t))
{
  perror("TCGETS2");
  return 3;
}

  t.c_cflag &= ~CBAUD;
  t.c_cflag |= BOTHER;
  t.c_ispeed = baud;
  t.c_ospeed = baud;

  if (ioctl(fd, TCSETS2, &t))
{
  perror("TCSETS2");
  return 4;
}

  if (ioctl(fd, TCGETS2, &t))
{
  perror("TCGETS2");
  return 5;
}

  printf("actual speed reported %d\n", t.c_ospeed);
  return 0;
}



-- 
Grant Edwards   grant.b.edwards
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Zero Piraeus
:

On 25 October 2012 16:53, Rivka Miller  wrote:
> I am looking for a regexp for a string not at the beginning of the
> line.

There are probably quite a few ways to do this, but '(?>> pattern = re.compile(r"(?>> re.findall(pattern, "this is some text")
['is', 'some', 'text']

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


bit count or bit set && Python3

2012-10-25 Thread Charles Hixson
In Python3 is there any good way to count the number of on bits in an 
integer (after an & operation)?
Alternatively, is there any VERY light-weight implementation of a bit 
set?  I'd prefer to use integers, as I'm probably going to need 
thousands of these, if the tests work out.  But before I can test, I 
need a decent bit counter.  (shift, xor, &, and | are already present 
for integer values, but I also need to count the number of "true" items 
after the logical operation.  So if a bitset is the correct approach, 
I'll need it to implement those operations, or their equivalents in 
terms of union and intersection.)


Or do I need to drop into C for this?

--
Charles Hixson

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


RE: turn list of letters into an array of integers

2012-10-25 Thread Prasad, Ramit
David Hutto wrote:
> On Wed, Oct 24, 2012 at 1:23 AM, seektime  wrote:
> > Here's some example code. The input is a list which is a "matrix" of 
> > letters:
> >a  b  a
> >b  b  a
> >
> > and I'd like to turn this into a Python array:
> >
> >   1 2 1
> >   2 2 1
> >
> > so 1 replaces a, and 2 replaces b. Here's the code I have so far:
> >
>  L=['a b a\n','b b a\n']
>  s=' '.join(L)
>  seq1=('a','b')
>  seq2=('1','2')
>  d = dict(zip(seq1,seq2))
>  # Define method to replace letters according to dictionary (got this from
> http://gommeitputor.wordpress.com/2008/09/27/search-replace-multiple-words-or-characters-with-python/).
> > ... def replace_all(text, dic):
> > ... for i, j in dic.iteritems():
> > ... text = text.replace(i, j)
> > ... return text
> > ...
> >
>  seq = replace_all(s,d)
>  print seq
> > 1 2 1
> >  2 2 1
> >
>  seq
> > '1 2 1\n 2 2 1\n'
> >
> I'd suggest, if this is what you're referring to:
> 
> x = seq.split('\n  ')
> array_list = [ ]
> next_3_d_array = []
> range_of_seq = len(seq)
> for num in range(0,range_of_seq):
>if num % 3 != 0:
>next_3_d_array.append(num)
>if num % 3 == 0:
>array_list.append(next_3_d_array)
>next_3_d_array = [ ]
> 

Wow, that looks complicated. Why hardcode to 3 instead of where ever
the newline is?

>>> [ int(x.strip()) for subseq in seq.split('\n') for x in subseq.split() ]
[1, 2, 1, 2, 2, 1]
>>> lst = []
# OR
>>> for subseq in seq.split('\n'):
... for x in subseq.split():
... lst.append( int(x.strip()))
... 
>>>

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Mark Lawrence

On 25/10/2012 17:08, Charles Hixson wrote:

On 10/25/2012 08:57 AM, Steven D'Aprano wrote:

On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote:


On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes
wrote:

Simple, easy, faster than a Python loop but not very elegant:

bin(number).count("1")

Unlikely to be fast.

Oh I don't know about that. Here's some timing results using Python 2.7:

py>  from timeit import Timer
py>  t = Timer('bin(number).count("1")', setup='number=2**10001-1')
py>  min(t.repeat(number=1, repeat=7))
0.6819710731506348

Compare to MRAB's suggestion:

def count_set_bits(number):
  count = 0
  while number:
  count += 1
  number&= number - 1
  return count

py>  t = Timer('count_set_bits(number)',
... setup='from __main__ import count_set_bits; number=2**10001-1')
py>  min(t.repeat(number=100, repeat=7))
4.141788959503174


That makes the "inelegant" solution using bin() and count() about 600
times faster than the mathematically clever solution using bitwise
operations.

On the other hand, I'm guessing that PyPy would speed up MRAB's version
significantly.




Really nice and good to know.  I had guessed the other way.   (As you
point out this is compiler dependent, and I'll be using Python3,
but...conversion from an int to a bit string must be a *lot* faster than
I had thought.)



The simple rule for Python performance is never guess anything as you'll 
invariably be wrong, time it and/or profile it, then change your code if 
and only if you have to.


--
Cheers.

Mark Lawrence.

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


Re: SSH Connection with Python - Oops

2012-10-25 Thread Peter Pearson
On 25 Oct 2012 16:55:46 GMT, Peter Pearson  wrote:
> On Thu, 25 Oct 2012 12:16:58 +0200, Schneider  wrote:
>> how can i create a SSH-Connection with python? I have to send some 
>> commands to the remote host and parse their answers.
>> greatz Johannes
>
> I've been using Twisted (twistedmatrix.com).  It is especially
> convenient for the server end.  Its organization is "event-driven",
> which you may or may not find convenient.

Oops!  The question was about SSH, and I was blathering about SSL.
Sorry.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: bit count or bit set && Python3

2012-10-25 Thread Steven D'Aprano
On Thu, 25 Oct 2012 14:20:00 -0600, Ian Kelly wrote:

> On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti  wrote:
>> Yes indeed! Python string operations are fast enough and its arithmetic
>> slow enough that I no longer assume I can beat a neat lexicographical
>> solution. Try defeating the following with arithmetic:
>>
>> def is_palindrom(n):
>>s = str(n)
>>return s = s[::-1]
> 
> Problems like these are fundamentally string problems, not math
> problems.  The question being asked isn't about some essential property
> of the number,  but about its digital representation.

Speaking as somebody who sometimes pretends to be a mathematician, I 
think you are wrong there. The property of being a palindrome may have 
been invented in reference to strings, but it is also a mathematical 
property of a number. Properties of the digits of numbers are properties 
of the relationship between the number and some sequence of divisors (the 
powers of some base), which makes them numeric properties.

It's interesting to consider properties of numbers which hold for *every* 
base. For example, I understand that the digits of pi are uniformly 
distributed regardless of which base you use.

Certainly mathematicians frequently ask questions about the digits of 
numbers, generally in base ten but also in other bases. Since the digits 
of a number are based on the numeric properties of the number, they 
certainly are essential to the number (modulo some base).

For example, apart from two itself[1], every prime number ends in a 1 bit 
in base two. In decimal, every factorial greater than 4! ends with a 
zero. A number is divisible by 9 if the sum of the (decimal) digits 
reduces to 9. A number is divisible by 5 if the last digit is 0 or 5. 
These are not accidents, they depend on the numeric properties.


> Certainly they can
> be reasoned about mathematically, but the fact remains that the math
> being done is about the properties of strings.

Strings of digits, which are numerically equal to the remainders when you 
divide the number by successively larger powers of some base.:

123 = 1*10**2 + 2*10**1 + 3*10**0

Hence, mathematical.

Of course, none of this challenges the fact that, at least in Python, 
reasoning about digits is often best done on the string representation 
rather than the number itself.



[1] Which makes 2 the oddest prime of all.


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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Rivka Miller
On Oct 25, 2:27 pm, Danny  wrote:
> Why you just don't give us the string/input, say a line or two, and what you 
> want off of it, so we can tell better what to suggest

no one has really helped yet.

I want to search and modify.

I dont wanna be tied to a specific language etc so I just want a
regexp and as many versions as possible. Maybe I should try in emacs
and so I am now posting to emacs groups also, although javascript has
rich set of regexp facilities.

examples

$hello$ should not be selected but
not hello but all of the $hello$ and $hello$ ... $hello$ each one
selected

=
original post
=


Hello Programmers,

I am looking for a regexp for a string not at the beginning of the
line.

For example, I want to find $hello$ that does not occur at the
beginning of the string, ie all $hello$ that exclude ^$hello$.

In addition, if you have a more difficult problem along the same
lines, I would appreciate it. For a single character, eg < not at the
beginning of the line, it is easier, ie

^[^<]+<

but I cant use the same method for more than one character string as
permutation is present and probably for more than one occurrence,
greedy or non-greedy version of [^<]+ would pick first or last but not
the middle ones, unless I break the line as I go and use the non-
greedy version of +. I do have the non-greedy version available, but
what if I didnt?

If you cannot solve the problem completely, just give me a quick
solution with the first non beginning of the line and I will go from
there as I need it in a hurry.

Thanks

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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Dave Angel
On 10/25/2012 09:08 PM, Rivka Miller wrote:
> On Oct 25, 2:27 pm, Danny  wrote:
>> Why you just don't give us the string/input, say a line or two, and what you 
>> want off of it, so we can tell better what to suggest
> no one has really helped yet.
>
> 
>
> first non beginning of the line and I will go from
> there as I need it in a hurry.
>
>

Call a tow truck and tell him to jump your spare tire from his left turn
signal.  That'll be about as effective.  But crying wolf to several
towns at once is probably a mistake.

-- 

DaveA

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


a.index(float('nan')) fails

2012-10-25 Thread mamboknave
>>> a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a
[nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.index(float('nan'))
Traceback (most recent call last):
  File "", line 1, in 
ValueError: list.index(x): x not in list

That means, the function .index() cannot detect nan values.
It happens on both Python 2.6 and Python 3.1

Is this a bug? Or I am not using .index() correctly?

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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Ed Morton

On 10/25/2012 8:08 PM, Rivka Miller wrote:

On Oct 25, 2:27 pm, Danny  wrote:

Why you just don't give us the string/input, say a line or two, and what you 
want off of it, so we can tell better what to suggest


no one has really helped yet.


Because there is no solution - there IS no _RE_ that will match a string not at 
the beginning of a line.


Now if you want to know how to extract a string that matches an RE in awk, 
that'd be (just one way):


   awk 'match($0,/.[$]hello[$]/) { print substr($0,RSTART+1,RLENGTH-1) }'

and other tools would have their ways of producing the same output, but that's 
not the question you're asking.


Ed.


I want to search and modify.

I dont wanna be tied to a specific language etc so I just want a
regexp and as many versions as possible. Maybe I should try in emacs
and so I am now posting to emacs groups also, although javascript has
rich set of regexp facilities.

examples

$hello$ should not be selected but
not hello but all of the $hello$ and $hello$ ... $hello$ each one
selected

=
original post
=


Hello Programmers,

I am looking for a regexp for a string not at the beginning of the
line.

For example, I want to find $hello$ that does not occur at the
beginning of the string, ie all $hello$ that exclude ^$hello$.

In addition, if you have a more difficult problem along the same
lines, I would appreciate it. For a single character, eg < not at the
beginning of the line, it is easier, ie

^[^<]+<

but I cant use the same method for more than one character string as
permutation is present and probably for more than one occurrence,
greedy or non-greedy version of [^<]+ would pick first or last but not
the middle ones, unless I break the line as I go and use the non-
greedy version of +. I do have the non-greedy version available, but
what if I didnt?

If you cannot solve the problem completely, just give me a quick
solution with the first non beginning of the line and I will go from
there as I need it in a hurry.

Thanks



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


Re: a.index(float('nan')) fails

2012-10-25 Thread Terry Reedy

On 10/25/2012 9:46 PM, mambokn...@gmail.com wrote:

a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a

[nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a.index(float('nan'))

Traceback (most recent call last):
   File "", line 1, in 
ValueError: list.index(x): x not in list

That means, the function .index() cannot detect nan values.
It happens on both Python 2.6 and Python 3.1

Is this a bug? Or I am not using .index() correctly?


It is a consequence of the following, which some people (but not all) 
believe is mandated by the IEEE standard.


>>> nan = float('nan')
>>> nan is nan
True
>>> nan == nan
False

>>> nanlist = [nan]
>>> nan in nanlist
True
>>> nanlist.index(nan)
0

Containment of nan in collection is tested by is, not ==.

>>> nan2 = float('nan')
>>> nan2 is nan
False
>>> nan2 == nan
False
>>> nan2 in nanlist
False

--
Terry Jan Reedy

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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread MRAB

On 2012-10-26 02:08, Rivka Miller wrote:

On Oct 25, 2:27 pm, Danny  wrote:

Why you just don't give us the string/input, say a line or two, and what you 
want off of it, so we can tell better what to suggest


no one has really helped yet.

I want to search and modify.

I dont wanna be tied to a specific language etc so I just want a
regexp and as many versions as possible. Maybe I should try in emacs
and so I am now posting to emacs groups also, although javascript has
rich set of regexp facilities.

examples

$hello$ should not be selected but
not hello but all of the $hello$ and $hello$ ... $hello$ each one
selected


[snip]
To match the literal "$hello$" except at the start of a line, use:

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


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Ben Bacarisse
Rivka Miller  writes:

> On Oct 25, 2:27 pm, Danny  wrote:
>> Why you just don't give us the string/input, say a line or two, and
>> what you want off of it, so we can tell better what to suggest
>
> no one has really helped yet.

Really?  I was going to reply but then I saw Janis had given you the
answer.  If it's not the answer, you should just reply saying what it is
that's wrong with it.

> I want to search and modify.

Ah.  That was missing from the original post.  You can't expect people
to help with questions that weren't asked!  To replace you will usually
have to capture the single preceding character.  E.g. in sed:

  sed -e 's/\(.\)$hello\$/\1XXX/'

but some RE engines (Perl's, for example) allow you specify zero-width
assertions.  You could, in Perl, write

  s/(?<=.)\$hello\$/XXX/

without having to capture whatever preceded the target string.  But
since Perl also has negative zero-width look-behind you can code your
request even more directly:

  s/(? I dont wanna be tied to a specific language etc so I just want a
> regexp and as many versions as possible. Maybe I should try in emacs
> and so I am now posting to emacs groups also, although javascript has
> rich set of regexp facilities.

You can't always have a universal solution because different PE
implementations have different syntax and semantics, but you should be
able to translate Janis's solution of matching *something* before your
target into every RE implementation around.

> examples
>
> $hello$ should not be selected but
> not hello but all of the $hello$ and $hello$ ... $hello$ each one
> selected

I have taken your $s to be literal.  That's not 100 obvious since $ is a
common (universal?) RE meta-character.


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


Re: a.index(float('nan')) fails

2012-10-25 Thread Cameron Simpson
On 25Oct2012 18:46, mambokn...@gmail.com  wrote:
| >>> a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
| >>> a
| [nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
| >>> a.index(float('nan'))
| Traceback (most recent call last):
|   File "", line 1, in 
| ValueError: list.index(x): x not in list
| 
| That means, the function .index() cannot detect nan values.
| It happens on both Python 2.6 and Python 3.1
| 
| Is this a bug? Or I am not using .index() correctly?

The special NaN float value always compares unequal, even to itself.
IEEE floating point FTW!

You're using index incorrectly, but only because it relies on ==
returning True, which it won't.

You can use math.isnan:

  http://docs.python.org/library/math.html#math.isnan
  http://docs.python.org/py3k/library/math.html#math.isnan

for the test instead. Nan requires special handling.

Cheers,
-- 
Cameron Simpson 

I'm not making any of this up you know. - Anna Russell
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a.index(float('nan')) fails

2012-10-25 Thread MRAB

On 2012-10-26 03:04, Terry Reedy wrote:

On 10/25/2012 9:46 PM, mambokn...@gmail.com wrote:

a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a

[nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a.index(float('nan'))

Traceback (most recent call last):
   File "", line 1, in 
ValueError: list.index(x): x not in list

That means, the function .index() cannot detect nan values.
It happens on both Python 2.6 and Python 3.1

Is this a bug? Or I am not using .index() correctly?


It is a consequence of the following, which some people (but not all)
believe is mandated by the IEEE standard.

  >>> nan = float('nan')
  >>> nan is nan
True
  >>> nan == nan
False

  >>> nanlist = [nan]
  >>> nan in nanlist
True
  >>> nanlist.index(nan)
0

Containment of nan in collection is tested by is, not ==.

  >>> nan2 = float('nan')
  >>> nan2 is nan
False
  >>> nan2 == nan
False
  >>> nan2 in nanlist
False


In summary, .index() looks for an item which is equal to its argument,
but it's a feature of NaN (as defined by the standard) that it doesn't
equal NaN, therefore .index() will never find it.

Another consequence is that the presence of a NaN in a list prevents
.sort() from sorting correctly.

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


Re: a.index(float('nan')) fails

2012-10-25 Thread mamboknave
On Thursday, October 25, 2012 7:16:02 PM UTC-7, Cameron Simpson wrote:

Of course!! How could I get into that trap??

Thanks to you & to Terry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quickie - Regexp for a string not at the beginning of the line

2012-10-25 Thread Mark Lawrence

On 25/10/2012 21:53, Rivka Miller wrote:

Hello Programmers,

I am looking for a regexp for a string not at the beginning of the
line.



Why bother with a silly regex thingy when simple string methods will 
suffice e.g.


'yourstring'.find('xyz', 1)

or

'yourstring'.index('xyz', 1)

or

'xyz' in 'yourstring'[1:]

--
Cheers.

Mark Lawrence.

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


  1   2   >