Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-08 Thread shaunak . bangale
On Sunday, February 7, 2016 at 1:23:32 AM UTC-7, dieter wrote:
> Shaunak Bangale  writes:
> > ...
> > while (count > 0):
> > try :
> > # read line from file:
> > print(file.readline())
> > # parse
> > parse_json(file.readline())
> > count = count - 1
> > except socket.error as e:
> > print('Connection fail', e)
> > print(traceback.format_exc())
> > ...
> > Error:
> > except socket.error as e:
> >  ^
> > SyntaxError: invalid syntax
> 
> Are you sure, that there is no invisible character at the end
> of that line?
> 
> When I try code like the above (in PYthon 2.7), there is no
> "SyntaxError":
> 
> >>> while(False):
> ... try :
> ... # read line from file:
> ... print(file.readline())
> ... # parse
> ... parse_json(file.readline())
> ... count = count - 1
> ... except socket.error as e:
> ... print('Connection fail', e)
> ... print(traceback.format_exc())
> ... 
> >>> 
> 
> 
> Another reason for your problem could be an older Python version.
> The "as" construct as part of the "except" clause is a more recent
> addition to Python.

Hi Dieter,

I typed that line again but didn't help. I am using Python 3.5.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Install Error

2016-02-08 Thread Mike S via Python-list

On 2/3/2016 1:55 PM, Barrie Taylor wrote:

Hi,

I am attempting to install and run Python3.5.1 on my Windows machine.

After installation on launching I am presented the attached error message.
It reads:
'The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing 
from your computer. Try reinstalling the program to fix this problem.'
Needless to say, I have tried uninstalling and reinstalling ad repairing the 
program a number of times to no avail.

Any help with this issue would be greatly appreciated.

Kind Regards,

Barrie Taylor


What OS? On my XP machine I got 3.4.4. to work, although I couldn't 
figure out how to get IPython notebook to work, so I'm focusing on 
getting that to work on my w7 and w10 boxes. Idk if you need Ipython.


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


Re: setup failed

2016-02-08 Thread Mike S via Python-list

On 2/4/2016 4:39 AM, Prince Thomas wrote:

Hi
I am an computer science engineer. I downloaded the python version 3.5.1.amd64 
and just python 3.5.1.
The problem is when I install the program setup is failed and showing  
0*80070570-The file or directory is
corrupted and unreadable. I install the newest visual c++ redist and still 
same. My os is win 8.1.
Please help me out of this


This sounds similar. Did you install the 32 bit or 64 bit version? If 
you installed the 32 bit version and don't object to using the 64bit 
version I would try installing that.

https://mail.python.org/pipermail/python-list/2015-September/697456.html

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


coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M

