[Tutor] easygui

2017-08-27 Thread George Sconyers via Tutor
Hello all. Need some help with easygui which my son is trying to run for a book 
he is working in. Using Python 2.7 we downloaded easygui and put it in an 
executable path. 
Code is:import easyguieasygui.msgbox("Hello there")
No popup occurs, cusor changes to a cross and no longer process mouse clicks. 
When we click on the bash shell the program terminates, cursor returns to 
normal, and the following error is delivered:
script.py: line 2: syntax error near unexpected token  '"Hello 
there"'script.py: line 2: 'easygui.msgbox("Hello there")'
We also tried:import easyguimsgbox("Hello there")
Returns the error message:script.py: line 2: syntax error near unexpected token 
 '"Hello there"'script.py: line 2: 'msgbox("Hello there")'
Checked on "easygui. sourceforge. net/tutorial.html#msgbox" and it appears I am 
using the correct syntax. Probably a stupid mistake but I am stumped. 
ThanksGeorge

Sent from Yahoo Mail for iPhone
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easygui

2017-08-27 Thread Alan Gauld via Tutor
On 27/08/17 03:07, George Sconyers via Tutor wrote:
> Hello all. Need some help with easygui which my son is trying to run for a 
> book he is working in.

I'm not sure what the current status of easygui is, I read
somewhere that it had been discontinued. But if you are
still finding a download for 2.7 then it should
presumably work... Ok, I see that the code is still
available, and being Tkinter based should still work
but the project was frozen in 2013.

> Using Python 2.7 we downloaded easygui and put it in an executable path. 
> Code is:import easyguieasygui.msgbox("Hello there")

The code appears to be mangled by the mail system. I assume you mean

import easygui
easygui.msgbox("Hello there")

> No popup occurs, cusor changes to a cross and no longer process mouse 
> clicks.> When we click on the bash shell the program terminates,

I assume from the bash reference that you are using either Linux
or MacOSX?

> script.py: line 2: syntax error near unexpected token  '"Hello there"
> 'script.py: line 2: 'easygui.msgbox("Hello there")'

How exactly are you running the program?
The message format does not look like a normal Python
traceback message.

BTW you can do very similar style coding to easygui using
raw Tkinter. You need to add the following 4 lines at the
top of your file (this is for Python v3):

import tkinter
import tkinter.messagebox as mb
tk = tkinter.Tk()
tk.withdraw()# hide the default blank window

Now you can use the various message boxes etc in a
very similar style to easygui:

mb.showinfo("Title here", "Text here")
mb.askyesno("Title", "Are you well?")

dir(mb)

will show the other options available.
You can also import tkinter.filedialog,colorchooser and font.

Just in case you can't get easygui to work.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best way to get root project directory in module search path

2017-08-27 Thread Cameron Simpson

On 26Aug2017 21:27, boB Stepp  wrote:

I have a project structure where I am developing various programs,
some in Pinnacle HotScript language, some in Perl, some in Python,
some shell scripts.  For the purpose of this question, my simplified
development file structure is:

~/_dev_Scripts
   /ScriptMenuSystem
   __init__.py
   test.py

   /py_utilities
   __init__.py
   test.py

When I am fully happy (Not really! ~(:>)) ) with everything I will
copy everything pertinent from "~_dev_Scripts" into a location like
"/usr/local/.../Scripts/boB_Scripts".  So all code must be able to
handle any change in location from where I am developing it.  On to my
difficulties ...

ScriptMenuSystem/test.py:

-
#!/usr/bin/env python

import sys
sys.path.append('..')

import py_utilities.test as t

t.test_print()


The trouble with this specific approach is that '..' relies on your _working_ 
directory being above ScriptMenuSystem i.e. the directory you shell's in; '..' 
is not related to the path to the test.py file. What yu want for this is the 
actual path to the code i.e. __file__.  Eg:


 sys.path.append(dirname(dirname(__file__)))

