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

2016-02-07 Thread Chris Angelico
On Sun, Feb 7, 2016 at 6:51 PM, Paul Rubin  wrote:
> Rustom Mody  writes:
>> Data (science) is after all the hot subject
>> A programmer moving into that field typically starts with python
>> A statistician typically starts R
>
> There aren't THAT many statisticians out there, compared to programmers.

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

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


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

2016-02-07 Thread dieter
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.

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


Re: asyncio - how to stop background task cleanly

2016-02-07 Thread Frank Millman

"Marko Rauhamaa"  wrote in message news:[email protected]...


I can't see your complete program, but here's mine, and it seems to be 
working




Thanks, Marko, I really appreciate your assistance.

I wanted to show you my complete program, but as it is quite long I 
distilled it down to its essence, and lo and behold it works!


So now I just have to go through my program and find where it differs - I 
must have a bug somewhere.


For the record, here is my stripped down version.

The main difference from yours is that I want to run a genuine 'loop 
forever', and only shut it down on receipt of some external signal.


I have never been able to get Ctrl+C to work properly on Windows, so I use a 
separate thread that simply waits for Enter.


You will see that if you press Enter after 'done' appears, the program 
closes instantly, but if you press it in between 'start' and 'done', it 
waits for the task to complete before it closes.


Frank

=

import asyncio, time
import threading

def main():
   loop = asyncio.get_event_loop()
   task = asyncio.async(background_task())
   threading.Thread(target=stop, args=(loop, task)).start()
   loop.run_forever()

@asyncio.coroutine
def background_task():
   try:
   while True:
   print('start')
   time.sleep(2)
   print('done')
   yield from asyncio.sleep(5)
   except asyncio.CancelledError:
   print('cleanup')
   print('DONE')

@asyncio.coroutine
def shutdown(loop, task):
   task.cancel()
   yield from asyncio.wait([task], loop=loop)
   loop.stop()

def stop(loop, task):
   input('Press  to stop\n')
   asyncio.run_coroutine_threadsafe(shutdown(loop, task), loop)

if __name__ == '__main__':
   main()


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


Re: asyncio - how to stop background task cleanly

2016-02-07 Thread Marko Rauhamaa
"Frank Millman" :

> I have never been able to get Ctrl+C to work properly on Windows, so I
> use a separate thread that simply waits for Enter.

Now you are leaving my realm of expertise, as I haven't programmed for
windows since Windows 1.0. Mixing threads, asyncio, input() and Windows
seems like begging for trouble, though.


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


trouble installing python

2016-02-07 Thread donald alsept via Python-list
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?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: trouble installing python

2016-02-07 Thread Mark Lawrence

On 07/02/2016 00:34, 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?



Hello and welcome.

This question has been asked and ansered repeatedly in the last few 
weeks so please search the archives for the answer.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: asyncio - how to stop background task cleanly

2016-02-07 Thread Frank Millman

"Marko Rauhamaa"  wrote in message news:[email protected]...


"Frank Millman" :

> I have never been able to get Ctrl+C to work properly on Windows, so I
> use a separate thread that simply waits for Enter.

Now you are leaving my realm of expertise, as I haven't programmed for
windows since Windows 1.0. Mixing threads, asyncio, input() and Windows
seems like begging for trouble, though.



Well, it is not such a big deal :-) The program I posted works 
cross-platform, so it will run on your linux box.


No matter, you have been an enormous help, and I am very grateful.

I found my bug, and my program is now running sweetly.

Frank


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


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

2016-02-07 Thread cl
I'm using this as a starting point for creating a grid to view and
edit some sqlite data:-

http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example

I can actually understand most of it which is a good start.  

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]))

I realise why this fails (I think) but how should one program this so
that:-
1 - the accented characters are displayed correctly in the grid cell
2 - One can edit the cell with accented characters

(I'll get round how to get the accented characters back to the
database later!)

My system (xubuntu 15.10) is all UTF8 so accented characters are
handled by the display, in terminals, etc. correctly.  I'm currently
using python 2.7 for this but would be quite happy to move to 3.4 if
this handles UTF8 better (I seem to remember it does maybe).

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


Re: Python's import situation has driven me to the brink of imsanity

2016-02-07 Thread Sivan Greenberg
in sake of providing another example, as I ran into the same problem and
fixed it the very same way, one can also take a look at:
(this is the main executable)
https://github.com/sivang/navistore/blob/master/navistore/backend/nhttpserver.py

Note the explicit imports in lines 19-22 to allow the executable finding
modules within the OS instance's site-packages structure.