Exceptions can be raised inside a coroutine using the throw(

Exceptions raised in this manner will originate at the currently 
executing yield state-ment in the coroutine.A coroutine can elect to 
catch exceptions and handle them as appropriate. It is not safe to use 
throw() as an asynchronous signal to a coroutine—it should never be 
invoked from a separate execution thread or in a signal handler.


What does Beazley mean by this: 'will originate at the currently 
executing yield state-ment in the coroutine'

If he's throw'ing an exception surely it originates at the throw:

def mycoroutine():
 while len(n) > 2: 
   n = (yield)

 throw('RuntimeError' "die!") 
--
Also: 'not safe to use throw() as an asynchronous signal to a coroutine—
it should never be invoked from a separate execution thread or in a 
signal handler.'

You can use throw within a coroutine to raise an exception.
How would you use it as an async-sig to a coroutine..
eg: you have two threads 
1. coroutine does except FooException:
2. throw(FooException, 'message')

so moment 'throw' runs and an exception is raised.. it'll propagate 
within thread-2 to its parent etc - how is thread-1 affected?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M
Veek. M wrote:

> 
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> 
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>n = (yield)
> 
>  throw('RuntimeError' "die!")
> --
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?
Veek. M wrote:

> 
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> 
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>n = (yield)
> 
>  throw('RuntimeError' "die!")
> --
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?

Also this bit:
***
If a coroutine returns values, some care is required if exceptions 
raised with throw() are being handled. If you raise an exception in a 
coroutine using throw(), the value passed to the next yield in the 
coroutine will be returned as the result of throw(). If
you need this value and forget to save it, it will be lost.
***

def coroutine():
  while True:
   line = (yield result)

  throw(FooException)

where is the question of a 'yield'? You'll exit the coroutine straight 
away..
-- 
https://mail.python.org/mailman/listinfo/python-list


Set Operations on Dicts

2016-02-08 Thread Marco Kaulea
Hi,

In one talk (I think it was [1]) it was described that sets are basically
dicts without the values.
Therefor it should be easy to apply set operations on dicts, for example:
{'a': 123, 'b': 456} & {'a'} => {'a': 123}
{'a': 123, 'b': 456} - {'a'} => {'b': 456}

This if currently not implemented. Is there a technical reason that this is
difficult or are
there a lot of corner cases that make it not worth the trouble?

I have not spend a lot of time on this, I just needed a feature like that
and was surprised that
it did not exists.

- Marco

[1] https://www.youtube.com/watch?v=C4Kc8xzcA68
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best/neatest way to get Unicode data from a database into a grid cell?

2016-02-08 Thread cl
Dietmar Schwertberger  wrote:
> On 07.02.2016 12:19, c...@isbd.net wrote:
> > However my database has quite a lot of Unicode data as there are
> > French (and other) names with accents etc.  What's the right way to
> > handle this reasonably neatly?  At the moment it traps an error at
> > line 37:-
> >  self.SetCellValue(row_num, i, str(cells[i]))
> The unicode versions of wxPython should have no problems with handling 
> unicode strings.
> The problem that you see here is that str(...) tries to convert your 
> unicode data into a non-unicode string, which of course fails.
> Do something like:
> 
> value = cells[i]
> if not isinstance(value, basestring):
> value = str(value)
> self.SetCellValue(row_num, i, value)
> 
Thanks, worked perfectly!  I had been trying to produce something
similar in tkinter but grids are a bit clumsy in tkinter.  This wx
implementation will do me nicely.


> Once you switch to Python 3 and Phoenix you have to modify this 
> slightly, e.g. by adding this to the top of your code:
> 
> try:
> basestring
> except:
> basestring = (bytes,str)
> 
Everything else is 3 compatible so moving should be fairly painless
if/when phoenix becomes available.


-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best/neatest way to get Unicode data from a database into a grid cell?

2016-02-08 Thread cl
Vlastimil Brom  wrote:
> 
> Hi,
> your code in
> http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example
> 
> seems to work for me after small changes with both python 2.7 and 3.4
> (using wx Phoenix)

Where are you getting phoenix from?  It's not in the Ubuntu
repositories and when I search on Google I seem to go in ever
decreasing circles and always end up finding wxPython for 2.7.

Did you build it from source yourself (I can do that but only if it's
necessary).


> the changes I made are:
> - encoding declaration at the beginning of the file (mainly for py 2,
> if utf-8 is used):
> 
> #! Python
> # -*- coding: utf-8 -*-
> 
> - removing the str(...) call in the offending line you mentioned:
>  self.SetCellValue(row_num, i, cells[i])
> (what was the intent of the conversion to str?)
> 
I've no idea what the intent is/was, I just copied the code as I found
it.  I guess the cell value could be numeric.


> - corrected event name (insted of EVT_GRID_CELL_CHANGE)
> self.Bind(gridlib.EVT_GRID_CELL_CHANGED, self.CellContentsChanged)
> 
> - normal App() call (instead of the PySimpleApp)
> 
> app = wx.App()
> 
> I randomly tried some characters using Latin, Greek etc. characters
> (within BMP) as well as Gothic - beyond the  range - the cell
> content seems to be saved and and newly loaded from the sqlite db on
> subsequent calls of the app.
> 
Thanks for your help, very useful.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best/neatest way to get Unicode data from a database into a grid cell?

2016-02-08 Thread Chris Angelico
On Mon, Feb 8, 2016 at 9:42 PM,   wrote:
> Vlastimil Brom  wrote:
>>
>> Hi,
>> your code in
>> http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example
>>
>> seems to work for me after small changes with both python 2.7 and 3.4
>> (using wx Phoenix)
>
> Where are you getting phoenix from?  It's not in the Ubuntu
> repositories and when I search on Google I seem to go in ever
> decreasing circles and always end up finding wxPython for 2.7.
>
> Did you build it from source yourself (I can do that but only if it's
> necessary).
>
>

http://wiki.wxpython.org/ProjectPhoenix has links to the source code
and to daily snapshots. I suspect the easiest way to get it will be
from source.

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


Re: setup failed

2016-02-08 Thread Mark Summerfield
If you need 32-bit Python on Windows my advice is to install 3.4. If you need 
32-bit and 64-bit Python on Windows, then I think it will only work with 3.4 
(or older), but not with 3.5's new installer.

I have tried installing 3.5.0 and 3.5.1 on several machines both 32- and 64-bit 
Windows. The 32-bit installer either doesn't work at all or won't work with two 
third-party packages I use: APSW and PyWin32. The 64-bit installer does seem to 
work on 64-bit machines, but in my particular case I need both 32- and 64-bit 
versions so use Python 3.4. Hopefully the problem will be fixed before 3.4's 
end of life:-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best/neatest way to get Unicode data from a database into a grid cell?

2016-02-08 Thread Chris Angelico
On Mon, Feb 8, 2016 at 9:22 PM,   wrote:
>> Once you switch to Python 3 and Phoenix you have to modify this
>> slightly, e.g. by adding this to the top of your code:
>>
>> try:
>> basestring
>> except:
>> basestring = (bytes,str)
>>
> Everything else is 3 compatible so moving should be fairly painless
> if/when phoenix becomes available.

Small point: Even for code as small as this, I would be inclined to
catch only the one exception you care about - in this case, NameError.
I try to avoid having a bare except clause anywhere other than a "log
and continue" or "react and reraise" kind of situation.

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


Re: setup failed

2016-02-08 Thread Mark Summerfield
On Monday, February 8, 2016 at 10:50:19 AM UTC, Mark Summerfield wrote:
> If you need 32-bit Python on Windows my advice is to install 3.4. If you need 
> 32-bit and 64-bit Python on Windows, then I think it will only work with 3.4 
> (or older), but not with 3.5's new installer.
> 
> I have tried installing 3.5.0 and 3.5.1 on several machines both 32- and 
> 64-bit Windows. The 32-bit installer either doesn't work at all or won't work 
> with two third-party packages I use: APSW and PyWin32. The 64-bit installer 
> does seem to work on 64-bit machines, but in my particular case I need both 
> 32- and 64-bit versions so use Python 3.4. Hopefully the problem will be 
> fixed before 3.4's end of life:-)

Ooops, of course you're already using the 64-bit installer. My advice is use 
Python 3.4 if you need to work on Windows: unlike the 3.5 installer I've never 
encountered a problem with the 3.4 (or 3.3) installers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Changing logging level only for my code?

2016-02-08 Thread egarrulo
I am using the "logging" module for my own package, but changing the level from 
"INFO" to "DEBUG" enables debugging statements from third-party libraries as 
well.  How can I avoid them?  Here is the code I am using:

import logging

logger = logging.getLogger(__name__)
log_file = logging.FileHandler("activity.log", mode='w')
log_file.setFormatter(logging.Formatter(fmt='%(levelname)s: %(message)s'))
logger.addHandler(log_file)
logging.basicConfig(level=logging.INFO) # or DEBUG

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


Re: Heap Implementation

2016-02-08 Thread Cem Karan
On Feb 7, 2016, at 10:15 PM, srinivas devaki  wrote:
> On Feb 8, 2016 7:07 AM, "Cem Karan"  wrote:
> > I know that there are methods of handling this from the client-side (tuples 
> > with unique counters come to mind), but if your library can handle it 
> > directly, then that could be useful to others as well.
> 
> yeah it is a good idea to do at client side.
> but if it should be introduced as feature into the library, instead of 
> tuples, we should just piggyback a single counter it to the self._indexes 
> dict, or better make another self._counts dict which will be light and fast.
> and if you think again with this method you can easily subclass with just 
> using self._counts dict  in your subclass. but still I think it is good to 
> introduce it as a feature in the library.
> 
> Regards
> Srinivas Devaki

Just to be 100% sure, you do mean to use the counters as UUIDs, right?  I don't 
mean that the elements in the heap get counted, I meant that the counter is a 
trick to separate different instances of (item, priority) pairs when you're 
pushing in the same item multiple times, but with different priorities.

Thanks,
Cem Karan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing logging level only for my code?

2016-02-08 Thread jmp

On 02/08/2016 12:29 PM, egarr...@gmail.com wrote:

I am using the "logging" module for my own package, but changing the level from "INFO" to 
"DEBUG" enables debugging statements from third-party libraries as well.  How can I avoid them?  
Here is the code I am using:

 import logging

 logger = logging.getLogger(__name__)
 log_file = logging.FileHandler("activity.log", mode='w')
 log_file.setFormatter(logging.Formatter(fmt='%(levelname)s: %(message)s'))
 logger.addHandler(log_file)
 logging.basicConfig(level=logging.INFO) # or DEBUG

Thank you.



Hi,

basicConfig will configure the *root* logger, the parent of all loggers. 
You need to set it to DEBUG, otherwise no DEBUG logs will be displayed.


However you can set levels to loggers independently, and you simply need 
to configure the third party logger to whatever level you want.


logging.basicConfig(level=logging.DEBUG)
logging.getLogger("yourthirdpartyloggername").setLevel(logging.INFO)

JM

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


Re: Changing logging level only for my code?

2016-02-08 Thread Peter Otten
egarr...@gmail.com wrote:

> I am using the "logging" module for my own package, but changing the level
> from "INFO" to "DEBUG" enables debugging statements from third-party
> libraries as well.  How can I avoid them?  Here is the code I am using:
> 
> import logging
> 
> logger = logging.getLogger(__name__)
> log_file = logging.FileHandler("activity.log", mode='w')
> log_file.setFormatter(logging.Formatter(fmt='%(levelname)s:
> %(message)s')) logger.addHandler(log_file)
> logging.basicConfig(level=logging.INFO) # or DEBUG

You can set the level for your logger, e. g.

$ cat loglevels.py
import logging

logging.basicConfig(
level=logging.INFO,
filename="activity.log",
format='%(levelname)s: %(message)s')

mylogger = logging.getLogger(__name__)
mylogger.setLevel(logging.DEBUG)

mylogger.info("my info")
mylogger.debug("my debug message")

otherlogger = logging.getLogger("some.lib")

otherlogger.info("some.lib info")
otherlogger.debug("some.lib debug message")

$ python3 loglevels.py 
$ cat activity.log 
INFO: my info
DEBUG: my debug message
INFO: some.lib info


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


New mailing for Python in Belgium

2016-02-08 Thread Stephane Wirtel

Hello everyone,

I would like to announce a new mailing list for the Belgian Python 
Users.


This main goal of this mailing list is mainly for the Python Community 
of Belgium, Dutch, French and German.
The main language is English, I don’t want to discuss about the 
languages, maybe in the future.


So, here is the mailing list: belg...@python.org you can subscribe via 
this link: https://mail.python.org/mailman/listinfo/belgium


Via this mailing list, you could discuss about:

* Python in Belgium
* Organise some events, meetups, AFPyro in Belgium
* Create a great community.
* Help for the PythonFOSDEM in Belgium
* etc…

Hope to see you in this ML.

Thank you so much,

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


[newbie] problem with geometry setting in Tkinter

2016-02-08 Thread jenswaelkens
I'm trying to set the geometry of my top window, but the size is
unaffected.
This is the code:

#!/usr/bin/env python
import Tkinter
top=Tkinter.Tk()
top.geometry=('900x460')
top.update_idletasks()
print (top.winfo_width())
print (top.winfo_height())
print (top.winfo_geometry())
top.mainloop()

and this is the result when running the code:
200
200
200x200+1+584


Can anyone here tell me what I am doing wrong and how to change it?
Thanks

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


Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Marco Kaulea writes:

> In one talk (I think it was [1]) it was described that sets are
> basically dicts without the values.  Therefor it should be easy to
> apply set operations on dicts, for example:
>
> {'a': 123, 'b': 456} & {'a'} => {'a': 123}
> {'a': 123, 'b': 456} - {'a'} => {'b': 456}
>
> This if currently not implemented. Is there a technical reason that
> this is difficult or are there a lot of corner cases that make it not
> worth the trouble?

I think nobody was quite willing to lay down the law on which dictionary
would take precedence when they have keys in common but different values
on those keys. Both ways make sense, and sometimes you want something
like arithmetic done to combine the values on common keys.

(A collection.Counter does some version of the latter, I think. Maybe
the answer is to create a custom class that allows what you want?)

But that's an interesting proposal to only allow sets as the second
argument. Those particular cases may not be *too* difficult to express
as comprehensions, though still quite a mouthful compared to your
suggestion:

{ k:d[k] for k in d if k in s } # d & s

{ k:d[k] for k in d if k not in s } # d - s

Perhaps there is already some more compact expression for these?

Also, what would be the nicest current way to express a priority union
of dicts?

{ k:(d if k in d else e)[k] for k in d.keys() | e.keys() }

> I have not spend a lot of time on this, I just needed a feature like
> that and was surprised that it did not exists.
>
> - Marco
>
> [1] https://www.youtube.com/watch?v=C4Kc8xzcA68
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] problem with geometry setting in Tkinter

2016-02-08 Thread Peter Otten
jenswaelk...@gmail.com wrote:

> I'm trying to set the geometry of my top window, but the size is
> unaffected.
> This is the code:
> 
> #!/usr/bin/env python
> import Tkinter
> top=Tkinter.Tk()
> top.geometry=('900x460')

That's an assignment, but geometry() is a method that you have to invoke:

top.geometry('900x460')

> top.update_idletasks()
> print (top.winfo_width())
> print (top.winfo_height())
> print (top.winfo_geometry())
> top.mainloop()
> 
> and this is the result when running the code:
> 200
> 200
> 200x200+1+584
> 
> 
> Can anyone here tell me what I am doing wrong and how to change it?
> Thanks
> 
> Jens


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


Re: Set Operations on Dicts

2016-02-08 Thread Marco Kaulea
On Mon, Feb 8, 2016 at 1:17 PM, Jussi Piitulainen <
jussi.piitulai...@helsinki.fi> wrote:

> I think nobody was quite willing to lay down the law on which dictionary
> would take precedence when they have keys in common but different values
> on those keys. Both ways make sense, and sometimes you want something
> like arithmetic done to combine the values on common keys.
>
I don't expect this to be really useful. I only thought about using a set
as a second argument.


But that's an interesting proposal to only allow sets as the second
> argument. Those particular cases may not be *too* difficult to express
> as comprehensions, though still quite a mouthful compared to your
> suggestion:
>
That is the restriction I had in mind.

{ k:d[k] for k in d if k in s } # d & s
>
> { k:d[k] for k in d if k not in s } # d - s
>
> That is basically what I did. But I expect this could be quite slow, since
it has to take
each value in s and perform a lookup in d. I would expect the pure set
implementation
to be more optimized.


Also, what would be the nicest current way to express a priority union
> of dicts?
>
> { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }

This seems like it might be useful for default configurations, as that is
currently
quite the hassle to do with `x = conf.get('x', fallback="default")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set Operations on Dicts

2016-02-08 Thread Grobu

You can use dictionary comprehension :

Say :
dict1 = {'a': 123, 'b': 456}
set1 = {'a'}

intersection :
>>> { key:dict1[key] for key in dict1 if key in set1 }
{'a': 123}

difference :
>>> { key:dict1[key] for key in dict1 if not key in set1 }
{'b': 456}

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


Re: Set Operations on Dicts

2016-02-08 Thread Matt Wheeler
On 8 February 2016 at 12:17, Jussi Piitulainen
 wrote:
> Also, what would be the nicest current way to express a priority union
> of dicts?
>
> { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }

Since Python 3.5: {**e, **d}


-- 
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Matt Wheeler writes:

> On 8 February 2016 at 12:17, Jussi Piitulainen wrote:
>> Also, what would be the nicest current way to express a priority union
>> of dicts?
>>
>> { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }
>
> Since Python 3.5: {**e, **d}

Thanks. I have considered this news briefly and I think I'll like it :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] problem with geometry setting in Tkinter

2016-02-08 Thread jenswaelkens
Op maandag 8 februari 2016 13:26:56 UTC+1 schreef Peter Otten:
> jenswaelk...@gmail.com wrote:
> 
> > I'm trying to set the geometry of my top window, but the size is
> > unaffected.
> > This is the code:
> > 
> > #!/usr/bin/env python
> > import Tkinter
> > top=Tkinter.Tk()
> > top.geometry=('900x460')
> 
> That's an assignment, but geometry() is a method that you have to invoke:
> 
> top.geometry('900x460')
> 
> > top.update_idletasks()
> > print (top.winfo_width())
> > print (top.winfo_height())
> > print (top.winfo_geometry())
> > top.mainloop()
> > 
> > and this is the result when running the code:
> > 200
> > 200
> > 200x200+1+584
> > 
> > 
> > Can anyone here tell me what I am doing wrong and how to change it?
> > Thanks
> > 
> > Jens

thanks a lot for helping me out

kind regards,
Jens
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A sets algorithm

2016-02-08 Thread Random832
On Sun, Feb 7, 2016, at 20:07, Cem Karan wrote:
>   a) Use Chris Angelico's suggestion and hash each of the files (use the 
> standard library's 'hashlib' for this).  Identical files will always have 
> identical hashes, but there may be false positives, so you'll need to verify 
> that files that have identical hashes are indeed identical.
>   b) If your files tend to have sections that are very different (e.g., 
> the first 32 bytes tend to be different), then you pretend that section of 
> the file is its hash.  You can then do the same trick as above. (the 
> advantage of this is that you will read in a lot less data than if you have 
> to hash the entire file).
>   c) You may be able to do something clever by reading portions of each 
> file.  That is, use zip() combined with read(1024) to read each of the files 
> in sections, while keeping hashes of the files.  Or, maybe you'll be able to 
> read portions of them and sort the list as you're reading.  In either case, 
> if any files are NOT identical, then you'll be able to stop work as soon as 
> you figure this out, rather than having to read the entire file at once.
> 
> The main purpose of these suggestions is to reduce the amount of reading
> you're doing.

hashing a file using a conventional hashing algorithm requires reading
the whole file. Unless the files are very likely to be identical _until_
near the end, you're better off just reading the first N bytes of both
files, then the next N bytes, etc, until you find somewhere they're
different. The filecmp module may be useful for this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set Operations on Dicts

2016-02-08 Thread Random832
On Mon, Feb 8, 2016, at 08:32, Matt Wheeler wrote:
> On 8 February 2016 at 12:17, Jussi Piitulainen
>  wrote:
> > Also, what would be the nicest current way to express a priority union
> > of dicts?
> >
> > { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }
> 
> Since Python 3.5: {**e, **d}

And before that... dict(ChainMap(d, e))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best/neatest way to get Unicode data from a database into a grid cell?

2016-02-08 Thread Vlastimil Brom
2016-02-08 11:42 GMT+01:00  :
> Vlastimil Brom  wrote:
>>
>> Hi,
>> your code in
>> http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example
>>
>> seems to work for me after small changes with both python 2.7 and 3.4
>> (using wx Phoenix)
>
> Where are you getting phoenix from?  It's not in the Ubuntu
> repositories and when I search on Google I seem to go in ever
> decreasing circles and always end up finding wxPython for 2.7.
>
> Did you build it from source yourself (I can do that but only if it's
> necessary).
>
>
...
Hi,
I'm glad it helped a bit. However, I have no experiences with running
wx phoenix on linux - I use it with windows (7) - where the mentioned
snapshot builds are available (e.g. as wheel files installable with
pip).
http://wxpython.org/Phoenix/snapshot-builds/
I believe, building and installing on linux should be doable, but I
can't supply any details, see e.g.:
http://wxpython.org/Phoenix/docs/html/#linux-unix-etc
the source files are available along with the snapshot builds:
http://wxpython.org/Phoenix/snapshot-builds/
hth,
 vbr
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Random832 writes:

> On Mon, Feb 8, 2016, at 08:32, Matt Wheeler wrote:
>> On 8 February 2016 at 12:17, Jussi Piitulainen wrote:
>> > Also, what would be the nicest current way to express a priority union
>> > of dicts?
>> >
>> > { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }
>> 
>> Since Python 3.5: {**e, **d}
>
> And before that... dict(ChainMap(d, e))

New in version 3.3. Thanks, I didn't know of that either. Reading the
docs, I think for the use I had in mind, ChainMap(d, e) itself would
suffice.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A sets algorithm

2016-02-08 Thread Chris Angelico
On Tue, Feb 9, 2016 at 1:49 AM, Random832  wrote:
> On Sun, Feb 7, 2016, at 20:07, Cem Karan wrote:
>>   a) Use Chris Angelico's suggestion and hash each of the files (use the 
>> standard library's 'hashlib' for this).  Identical files will always have 
>> identical hashes, but there may be false positives, so you'll need to verify 
>> that files that have identical hashes are indeed identical.
>>   b) If your files tend to have sections that are very different (e.g., 
>> the first 32 bytes tend to be different), then you pretend that section of 
>> the file is its hash.  You can then do the same trick as above. (the 
>> advantage of this is that you will read in a lot less data than if you have 
>> to hash the entire file).
>>   c) You may be able to do something clever by reading portions of each 
>> file.  That is, use zip() combined with read(1024) to read each of the files 
>> in sections, while keeping hashes of the files.  Or, maybe you'll be able to 
>> read portions of them and sort the list as you're reading.  In either case, 
>> if any files are NOT identical, then you'll be able to stop work as soon as 
>> you figure this out, rather than having to read the entire file at once.
>>
>> The main purpose of these suggestions is to reduce the amount of reading
>> you're doing.
>
> hashing a file using a conventional hashing algorithm requires reading
> the whole file. Unless the files are very likely to be identical _until_
> near the end, you're better off just reading the first N bytes of both
> files, then the next N bytes, etc, until you find somewhere they're
> different. The filecmp module may be useful for this.

That's fine for comparing one file against one other. He started out
by saying he already had a way to compare files for equality. What he
wants is a way to capitalize on that to find all the identical files
in a group. A naive approach would simply compare every file against
every other, for O(N*N) comparisons - but a hash lookup can make that
O(N) on the files themselves, plus (I think) an O(N log N) hash
comparison job, which has much lower constant factors. The key here is
the hashing algorithm though.

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


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Jason Swails
On Sun, Feb 7, 2016 at 2:58 AM, Chris Angelico  wrote:

>
> Would writing a script to figure out whether there are more
> statisticians or programmers be a statistician's job or a
> programmer's?
>

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


Re: [newbie] problem with geometry setting in Tkinter

2016-02-08 Thread Christian Gollwitzer

Am 08.02.16 um 15:34 schrieb jenswaelk...@gmail.com:

Op maandag 8 februari 2016 13:26:56 UTC+1 schreef Peter Otten:

jenswaelk...@gmail.com wrote:


I'm trying to set the geometry of my top window, but the size is
unaffected.
This is the code:


top.geometry('900x460')





thanks a lot for helping me out


The answer is correct, however I think that in general it is wrong to 
set the geometry like this. grid and pack compute the necessary space 
for the window themselves, which adapts to the length of labels on 
buttons etc. In all GUI programs I've written, the only place to set 
window sizes in pixels have been canvas widgets, which do not have a 
native size derived from the content. Setting the size on the toplevel, 
and in absolute numbers, i.e. not computed from other sizes, is likely 
to lead to problems on different platforms, font sizes or theme changes.


Christian

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


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Ian Kelly
On Sat, Feb 6, 2016 at 1:54 PM, Rick Johnson
 wrote:
> On Wednesday, February 3, 2016 at 12:02:35 AM UTC-6, John Ladasky wrote:
>
>> Rick, you don't like Python?
>
> If i didn't like Python, then i would happily let it self-
> destruct, yes? The problem is, i *DO* like Python. Python2
> was a great language, but python3 has been a disaster. Heck,
> even the BDFL has warned that Python4 cannot introduce as
> many backwards incompatible changes as python3. So obviously,
> he is admitting that Python3 was a disaster.

That's a complete non sequitur. "George Lucas notes that he can't pull
the Darth Vader reveal again. So obviously, he is admitting that
Empire Strikes Back was a disaster."

Besides, you're forgetting that the whole point of having so many
backwards incompatible changes in Python 3 in the first place was to
get them out of the way and not have to do them further into the
future. Python 4.0 has never been planned to be anything more than an
incremental release like Python 2.0, and AIUI the only reason any of
the core devs are even talking about Python 4 at this point in time is
because Guido doesn't like "3.10" as a version number.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Random832
On Mon, Feb 8, 2016, at 10:40, Ian Kelly wrote:
> Besides, you're forgetting that the whole point of having so many
> backwards incompatible changes in Python 3 in the first place was to
> get them out of the way and not have to do them further into the
> future. Python 4.0 has never been planned to be anything more than an
> incremental release like Python 2.0, and AIUI the only reason any of
> the core devs are even talking about Python 4 at this point in time is
> because Guido doesn't like "3.10" as a version number.

I still think we should just retroactively declare 3.5 to be python 5,
and then keep going with python 6, 7, etc...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Chris Angelico
On Tue, Feb 9, 2016 at 2:44 AM, Random832  wrote:
> On Mon, Feb 8, 2016, at 10:40, Ian Kelly wrote:
>> Besides, you're forgetting that the whole point of having so many
>> backwards incompatible changes in Python 3 in the first place was to
>> get them out of the way and not have to do them further into the
>> future. Python 4.0 has never been planned to be anything more than an
>> incremental release like Python 2.0, and AIUI the only reason any of
>> the core devs are even talking about Python 4 at this point in time is
>> because Guido doesn't like "3.10" as a version number.
>
> I still think we should just retroactively declare 3.5 to be python 5,
> and then keep going with python 6, 7, etc...

http://dirtsimple.org/2004/12/python-is-not-java.html

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


Confused by wxpython documentation

2016-02-08 Thread cl
I'm playing around with some existing code that uses wxpython.  I've
been trying to understand a basic bit about the import statement and
so went to the beginning of the wxPython on line documents.

Going from the top to the "Hello World Example" (can't give URL as the
URL is the same for all the docs, http://www.wxpython.org/onlinedocs.php)
the first thing I see is some C/C++ code.  What's this about? How have
I got to the underlying C/C++ implementation rather than the Python
documentation?

I realise the Python usage follows the underlying C/C++ very closely
but seeing the C/C++ doesn't really help me write my Python too much.

Looking more closely what I've actually been taken to is the wxWidgets
documentation which, not unreasonably, is in C/C++.

... but where do I find the Python documentation for this?

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


A question about imports in wxpython

2016-02-08 Thread cl
I'm playing with some code that uses the wxpython grid.  *Every*
example I have seen starts with the imports:-

import wx
import wx.grid as Gridlib

As Gridlib is exactly the same number of characters as wx.grid I
really don't see the point.  Am I missing something?

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Random832
On Mon, Feb 8, 2016, at 10:46, Chris Angelico wrote:
> > I still think we should just retroactively declare 3.5 to be python 5,
> > and then keep going with python 6, 7, etc...
> 
> http://dirtsimple.org/2004/12/python-is-not-java.html

Java's hardly the only, or even the first, project to drop a version
number. (I think the first may actually have been GNU Emacs), and it's
certainly not the only one with a release schedule that frequently
increments the major version number.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set Operations on Dicts

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 5:47 AM, Grobu  wrote:
> You can use dictionary comprehension :
>
> Say :
> dict1 = {'a': 123, 'b': 456}
> set1 = {'a'}
>
> intersection :
 { key:dict1[key] for key in dict1 if key in set1 }
> {'a': 123}
>
> difference :
 { key:dict1[key] for key in dict1 if not key in set1 }
> {'b': 456}

dict does already expose set-like views. How about:

{k: d[k] for k in d.keys() & s}  # d & s
{k: d[k] for k in d.keys() - s}  # d - s
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A question about imports in wxpython

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 8:44 AM,   wrote:
> I'm playing with some code that uses the wxpython grid.  *Every*
> example I have seen starts with the imports:-
>
> import wx
> import wx.grid as Gridlib
>
> As Gridlib is exactly the same number of characters as wx.grid I
> really don't see the point.  Am I missing something?

You're not missing anything. I've actually never seen that before (or
at least never noticed). The first hit when searching "import wx.grid"
is http://wxpython.org/Phoenix/docs/html/grid_overview.html which
doesn't use the "as" (but I see some hits farther down that do).

Probably the author of that code was just trying to save a dict lookup
every time "wx.grid" is referenced (which has to look up "wx" in the
globals and then "grid" as an attribute). Seems like an unnecessary
micro-optimization to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Confused by wxpython documentation

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 8:36 AM,   wrote:
> I'm playing around with some existing code that uses wxpython.  I've
> been trying to understand a basic bit about the import statement and
> so went to the beginning of the wxPython on line documents.
>
> Going from the top to the "Hello World Example" (can't give URL as the
> URL is the same for all the docs, http://www.wxpython.org/onlinedocs.php)
> the first thing I see is some C/C++ code.  What's this about? How have
> I got to the underlying C/C++ implementation rather than the Python
> documentation?
>
> I realise the Python usage follows the underlying C/C++ very closely
> but seeing the C/C++ doesn't really help me write my Python too much.
>
> Looking more closely what I've actually been taken to is the wxWidgets
> documentation which, not unreasonably, is in C/C++.
>
> ... but where do I find the Python documentation for this?

wiki.wxpython.org was the site I used when I was developing wxPython.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Confused by wxpython documentation

2016-02-08 Thread cl
Ian Kelly  wrote:
> On Mon, Feb 8, 2016 at 8:36 AM,   wrote:
> > I'm playing around with some existing code that uses wxpython.  I've
> > been trying to understand a basic bit about the import statement and
> > so went to the beginning of the wxPython on line documents.
> >
> > Going from the top to the "Hello World Example" (can't give URL as the
> > URL is the same for all the docs, http://www.wxpython.org/onlinedocs.php)
> > the first thing I see is some C/C++ code.  What's this about? How have
> > I got to the underlying C/C++ implementation rather than the Python
> > documentation?
> >
> > I realise the Python usage follows the underlying C/C++ very closely
> > but seeing the C/C++ doesn't really help me write my Python too much.
> >
> > Looking more closely what I've actually been taken to is the wxWidgets
> > documentation which, not unreasonably, is in C/C++.
> >
> > ... but where do I find the Python documentation for this?
> 
> wiki.wxpython.org was the site I used when I was developing wxPython.

Thanks, a good place to start, and it tells me why I get to the C/C++
docs for wxWidgets.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-08 Thread Chris Warrick
On 8 February 2016 at 16:53, Random832  wrote:
> On Mon, Feb 8, 2016, at 10:46, Chris Angelico wrote:
>> > I still think we should just retroactively declare 3.5 to be python 5,
>> > and then keep going with python 6, 7, etc...
>>
>> http://dirtsimple.org/2004/12/python-is-not-java.html
>
> Java's hardly the only, or even the first, project to drop a version
> number. (I think the first may actually have been GNU Emacs), and it's
> certainly not the only one with a release schedule that frequently
> increments the major version number.
> --
> https://mail.python.org/mailman/listinfo/python-list

In fact, this was done by a very popular Python project two years ago.
That project is pip, which went from 1.5.6 to 6.0, and is now at
8.0.2.

And its best friend setuptools is up to version 20.0.

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Syntax Highlightning for C API of CPython in VIM

2016-02-08 Thread Stephane Wirtel

Hi everyone,

With my talk "Exploring our Python Interpreter", I think this VIM plugin 
can be useful for the community. It's a syntax highlighter for the C API 
of CPython 3.5 and 3.6. I used Clang for the parsing and automatically 
generated the keywords for VIM.


PyObject and the others typedefs of CPython will have the defined color 
of your favourite editor and it's the same for the enums, the typedefs, 
the functions and the macros.


Where can you use this VIM plugin ? If you want to write a CPython 
extension or if you want to hack in the CPython code.


Check this screenshot: http://i.imgur.com/0k13KOU.png

Here is the repository:

https://github.com/matrixise/cpython-vim-syntax

Please, if you see some issues, tell me via an issue on Github.

Thank you so much,

Stephane

--
Stéphane Wirtel - http://wirtel.be - @matrixise
--
https://mail.python.org/mailman/listinfo/python-list


Help using Thread (or other method)

2016-02-08 Thread Brendan Simon (eTRIX)
Hi.  Need some urgent help.

I have a python app that uses `select` to wait for data from an arm
embedded linux kernel interrupt (every second).  The mainloop of the app
then grabs data from some memory mapped area, processes it and then does
a http post to a server.

The problem is the http post can take multiple seconds to respond and
blocks (using the requests module).  So I've put the posting in a
separate thread (using Thread module) and send data to it via a queue
(using the Queue module).  The http post can send multiple records by
pulling multiple items off the queue.  Sounds good so far, right?

This seems to work ok, but I also need to grab data from some other
serial devices (gps, weather station) to put in the post.  Various
sample apps on the web use Threads to do this, so I am doing the same. 
One of the threads uses `select` (see below), following by readlines()
on the device.

   def __init__(self):
select_timeout = 1
serial_timeout = 0.1

def wait_and_process(self):
'''wait for data and process it.'''
r = select.select([self.serial_dev], [], [], self.select_timeout)
if not r[0]:
#print("DEBUG: TIMEOUT: wait_and_process")
return

#print("DEBUG: Weather Data Captured")
for s in self.serial_dev.readlines():
#print("INFO: Data: {}".format(s))

temperature = extract_temperature(s)

Is using `select` here redundant?  It's used to block the thread until
the first character is ready to be read from the serial device?
Then I use serial_dev.readlines() to read the stream of chars from the
weather station.
Is that ok?

Does readline block until it sees an end-of-line char?  i.e. does it
only wake up the thread when it has a _complete string_ or a timeout of
0.1 seconds?
or will the process block other threads from running while it is
gathering the chars until the newline?
I'm assuming the gathering of chars will happen in the background and
allow my main thread to run, correct?

My application mainloop (not the threads) needs to be high priority and
always process as soon as it gets an interrupt.  Is using select a good
way of doing this?  How can I ensure that no other threads are utilizing
the CPU, etc?  I'm worried about the GIL (I'm using CPython 2.7).

Is there a better way of structuring this to ensure I process the
interrupt and not get interrupt overrun?

Is using select only in the mainloop, with multiple file descriptors, a
better way of doing things, so that I can process the file descriptor of
interest first, before any others if set?

Is using the multiprocessing module a better option?

Thanks for any advice,
Brendan.

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


Re: Help using Thread (or other method)

2016-02-08 Thread Marko Rauhamaa
"Brendan Simon (eTRIX)" :

> Is using `select` here redundant?

Yes, if you go the thread way, select() is usually redundant and just
gets in the way.

> Does readline block until it sees an end-of-line char? i.e. does it
> only wake up the thread when it has a _complete string_ or a timeout
> of 0.1 seconds?

You probably don't want to mix 0.1-second timeouts with readline but let
the readline thread do its job: call readline in an indefinite wait. You
can then have Queue.get(timeout=...) do the timeout if necessary.

> or will the process block other threads from running while it is
> gathering the chars until the newline?

It shouldn't, but I didn't try it out.

> My application mainloop (not the threads) needs to be high priority
> and always process as soon as it gets an interrupt. Is using select a
> good way of doing this? How can I ensure that no other threads are
> utilizing the CPU, etc? I'm worried about the GIL (I'm using CPython
> 2.7).

Here's what you can do:

 * Have your main, high-priority thread have an input Queue coming in.
   Have the peripheral threads put their notifications in that Queue.

 * Have each peripheral thread have an acknowledgement threading.Event
   object. Whenever a peripheral thread has put a notification in the
   Queue, have it wait for the main thread to set() the acknowledgement
   event before looking for more work. That prevents the peripheral
   threads from starving the CPU under any circumstances.

> Is using select only in the mainloop, with multiple file descriptors, a
> better way of doing things, so that I can process the file descriptor of
> interest first, before any others if set?
>
> Is using the multiprocessing module a better option?

I don't know enough of your exact problem to recommend anything. Both of
those are viable options. However, readline() and select() don't mix
well at all because readline() does buffered I/O and select() deals with
raw file descriptors.

If you *can* reduce all functionality to file descriptor operations, use
os.read() and os.write() only. Then, you can use select.select() or
select.epoll() and not bother with threads at all. However, then you'll
have to implement HTTP processing on your own.

(The same goes for 3.5's new async/await scheme: https://docs.python.or
g/3/whatsnew/3.5.html?highlight=asyncio#pep-492-coroutines-with-async-a
nd-await-syntax>.)

Given the nature of your questions, you might want to stick with
threads. Your problem will then be how to shut down those threads in the
end...


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


Searching Sets (Lottery Results)

2016-02-08 Thread MrPink
This is a continuation of my pursuit to learn Python. I have been tinkering 
with this for a number of years and I am back at it again.  I am stuck and need 
some guidance.

This is related to other posts that I have made in the past.  
For example: Searching for Lottery drawing list of ticket match: 
https://groups.google.com/forum/#!topic/comp.lang.python/sHLPrfmY3q4

I have a text file with the results of lottery drawing like so:
Draw Date   WB1 WB2 WB3 WB4 WB5 PB  PP
01/02/2016  42  15  06  05  29  10  2
12/30/2015  12  61  54  38  36  22  3
12/26/2015  65  40  44  59  27  20  2
12/23/2015  67  16  63  38  55  25  4
12/19/2015  30  68  59  41  28  10  2
12/16/2015  09  42  10  55  32  06  2
12/12/2015  62  02  30  19  14  22  2
12/09/2015  16  46  10  56  07  01  2
12/05/2015  47  33  68  27  13  13  2
12/02/2015  14  18  19  64  32  09  2
11/28/2015  47  02  66  67  06  02  3
11/25/2015  53  16  69  58  29  21  2
11/21/2015  37  57  47  50  52  21  3
11/18/2015  40  17  46  69  41  06  2
11/14/2015  66  37  22  14  45  05  3
11/11/2015  26  04  32  55  64  18  3
11/07/2015  50  53  07  16  25  15  2
11/04/2015  12  02  17  20  65  17  4
10/31/2015  09  47  20  25  68  07  2
10/28/2015  56  62  54  63  04  10  2
10/24/2015  20  31  56  64  60  02  3
10/21/2015  57  32  30  42  56  11  4

I load the lottery drawings into memory for searching with the following code 
although, it is incomplete.  I am stuck and need some guidance.

The set datatype seems to be the best for searching, but how best can I 
implement it?

And I want the results to highlight the numbers that were matched.  For 
example, if the white balls in the drawing are: 
"42 15 06 05 29"

AND the numbers on the lottery ticket are: 
"06 15 32 42 56"

THEN the display might look like: 
"06* 15* 32 42* 56" 

WHERE * signifies a match.

###
from datetime import datetime

class Powerball(object):
"""Summary of class here.

Longer class information. . .Longer
Attributes:
fileName: File name to load drawing from.
"""
# class Constants
_DATE_FORMAT = '%m/%d/%Y'
# class variables
fileName = 'pb.txt'

# class initialization
def __init__(self, pFileName):
"""Return a Powerball Object with a set of winning drawings.
:param pFileName:
"""
self.fileName = pFileName

# Open file and load drawing data sets.
def readFileData(self):
"""File to open and load data from. """
f = open(self.fileName, 'r')
# read the whole file into a list of lines.
lines = f.readlines()
f.close()
# For each line in the list of lines. . .
for line in lines[1:50]:
# split the string on whitespace into a list of strings
fields = line.split()
d = datetime.strptime(fields[0], self._DATE_FORMAT).date()
# Use list comprehension to create a frozenset.
wb = frozenset(int(num_str) for num_str in fields[1:6])
pb = int(fields[6])
t = tuple([wb, pb, d])
# Store t into a data structure for searching later. . .
# Not sure of best way to do this. . . 


p = Powerball("pb.txt")
p.readFileData()
#
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 2:17 AM, Veek. M  wrote:
> 
> Exceptions can be raised inside a coroutine using the throw(
>
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> 
>
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
>
> If he's throw'ing an exception surely it originates at the throw:
>
> def mycoroutine():
>  while len(n) > 2:
>n = (yield)
>
>  throw('RuntimeError' "die!")

The "throw" is not called from inside the coroutine. It's a method of
the generator object, and it's used by the calling code. It's similar
to calling the send method, except that instead of passing a value to
be returned by the yield expression, it passes an exception to be
raised inside the coroutine at the yield expression.

Example:

def mycoroutine():
  n = 0
  while True:
try:
  n = (yield n)
except SomeException:
  n = 42

coro = mycoroutine()
coro.next()
for i in range(100):
  if i % 6 == 0:
coro.send(i % 6)
  else:
coro.throw(SomeException())


> Also this bit:
> ***
> If a coroutine returns values, some care is required if exceptions
> raised with throw() are being handled. If you raise an exception in a
> coroutine using throw(), the value passed to the next yield in the
> coroutine will be returned as the result of throw(). If
> you need this value and forget to save it, it will be lost.
> ***
>
> def coroutine():
>   while True:
>line = (yield result)
>
>   throw(FooException)
>
> where is the question of a 'yield'? You'll exit the coroutine straight
> away..

Taking my example from above, after SomeException is caught, the next
value yielded inside the coroutine will be the return value of the
coro.throw() call. This may be surprising if you're only expecting
coro.send() and not coro.throw() to return yielded values.
-- 
https://mail.python.org/mailman/listinfo/python-list


Asyncio thought experiment

2016-02-08 Thread Marko Rauhamaa

As I stated in an earlier post, a normal subroutine may turn out to be
blocking. To make it well-behaved under asyncio, you then dutifully tag
the subroutine with "async" and adorn the blocking statement with
"await". Consequently, you put "await" in front of all calls to the
subroutine and cascade the "async"s and "await"s all the way to the top
level.

Now what would prevent you from making *every* function an "async" and
"await"ing *every* function call? Then, you would never fall victim to
the cascading async/await.

And if you did that, why bother sprinkling async's and await's
everywhere? Why not make every single function call an await implicitly
and every single subroutine an async? In fact, that's how everything
works in multithreading: blocking statements don't need to be ornamented
in any manner.


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


Re: Set Operations on Dicts

2016-02-08 Thread Ben Finney
Marco Kaulea  writes:

> In one talk (I think it was [1]) it was described that sets are basically
> dicts without the values.

It seems an unhelpful thing to say about ‘set’, I disagree with that
characterisation.

> Therefor it should be easy to apply set operations on dicts

Yes, that's one reason I disagree; it leads to incorrect inferences like
this :-)

-- 
 \“I went to court for a parking ticket; I pleaded insanity. I |
  `\   said ‘Your Honour, who in their right mind parks in the passing |
_o__)   lane?’” —Steven Wright |
Ben Finney

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


Re: trouble installing python

2016-02-08 Thread Oscar Benjamin
On 7 Feb 2016 09:50, "donald alsept via Python-list" 
wrote:
>
> Hello,
> I'm trying to install the 3.5.1 of Python and am running windows 7. I
keep getting an error about api-ms-win-crt-runtime-|1-1-0.dll not being
installed. Any advice on what is wrong?

Hi Donald, you're seeing this problem because Python 3.5 on Windows 7 needs
a runtime update. You can get this update from Microsoft:

https://support.microsoft.com/en-us/kb/2999226

Alternatively you can install Python 3.4 instead since 3.4 doesn't need the
update.

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


Re: Searching Sets (Lottery Results)

2016-02-08 Thread Chris Angelico
On Tue, Feb 9, 2016 at 8:45 AM, MrPink  wrote:
> I load the lottery drawings into memory for searching with the following code 
> although, it is incomplete.  I am stuck and need some guidance.
>
> The set datatype seems to be the best for searching, but how best can I 
> implement it?
>
> And I want the results to highlight the numbers that were matched.  For 
> example, if the white balls in the drawing are:
> "42 15 06 05 29"
>
> AND the numbers on the lottery ticket are:
> "06 15 32 42 56"
>
> THEN the display might look like:
> "06* 15* 32 42* 56"
>
> WHERE * signifies a match.
>

This suggests that there is an order to the numbers on your ticket
(you want to print them out in the same order), but not to the winning
numbers, which are simply a set. The easiest way to handle that would
be to iterate over your numbers, asking "if number in
winning_numbers:", and printing out a "match" marker if it is or a
"non-match" marker if it isn't.

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


Re: Help using Thread (or other method)

2016-02-08 Thread Chris Angelico
On Tue, Feb 9, 2016 at 7:59 AM, Brendan Simon (eTRIX)
 wrote:
> My application mainloop (not the threads) needs to be high priority and
> always process as soon as it gets an interrupt.  Is using select a good
> way of doing this?  How can I ensure that no other threads are utilizing
> the CPU, etc?  I'm worried about the GIL (I'm using CPython 2.7).
>

