Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Terry Reedy

On 5/19/2016 4:10 PM, Mike Driscoll wrote:

On Thursday, May 19, 2016 at 11:23:53 AM UTC-5, Terry Reedy wrote:



In my case, I learned better how to test IDLE from a user perspective.
For tkinter apps, an external program such as Selenium is not needed.
Tk/tkinter have the simulated event generation and introspection needed
to simulate a user hitting keys, clicking mouse buttons, and reading the
screen.



I am curious. Where is this documented? Are you referring to calling

> the invoke() method on each widget?

For widget commands, yes.  (And thanks for reminding of the method.)

For events:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html
(An indispensible tkinter reference) says:
'''
w.event_generate(sequence, **kw)

This method causes an event to trigger without any external 
stimulus. The handling of the event is the same as if it had been 
triggered by an external stimulus. The sequence argument describes the 
event to be triggered. You can set values for selected fields in the 
Event object by providing keyword=value arguments, where the keyword 
specifies the name of a field in the Event object.


See Section 54, “Events” for a full discussion of events.
'''
This omits some essentials.  tcl.tk/man/tcl8.6/TkCmd/event.htm
has much more, including the need to put focus on Text and Entry.

From Stackoverflow, I learned that .update() is needed *before* 
.event_generate.  The following works.


import tkinter as tk
root = tk.Tk()
def prt():
print('Handler called')
button = tk.Button(root, text='Click', command=prt)
button.place(x=20, y=20)
def ev(e):
print(e.x, e.y)
button.bind('', ev)
button.update()
button.event_generate('', x=0, y=0)
button.event_generate('')
button.invoke()
entry=tk.Entry(root)
entry.place(x=20, y=50)
entry.focus_force()
entry.update()
entry.event_generate('')

The event is reported, the handler is called, and 'a' is inserted. 
(Inserting text could be done more easily, but some key events such as 
 do have text equivalents.)


--
Terry Jan Reedy



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


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Mike Driscoll
On Thursday, May 19, 2016 at 11:23:53 AM UTC-5, Terry Reedy wrote:
> On 5/19/2016 11:33 AM, Mike Driscoll wrote:
> > On Wednesday, May 18, 2016 at 4:48:28 PM UTC-5, Andrew Farrell wrote:
> >> Hi Jacob,
> >>
> >> You are probably looking for the book Test-Driven Development with Python
> >> .
> 
> Electronic version is free online.
> 
> > I was under the impression that this book is primarily aimed at 
> > Python/Django web testing. I saw
> 
> It is.  However, the first four chapters cover the general principles of 
> TDD, so one can read them while thinking of web development as just an 
> illustrative example.
> 
> In my case, I learned better how to test IDLE from a user perspective. 
> For tkinter apps, an external program such as Selenium is not needed. 
> Tk/tkinter have the simulated event generation and introspection needed 
> to simulate a user hitting keys, clicking mouse buttons, and reading the 
> screen.
> 
> -- 
> Terry Jan Reedy

I am curious. Where is this documented? Are you referring to calling the 
invoke() method on each widget?

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Terry Reedy

On 5/19/2016 11:33 AM, Mike Driscoll wrote:

On Wednesday, May 18, 2016 at 4:48:28 PM UTC-5, Andrew Farrell wrote:

Hi Jacob,

You are probably looking for the book Test-Driven Development with Python
.


Electronic version is free online.


I was under the impression that this book is primarily aimed at Python/Django 
web testing. I saw


It is.  However, the first four chapters cover the general principles of 
TDD, so one can read them while thinking of web development as just an 
illustrative example.


In my case, I learned better how to test IDLE from a user perspective. 
For tkinter apps, an external program such as Selenium is not needed. 
Tk/tkinter have the simulated event generation and introspection needed 
to simulate a user hitting keys, clicking mouse buttons, and reading the 
screen.


--
Terry Jan Reedy

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Jacob Scott
Indeed, I skimmed the TOC for Test-Driven Development with Python and it
does look to be rather Django-centric (which makes it a bit less helpful to
me). I will take a look at "Testing Python: Applying Unit Testing, TDD, BDD
and Acceptance Testing"!

Thanks,

Jacob

On Thu, May 19, 2016 at 8:33 AM, Mike Driscoll  wrote:

> On Wednesday, May 18, 2016 at 4:48:28 PM UTC-5, Andrew Farrell wrote:
> > Hi Jacob,
> >
> > You are probably looking for the book Test-Driven Development with Python
> > .
> > You'll also want to look at py.test 
> >
> > Cheers!
> > Andrew Farrell
>
> I was under the impression that this book is primarily aimed at
> Python/Django web testing. I saw "Testing Python: Applying Unit Testing,
> TDD, BDD and Acceptance Testing" is getting good reviews too though.
>
> Mike
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Mike Driscoll
On Wednesday, May 18, 2016 at 4:48:28 PM UTC-5, Andrew Farrell wrote:
> Hi Jacob,
> 
> You are probably looking for the book Test-Driven Development with Python
> .
> You'll also want to look at py.test 
> 
> Cheers!
> Andrew Farrell

I was under the impression that this book is primarily aimed at Python/Django 
web testing. I saw "Testing Python: Applying Unit Testing, TDD, BDD and 
Acceptance Testing" is getting good reviews too though. 

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Chris Angelico
On Thu, May 19, 2016 at 7:13 PM, Gregory Ewing
 wrote:
> Chris Angelico wrote:
>>
>> [1] Some people's names can't be represented in Unicode.
>
>
> Like this fellow's, for instance:
>
> https://www.youtube.com/watch?v=hNoS2BU6bbQ

His name actually CAN be represented in Unicode. It's his address that
I'd have trouble with.

(Smart copper, by the way.)

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-19 Thread Gregory Ewing

Chris Angelico wrote:

[1] Some people's names can't be represented in Unicode.


Like this fellow's, for instance:

https://www.youtube.com/watch?v=hNoS2BU6bbQ

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Rustom Mody
On Thursday, May 19, 2016 at 9:26:39 AM UTC+5:30, Steven D'Aprano wrote:
> On Thursday 19 May 2016 13:31, Rustom Mody wrote:
> 
> > On Thursday, May 19, 2016 at 6:26:26 AM UTC+5:30, Ben Finney wrote:
> 
> >> Code Like A Pythonista was written in the Python 2 era
> >> 
> >> but is still excellent advice today.
> > 
> > 
> > If  is python-2 and still the best excellent advice today
> > doesn't it go somewhat counter to pyhthon-2 is a dead-end ?? :-)
> 
> Ben didn't say that it is the *best* excellent advice.
> 
> But even if he did, your conclusion is invalid. I still have books written 
> for 
> Python 1.4 and 1.5 that I turn to when I can't remember % targets and regular 
> expression codes, and my copy of the Python Cookbook is still full of 
> excellent 
> code even though it is written for Python 2.

And you have the knowledge and experience to know that when you refer to
a python 2 (or 1) reference and apply it in 3 and there are minor glitches,
you know where to go for clarifications.

Maybe the OP also knows... maybe not...
No case against python3, just that the pro-3 rhetoric need not be so shrill

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Steven D'Aprano
On Thursday 19 May 2016 13:31, Rustom Mody wrote:

> On Thursday, May 19, 2016 at 6:26:26 AM UTC+5:30, Ben Finney wrote:

>> Code Like A Pythonista was written in the Python 2 era
>> 
>> but is still excellent advice today.
> 
> 
> If  is python-2 and still the best excellent advice today
> doesn't it go somewhat counter to pyhthon-2 is a dead-end ?? :-)

Ben didn't say that it is the *best* excellent advice.

But even if he did, your conclusion is invalid. I still have books written for 
Python 1.4 and 1.5 that I turn to when I can't remember % targets and regular 
expression codes, and my copy of the Python Cookbook is still full of excellent 
code even though it is written for Python 2.


> Need to point this out since the opposite case to Chris'
> "switch to 3 or else suffer in hell..." needs to be articulated:
> 
> Python-3 is nice but 2 is ok. The diffs are not such a big deal

Python 2 is still an excellent language. It's just not going forward -- no new 
features will be added to it.


-- 
Steve

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Chris Angelico
On Thu, May 19, 2016 at 1:31 PM, Rustom Mody  wrote:
> On Thursday, May 19, 2016 at 6:26:26 AM UTC+5:30, Ben Finney wrote:
>> Code Like A Pythonista was written in the Python 2 era
>> 
>> but is still excellent advice today.
>
>
> If  is python-2 and still the best excellent advice today
> doesn't it go somewhat counter to pyhthon-2 is a dead-end ?? :-)

Nope. Py2 is a dead end because it isn't moving forward. It's staying
right where it is. There can certainly be advice written about Python
2 that is worth reading, though. In fact, I have some books written
about REXX on OS/2 which I would recommend to someone learning Python
on Linux. I probably have some books from the 1980s that are still
worth reading. (Software books older than that won't be on my shelf,
but quite likely do exist.)