Here's is Navistore's setup.py for reference (I actually use this project
as a study project in a Python course for programmers I'm running):
- https://github.com/sivang/navistore/blob/master/setup.py

You may find this to be helpful:
- https://pythonhosted.org/setuptools/setuptools.html#using-find-packages

To make Navistore's main executable be avail at command line I used this:
-
http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html

On Sun, Feb 7, 2016 at 8:34 AM, Kevin Conway 
wrote:

> > My question is: is this crazy? Please tell me there's a better way and I
> just wasted my time creating this package.
>
> There is a better way and you have wasted your time creating this package.
>
> I hear your problem statement as asking two questions. The first is: What
> is the right way to include executable content in my Python project? The
> second is: How do I expose executable content from a Python project?
>
> As to the first question, from your project README:
> > Say you have a python project (not a package), with the following
> structure:
>
> All Python code that you want to install and make available in any form,
> import or executable, _must_ be contained within a Python package.
> Organizing Python code in any way other than Python packages will result in
> the challenges you have described. The correct way to include executable
> content is to place the Python code within the package structure. It should
> not be put in other directories within the repository root.
>
> As to the second question, once all Python code is contained within a
> package that can be installed you can use setuptools entry points to expose
> the executable code. The setup() function from setuptools that is used to
> create setup.py files has an argument called 'entry_points' that allows you
> to expose executable content over the command line. See [1] and [2] for
> more details.
>
> Feel free to reach out to me off-list if you have a specific project you
> need advice on. The rules for organizing and packaging Python code aren't
> complex but they tend to cause new Python developers to stumble at first. A
> general rule I give everyone when talking about packaging or importing
> code: If you have to modify sys.path to makes something work then you have
> most certainly made a mistake.
>
> [1]
>
> https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
> [2]
>
> http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html#the-console-scripts-entry-point
>
>
> On Sat, Feb 6, 2016 at 8:54 PM Chris Angelico  wrote:
>
> > On Sun, Feb 7, 2016 at 1:47 PM,   wrote:
> > > Imsanity allows you to make imports usable (not ideal, but at least
> > usable) for python projects without having to manage PYTHONPATHs or do
> > whacky stuff like running files with python -m or put even whackier
> > boilerplate at the top of every file. And all it requires is 'import
> > imsanity' at the top of every file. You can put it in a macro or even
> just
> > type it because it's short and easy to remember.
> > >
> > > My question is: is this crazy? Please tell me there's a better way and
> I
> > just wasted my time creating this package. There's nothing I'd like to
> hear
> > more.
> >
> > Well, anything that makes you type "import imsanity" at the top of
> > every script MUST be crazy. :) I don't know about the actual
> > content/purpose though. Good luck with it!
> >
> > ChrisA
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Sivan Greenberg
Co founder & CTO
Vitakka Consulting
-- 
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-07 Thread Chris Angelico
On Sun, Feb 7, 2016 at 10:19 PM,   wrote:
> My system (xubuntu 15.10) is all UTF8 so accented characters are
> handled by the display, in terminals, etc. correctly.  I'm currently
> using python 2.7 for this but would be quite happy to move to 3.4 if
> this handles UTF8 better (I seem to remember it does maybe).

As a general rule, yes, Python 3 handles Unicode somewhat better than
Python 2 does. The main reason for this is that the native string type
is now a Unicode string, instead of the native string type being a
byte string; so "abcd" is not a string of bytes that happen to be
encoded as UTF-8, it is actually a string of Unicode characters. For
the most part, you'll be able to ignore character encodings and such,
and just work with text.

So the question then becomes: Under Python 3, can you use regular
string objects for both the things you're working with? I can confirm
that the inbuilt sqlite3 module works just fine with Unicode text; so
all you need to do is try out your GUI code under Python 3. I've no
idea how good wx support in Py3 is, so you might find you need to
switch GUI toolkits to get everything working; but whether it's with
wxWidgets, GTK, QT, or some other library, you should be able to put
something together under Python 3 that "just works" as regards
Unicode. Caveat: I haven't done any serious GUI programming using
Python.

(Note that "just works" can actually be a lot of hassle if you truly
want to support *all* of Unicode. I've seen programs that don't behave
correctly in the presence of combining characters, or right-to-left
text, or zero-width characters, or non-BMP characters; but you can get
far better support for the same amount of effort if you use Py3 and
Unicode than if you try to do things manually under Py2.)