First and foremost, don't worry about the GIL. It's almost never a
problem, so wait until you have proof of its guilt before you start
trying to fire it. In your situation, your quoted problem is that HTTP
requests take time - in other words, that your code is blocking - so
nothing's going to be spinning in the CPU.

The most obvious solutions to your initial problem are (1) threads,
and (2) async I/O. If there's only one part of your code that's ever
at risk of problematic blocking, you might be able to do that part
asynchronously and most of the rest of the code with simple blocking
calls; what that'd mean is that you won't process HTTP responses until
your code next "falls idle" in its primary loop (waiting for another
interrupt), which is pretty much what you'd have with any other scheme
anyway. You could look into a Python 3.5 async/await model, using a
library like this (never used it, just found it on Google):

https://github.com/KeepSafe/aiohttp

Otherwise, threads are great.

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


Re: Heap Implementation

2016-02-08 Thread srinivas devaki
On Feb 8, 2016 5:17 PM, "Cem Karan"  wrote:
>
> On Feb 7, 2016, at 10:15 PM, srinivas devaki 
wrote:
> > On Feb 8, 2016 7:07 AM, "Cem Karan"  wrote:
> > > I know that there are methods of handling this from the client-side
(tuples with unique counters come to mind), but if your library can handle
it directly, then that could be useful to others as well.
> >
> > yeah it is a good idea to do at client side.
> > but if it should be introduced as feature into the library, instead of
tuples, we should just piggyback a single counter it to the self._indexes
dict, or better make another self._counts dict which will be light and fast.
> > and if you think again with this method you can easily subclass with
just using self._counts dict  in your subclass. but still I think it is
good to introduce it as a feature in the library.
> >
> > Regards
> > Srinivas Devaki
>
> I meant that the counter is a trick to separate different instances of
(item, priority) pairs when you're pushing in the same item multiple times,
but with different priorities.