but that requires your module to know its own location within your library i.e.  
that it is .../dev_scripts/ScriptMenuSystem/test.py, and therefore that you 
want .../dev_scripts. Fragile. (And __file__ doesn't work in some more arcane 
ways of distributing modules, such as in zip files but let's not go there, not 
least because I've never done that myself).


The usual approach is to burden the user of the module. They're using it: 
ideally they have "installed" it by putting it somewhere sensible. So in a 
sense you should be able to rely on $PYTHONPATH already being set up.


Think about the end game: supposing people are using your modules in some 
larger system (eg a virtualenv). Do they really even _want_ you mucking about 
with their $PYTHONPATH or sys.path or, really, anything that will affect how 
modules are looked up?


Personally, I rely on $PYTHONPATH being right when I'm called. If someone's 
"installed" your module (eg by putting it in site-packages or wherever) the 
path is already set up. For my development purposes I invoke my code via a 
"dev" shell function which prehacks $PYTHONPATH to have my local lib directory 
at the start of $PYTHONPATH. Again, the module itself can then trust that 
things are as they should be. Which is as it should be.


Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easygui

2017-08-27 Thread Mats Wichmann
On 08/27/2017 02:14 AM, Alan Gauld via Tutor wrote:
> On 27/08/17 03:07, George Sconyers via Tutor wrote:
>> Hello all. Need some help with easygui which my son is trying to run for a 
>> book he is working in.
> 
> I'm not sure what the current status of easygui is, I read
> somewhere that it had been discontinued. But if you are
> still finding a download for 2.7 then it should
> presumably work... Ok, I see that the code is still
> available, and being Tkinter based should still work
> but the project was frozen in 2013.
> 
>> Using Python 2.7 we downloaded easygui and put it in an executable path. 
>> Code is:import easyguieasygui.msgbox("Hello there")
> 
> The code appears to be mangled by the mail system. I assume you mean
> 
> import easygui
> easygui.msgbox("Hello there")
> 
>> No popup occurs, cusor changes to a cross and no longer process mouse 
>> clicks.> When we click on the bash shell the program terminates,
> 
> I assume from the bash reference that you are using either Linux
> or MacOSX?
> 
>> script.py: line 2: syntax error near unexpected token  '"Hello there"
>> 'script.py: line 2: 'easygui.msgbox("Hello there")'
> 
> How exactly are you running the program?
> The message format does not look like a normal Python
> traceback message.

Indeed, that's an error from bash running the script, not Python.

If you're on Linux (or Mac), and your script is named example.py, run it as:

python example.py

or perhaps less intuitively, stick a first line in it to tell the system
to have Python run it, so your script looks like this (there are
possible variants on that magic first line, but this one should work
whichever your platform is, as long as it is the first line):

#!/usr/bin/env python
import easygui
easygui.msgbox("Hello there")


and you can then run the script directly.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best way to get root project directory in module search path

2017-08-27 Thread boB Stepp
On Sun, Aug 27, 2017 at 2:13 AM, Cameron Simpson  wrote:
> On 26Aug2017 21:27, boB Stepp  wrote:
>>
>> I have a project structure where I am developing various programs,
>> some in Pinnacle HotScript language, some in Perl, some in Python,
>> some shell scripts.  For the purpose of this question, my simplified
>> development file structure is:
>>
>> ~/_dev_Scripts
>>/ScriptMenuSystem
>>__init__.py
>>test.py
>>
>>/py_utilities
>>__init__.py
>>test.py
>>
>> When I am fully happy (Not really! ~(:>)) ) with everything I will
>> copy everything pertinent from "~_dev_Scripts" into a location like
>> "/usr/local/.../Scripts/boB_Scripts".  So all code must be able to
>> handle any change in location from where I am developing it.  On to my
>> difficulties ...
>>
>> ScriptMenuSystem/test.py:
>>
>> -
>> #!/usr/bin/env python
>>
>> import sys
>> sys.path.append('..')
>>
>> import py_utilities.test as t
>>
>> t.test_print()
>
>
> The trouble with this specific approach is that '..' relies on your
> _working_ directory being above ScriptMenuSystem i.e. the directory you
> shell's in; '..' is not related to the path to the test.py file. What yu
> want for this is the actual path to the code i.e. __file__.  Eg:
>
>  sys.path.append(dirname(dirname(__file__)))

I tried to use this (on Python 2.4/2.6 -- one server has 2.4, the
other 2.6) and got the following:

Traceback (most recent call last):
File "... /test.py", line 9, in 
sys.path.append(dirname(dirname(__file__)))
NameError:  name 'dirname' is not defined

Is this a version issue or do I have the syntax wrong?

Anyway, I tried something a bit different that implemented this:

pgm_dir = os.path.dirname(os.path.abspath(__file__))
pgm_root = pgm_dir.replace('/ScriptMenuSystem', '')
sys.path.append(pgm_root)

And this gave me the results I wanted.  Funny that 'dirname' works
with os.path but not with sys.path.

BTW, I have not used os.walk yet, but would that be better than my bit
of string manipulation above?  If yes, an example of walking up and
getting the directory exactly one level up from __file__ would be
appreciated.