ChrisA
-- 
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-07 Thread cl
Chris Angelico  wrote:
> 
> So the question then becomes: Under Python 3, can you use regular
> string objects for both the things you're working with? I can confirm
> that the inbuilt sqlite3 module works just fine with Unicode text; so
> all you need to do is try out your GUI code under Python 3. I've no
> idea how good wx support in Py3 is, so you might find you need to
> switch GUI toolkits to get everything working; but whether it's with
> wxWidgets, GTK, QT, or some other library, you should be able to put
> something together under Python 3 that "just works" as regards
> Unicode. Caveat: I haven't done any serious GUI programming using
> Python.
> 
Sadly wxpython doesn't work at all in Python 3.  There's a project
called Phoenix to move it across to Python 3 but it's nowhere near
stable yet, which is rather a pity.

So the question is do I stay with 2.7 and live with the Unicode
difficulties or do I try and move to another GUI that does work with
Python 3.  

Are there any Python 3 GUIs that would be reasonably easy to move to?
E.g. ones which have a grid object and which work in the same sort of
way as wxpython in general?

-- 
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-07 Thread Chris Angelico
On Sun, Feb 7, 2016 at 11:42 PM,   wrote:
>
> Are there any Python 3 GUIs that would be reasonably easy to move to?
> E.g. ones which have a grid object and which work in the same sort of
> way as wxpython in general?

Grid object? I'm not sure. But all of my GUI work of late has been
with GTK, and I'm pretty happy with it. (Granted, that's with Pike,
not Python, so the exact API is slightly different; but I know there
is good support for Python 3.) You may also want to look into Tk /
tkinter, although its Unicode support is (or was, last I heard)
limited to the Basic Multilingual Plane - the 64K most commonly used
characters.

ChrisA
-- 
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-07 Thread cl
Chris Angelico  wrote:
> On Sun, Feb 7, 2016 at 11:42 PM,   wrote:
> >
> > Are there any Python 3 GUIs that would be reasonably easy to move to?
> > E.g. ones which have a grid object and which work in the same sort of
> > way as wxpython in general?
> 
> Grid object? I'm not sure. But all of my GUI work of late has been
> with GTK, and I'm pretty happy with it. (Granted, that's with Pike,
> not Python, so the exact API is slightly different; but I know there
> is good support for Python 3.) You may also want to look into Tk /
> tkinter, although its Unicode support is (or was, last I heard)
> limited to the Basic Multilingual Plane - the 64K most commonly used
> characters.
> 
Tkinter is certainly a possibility.  Basic Unicode support will be
fine, I'm only interested in handling European accented characters.

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


Re: Python's import situation has driven me to the brink of imsanity

2016-02-07 Thread dimva13
I see that this would work once you've installed the package, but how do you 
develop it? Say you are working on a change that modifies both email.py and 
reports.py. Do you run setup.py every time you make a change in email.py?

On Sunday, February 7, 2016 at 1:35:15 AM UTC-5, Kevin Conway wrote:
> > My question is: is this crazy? Please tell me there's a better way and I
> just wasted my time creating this package.
> 
> There is a better way and you have wasted your time creating this package.
> 
> I hear your problem statement as asking two questions. The first is: What
> is the right way to include executable content in my Python project? The
> second is: How do I expose executable content from a Python project?
> 
> As to the first question, from your project README:
> > Say you have a python project (not a package), with the following
> structure:
> 
> All Python code that you want to install and make available in any form,
> import or executable, _must_ be contained within a Python package.
> Organizing Python code in any way other than Python packages will result in
> the challenges you have described. The correct way to include executable
> content is to place the Python code within the package structure. It should
> not be put in other directories within the repository root.
> 
> As to the second question, once all Python code is contained within a
> package that can be installed you can use setuptools entry points to expose
> the executable code. The setup() function from setuptools that is used to
> create setup.py files has an argument called 'entry_points' that allows you
> to expose executable content over the command line. See [1] and [2] for
> more details.
> 
> Feel free to reach out to me off-list if you have a specific project you
> need advice on. The rules for organizing and packaging Python code aren't
> complex but they tend to cause new Python developers to stumble at first. A
> general rule I give everyone when talking about packaging or importing
> code: If you have to modify sys.path to makes something work then you have
> most certainly made a mistake.
> 
> [1]
> https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
> [2]
> http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html#the-console-scripts-entry-point
> 
> 
> On Sat, Feb 6, 2016 at 8:54 PM Chris Angelico  wrote:
> 
> > On Sun, Feb 7, 2016 at 1:47 PM,   wrote:
> > > Imsanity allows you to make imports usable (not ideal, but at least
> > usable) for python projects without having to manage PYTHONPATHs or do
> > whacky stuff like running files with python -m or put even whackier
> > boilerplate at the top of every file. And all it requires is 'import
> > imsanity' at the top of every file. You can put it in a macro or even just
> > type it because it's short and easy to remember.
> > >
> > > My question is: is this crazy? Please tell me there's a better way and I
> > just wasted my time creating this package. There's nothing I'd like to hear
> > more.
> >
> > Well, anything that makes you type "import imsanity" at the top of
> > every script MUST be crazy. :) I don't know about the actual
> > content/purpose though. Good luck with it!
> >
> > ChrisA
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

