Re: How to bounce the ball forever around the screen

2015-12-04 Thread Peter Otten
phamton...@gmail.com wrote:

> from Tkinter import *
> window = Tk()
> canvas = Canvas(window, width=500, height=500, background="green")
> canvas.pack()
> 
> def move_ball(speed_x, speed_y):
> box = canvas.bbox("ball")
> x1 = box[0]
> y1 = box[1]
> x2 = box[2]
> y2 = box[3]
> 
> if x1 <= 0:
> speed_x = 0
> speed_y = 0
> 
> canvas.move("ball", speed_x, speed_y)
> canvas.after(30, move_ball, speed_x, speed_y)
> 
> canvas.create_oval(225, 225, 275, 275, fill="blue", tags="ball")
> 
> move_ball(-10, 7)
> 
> mainloop()
> 
>  where in the code should i change to make the ball bounce around forever. 
is it the x and y?

When the ball hits a wall you have to "reflect" it; for a vertical wall 
speed_x has to change:

if x1 <= 0: # ball hits left wall
speed_x = -speed_x
elif ... # ball hits right wall
speed_x = -speed_x

Can you come up with the correct check for the right wall?
To try if this part works correctly temporarily change

> move_ball(-10, 7)

to

move_ball(-10, 0)

so that the ball only moves in horizontal direction. Once this works handle 
the vertical walls and speed_y the same way.


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


Re: getting fileinput to do errors='ignore' or 'replace'?

2015-12-04 Thread Serhiy Storchaka

On 04.12.15 00:26, Oscar Benjamin wrote:

On 3 Dec 2015 16:50, "Terry Reedy"  wrote:

fileinput is an ancient module that predates iterators (and generators)

and context managers. Since by 2.7 open files are both context managers and
line iterators, you can easily write your own multi-file line iteration
that does exactly what you want.  At minimum:


for file in files:
 with codecs.open(file, errors='ignore') as f
 # did not look up signature,
 for line in f:
 do_stuff(line)


The above is fine but...


To make this reusable, wrap in 'def filelines(files):' and replace

'do_stuff(line)' with 'yield line'.

That doesn't work entirely correctly as you end up yielding from inside a
with statement. If the user of your generator function doesn't fully
consume the generator then whichever file is currently open is not
guaranteed to be closed.


You can convert the generator to context manager and use it in the with 
statement to guarantee closing.


with contextlib.closing(filelines(files)) as f:
for line in f:
...


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


Re: Is vars() the most useless Python built-in ever?

2015-12-04 Thread Serhiy Storchaka

On 02.12.15 11:28, Chris Angelico wrote:

On Wed, Dec 2, 2015 at 7:22 PM, Serhiy Storchaka  wrote:

On 01.12.15 03:00, Steven D'Aprano wrote:

I'm trying to understand why vars() exists. Does anyone use it?

I use vars() exclusively for introspection in interactive environment. As
well as dir() and help(). Sad that it doesn't work with __slots__.

Maybe the upshot of all this is a post to python-ideas recommending
that vars() grow support for __slots__ types? If it's most often used
interactively, this would make it more useful there, and it wouldn't
break backward compatibility unless there's some way that people are
depending on it raising an exception.


It already was discussed few times. And there is even open issue for 
this: http://bugs.python.org/issue13290.



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


Re: getting fileinput to do errors='ignore' or 'replace'?

2015-12-04 Thread Oscar Benjamin
On 4 Dec 2015 08:36, "Serhiy Storchaka"  wrote:
>
> On 04.12.15 00:26, Oscar Benjamin wrote:
>>
>> On 3 Dec 2015 16:50, "Terry Reedy"  wrote:
>>>
>>> fileinput is an ancient module that predates iterators (and generators)
>>
>> and context managers. Since by 2.7 open files are both context managers
and
>> line iterators, you can easily write your own multi-file line iteration
>> that does exactly what you want.  At minimum:
>>>
>>>
>>> for file in files:
>>>  with codecs.open(file, errors='ignore') as f
>>>  # did not look up signature,
>>>  for line in f:
>>>  do_stuff(line)
>>
>>
>> The above is fine but...
>>
>>> To make this reusable, wrap in 'def filelines(files):' and replace
>>
>> 'do_stuff(line)' with 'yield line'.
>>
>> That doesn't work entirely correctly as you end up yielding from inside a
>> with statement. If the user of your generator function doesn't fully
>> consume the generator then whichever file is currently open is not
>> guaranteed to be closed.
>
>
> You can convert the generator to context manager and use it in the with
statement to guarantee closing.
>
> with contextlib.closing(filelines(files)) as f:
> for line in f:
> ...