oh okay, I'm way too off.

what you are asking for is a Priority Queue like feature.

but the emphasis is on providing extra features to heap data structure.

and xheap doesn't support having duplicate items.

and if you want to insert same items with distinct priorities, you can
provide the priority with key argument to the xheap. what xheap doesn't
support is having same keys/priorities.
So I got confused and proposed a method to have same keys.

Regards
Srinivas Devaki
Junior (3rd yr) student at Indian School of Mines,(IIT Dhanbad)
Computer Science and Engineering Department
ph: +91 9491 383 249
telegram_id: @eightnoteight
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A sets algorithm

2016-02-08 Thread Steven D'Aprano
On Tuesday 09 February 2016 02:11, Chris Angelico wrote:

> That's fine for comparing one file against one other. He started out
> by saying he already had a way to compare files for equality. What he
> wants is a way to capitalize on that to find all the identical files
> in a group. A naive approach would simply compare every file against
> every other, for O(N*N) comparisons - but a hash lookup can make that
> O(N) on the files themselves, plus (I think) an O(N log N) hash
> comparison job, which has much lower constant factors.

You're mixing up N's there :-) In the first two (compare every file against 
every other file, and hash lookup), N stands for the number of files. But in 
the hash comparison, well, I'm not sure what you mean by that, unless you 
mean the calculation of the hash itself, which will be O(M) where M is the 
size of the file.