-- 
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-07 Thread Dietmar Schwertberger

On 07.02.2016 12:19, [email protected] 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)

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)


Regards,

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


Re: Python's import situation has driven me to the brink of imsanity

2016-02-07 Thread Kevin Conway
You can use 'setup.py develop' or 'pip install -e' to install your package
in editable mode. It makes it so your local code is used. Modifications are
seen immediately.

On Sun, Feb 7, 2016, 08:16   wrote:

> I see that this would work once you've installed the package, but how do
> you develop it? Say you are working on a change that modifies both email.py
> and reports.py. Do you run setup.py every time you make a change in
> email.py?
>
> On Sunday, February 7, 2016 at 1:35:15 AM UTC-5, Kevin Conway wrote:
> > > My question is: is this crazy? Please tell me there's a better way and
> I
> > just wasted my time creating this package.
> >
> > There is a better way and you have wasted your time creating this
> package.
> >
> > I hear your problem statement as asking two questions. The first is: What
> > is the right way to include executable content in my Python project? The
> > second is: How do I expose executable content from a Python project?
> >
> > As to the first question, from your project README:
> > > Say you have a python project (not a package), with the following
> > structure:
> >
> > All Python code that you want to install and make available in any form,
> > import or executable, _must_ be contained within a Python package.
> > Organizing Python code in any way other than Python packages will result
> in
> > the challenges you have described. The correct way to include executable
> > content is to place the Python code within the package structure. It
> should
> > not be put in other directories within the repository root.
> >
> > As to the second question, once all Python code is contained within a
> > package that can be installed you can use setuptools entry points to
> expose
> > the executable code. The setup() function from setuptools that is used to
> > create setup.py files has an argument called 'entry_points' that allows
> you
> > to expose executable content over the command line. See [1] and [2] for
> > more details.
> >
> > Feel free to reach out to me off-list if you have a specific project you
> > need advice on. The rules for organizing and packaging Python code aren't
> > complex but they tend to cause new Python developers to stumble at
> first. A
> > general rule I give everyone when talking about packaging or importing
> > code: If you have to modify sys.path to makes something work then you
> have
> > most certainly made a mistake.
> >
> > [1]
> >
> https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
> > [2]
> >
> http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html#the-console-scripts-entry-point
> >
> >
> > On Sat, Feb 6, 2016 at 8:54 PM Chris Angelico  wrote:
> >
> > > On Sun, Feb 7, 2016 at 1:47 PM,   wrote:
> > > > Imsanity allows you to make imports usable (not ideal, but at least
> > > usable) for python projects without having to manage PYTHONPATHs or do
> > > whacky stuff like running files with python -m or put even whackier
> > > boilerplate at the top of every file. And all it requires is 'import
> > > imsanity' at the top of every file. You can put it in a macro or even
> just
> > > type it because it's short and easy to remember.
> > > >
> > > > My question is: is this crazy? Please tell me there's a better way
> and I
> > > just wasted my time creating this package. There's nothing I'd like to
> hear
> > > more.
> > >
> > > Well, anything that makes you type "import imsanity" at the top of
> > > every script MUST be crazy. :) I don't know about the actual
> > > content/purpose though. Good luck with it!
> > >
> > > ChrisA
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python's import situation has driven me to the brink of imsanity

2016-02-07 Thread Sivan Greenberg
I if it's more than few files, I use 'develop' as Kevin noted in the note
before:
- http://www.ewencp.org/blog/a-brief-introduction-to-packaging-python/

Hope this helps..

-Sivan

On Sun, Feb 7, 2016 at 6:09 PM, Kevin Conway 
wrote:

> You can use 'setup.py develop' or 'pip install -e' to install your package
> in editable mode. It makes it so your local code is used. Modifications are
> seen immediately.
>
> On Sun, Feb 7, 2016, 08:16   wrote:
>
> > I see that this would work once you've installed the package, but how do
> > you develop it? Say you are working on a change that modifies both
> email.py
> > and reports.py. Do you run setup.py every time you make a change in
> > email.py?
> >
> > On Sunday, February 7, 2016 at 1:35:15 AM UTC-5, Kevin Conway wrote:
> > > > My question is: is this crazy? Please tell me there's a better way
> and
> > I
> > > just wasted my time creating this package.
> > >
> > > There is a better way and you have wasted your time creating this
> > package.
> > >
> > > I hear your problem statement as asking two questions. The first is:
> What
> > > is the right way to include executable content in my Python project?
> The
> > > second is: How do I expose executable content from a Python project?
> > >
> > > As to the first question, from your project README:
> > > > Say you have a python project (not a package), with the following
> > > structure:
> > >
> > > All Python code that you want to install and make available in any
> form,
> > > import or executable, _must_ be contained within a Python package.
> > > Organizing Python code in any way other than Python packages will
> result
> > in
> > > the challenges you have described. The correct way to include
> executable
> > > content is to place the Python code within the package structure. It
> > should
> > > not be put in other directories within the repository root.
> > >
> > > As to the second question, once all Python code is contained within a
> > > package that can be installed you can use setuptools entry points to
> > expose
> > > the executable code. The setup() function from setuptools that is used
> to
> > > create setup.py files has an argument called 'entry_points' that allows
> > you
> > > to expose executable content over the command line. See [1] and [2] for
> > > more details.
> > >
> > > Feel free to reach out to me off-list if you have a specific project
> you
> > > need advice on. The rules for organizing and packaging Python code
> aren't
> > > complex but they tend to cause new Python developers to stumble at
> > first. A
> > > general rule I give everyone when talking about packaging or importing
> > > code: If you have to modify sys.path to makes something work then you
> > have
> > > most certainly made a mistake.
> > >
> > > [1]
> > >
> >
> https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
> > > [2]
> > >
> >
> http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html#the-console-scripts-entry-point
> > >
> > >
> > > On Sat, Feb 6, 2016 at 8:54 PM Chris Angelico 
> wrote:
> > >
> > > > On Sun, Feb 7, 2016 at 1:47 PM,   wrote:
> > > > > Imsanity allows you to make imports usable (not ideal, but at least
> > > > usable) for python projects without having to manage PYTHONPATHs or
> do
> > > > whacky stuff like running files with python -m or put even whackier
> > > > boilerplate at the top of every file. And all it requires is 'import
> > > > imsanity' at the top of every file. You can put it in a macro or even
> > just
> > > > type it because it's short and easy to remember.
> > > > >
> > > > > My question is: is this crazy? Please tell me there's a better way
> > and I
> > > > just wasted my time creating this package. There's nothing I'd like
> to
> > hear
> > > > more.
> > > >
> > > > Well, anything that makes you type "import imsanity" at the top of
> > > > every script MUST be crazy. :) I don't know about the actual
> > > > content/purpose though. Good luck with it!
> > > >
> > > > ChrisA
> > > > --
> > > > https://mail.python.org/mailman/listinfo/python-list
> > > >
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Sivan Greenberg
Co founder & CTO
Vitakka Consulting
-- 
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-07 Thread Vlastimil Brom
2016-02-07 12:19 GMT+01:00  :
> I'm using this as a starting point for creating a grid to view and
> edit some sqlite data:-
> 
> http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example
>
> I can actually understand most of it which is a good start.
>
> 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]))
>
> I realise why this fails (I think) but how should one program this so
> that:-
> 1 - the accented characters are displayed correctly in the grid cell
> 2 - One can edit the cell with accented characters
>
> (I'll get round how to get the accented characters back to the
> database later!)
>
> My system (xubuntu 15.10) is all UTF8 so accented characters are
> handled by the display, in terminals, etc. correctly.  I'm currently
> using python 2.7 for this but would be quite happy to move to 3.4 if
> this handles UTF8 better (I seem to remember it does maybe).
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

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)
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?)

- 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.

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


Handling Print Events

2016-02-07 Thread Malik Brahimi
Hello guys,

So basically, a print job (.SPL file) is generated during local spooling as
soon as a document is requested to print. I do NOT want to handle this
event.

I want to trigger a callback function as soon as a print job reaches a
print server regardless of whether it begins to immediately print there or
not. Essentially, even on a high volume network, I want to catch the even
in which the print job reaches the print server, not the event in which the
print job is initially created.

Is there anyway to do that with Python? I would also like to get the JobId
of said print job as this event is caught.

http://stackoverflow.com/questions/35228080/print-servers-and-triggering-callbacks
http://stackoverflow.com/questions/34519642/received-print-job-python

Thanks,

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


A sets algorithm

2016-02-07 Thread Paulo da Silva
Hello!