Or you can use fileinput which is designed to be exactly this kind of
context manager and to be used in this way. Although fileinput is slightly
awkward in defaulting to reading stdin.

--
Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


problem

2015-12-04 Thread Dylan Goodwin
Every time I try and run python 3.5 it keeps coming up with modify, repair
or uninstall
-- 
https://mail.python.org/mailman/listinfo/python-list


python 3.5.0rc1 problem opening IDLE in windows

2015-12-04 Thread Nicky Mac
Dear python team
since windows applied some updates last night to my windows 10 and windows
7 systems,
I can't open anything with IDLE as I usually do.
On windows 10 (64bit with 64bit python), I performed the installation
repair procedure.
* Edit with IDLE does not appear as an installed windows program.
* when I open my usual shortcut to IDLE (
C:\Python\Python35\Lib\idlelib\idle.py)
 something briefly flashes on the screen *and immediately disappears.*
* if I directly open a python program in windows, I get these options (as
usual):-
edit with IDLE (-> python launcher for windows console)
edit with IDLE  (-> edit with IDLE (64bit))  - which I always
use, *nothing
happens*

Any ideas gratefully received!!!
sincerely
Nick "Mac" McElwaine
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib2.urlopen() crashes on Windows 2008 Server

2015-12-04 Thread Ulli Horlacher
Dennis Lee Bieber  wrote:

> >I have a Python2 program which runs fine on Windows 7, but
> >crashes on Windows 2008 Server R2 64 bit:
> >
> >downloading http://fex.belwue.de/download/7za.exe
> >Traceback (most recent call last):
> >  File "", line 1992, in 
> >  File "", line 180, in main
> >  File "", line 329, in get_ID
> >  File "", line 1627, in check_7z
> >  File "C:\Software\Python\lib\urllib2.py", line 154, in urlopen
> >  File "C:\Software\Python\lib\urllib2.py", line 431, in open
> >  File "C:\Software\Python\lib\urllib2.py", line 449, in _open
> >  File "C:\Software\Python\lib\urllib2.py", line 409, in _call_chain
> >  File "C:\Software\Python\lib\urllib2.py", line 1227, in http_open
> >  File "C:\Software\Python\lib\urllib2.py", line 1200, in do_open
> >  File "C:\Software\Python\lib\httplib.py", line 1132, in getresponse
> >  File "C:\Software\Python\lib\httplib.py", line 485, in begin
> >  File "C:\Software\Python\lib\mimetools.py", line 25, in __init__
> >  File "C:\Software\Python\lib\rfc822.py", line 108, in __init__
> >  File "C:\Software\Python\lib\httplib.py", line 319, in readheaders
> >  File "C:\Software\Python\lib\socket.py", line 480, in readline
> >error: [Errno 10054] Eine vorhandene Verbindung wurde vom Remotehost 
> >geschlossen
> >
> 
> Per MSDN:
> """
> WSAECONNRESET
> 10054
> 
> 
> 
> Connection reset by peer.
> 
> An existing connection was forcibly closed by the remote host. 

This is not true.
The server is under my control. Die client has terminated the connection
(or a router between).


How can I trap this within the python program?
I see no exception.


-- 
Ullrich Horlacher  Server und Virtualisierung
Rechenzentrum IZUS/TIK E-Mail: horlac...@tik.uni-stuttgart.de
Universitaet Stuttgart Tel:++49-711-68565868
Allmandring 30aFax:++49-711-682357
70550 Stuttgart (Germany)  WWW:http://www.tik.uni-stuttgart.de/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python 3.5.0rc1 problem opening IDLE in windows

2015-12-04 Thread Laura Creighton
In a message of Fri, 04 Dec 2015 11:26:59 +, Nicky Mac writes:
>Dear python team
>since windows applied some updates last night to my windows 10 and windows
>7 systems,
>I can't open anything with IDLE as I usually do.
>On windows 10 (64bit with 64bit python), I performed the installation
>repair procedure.
>* Edit with IDLE does not appear as an installed windows program.
>* when I open my usual shortcut to IDLE (
>C:\Python\Python35\Lib\idlelib\idle.py)
> something briefly flashes on the screen *and immediately disappears.*
>* if I directly open a python program in windows, I get these options (as
>usual):-
>edit with IDLE (-> python launcher for windows console)
>edit with IDLE  (-> edit with IDLE (64bit))  - which I always
>use, *nothing
>happens*
>
>Any ideas gratefully received!!!
>sincerely
>Nick "Mac" McElwaine

Go to a console window and type python -m idlelib

See if you get a useful traceback, and post it here.  (If idle just
works, tell us that, instead.)

Laura Creighton

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


Re: problem

2015-12-04 Thread Laura Creighton
In a message of Thu, 03 Dec 2015 21:25:53 +0200, Dylan Goodwin writes:
>Every time I try and run python 3.5 it keeps coming up with modify, repair
>or uninstall
>-- 
>https://mail.python.org/mailman/listinfo/python-list

What OS are you running?

If windows XP, your problem is that your OS is too old.  Python 3.5 is
not supported.  Stick with 3.4 or upgrade your OS.

If something else, you need to install 
the available service packs.  For instance, if you are running
Windows 7, https://www.microsoft.com/en-us/download/details.aspx?id=5842
is where you get service pack 1.

Laura Creighton

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


Re: [Python-ideas] Missing Core Feature: + - * / | & do not call __getattr__

2015-12-04 Thread Ian Kelly
On Fri, Dec 4, 2015 at 7:20 AM, Stephan Sahm  wrote:
> Dear all,
>
> I just stumbled upon a very weird behaviour of python 2 and python 3. At
> least I was not able to find a solution.
>
> The point is to dynamically define __add__, __or__ and so on via __getattr__
> (for example by deriving them from __iadd__ or similar in a generic way).
> However this very intuitive idea is currently NOT POSSIBLE because * - * / &
> | and so on just bypass this standard procedure.
>
> I found two stackoverflow contributions stating this:
> http://stackoverflow.com/questions/11629287/python-how-to-forward-an-instances-method-call-to-its-attribute
> http://stackoverflow.com/questions/33393474/lazy-evaluation-forward-operations-to-deferred-value
>
> Neither the mentioned posts, nor I myself can see any reason why this is the
> way it is, nor how the operators are actually implemented to maybe bypass
> this unintuitive behaviour.

The cited reason is "speed optimizations":
https://docs.python.org/3/reference/datamodel.html#special-method-lookup

But there may be other reasons as well. __getattr__ and
__getattribute__ are meant to implement *attribute* lookup. Special
methods are best not regarded as attributes (although they can be
looked up as such), but as implementation of class behavior. You'll
note that special methods also aren't resolved in the instance dict,
but only in the class dict; this is viewed as a matter of correctness,
so that special methods work correctly on class objects (invoking the
metaclass) as well as on instances. In some cases there may also be a
chicken-and-egg problem; should Python call __getattribute__ in order
to resolve the __getattribute__ method?
-- 
https://mail.python.org/mailman/listinfo/python-list


Frozen apps (py2exe, cx_freeze) built with Python 3.5

2015-12-04 Thread d...@forestfield.co.uk
Python 3.5 will not run under Windows XP, but what about applications created 
using py2exe or cx_freeze under Windows 7, 8 or 10, is there any knowledge of 
whether they will run under XP?

Regards,
David Hughes
Forestfield Software
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Frozen apps (py2exe, cx_freeze) built with Python 3.5

2015-12-04 Thread Ian Kelly
On Fri, Dec 4, 2015 at 10:21 AM, d...@forestfield.co.uk
 wrote:
> Python 3.5 will not run under Windows XP, but what about applications created 
> using py2exe or cx_freeze under Windows 7, 8 or 10, is there any knowledge of 
> whether they will run under XP?

I wouldn't expect them to. Those bundlers are just packaging up the
Python binary, so they should have all the same problems.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Frozen apps (py2exe, cx_freeze) built with Python 3.5

2015-12-04 Thread Zachary Ware
On Fri, Dec 4, 2015 at 10:37 AM, Ian Kelly  wrote:
> On Fri, Dec 4, 2015 at 10:21 AM, d...@forestfield.co.uk
>  wrote:
>> Python 3.5 will not run under Windows XP, but what about applications 
>> created using py2exe or cx_freeze under Windows 7, 8 or 10, is there any 
>> knowledge of whether they will run under XP?
>
> I wouldn't expect them to. Those bundlers are just packaging up the
> Python binary, so they should have all the same problems.

I'll confirm that expectation.

-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exclude text within quotation marks and words beginning with a capital letter

2015-12-04 Thread Jason Friedman
>
> I am working on a program that is written in Python 2.7 to be compatible
> with the POS tagger that I import from Pattern. The tagger identifies all
> the nouns in a text. I need to exclude from the tagger any text that is
> within quotation marks, and also any word that begins with an upper case
> letter (including words at the beginning of sentences).
>
> Any advice on coding that would be gratefully received. Thanks.
>

Perhaps overkill, but wanted to make sure you knew about the Natural
Language Toolkit:  http://www.nltk.org/.
-- 
https://mail.python.org/mailman/listinfo/python-list


counting unique numpy subarrays

2015-12-04 Thread duncan smith
Hello,
  I'm trying to find a computationally efficient way of identifying
unique subarrays, counting them and returning an array containing only
the unique subarrays and a corresponding 1D array of counts. The
following code works, but is a bit slow.

###

from collections import Counter
import numpy

def bag_data(data):
# data (a numpy array) is bagged along axis 0
# returns concatenated array and corresponding array of counts
vec_shape = data.shape[1:]
counts = Counter(tuple(arr.flatten()) for arr in data)
data_out = numpy.zeros((len(counts),) + vec_shape)
cnts = numpy.zeros((len(counts,)))
for i, (tup, cnt) in enumerate(counts.iteritems()):
data_out[i] = numpy.array(tup).reshape(vec_shape)
cnts[i] =  cnt
return data_out, cnts

###

I've been looking through the numpy docs, but don't seem to be able to
come up with a clean solution that avoids Python loops. TIA for any
useful pointers. Cheers.

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] Using functools.lru_cache only on some arguments of a function

2015-12-04 Thread Ian Kelly
On Fri, Dec 4, 2015 at 2:44 PM, Bill Winslow  wrote:
> This is a question I posed to reddit, with no real resolution:
> https://www.reddit.com/r/learnpython/comments/3v75g4/using_functoolslru_cache_only_on_some_arguments/
>
> The summary for people here is the following:
>
> Here's a pattern I'm using for my code:
>
> def deterministic_recursive_calculation(input, partial_state=None):
> condition = do_some_calculations(input)
> if condition:
> return deterministic_recursive_calculation(reduced_input,
> some_state)
>
> Basically, in calculating the results of the subproblem, the subproblem can
> be calculated quicker by including/sharing some partial results from the
> superproblem. (Calling the subproblem without the partial state still gives
> the same result, but takes substantially longer.)
>
> I want to memoize this function for obvious reasons, but I need the
> lru_cache to ignore the partial_state argument, for its value does not
> affect the output, only the computation expense.
>
> Is there any reasonable way to do this?

What form does the partial_state take? Would it be reasonable to
design it with __eq__ and __hash__ methods so that each partial state
(or a wrapper of it) is considered equal?
-- 
https://mail.python.org/mailman/listinfo/python-list


Unicode failure

2015-12-04 Thread D'Arcy J.M. Cain
I thought that going to Python 3.4 would solve my Unicode issues but it
seems I still don't understand this stuff.  Here is my script.

#! /usr/bin/python3
# -*- coding: UTF-8 -*-
import sys 
print(sys.getdefaultencoding()) 
print(u"\N{TRADE MARK SIGN}") 

And here is my output.

utf-8
Traceback (most recent call last):
  File "./g", line 5, in 
print(u"\N{TRADE MARK SIGN}")
UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
position 0: ordinal not in range(128)

What am I missing?

TIA.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem in pip

2015-12-04 Thread Ali Zarkesh
My pip can't download or upgrade anything
I use python 3.5 (win 32) and my pip version is 7.1.2.
The error message is this:

Exception:
Traceback (most recent call last):
File "c:\program files\python 3.5\lib\site-packages\pip\basecommand.py",
line 211, in main
status = self.run(options, args)
File "c:\program files\python
3.5\lib\site-packages\pip\commands\install.py", line 311, in run
root=options.root_path,
File "c:\program files\python 3.5\lib\site-packages\pip\req\req_set.py",
line 646, in install
**kwargs
File "c:\program files\python
3.5\lib\site-packages\pip\req\req_install.py", line 803, in install
self.move_wheel_files(self.source_dir, root=root)
File "c:\program files\python
3.5\lib\site-packages\pip\req\req_install.py", line 998, in move_wheel_files
insolated=self.isolated,
File "c:\program files\python 3.5\lib\site-packages\pip\wheel.py", line
339, in move_wheel_files
clobber(source, lib_dir, True)
File "c:\program files\python 3.5\lib\site-packages\pip\wheel.py", line
317, in clobber
shutil.copyfile(srcfile, destfile)
File "c:\program files\python 3.5\lib\shutil.py", line 115, in copyfile
with open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'c:\program files\python
3.5\Lib\site-packages\PyWin32.chm'

What do I do?
-- 
https://mail.python.org/mailman/listinfo/python-list


problem fixed

2015-12-04 Thread Ali Zarkesh
My problem fixed
That was only about User Account Control
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: counting unique numpy subarrays

2015-12-04 Thread Albert-Jan Roskam
Hi

(Sorry for topposting)

numpy.ravel is faster than numpy.flatten (no copy)
numpy.empty is faster than numpy.zeros
numpy.fromiter might be useful to avoid the loop (just a hunch)

Albert-Jan

> From: duncan@invalid.invalid
> Subject: counting unique numpy subarrays
> Date: Fri, 4 Dec 2015 19:43:35 +
> To: python-list@python.org
> 
> Hello,
>   I'm trying to find a computationally efficient way of identifying
> unique subarrays, counting them and returning an array containing only
> the unique subarrays and a corresponding 1D array of counts. The
> following code works, but is a bit slow.
> 
> ###
> 
> from collections import Counter
> import numpy
> 
> def bag_data(data):
> # data (a numpy array) is bagged along axis 0
> # returns concatenated array and corresponding array of counts
> vec_shape = data.shape[1:]
> counts = Counter(tuple(arr.flatten()) for arr in data)
> data_out = numpy.zeros((len(counts),) + vec_shape)
> cnts = numpy.zeros((len(counts,)))
> for i, (tup, cnt) in enumerate(counts.iteritems()):
> data_out[i] = numpy.array(tup).reshape(vec_shape)
> cnts[i] =  cnt
> return data_out, cnts
> 
> ###
> 
> I've been looking through the numpy docs, but don't seem to be able to
> come up with a clean solution that avoids Python loops. TIA for any
> useful pointers. Cheers.
> 
> Duncan
> -- 
> https://mail.python.org/mailman/listinfo/python-list
  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem in pip

2015-12-04 Thread Zachary Ware
On Fri, Dec 4, 2015 at 12:02 PM, Ali Zarkesh  wrote:
> My pip can't download or upgrade anything
> I use python 3.5 (win 32) and my pip version is 7.1.2.
> The error message is this:
>
> Exception:
> Traceback (most recent call last):
> ...
> PermissionError: [Errno 13] Permission denied: 'c:\program files\python
> 3.5\Lib\site-packages\PyWin32.chm'
>
> What do I do?

It looks like you're trying to install in the global environment,
which requires administrative privileges.  You'll either need to do
the above from an Administrative Command Prompt or create a virtual
environment (py -3.5 -m venv ) and try it using the pip from the venv.

Hope this helps,
-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Unicode failure

2015-12-04 Thread Albert-Jan Roskam
I think you need to use a raw unicode string, ur

>>> unicodedata.name(ur'\u2122')
'TRADE MARK SIGN'

> Date: Fri, 4 Dec 2015 13:07:38 -0500
> From: da...@vybenetworks.com
> To: python-list@python.org
> Subject: Unicode failure
> 
> I thought that going to Python 3.4 would solve my Unicode issues but it
> seems I still don't understand this stuff.  Here is my script.
> 
> #! /usr/bin/python3
> # -*- coding: UTF-8 -*-
> import sys 
> print(sys.getdefaultencoding()) 
> print(u"\N{TRADE MARK SIGN}") 
> 
> And here is my output.
> 
> utf-8
> Traceback (most recent call last):
>   File "./g", line 5, in 
> print(u"\N{TRADE MARK SIGN}")
> UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
> position 0: ordinal not in range(128)
> 
> What am I missing?
> 
> TIA.
> 
> -- 
> D'Arcy J.M. Cain
> Vybe Networks Inc.
> http://www.VybeNetworks.com/
> IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
> -- 
> https://mail.python.org/mailman/listinfo/python-list
  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode failure

2015-12-04 Thread Oscar Benjamin
On 4 Dec 2015 22:34, "D'Arcy J.M. Cain"  wrote:
>
> I thought that going to Python 3.4 would solve my Unicode issues but it
> seems I still don't understand this stuff.  Here is my script.
>
> #! /usr/bin/python3
> # -*- coding: UTF-8 -*-
> import sys
> print(sys.getdefaultencoding())
> print(u"\N{TRADE MARK SIGN}")
>
> And here is my output.
>
> utf-8
> Traceback (most recent call last):
>   File "./g", line 5, in 
> print(u"\N{TRADE MARK SIGN}")
> UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
> position 0: ordinal not in range(128)
>
> What am I missing?

The important thing is not the default encoding but the encoding associated
with stdout. Try printing sys.stdout.encoding to see what that is. It may
depend what terminal you're trying to print out in. Are you using cmd.exe?
If on Unix what's the value of LANG environment variable?

--
Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode failure

2015-12-04 Thread Peter Otten
D'Arcy J.M. Cain wrote:

> I thought that going to Python 3.4 would solve my Unicode issues but it
> seems I still don't understand this stuff.  Here is my script.
> 
> #! /usr/bin/python3
> # -*- coding: UTF-8 -*-
> import sys
> print(sys.getdefaultencoding())
> print(u"\N{TRADE MARK SIGN}")
> 
> And here is my output.
> 
> utf-8
> Traceback (most recent call last):
>   File "./g", line 5, in 
> print(u"\N{TRADE MARK SIGN}")
> UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
> position 0: ordinal not in range(128)
> 
> What am I missing?

"""The character encoding is platform-dependent. Under Windows, if the 
stream is interactive (that is, if its isatty() method returns True), the 
console codepage is used, otherwise the ANSI code page. Under other 
platforms, the locale encoding is used (see locale.getpreferredencoding()).
"""

https://docs.python.org/dev/library/sys.html#sys.stdout



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


Re: counting unique numpy subarrays

2015-12-04 Thread Peter Otten
duncan smith wrote:

> Hello,
>   I'm trying to find a computationally efficient way of identifying
> unique subarrays, counting them and returning an array containing only
> the unique subarrays and a corresponding 1D array of counts. The
> following code works, but is a bit slow.
> 
> ###
> 
> from collections import Counter
> import numpy
> 
> def bag_data(data):
> # data (a numpy array) is bagged along axis 0
> # returns concatenated array and corresponding array of counts
> vec_shape = data.shape[1:]
> counts = Counter(tuple(arr.flatten()) for arr in data)
> data_out = numpy.zeros((len(counts),) + vec_shape)
> cnts = numpy.zeros((len(counts,)))
> for i, (tup, cnt) in enumerate(counts.iteritems()):
> data_out[i] = numpy.array(tup).reshape(vec_shape)
> cnts[i] =  cnt
> return data_out, cnts
> 
> ###
> 
> I've been looking through the numpy docs, but don't seem to be able to
> come up with a clean solution that avoids Python loops. 

Me neither :(

> TIA for any
> useful pointers. Cheers.

Here's what I have so far:

def bag_data(data):
counts = numpy.zeros(data.shape[0])
seen = {}
for i, arr in enumerate(data):
sarr = arr.tostring()
if sarr in seen:
counts[seen[sarr]] += 1
else:
seen[sarr] = i
counts[i] = 1
nz = counts != 0
return numpy.compress(nz, data, axis=0), numpy.compress(nz, counts)

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


Re: Unicode failure

2015-12-04 Thread Terry Reedy

On 12/4/2015 1:07 PM, D'Arcy J.M. Cain wrote:

I thought that going to Python 3.4 would solve my Unicode issues


Within Python itself, that should be mostly true.  As soon as you send 
text to a display, the rules of the display device take over.


#! /usr/bin/python3
# -*- coding: UTF-8 -*-


Redundant, as this is the default for 3.x


import sys
print(sys.getdefaultencoding())
print(u"\N{TRADE MARK SIGN}")

And here is my output.

utf-8
Traceback (most recent call last):
   File "./g", line 5, in 
 print(u"\N{TRADE MARK SIGN}")
UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in
position 0: ordinal not in range(128)


Tk widgets, and hence IDLE windows, will print any character from \u 
to \u without raising, even if the result is blank or ￿.  Higher 
codepoints fail, but allowing the entire BMP is better than any Windows 
codepage.


--
Terry Jan Reedy


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


msvcr100.dll missing ... error started after Windows 10 update to 10586.17

2015-12-04 Thread Glenn Linderman

My wife's 64-bit Win8 home machine has 32-bit Python 3.3 installed.

Then it upgraded to Win 8.1. Then I upgraded it to Win 10. Then I 
upgraded it to Threshold 2. It gets regular automatic updates also, like 
the one last night to build 10586.17.


That's the history.

When she tried a python script today, it failed, with an error saying 
that MSVCR100.dll was missing.


After a few false starts, like being surprised that the error happened 
when it worked yesterday, and that there was an MSVCR100.dll in 
%windir%\system32, doing a search for all MSVCR100.dll on her machine 
discovered quite a few in various application directories, but then also 
one in \windows.old\WINDOWS\SysWOW64, the light-bulb blinked on, I 
copied that one to the \python33 directory, and everything works.


Why M$ chose to delete MSVCR100.dll from %windir%\SysWOW64 in the recent 
update is a mystery, however.


So this is just a data point and warning and solution, not really an 
expectation that anyone will be able to explain M$.


Glenn
--
https://mail.python.org/mailman/listinfo/python-list


Re: counting unique numpy subarrays

2015-12-04 Thread duncan smith
On 04/12/15 22:36, Albert-Jan Roskam wrote:
> Hi
> 
> (Sorry for topposting)
> 
> numpy.ravel is faster than numpy.flatten (no copy)
> numpy.empty is faster than numpy.zeros
> numpy.fromiter might be useful to avoid the loop (just a hunch)
> 
> Albert-Jan
> 

Thanks, I'd forgotten the difference between numpy. flatten and
numpy.ravel. I wasn't even aware of numpy.empty.

Duncan

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


Re: counting unique numpy subarrays

2015-12-04 Thread duncan smith
On 04/12/15 23:06, Peter Otten wrote:
> duncan smith wrote:
> 
>> Hello,
>>   I'm trying to find a computationally efficient way of identifying
>> unique subarrays, counting them and returning an array containing only
>> the unique subarrays and a corresponding 1D array of counts. The
>> following code works, but is a bit slow.
>>
>> ###
>>
>> from collections import Counter
>> import numpy
>>
>> def bag_data(data):
>> # data (a numpy array) is bagged along axis 0
>> # returns concatenated array and corresponding array of counts
>> vec_shape = data.shape[1:]
>> counts = Counter(tuple(arr.flatten()) for arr in data)
>> data_out = numpy.zeros((len(counts),) + vec_shape)
>> cnts = numpy.zeros((len(counts,)))
>> for i, (tup, cnt) in enumerate(counts.iteritems()):
>> data_out[i] = numpy.array(tup).reshape(vec_shape)
>> cnts[i] =  cnt
>> return data_out, cnts
>>
>> ###
>>
>> I've been looking through the numpy docs, but don't seem to be able to
>> come up with a clean solution that avoids Python loops. 
> 
> Me neither :(
> 
>> TIA for any
>> useful pointers. Cheers.
> 
> Here's what I have so far:
> 
> def bag_data(data):
> counts = numpy.zeros(data.shape[0])
> seen = {}
> for i, arr in enumerate(data):
> sarr = arr.tostring()
> if sarr in seen:
> counts[seen[sarr]] += 1
> else:
> seen[sarr] = i
> counts[i] = 1
> nz = counts != 0
> return numpy.compress(nz, data, axis=0), numpy.compress(nz, counts)
> 

Three times as fast as what I had, and a bit cleaner. Excellent. Cheers.

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode failure

2015-12-04 Thread Random832
On 2015-12-04, Terry Reedy  wrote:
> Tk widgets, and hence IDLE windows, will print any character from \u 
> to \u without raising, even if the result is blank or �.  Higher 
> codepoints fail, but allowing the entire BMP is better than any Windows 
> codepage.

Well, any bar 1200, 1201, 12000, 12001, 65000, 65001, and 54936.

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


Re: Unicode failure

2015-12-04 Thread D'Arcy J.M. Cain
On Fri, 4 Dec 2015 22:49:49 +
Albert-Jan Roskam  wrote:
> I think you need to use a raw unicode string, ur
> 
> >>> unicodedata.name(ur'\u2122')
> 'TRADE MARK SIGN'

That seems to work in 2.x but not 3.x.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode failure

2015-12-04 Thread Terry Reedy

On 12/4/2015 10:22 PM, Random832 wrote:

On 2015-12-04, Terry Reedy  wrote:

Tk widgets, and hence IDLE windows, will print any character from \u
to \u without raising, even if the result is blank or �.  Higher
codepoints fail, but allowing the entire BMP is better than any Windows
codepage.


Well, any bar 1200, 1201, 12000, 12001, 65000, 65001, and 54936.


Test before you post.

>>> for cp in 1200, 1201, 12000, 12001, 65000, 65001, 54936:
print(chr(cp))


Ұ
ұ
⻠
⻡
�
�
횘



--
Terry Jan Reedy


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


Re: Unicode failure

2015-12-04 Thread D'Arcy J.M. Cain
On Fri, 4 Dec 2015 18:28:22 -0500
Terry Reedy  wrote:
> On 12/4/2015 1:07 PM, D'Arcy J.M. Cain wrote:
> > I thought that going to Python 3.4 would solve my Unicode issues
> 
> Within Python itself, that should be mostly true.  As soon as you
> send text to a display, the rules of the display device take over.

OK but my display (xterm) can display those characters.  I see it when
I dump unicode text from my database.

> > #! /usr/bin/python3
> > # -*- coding: UTF-8 -*-
> 
> Redundant, as this is the default for 3.x

I assumed so but belt and suspenders, right?

> Tk widgets, and hence IDLE windows, will print any character from
> \u to \u without raising, even if the result is blank or �.
> Higher codepoints fail, but allowing the entire BMP is better than
> any Windows codepage.

Not sure I follow all this but to be clear, I am not using Tk, Idle or
Windows.  I guess I should have mentioned that I am on Unix but I
thought that the hash-bang would have given that away.  To be complete,
I am running xterms on Xubuntu connected to NetBSD 7.0.  The data is
coming from a PostgreSQL 9.3.5 database.  I am using a beta of PyGreSQL
5.0 (I am the lead developer for it) and I checked and the type
returned is str, not bytes.  The database encoding is UTF8.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@vex.net VoIP: sip:da...@vybenetworks.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode failure

2015-12-04 Thread Chris Angelico
On Sat, Dec 5, 2015 at 5:06 PM, Terry Reedy  wrote:
> On 12/4/2015 10:22 PM, Random832 wrote:
>>
>> On 2015-12-04, Terry Reedy  wrote:
>>>
>>> Tk widgets, and hence IDLE windows, will print any character from \u
>>> to \u without raising, even if the result is blank or �.  Higher
>>> codepoints fail, but allowing the entire BMP is better than any Windows
>>> codepage.
>>
>>
>> Well, any bar 1200, 1201, 12000, 12001, 65000, 65001, and 54936.
>
>
> Test before you post.
>
 for cp in 1200, 1201, 12000, 12001, 65000, 65001, 54936:
> print(chr(cp))
>
>
> Ұ
> ұ
> ⻠
> ⻡
> �
> �
> 횘

Those numbers aren't codepoints, they're code pages. Specifically,
they're UTF-16, UTF-32, UTF-8, and I'm not sure what 54936 is.

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


Re: Unicode failure

2015-12-04 Thread Terry Reedy

On 12/4/2015 11:15 PM, D'Arcy J.M. Cain wrote:

On Fri, 4 Dec 2015 22:49:49 +
Albert-Jan Roskam  wrote:

I think you need to use a raw unicode string, ur


Nope.  The 'r' prefix does not disable unicode escapes.


unicodedata.name(ur'\u2122')

'TRADE MARK SIGN'


If if did, the string above would have 6 chars instead of 1 and the 
above would not work.



That seems to work in 2.x but not 3.x.


'u' was restored in 3.3 as a do-nothing prefix, but the combination 'ur' 
was not.


--
Terry Jan Reedy

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