Unfortunately, even using a hash, you still have to do O(N**2) work, or 
rather, O(N*M) where N is the number of files and M is their size.


Assuming that duplicate files are relatively rare, the best way to do this 
is to collate files with the same size. Looking up the file size ought to be 
pretty fast. It is an IO operation, but it is approximately constant time in 
the sense that it doesn't actually depend on the size of the file.

(If you're using a programming language or operating system where the only 
way to find out how big a file is to open it and count the bytes, this 
obviously won't work for you.)


So let's collect files into a separate bucket for each unique file size:

bysize = {}
for file in files:
bysize.setdefault(file.getsize(), []).append(file)



In practice, you may find that nearly all files have different sizes, and 
most of the bucks have only a single file in them. Those files you can just 
ignore, as they must be unique.

So instead of having to compare N files against each of the others, you are 
left with a much smaller number of buckets, each with (hopefully) only two 
or three files of the same size. You might have gone from N=100 to 
thirty buckets with five files each.

And most of those files will probably differ on the very first byte, or at 
least within the first 16K of bytes, so it's hardly worth hashing them 
(especially if the hash algorithm is an expensive cryptographic hash, or if 
the file is large).

I would delay the hash comparison as long as possible, something like this:


def __hash__(self):
if self._hash is None:
self._hash = calculate_hash()  # expensive, so we cache it
return self._hash


def __eq__(self, other):
size = self.getsize()
if size != other.getsize():
return False
if self._hash and other._hash:
# Both hashes are already done, so might as well use them.
return (
hash(self) == hash(other) 
# in case of collisions...
and self.read() == other.read()
)
if size < MAXSIZE:
return self.read(size) == other.read(size)
else:
if self.read(MAXSIZE) != other.read(MAXSIZE):
return False
else:
 return (
hash(self) == hash(other) 
# in case of collisions...
and self.read() == other.read()
)


If your hash is strong enough that collisions are vanishingly rare, you 
could ignore the `and self.read() == other.read()` check.





-- 
Steve

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


from scipy.linalg import _fblas ImportError: DLL load failed: The specified module could not be found.

2016-02-08 Thread Mike S via Python-list
I have Python 3.4.4 installed on Windows 7, also IPython, scipy, numpy, 
statsmodels, and a lot of other modules, and am working through this 
tutorial

http://www.analyticsvidhya.com/blog/2016/02/time-series-forecasting-codes-python/

In Ipython notebook I run this code

from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
#determining rolling statistics
rolmean = pd.rolling_mean(timeseries, window=12)
rolstd  = pd.rolling_std(timeseries, window=12)
#plot rolling statistics
orig = plt.plot(timeseries, color='blue',  label='Original')
mean = plt.plot(rolmean,color='red',   label='Rolling Mean')
std  = plt.plot(rolstd, color='black', label='Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean and Standard Deviation')
plt.show(block=false)
#perform Dickey=Fuller test
print('Results of Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test 
Statistic','p-value','#Lags Used','Number of Observations Used'])

for key, value in dftest[4].items():
dfoutput['Critical Value (%s)'%key]=value
print(dfoutput)
test_stationary(ts

And see this output:
---
ImportError  Traceback (most recent call last)
 in ()
> 1 from statsmodels.tsa.stattools import adfuller
  2 def test_stationarity(timeseries):
  3 #determining rolling statistics
  4 rolmean = pd.rolling_mean(timeseries, window=12)
  5 rolstd  = pd.rolling_std(timeseries, window=12)

c:\python34\lib\site-packages\statsmodels\__init__.py in ()
  6
  7 from warnings import simplefilter
> 8 from .tools.sm_exceptions import (ConvergenceWarning, 
CacheWriteWarning,

  9  IterationLimitWarning, InvalidTestWarning)
 10

c:\python34\lib\site-packages\statsmodels\tools\__init__.py in ()
> 1 from .tools import add_constant, categorical

c:\python34\lib\site-packages\statsmodels\tools\tools.py in ()
  6 import numpy.lib.recfunctions as nprf
  7 import numpy.linalg as L
> 8 from scipy.linalg import svdvals
  9 from statsmodels.distributions import (ECDF, monotone_fn_inverter,
 10StepFunction)

c:\python34\lib\site-packages\scipy\linalg\__init__.py in ()
172 from .linalg_version import linalg_version as __version__
173
--> 174 from .misc import *
175 from .basic import *
176 from .decomp import *

c:\python34\lib\site-packages\scipy\linalg\misc.py in ()
  3 import numpy as np
  4 from numpy.linalg import LinAlgError
> 5 from .blas import get_blas_funcs
  6 from .lapack import get_lapack_funcs
  7

c:\python34\lib\site-packages\scipy\linalg\blas.py in ()
153 import numpy as _np
154
--> 155 from scipy.linalg import _fblas
156 try:
157 from scipy.linalg import _cblas

ImportError: DLL load failed: The specified module could not be found.

Do I read this correctly to mean that the very last import statement is 
the one having the problem,


"from scipy.linalg import _fblas"

How do I troubleshoot this? I'm wondering if I have version conflict 
between two modules.


Thanks,
Mike
--
https://mail.python.org/mailman/listinfo/python-list


Re: Heap Implementation

2016-02-08 Thread Cem Karan

On Feb 8, 2016, at 10:12 PM, srinivas devaki  wrote:

> 
> On Feb 8, 2016 5:17 PM, "Cem Karan"  wrote:
> >
> > On Feb 7, 2016, at 10:15 PM, srinivas devaki  
> > wrote:
> > > On Feb 8, 2016 7:07 AM, "Cem Karan"  wrote:
> > > > I know that there are methods of handling this from the client-side 
> > > > (tuples with unique counters come to mind), but if your library can 
> > > > handle it directly, then that could be useful to others as well.
> > >
> > > yeah it is a good idea to do at client side.
> > > but if it should be introduced as feature into the library, instead of 
> > > tuples, we should just piggyback a single counter it to the self._indexes 
> > > dict, or better make another self._counts dict which will be light and 
> > > fast.
> > > and if you think again with this method you can easily subclass with just 
> > > using self._counts dict  in your subclass. but still I think it is good 
> > > to introduce it as a feature in the library.
> > >
> > > Regards
> > > Srinivas Devaki
> >
> > I meant that the counter is a trick to separate different instances of 
> > (item, priority) pairs when you're pushing in the same item multiple times, 
> > but with different priorities.
> 
> oh okay, I'm way too off.
> 
> what you are asking for is a Priority Queue like feature.
> 
> but the emphasis is on providing extra features to heap data structure.
> 
> and xheap doesn't support having duplicate items.
> 
> and if you want to insert same items with distinct priorities, you can 
> provide the priority with key argument to the xheap. what xheap doesn't 
> support is having same keys/priorities.
> So I got confused and proposed a method to have same keys.
> 
> Regards
> Srinivas Devaki

No problem, that's what I thought happened.  And you're right, I'm looking for 
a priority queue (not the only reason to use a heap, but a pretty important 
reason!)

Thanks,
Cem Karan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A sets algorithm

2016-02-08 Thread Chris Angelico
On Tue, Feb 9, 2016 at 3:13 PM, Steven D'Aprano
 wrote:
> On Tuesday 09 February 2016 02:11, Chris Angelico wrote:
>
>> That's fine for comparing one file against one other. He started out
>> by saying he already had a way to compare files for equality. What he
>> wants is a way to capitalize on that to find all the identical files
>> in a group. A naive approach would simply compare every file against
>> every other, for O(N*N) comparisons - but a hash lookup can make that
>> O(N) on the files themselves, plus (I think) an O(N log N) hash
>> comparison job, which has much lower constant factors.
>
> You're mixing up N's there :-) In the first two (compare every file against
> every other file, and hash lookup), N stands for the number of files. But in
> the hash comparison, well, I'm not sure what you mean by that, unless you
> mean the calculation of the hash itself, which will be O(M) where M is the
> size of the file.
>
> Unfortunately, even using a hash, you still have to do O(N**2) work, or
> rather, O(N*M) where N is the number of files and M is their size.
>

My intention was that every N was "number of files being compared",
but it's possible I made a mistake elsewhere. Assuming the hashes
become integers too large for a bucket sort (which they certainly
will, unless you want inordinate numbers of hash collisions), the code
would look something like this:

hash_to_filename = defaultdict(list)
for fn in files:
# Step 1: Hash every file.
hash = calculate_hash(fn)

# Step 2: Locate all pairs of files with identical hashes
hash_to_filename[hash].append(fn)


This loop executes once for each file, so calculate_hash() is called
once for each file. The cost of that is O(N), or possibly O(N*M). If
you're fairly sure the files are going to differ in size, you could
use the file size *as* the hash - it's cheap to calculate (no M
component whatsoever - just a stat call, and even that could be
optimized away in some cases, eg if you're scanning a whole
directory), but isn't cryptographically secure and ignores file
content altogether.

The second step involves one dictionary lookup or insertion for each
file. That's an O(log N) operation, where N is the number of elements
in the dictionary. So yes, it's not quite the same as the number of
files (if every file exists exactly twice, this N will be half the
other N), but it's broadly going to correspond. And an O(log N)
operation performed N times has an overall cost of O(N log N).

I might have something wrong here, but definitely I meant to have the
Ns all mean the same thing, like X on a Magic: The Gathering card. :)

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