This may not be a strict python question, but ...

Suppose I have already a class MyFile that has an efficient method (or
operator) to compare two MyFile s for equality.

What is the most efficient way to obtain all sets of equal files (of
course each set must have more than one file - all single files are
discarded)?

Thanks for any suggestions.
Paulo
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A sets algorithm

2016-02-07 Thread Chris Angelico
On Mon, Feb 8, 2016 at 8:46 AM, Paulo da Silva
 wrote:
> Hello!
>
> This may not be a strict python question, but ...
>
> Suppose I have already a class MyFile that has an efficient method (or
> operator) to compare two MyFile s for equality.
>
> What is the most efficient way to obtain all sets of equal files (of
> course each set must have more than one file - all single files are
> discarded)?
>
> Thanks for any suggestions.

Hash them in some way.

This has two costs:
1) You need to figure out some hashing algorithm such that any two
equal files have the same hash, and ideally, that unequal files will
generally have unequal hashes.
2) Hash each file once, and then do your comparisons on the hashes,
finally testing for actual equality only on those with the same hash.
(The last step takes care of hash collisions - where different files
happen to have the same hash - so ideally, you shouldn't have to do
this often.)

If your definition of "equal" among MyFiles is simply based on the
file content, it's easy - just hash the content. But if there are ways
for files to be considered equal without being bit-for-bit identical,
you'll have to reflect that in the hash. For instance, if you consider
files equal if they differ only in whitespace, then you'd need to
convert all whitespace to a single space before hashing; or if you
don't care about the order of lines in the file, you could either hash
the lines separately and sum/xor the hashes, or sort the lines before
hashing. But the rest of the job is pretty straight-forward.

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


Re: A sets algorithm

2016-02-07 Thread Oscar Benjamin
On 7 Feb 2016 21:51, "Paulo da Silva" 
wrote:
>
> Hello!
>
> This may not be a strict python question, but ...
>
> Suppose I have already a class MyFile that has an efficient method (or
> operator) to compare two MyFile s for equality.
>
> What is the most efficient way to obtain all sets of equal files (of
> course each set must have more than one file - all single files are
> discarded)?

If you can make the MyFiles hashable then this is easily done with
defaultdict(list).

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


Re: A sets algorithm

2016-02-07 Thread Tim Chase
On 2016-02-07 21:46, Paulo da Silva wrote:
> Suppose I have already a class MyFile that has an efficient method
> (or operator) to compare two MyFile s for equality.
> 
> What is the most efficient way to obtain all sets of equal files (of
> course each set must have more than one file - all single files are
> discarded)?

If your MyFile grows a __hash__ method that is based off the
uniqueness-qualities that you're comparing, you can use
collections.Counter and filter the results:

  from collections import Counter
  interesting = [
my_file
for my_file, count
in Counter(generate_MyFile_objects()).iteritems()
if count > 1
]

Which should be roughly O(n).  It also has the advantage of iterating
over your generator once.

If you the MyFile objects can be unique but compare for equality
(e.g. two files on the file-system that have the same SHA1 hash, but
you want to know the file-names), you'd have to do a paired search
which would have worse performance and would need to iterate over the
data multiple times:

  all_files = list(generate_MyFile_objects())
  interesting = [
(my_file1, my_file2)
for i, my_file1
in enumerate(all_files, 1)
for my_file2
in all_files[i:]
if my_file1 == my_file2
]

-tkc



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


Re: A sets algorithm

2016-02-07 Thread Paulo da Silva
Às 22:17 de 07-02-2016, Tim Chase escreveu:
> On 2016-02-07 21:46, Paulo da Silva wrote:
...

> 
> If you the MyFile objects can be unique but compare for equality
> (e.g. two files on the file-system that have the same SHA1 hash, but
> you want to know the file-names), you'd have to do a paired search
> which would have worse performance and would need to iterate over the
> data multiple times:
> 
>   all_files = list(generate_MyFile_objects())
>   interesting = [
> (my_file1, my_file2)
> for i, my_file1
> in enumerate(all_files, 1)
> for my_file2
> in all_files[i:]
> if my_file1 == my_file2
> ]
> 
"my_file1 == my_file2" can be implemented into MyFile class taking
advantage of caching sizes (if different files are different), hashes or
even content (for small files) or file headers (first n bytes).
However this seems to have a problem:
all_files: a b c d e ...
If a==b then comparing b with c,d,e is useless.

May be using several steps with dict - sizes, then hashes for same sizes
files, etc ...