> Need to point this out since the opposite case to Chris'
> "switch to 3 or else suffer in hell..." needs to be articulated:

That is not my stance. Python 2 is still usable - it just isn't moving forward.

> Python-3 is nice but 2 is ok. The diffs are not such a big deal

The differences are getting to be a bigger and bigger deal. I wouldn't
advise anyone to use Python 2.4 unless compatibility with Red Hat
Enterprise Linux 5, because 2.4 misses out on heaps of stuff that
newer versions have. It's the same with 2.7 - use it if compatibility
with systems without 3.x is important, otherwise use the latest. (Of
course, that's new projects. Existing code implies a porting cost,
which has to be factored in; but making the jump to 3.5 or 3.6 is well
worth it IMO.)

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Rustom Mody
On Thursday, May 19, 2016 at 6:26:26 AM UTC+5:30, Ben Finney wrote:
> Jacob Scott  writes:
> 
> > Today, I'm happily writing primarily Python (unfortunately, 2.7 -- but I'm
> > not sure it makes that much of a difference)
> 
> Python 2.7 is still viable, but is certainly a dead end. The difference
> increases month by month, and the advantage is only going to increase to
> Python 3.
> 
> Any new code base should not be written in Python 2. Any libraries you
> need which don't work yet on Python 3 should be seriously reconsidered.
> 
> > I'd appreciate any pointers to resources I might have missed, general
> > thoughts on the topic, etc.
> 
> Code Like A Pythonista was written in the Python 2 era
> 
> but is still excellent advice today.


If  is python-2 and still the best excellent advice today
doesn't it go somewhat counter to pyhthon-2 is a dead-end ?? :-) 


Need to point this out since the opposite case to Chris' 
"switch to 3 or else suffer in hell..." needs to be articulated:

Python-3 is nice but 2 is ok. The diffs are not such a big deal
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Chris Angelico
On Thu, May 19, 2016 at 11:15 AM, Jacob Scott  wrote:
> I think I would be (perhaps pleasantly) surprised if there was a wide gulf
> between Python 2.7 and Python 3 *in terms of advice/resources applicable to
> my original question*. Based on my (admittedly shallow) understanding of
> overall Python 2.7/3 differences, the biggest changes (from e.g.
> http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html) tend to
> be a bit lower level (utf-8 str) than what I'm focused on (maintainable and
> testable classes, functions, modules, etc).

It's not really about UTF-8 (and the article you link to is flat wrong
in describing Py3's str type as UTF-8); it's about the recognition of
a fundamental difference between text and bytes. Looking at it another
way: Python 2 lets you pretend it's all easy, so long as you're
working with the simple Latin letters A-Z and a-z, but when you need
anything else (other alphabets, non-ASCII symbols like dashes and
quotes and emoji, diacritical marks, etc, etc), Python 2 doesn't help
you at all, and suddenly you're on your own. Python 3 forces you to
think up-front about things (a little bit of extra work/hassle), but
then gives you all the world's languages for free. That's a very high
level feature: you ask a user to enter his/her name, and instead of
requiring that it be English without diacritical marks, you can accept
(almost[1]) anything. Sometimes, UTF-8 lets you *pretend* that Python
2 (or C or PHP or whatever other language you pick) can handle all
those other characters too, but it's a massively leaky abstraction.

The other broad change in Python 3 is a shift in focus from concrete
lists to lazy objects. In Py2, range() returns a list of numbers; in
Py3, it returns a "range object", which functions just like that list
in many ways (subscripting, iterating, etc work the same way), but is
far more memory-efficient on huge ranges. In Py2, a dictionary's
keys() method returns a list; in Py3, it returns a special view
object. That kind of thing.

Quite a few of the other differences mentioned in that article are
actually the removal of already-deprecated syntax. For instance, the
'except NameError as err:' syntax works in 2.6 and 2.7 as well as 3.x,
and is the recommended way to spell it (unless you need to support
ancient Pythons - 2.5 is pretty old now, but some people do support
it). For backward compatibility, all of the 2.x line still supports
the old syntax, but new code should be using the reliable syntax - the
comma is ambiguous (what if you wanted to accept more than one
exception type?). Similarly, you shouldn't be calling an iterator's
.next() method directly; always call the built-in next() function.
Python 3 made this a lot clearer by renaming the method to __next__(),
which clarifies that this is a "magic method" or "special method.
Dunder methods are for defining, not calling. (Rule of thumb, not
entirely true, but close on.)