Re: A sets algorithm

2016-02-08 Thread Gregory Ewing

Chris Angelico wrote:

hash_to_filename = defaultdict(list)
for fn in files:
# Step 1: Hash every file.
hash = calculate_hash(fn)

# Step 2: Locate all pairs of files with identical hashes
hash_to_filename[hash].append(fn)


I think you can avoid hashing the files altogether.

First divide the files into groups of the same size. Then for
each group, open all the files at once, read them in parallel
and compare them with each other.

As soon as you find a difference, split the group into
smaller groups. When a group gets down to just one file, you
can stop reading that file.

Assuming that most of the differing files differ near the
beginning, this strategy means that you will hardly ever
have to read a whole file. Hashing the files beforehand,
in contrast, requires reading all of every file every
time.

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


Re: Set Operations on Dicts

2016-02-08 Thread Grobu

On 08/02/16 17:12, Ian Kelly wrote:


dict does already expose set-like views. How about:

{k: d[k] for k in d.keys() & s}  # d & s
{k: d[k] for k in d.keys() - s}  # d - s


Interesting. But seemingly only applies to Python 3.
--
https://mail.python.org/mailman/listinfo/python-list


asyncio and blocking - an update

2016-02-08 Thread Frank Millman

Hi all

Some of you may have been following my attempts to modify my asyncio app so 
that it does not block when accessing the database. Here is an update.