Another solution I thought of, could be defining some methods (I still
don't know which ones) in MyFile so that I could use sets intersection.
Would this one be a faster solution?

Thanks

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


Re: Python's import situation has driven me to the brink of imsanity

2016-02-07 Thread dimva13
Running python setup.py develop doesn't work, it gives me this error: error: 
invalid command 'develop'

Running pip install -e . does work. This is somewhat reasonable, and I think 
may work for my purposes, but it has the following drawbacks:
- you have to create a fake package with a fake version, and all the setup.py 
boilerplate
- to get to your python files, you have to go through an additional 
subdirectory (ex: project_name/project_name/package.py)
- you have to remember to run pip install -e . before you can use anything 
inside the project
- you should be using a virtualenv (I am), otherwise things can get messy if 
you have these fake projects installed in a global location

All in all, this seems like a hack - I'm creating a fake package with a fake 
version just so I can do imports sanely. Still less of a hack than imsanity, 
though.

Thanks for the advice! I just wish it was better documented and easier to find. 
I have several years of professional python experience at 3 different 
companies, and I've never seen this solution anywhere - all the companies 
solved it by setting PYTHONPATH to the base directory. I'll try to spread the 
word.

On Sunday, February 7, 2016 at 11:09:35 AM UTC-5, Kevin Conway wrote:
> You can use 'setup.py develop' or 'pip install -e' to install your package
> in editable mode. It makes it so your local code is used. Modifications are
> seen immediately.
> 
> On Sun, Feb 7, 2016, 08:16   wrote:
> 
> > I see that this would work once you've installed the package, but how do
> > you develop it? Say you are working on a change that modifies both email.py
> > and reports.py. Do you run setup.py every time you make a change in
> > email.py?
> >
> > On Sunday, February 7, 2016 at 1:35:15 AM UTC-5, Kevin Conway wrote:
> > > > My question is: is this crazy? Please tell me there's a better way and
> > I
> > > just wasted my time creating this package.
> > >
> > > There is a better way and you have wasted your time creating this
> > package.
> > >
> > > I hear your problem statement as asking two questions. The first is: What
> > > is the right way to include executable content in my Python project? The
> > > second is: How do I expose executable content from a Python project?
> > >
> > > As to the first question, from your project README:
> > > > Say you have a python project (not a package), with the following
> > > structure:
> > >
> > > All Python code that you want to install and make available in any form,
> > > import or executable, _must_ be contained within a Python package.
> > > Organizing Python code in any way other than Python packages will result
> > in
> > > the challenges you have described. The correct way to include executable
> > > content is to place the Python code within the package structure. It
> > should
> > > not be put in other directories within the repository root.
> > >
> > > As to the second question, once all Python code is contained within a
> > > package that can be installed you can use setuptools entry points to
> > expose
> > > the executable code. The setup() function from setuptools that is used to
> > > create setup.py files has an argument called 'entry_points' that allows
> > you
> > > to expose executable content over the command line. See [1] and [2] for
> > > more details.
> > >
> > > Feel free to reach out to me off-list if you have a specific project you
> > > need advice on. The rules for organizing and packaging Python code aren't
> > > complex but they tend to cause new Python developers to stumble at
> > first. A
> > > general rule I give everyone when talking about packaging or importing
> > > code: If you have to modify sys.path to makes something work then you
> > have
> > > most certainly made a mistake.
> > >
> > > [1]
> > >
> > https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
> > > [2]
> > >
> > http://python-packaging.readthedocs.org/en/latest/command-line-scripts.html#the-console-scripts-entry-point
> > >
> > >
> > > On Sat, Feb 6, 2016 at 8:54 PM Chris Angelico  wrote:
> > >
> > > > On Sun, Feb 7, 2016 at 1:47 PM,   wrote:
> > > > > Imsanity allows you to make imports usable (not ideal, but at least
> > > > usable) for python projects without having to manage PYTHONPATHs or do
> > > > whacky stuff like running files with python -m or put even whackier
> > > > boilerplate at the top of every file. And all it requires is 'import
> > > > imsanity' at the top of every file. You can put it in a macro or even
> > just
> > > > type it because it's short and easy to remember.
> > > > >
> > > > > My question is: is this crazy? Please tell me there's a better way
> > and I
> > > > just wasted my time creating this package. There's nothing I'd like to
> > hear
> > > > more.
> > > >
> > > > Well, anything that makes you type "import imsanity" at the top of
> > > > every script MUST be crazy. :) I don't know about the actual
> > > > content/purpose though. Good luck with it!
> > > >
> > > > ChrisA
> 

Re: A sets algorithm