For the rest, Python 3 basically has the advantage of six additional
years of development time. That's given us features like exception
chaining, core support for virtual environments, a variety of new
stdlib modules, support for asynchronous I/O, a new multiplication
operator, and soon (3.6), interpolated string 'literals' (more like a
list comprehension than a string literal). And that gap is ever
widening. Using 2.7 means abandoning all those features. It's still in
support, but it's bug fixes and security patches only - which, as the
Red Queen explained to Alice, is where you have to run as fast as you
can just to stay in one place.

ChrisA

[1] Some people's names can't be represented in Unicode. But excluding
those names is a lot less significant than excluding the huge
proportion of people whose names aren't representable in ASCII.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Jacob Scott
Ah, what I should have done is note that I am writing Python 2.7 (and this
is at work, with all that entails...), but am happy to take advice that
applies only to Python 3 (even 3.5 or 3.6.0a1!) and work backwards to apply
it to Python 2.7.

I think I would be (perhaps pleasantly) surprised if there was a wide gulf
between Python 2.7 and Python 3 *in terms of advice/resources applicable to
my original question*. Based on my (admittedly shallow) understanding of
overall Python 2.7/3 differences, the biggest changes (from e.g.
http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html) tend to
be a bit lower level (utf-8 str) than what I'm focused on (maintainable and
testable classes, functions, modules, etc).

Thanks for the pointer to Code Like A Pythonista and the feedback on 2.7 vs
3!

Jacob

On Wed, May 18, 2016 at 5:55 PM, Ben Finney 
wrote:

> Jacob Scott  writes:
>
> > Today, I'm happily writing primarily Python (unfortunately, 2.7 -- but
> I'm
> > not sure it makes that much of a difference)
>
> Python 2.7 is still viable, but is certainly a dead end. The difference
> increases month by month, and the advantage is only going to increase to
> Python 3.
>
> Any new code base should not be written in Python 2. Any libraries you
> need which don't work yet on Python 3 should be seriously reconsidered.
>
> > I'd appreciate any pointers to resources I might have missed, general
> > thoughts on the topic, etc.
>
> Code Like A Pythonista was written in the Python 2 era
> 
> but is still excellent advice today.
>
> --
>  \ “I have the simplest tastes. I am always satisfied with the |
>   `\best.” —Oscar Wilde, quoted in _Chicago Brothers of the Book_, |
> _o__) 1917 |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Ben Finney
Jacob Scott  writes:

> Today, I'm happily writing primarily Python (unfortunately, 2.7 -- but I'm
> not sure it makes that much of a difference)

Python 2.7 is still viable, but is certainly a dead end. The difference
increases month by month, and the advantage is only going to increase to
Python 3.

Any new code base should not be written in Python 2. Any libraries you
need which don't work yet on Python 3 should be seriously reconsidered.

> I'd appreciate any pointers to resources I might have missed, general
> thoughts on the topic, etc.

Code Like A Pythonista was written in the Python 2 era

but is still excellent advice today.

-- 
 \ “I have the simplest tastes. I am always satisfied with the |
  `\best.” —Oscar Wilde, quoted in _Chicago Brothers of the Book_, |
_o__) 1917 |
Ben Finney

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


Re: Resources/pointers for writing maintable, testable Python

2016-05-18 Thread Andrew Farrell
Hi Jacob,

You are probably looking for the book Test-Driven Development with Python
.
You'll also want to look at py.test 

Cheers!
Andrew Farrell

On Wed, May 18, 2016 at 5:01 PM, Jacob Scott  wrote:

> Many years ago, when I was primarily writing Java, I found Misko
> Hevery's Guide:
> Writing Testable Code
> 
> to
> be incredibly helpful in guiding the design and structure of my codebase,
> and as reference for checking if my code was smelly.
>
> Today, I'm happily writing primarily Python (unfortunately, 2.7 -- but I'm
> not sure it makes that much of a difference), but I haven't found anything
> that speaks to me in the same way. Some of the best resources I've found,
> but which don't quite cover all of what I'm looking for, include
>
>- PEP-8 and PEP-20
>- The Hitchhiker's Guide to Python
>
>- Effective Python 
>
> I'd appreciate any pointers to resources I might have missed, general
> thoughts on the topic, etc.
>
> Thanks,
>
> Jacob
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list