I came up with what felt like a good idea. Run the database handler in a 
separate thread, pass requests to it using a queue.Queue, and get it to pass 
results back using an asyncio.Queue.


It works, but I had a vague sense that performance was a bit sluggish, so I 
tried the 'recommended' approach of using asyncio.run_in_executor() to 
execute database calls in a separate thread. It felt a bit faster.


Now I have written a proper timing test, and the recommended approach is 
much faster. I am not 100% sure of the reason, but I think the problem is 
that, with my method, when the database tries to 'put' a row on the return 
queue, it has to use 'loop.call_soon_threadsafe()', and this seems to create 
a bottleneck.


It would not matter so much if I used cur.fetchall(), and sent all the rows 
back in one 'put', but I really wanted to iterate over the cursor and 'put' 
a row at a time. The idea was that it would be truly asynchronous from end 
to end - the database handler would retrieve rows one at a time, and via the 
queue my app would process them one at a time, all without blocking.


I can switch to fetchall(), but then there is no benefit over the 
recommended approach. Although fetchall() is non-blocking, once the rows are 
received as a list the function that processes them effectively does block. 
If that became a problem one could use an Asynchronous Iterator to process 
the list, but I have not had that need yet.


Writing timing tests is tricky. It is possible that under some 
circumstances, with a heavy load, a large table, and a time-consuming 
function to process each row, the overhead of call_soon_threadsafe() would 
be minimised and my approach might be effective. For now, however, I will 
regretfully abandon my approach and stick with run_in_executor().


Frank Millman


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