2016-02-07 Thread Tim Chase
On 2016-02-08 00:05, Paulo da Silva wrote:
> Às 22:17 de 07-02-2016, Tim Chase escreveu:
>>   all_files = list(generate_MyFile_objects())
>>   interesting = [
>> (my_file1, my_file2)
>> for i, my_file1
>> in enumerate(all_files, 1)
>> for my_file2
>> in all_files[i:]
>> if my_file1 == my_file2
>> ]
> 
> "my_file1 == my_file2" can be implemented into MyFile class taking
> advantage of caching sizes (if different files are different),
> hashes or even content (for small files) or file headers (first n
> bytes). However this seems to have a problem:
> all_files: a b c d e ...
> If a==b then comparing b with c,d,e is useless.

Depends on what the OP wants to have happen if more than one input
file is equal. I.e., a == b == c.  Does one just want "a has
duplicates" (and optionally "and here's one of them"), or does one
want "a == b", "a == c" and "b == c" in the output?

> Another solution I thought of, could be defining some methods (I
> still don't know which ones) in MyFile so that I could use sets
> intersection. Would this one be a faster solution?

Adding __hash__ would allow for the set operations, but would
require (as ChrisA points out) knowing how to create a hash function
that encompasses the information you want to compare.

-tkc


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


Re: A sets algorithm

2016-02-07 Thread Cem Karan

On Feb 7, 2016, at 4:46 PM, Paulo da Silva  
wrote:

> Hello!
> 
> This may not be a strict python question, but ...
> 
> Suppose I have already a class MyFile that has an efficient method (or
> operator) to compare two MyFile s for equality.
> 
> What is the most efficient way to obtain all sets of equal files (of
> course each set must have more than one file - all single files are
> discarded)?
> 
> Thanks for any suggestions.

If you're after strict equality (every byte in a pair of files is identical), 
then here are a few heuristics that may help you:

1) Test for file length, without reading in the whole file.  You can use 
os.path.getsize() to do this (I hope that this is a constant-time operation, 
but I haven't tested it).  As Oscar Benjamin suggested, you can create a 
defaultdict(list) which will make it possible to gather lists of files of equal 
size.  This should help you gather your potentially identical files quickly.

2) Once you have your dictionary from above, you can iterate its values, each 
of which will be a list.  If a list has only one file in it, you know its 
unique, and you don't have to do any more work on it.  If there are two files 
in the list, then you have several different options:
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.  Storage tends to be slow, and any tricks that reduce the number of 
bytes you need to read in will be helpful to you.

Good luck!
Cem Karan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Heap Implementation

2016-02-07 Thread Cem Karan

On Jan 30, 2016, at 5:47 PM, Sven R. Kunze  wrote:

> Hi again,
> 
> as the topic of the old thread actually was fully discussed, I dare to open a 
> new one.
> 
> I finally managed to finish my heap implementation. You can find it at 
> https://pypi.python.org/pypi/xheap + https://github.com/srkunze/xheap.
> 
> I described my motivations and design decisions at 
> http://srkunze.blogspot.com/2016/01/fast-object-oriented-heap-implementation.html
>  
> 
> @Cem
> You've been worried about a C implementation. I can assure you that I did not 
> intend to rewrite the incredibly fast and well-tested heapq implementation. I 
> just re-used it.
> 
> I would really be grateful for your feedback as you have first-hand 
> experience with heaps.

<>

My apologies for not writing sooner, but work has been quite busy lately (and 
likely will be for some time to come).

I read your approach, and it looks pretty good, but there may be one issue with 
it; how do you handle the same item being pushed into the heap more than once?  
In my simple simulator, I'll push the same object into my event queue multiple 
times in a row.  The priority is the moment in the future when the object will 
be called.  As a result, items don't have unique priorities.  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.

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


Re: A sets algorithm

2016-02-07 Thread Paulo da Silva
Às 21:46 de 07-02-2016, Paulo da Silva escreveu:
> Hello!
> 
> This may not be a strict python question, but ...
> 
> Suppose I have already a class MyFile that has an efficient method (or
> operator) to compare two MyFile s for equality.
> 
> What is the most efficient way to obtain all sets of equal files (of
> course each set must have more than one file - all single files are
> discarded)?
> 

After reading all suggestions I decided to try first the
defaultdict(list), as first suggested by Oscar, in several steps. First
with sizes and then with other partial contents or/and "strong" hashes
as suggested by Cem.

Thank you very much to all who responded for all helpful suggestions.
If I find something better I'll report here.
Paulo

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


Re: Heap Implementation

2016-02-07 Thread srinivas devaki
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
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