> but that requires your module to know its own location within your library
> i.e.  that it is .../dev_scripts/ScriptMenuSystem/test.py, and therefore
> that you want .../dev_scripts. Fragile. (And __file__ doesn't work in some
> more arcane ways of distributing modules, such as in zip files but let's not
> go there, not least because I've never done that myself).

I have been using the same project structure for some years now, so in
that sense it is well-established and not fragile.  I don't anticipate
it changing.  There is actually no installation in a traditional
sense.  Once I develop something worth using, it gets copied (by me as
root) from my user area to an accessible location to those users that
requested access.  I had been doing this copying to each user's area,
but that has grown unwieldy as my couple of users have grown into
around 15.  So I am moving to put everything in a centrally accessible
location and will probably adopt Steve's suggestion to establish a
group with permissions to use these programs.

So I think this idea (Or some variation thereof.) is what I need to
do.  Or the good experts here show me I'm engaged in foolishness!

Cheers!


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best way to get root project directory in module search path

2017-08-27 Thread Cameron Simpson

On 27Aug2017 14:27, boB Stepp  wrote:

On Sun, Aug 27, 2017 at 2:13 AM, Cameron Simpson  wrote:

On 26Aug2017 21:27, boB Stepp  wrote:

import sys
sys.path.append('..')

[...]

The trouble with this specific approach is that '..' relies on your
_working_ directory being above ScriptMenuSystem i.e. the directory you
shell's in; '..' is not related to the path to the test.py file. What yu
want for this is the actual path to the code i.e. __file__.  Eg:

 sys.path.append(dirname(dirname(__file__)))


I tried to use this (on Python 2.4/2.6 -- one server has 2.4, the
other 2.6) and got the following:

Traceback (most recent call last):
   File "... /test.py", line 9, in 
   sys.path.append(dirname(dirname(__file__)))
NameError:  name 'dirname' is not defined


from os.path import dirname


Is this a version issue or do I have the syntax wrong?


Just a missing import. You'll find dirname in the Index section of the Python 
docs. Handy for locating something just mentioned.



Anyway, I tried something a bit different that implemented this:

pgm_dir = os.path.dirname(os.path.abspath(__file__))
pgm_root = pgm_dir.replace('/ScriptMenuSystem', '')
sys.path.append(pgm_root)


Calling os.path.dirname a second time does what your:

 pgm_dir.replace('/ScriptMenuSystem', '')

does, but reliably. Supposing you have the misfortune to have 
'/ScriptMenuSystem' higher up in the full path also? Supposing your package 
changes its name in the future, or is installed with another name? Fragility.



And this gave me the results I wanted.  Funny that 'dirname' works
with os.path but not with sys.path.


No no. "dirname" comes from the "os.path" module. That is all.


BTW, I have not used os.walk yet, but would that be better than my bit
of string manipulation above?  If yes, an example of walking up and
getting the directory exactly one level up from __file__ would be
appreciated.


Well:

 from os.path import abspath, dirname, join
 code_dir = abspath(join(dirname(__file), '..'))

or:

 from os.path import abspath, dirname, join
 code_dir = abspath(join(dirname(__file), '../..'))

depending how high you want to go.

Given that os.walk walks _down_ the tree from some directory, how do you think 
it would help you?



but that requires your module to know its own location within your library
i.e.  that it is .../dev_scripts/ScriptMenuSystem/test.py, and therefore
that you want .../dev_scripts. Fragile. (And __file__ doesn't work in some
more arcane ways of distributing modules, such as in zip files but let's not
go there, not least because I've never done that myself).


I have been using the same project structure for some years now, so in
that sense it is well-established and not fragile.


It is more that embedded specific hardwired knowledge in the code is an 
inherently fragile situation, if only because it must be remembered when naming 
changes occur. And they do. I have a quite large package I've been working on 
for a decade and am very seriously contemplating changing its name in the next 
month or so. Fortunately the installed base is small.



I don't anticipate
it changing.  There is actually no installation in a traditional
sense.  Once I develop something worth using, it gets copied (by me as
root) from my user area to an accessible location to those users that
requested access.  I had been doing this copying to each user's area,
but that has grown unwieldy as my couple of users have grown into
around 15.


For simple packages/modules an install _is_ just a copy. So you're performing 
the install step.



So I am moving to put everything in a centrally accessible
location


Very sound. But to use it your users should have that location as part of their 
python path, or the code needs to be invoked by some wrapper which can insert 
the needed path. Both these things live _outside_ the package code.



and will probably adopt Steve's suggestion to establish a
group with permissions to use these programs.


Note that this is normally only needed for code that is privileged i.e. runs 
with permissions different to those of the user.



So I think this idea (Or some variation thereof.) is what I need to
do.  Or the good experts here show me I'm engaged in foolishness!


I think you're better off arranging your users' environments to include the 
shared library area, or providing a wrapper shell script which does that and 
then invokes the real code.


Eg:

 #!/bin/sh
 PYTHONPATH=/path/to/your/shared/lib:$PYTHONPATH
 export PYTHONPATH
 python -m your.module.name ${1+"$@"}

Such a script has the advantage that the $PYTHONPATH change applies only to the 
python running your module, not to your users' wider environment.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Need advice on testing python code.

2017-08-27 Thread Anubhav Yadav
Hello.

I am a python developer and I write a lot of python code everyday. I try to do 
as much unit testing as possible. But I want to be better at it, I want to 
write more test cases, specially that rely on database insertions and reads and 
file io. Here are my use-cases for testing. 
How to test if things are going into the database properly or not? 
(mysql/mongo). I want to be able to create a test database environment as 
simple as possible. Create and delete the test environment before each 
functional test case is run. 
Sometimes I write code that read some data from some rabbitmq queue and do 
certain things. How can I write end to end functional test that creates a test 
rabbitmq environment (exchanges and queues) -> wait for sometime -> see if the 
intended work has been done -> delete the test environment. 
I want to be able to make sure that any new commit on my self hosted gitlab 
server should first run all functional test cases first before accepting the 
merge. 
Since we use lot of docker here to deploy modules to productions, I want to 
write functional test cases that test the whole system as a whole and see if 
things are happening the way they are supposed to happen or not. This means 
firing up lot of docker containers, lot of test databases with some data, and 
run all the test cases from an end user point of view. 
Can you suggest me the right python testing frameworks that I should be using? 
Right now I am using unittest to write test cases and manual if/else statements 
to run the functional test cases. 
I try to create rabbitmq queues and bind them to rabbitmq exchanges using the 
pika module. I then run the module using python -m moduleName and then sleep 
for sometime. Then I kill the processs (subprocess) and then I see if the 
intended consequences have happened or not. It's a pain in the ass to be doing 
so many things for test cases. I clearly need to learn how to do things better. 
Any suggestion/book/article/course/video will help me immensely in becoming a 
developer who writes better code with lot of test cases. 

Thanks for reading. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fw: easygui

2017-08-27 Thread George Sconyers via Tutor
Mats, that first line was the problem.
Alan, I like the Tkinter idea. Had to pull that package for easygui to work. I 
will look at transitioning once my son works through the easygui chapter. 
Trying to teach him how to learn as much as how to program. 
Thank you both! George





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What exactly does the three dots do? Why such as thing?

2017-08-27 Thread C W
Today, I ran into ellipsis again. This time, it is COMPLETELY different
from before.

Here is a reproducible code:

import numpy as np
import matplotlib.pyplot as plt
...
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.title('Week cycle')
plt.xlabel('Month')
plt.ylabel('Price')
plt.plot(x, y)


If I leave out the ellipsis, plot title and label will not show. It's doing
interactive plot. How am I suppose to know ellipsis is doing interactive
plotting? No way I could have guessed.

Matplotlib documentation  explains that it
recreated itself from Matlab plots.

Is it something similar to Matlab like
>>> hold on;

That's fine! But, how many identities will ellipsis take on? How do you
experienced users deal with these ambiguities?



On Fri, Aug 11, 2017 at 9:24 PM, Steven D'Aprano 
wrote:

> On Fri, Aug 11, 2017 at 07:57:09AM -0600, Mats Wichmann wrote:
> > On 08/10/2017 05:23 PM, Alan Gauld via Tutor wrote:
> > > On 10/08/17 14:39, C W wrote:
> > >
> > >> I suppose it's just a place holder, though I don't know when I would
> use it
> > >> in my every day life.
> > >
> > > Probably never.
> > >
> > > Like most programming languages Python has a load of rarely used,
> > > obscure features. Most Python programmers never use ellipses,
> >
> > I guess what this means is when I post code snippets with some lines
> > elided for greater readability of the point being made I should not use
> > ellipses for that, as they're actually a syntactic element!   :)
>
> No, go right ahead and continue using ... for elided lines. Python 3
> makes that syntactically legal, and the fact that elided code may be
> syntactically correct is one of the reasons that was done.
>
> In Python 2, ... was just the *display* form of Ellipsis, and wasn't
> legal except in slice notation: a[...]. Python 3 made ... syntactic
> sugar for Ellipse everywhere, not just in slices, which makes:
>
> x = ...
>
> class X:
> ...
>
> perfectly legal code. (Perhaps not *meaningful* code, but that's okay.)
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor