Re: [Tutor] Do not understand code snippet from "26.8. test — Regression tests package for Python"

2017-04-18 Thread Martin A. Brown

Greetings,

>Thank you very much Martin; you filled in a lot of details.  I had an
>overall understanding of what unittest does, but you have now enhanced
>that understanding substantially.

Happy to help!  I'll introduce you to my little menagerie below!

>I'm still iffy on how the mixin class gets its test method called 
>when this class does not subclass from unittest.TestCase, but I 
>think I may have an idea now on how it is happening.  Let's get to 
>that part of your response.

Well, the mixin technique is not strictly speaking unittest related, 
but more a matter of understanding multiple inheritance.  Once you 
understand that a bit better, I think you'll understand why this 
technique for using unittest works as it does.

>> I'll make a few observations:
>>
>>   - [on unittest] the unit testing tools use classes because it's a
>> natural way to accommodate the goal of reproducibly setting up
>> arguments and/or an environment for each test (note that each
>> TestCase can have its own setUp() and tearDown() methods; this
>> allows isolation)
>>
>>   - [on unittest] each test collected by the TestLoader can be any
>> Python class (as long as it is also derived from
>> unittest.TestCase)

I'll emphasize this point before going on further.  All 
unittest.TestLoader cares about is that it has found (for example) 
an instance of something that is a unittest.TestCase.  Your class 
can inherit from any number of other classes, but 
unittest.TestLoader will not find it, unless it also derives from 
unittest.TestCase.

Now, on to the MRO bits.

>>   - [on your classes] your classes use a multiple inheritance
>> model, deriving from TestFuncAcceptsSequencesMixin; when
>> instantiated, they'll have all of the expected TestCase methods
>> and the method called 'test_func'
>
>It is here that I am struggling.  If the mixin class does not inherit
>from unittest.TestCase, then how is test_func ever seen?

Your classes (AcceptLists, AcceptTuples, AcceptStrings) specify both 
unittest.TestCase and TestFuncAcceptsSequencesMixin.  This is 
multiple inheritance.  (N.B. I'm not sure where to recommend further 
reading on MRO, but others on the list may know.)

So, how is test_func ever seen?  After your class is defined (and 
instantiated), the instance has access to all of the methods of all 
of the parent classes.

In your case:

  * One of the parent classes of AcceptTuples is 
TestFuncAcceptsSequencesMixin which defines the method 
test_func.

  * The method 'test_func' matches the expectation of unittest when 
it goes looking for any method that matches the name 'test_*'.  

The number of methods on instances of unittest.TestCase class is 
higher (see at bottom of this email), but you will see your 
test_func method exists on each instance of the classes you created.

>This answers one important thing I was wondering about:  How do the
>classes AcceptLists, AcceptStrings, and AcceptTuples get instantiated?
>Apparently the unittest machinery does this for me.

Yes.

>> - for each method name starting with 'test_' (you have only
>>   'test_func') TestRunner will:
>
>And here is my precise sticking point:  How does the TestRunner 
>find test_func?  The classes it has collected and instantiated 
>(AcceptLists, AcceptStrings and AcceptTuples) do not themselves 
>call and make use of the test_func method they inherit from the 
>mixin class.

It looks for methods whose names match a specific pattern.  The name 
should start with 'test_*' (this is configurable if you wanted your 
tests to begin with 'frobnitz_', but I haven't seen anybody do 
this).

>>   - execute the T.setUp() method if it exists
>>
>>   - TestRunner will execute the method 'test_func'
>
>The only thing that makes sense to me is that the TestRunner 
>follows the MRO of the inherited classes and checks for any 
>test_xxx methods that might exist in those superclasses.  Is this 
>correct or do I have a conceptual misunderstanding?

No misunderstanding.  All that's happening here is that unittest is 
defining your class (which imports / inherits everything it needs) 
and then is looking for the 'test_*' methods.

You may (or may not) benefit from studying the MRO any further, but 
here's a function you could call to see multiple inheritance in 
action.  Feed this function an instance of your class:

  def log_class_method_names(*args):
  import inspect
  for o in args:
  logging.info("Class %s found", o.__class__.__name__)
  for methodname, _ in inspect.getmembers(o, inspect.ismethod):
  logging.info("Class %s has method %s", o.__class__.__name__, 
methodname)

If you try that with three instances of your classes, you should see all of the
methods that unittest will see after the class is instantiated (see also at the
foot of this email).

See below my signature if you are a chimera aficianado,

-Martin

sample script to identify chimera features

Re: [Tutor] Visual Studio Community 2017

2017-04-18 Thread Jos Kerc
Hi,

you might want to look at things like Anaconda, Enthought, ... They come
with lots of packages/libraries pre-installed and a complete programming
environment. For small project, you could even look at Jupyter. I t allows
you to have your program, data & results (IIRC even pics & vids) in
something like a webpage.
Have fun.
On Apr 18, 2017 7:08 PM, "Rafael Knuth"  wrote:

> I wanted to start my first project using matplotlib (I have never
> worked with libraries before). I am trying to get started with VS
> Community 2017, and I am having trouble performing the most basic
> tasks such as installing matplotlib. Anyone here using VS 2017? Or,
> can anyone recommend an alternative to VS for Windows? Thanks!
> ___
> 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


Re: [Tutor] Visual Studio Community 2017

2017-04-18 Thread Mats Wichmann


...

naturally, far better than what I wrote, is:

https://docs.microsoft.com/en-us/visualstudio/python/python-environments



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


Re: [Tutor] Visual Studio Community 2017

2017-04-18 Thread Mats Wichmann
On 04/18/2017 11:14 AM, Alan Gauld via Tutor wrote:
> On 18/04/17 17:55, Rafael Knuth wrote:
>> I wanted to start my first project using matplotlib (I have never
>> worked with libraries before). 
> 
> Every time you do an import you are using a library.
> eg
> 
> import sys
> import os
> etc
> 
> It's no big deal.
> 
>> I am trying to get started with VS Community 2017, 
>> and I am having trouble performing the most basic
>> tasks such as installing matplotlib. Anyone here using VS 2017? 
> 
> VS is great for .NET development for Windows but frankly
> for anything else I prefer Eclipse or Netbeans as IDEs.
> But personally I don't much like big greedy IDEs (unless
> I'm writing Java) so I tend to just use a text editor
> (vim or notepad++ on windows) and a Python shell
>  - possibly even IDLE. I also use the Pythonwin IDE
> if I'm doing any COM type work because of its debugger
> and built in COM browser.
> 
> But editors and IDEs are very personal choices. The best
> thing is to download a few and play with them. There is
> no shortage of candidates to pick from!
> 

PyCharm :)

anyway, some thoughts on using VS, which I only recommend if you're
already a dedicated VS user (same general comment as Alan).

"libraries" other than the python standard library do need installation,
which in many cases is done through the PIP tool (on Windows there are
often also pre-built packages installed the normal way, which you can
resort to if something doesn't work using PIP - since you have a full
build environment known to be there with VS 2017, it should work though.

VS has a Python tools addon, which you should install if it's not there
already.

you can then go Tools -> Options -> Python Tools -> Environment Options

look at the paths in the existing environment (matching your installed
Python version) and remember them

create a new environment by clicking Add Environment (the existing one
will probably work, but I don't know how to get around VS's idea it's
fully managing it, and we want to install packages).

in new environment,  fill in the paths.  There's a way to auto-detect
(in a different screen) but on my VS 2015 install at least it seems to
always be greyed out.

You should have a Python Environments on the right, another tab where
there's usually the Solution Explorer.  If it's not open, you can get it
from View -> Other Windows.

pick "pip" from the drop-down, and search for the pkgs you want - just
typing matplotlib should give you a bunch of choices, and that should
also deal with dependencies.  That windo may also offer some existing
pkgs that need updating.


anyway, this sort of thing has worked for me in limited experiments.
Then it's just "import whatever" as Alan says.




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


Re: [Tutor] Visual Studio Community 2017

2017-04-18 Thread Alan Gauld via Tutor
On 18/04/17 17:55, Rafael Knuth wrote:
> I wanted to start my first project using matplotlib (I have never
> worked with libraries before). 

Every time you do an import you are using a library.
eg

import sys
import os
etc

It's no big deal.

> I am trying to get started with VS Community 2017, 
> and I am having trouble performing the most basic
> tasks such as installing matplotlib. Anyone here using VS 2017? 

VS is great for .NET development for Windows but frankly
for anything else I prefer Eclipse or Netbeans as IDEs.
But personally I don't much like big greedy IDEs (unless
I'm writing Java) so I tend to just use a text editor
(vim or notepad++ on windows) and a Python shell
 - possibly even IDLE. I also use the Pythonwin IDE
if I'm doing any COM type work because of its debugger
and built in COM browser.

But editors and IDEs are very personal choices. The best
thing is to download a few and play with them. There is
no shortage of candidates to pick from!

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


[Tutor] Visual Studio Community 2017

2017-04-18 Thread Rafael Knuth
I wanted to start my first project using matplotlib (I have never
worked with libraries before). I am trying to get started with VS
Community 2017, and I am having trouble performing the most basic
tasks such as installing matplotlib. Anyone here using VS 2017? Or,
can anyone recommend an alternative to VS for Windows? Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] classproperty for Python 2.7 (read-only enough)

2017-04-18 Thread Mats Wichmann
On 04/18/2017 04:00 AM, Thomas Güttler wrote:
> I would like to have read-only class properties in Python.
> 
> I found this
> http://stackoverflow.com/questions/128573/using-property-on-classmethods
> But there are a lot of discussions of things which I don't understand.
> 
> I want to be a user of class properties, not an implementer of the details.
> 
> I found this: https://pypi.python.org/pypi/classproperty
> 
> But above release is more then ten years old. I am unsure if it's dead
> or mature.
> 
> I am using Python 2.7 and attribute getters would be enough, no
> attribute setter is needed.
> 
> My use case is configuration, not fancy algorithms or loops.
> 
> Regards,
>   Thomas Güttler
> 

Not clear if you're asking for something more than the standard Python
properties.  The discussion you mention does go into additional
discussion which describes techniques for applying this to a class
attribute. In other words, is this quote a problem for you?

"The get method [of a property] won't be called when the property is
accessed as a class attribute (C.x) instead of as an instance attribute
(C().x). "

If applying to instance attributes is fine for your case, then the
property() call or the related decorators should do the trick, as in:

def get_temperature(self):
return self._temperature

def set_temperature(self, value):
self._temperature = value

temperature = property(get_temperature,set_temperature)

or, as is probably preferred:

@property
def temperature(self):
return self._temperature

@temperature.setter
def temperature(self, value):
self._temperature = value

(obviously both a getter and setter)

Note it's Python convention to use an underscore-prefixed variable name
to indicate it is "private", and you often see a backing store for the
property written like I did above, but nothing in Python enforces this
privacy, someone could fiddle directly with instance._temperature

Also ask yourself whether you really _need_ a property here?  Or would a
public data member be sufficient.  (Just asking, have no idea what's
inside your use case)

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


Re: [Tutor] classproperty for Python 2.7 (read-only enough)

2017-04-18 Thread Alan Gauld via Tutor
On 18/04/17 11:00, Thomas Güttler wrote:
> I would like to have read-only class properties in Python.

Is there a specific reason why? Do you think for example
that users of the class will deliberately try to modify
the attribute? Normally in Python we leave all attributes
public and unprotected (possibly with a naming hint) and
rely on our users being sensible.

> I want to be a user of class properties, not an implementer of the details.

I'm not sure what exactly you mean by that but...

> I am using Python 2.7 and attribute getters would be enough, no attribute 
> setter is needed.
> 

The default for @property is a read only attribute so

class Spam(object):
   @property
   def eggs(self):
   return self._eggs

   def __init__(self,value):
   self._eggs = value   #initialise it

s = Spam(66)

print s.eggs# ok
s.eggs = 666# error, read only.
s._eggs = 666  # can be set
print s._eggs  # still 66
print s.eggs# oops is now 666

Is that sufficient? If so easy.

But if you want to prevent access to _eggs
from outside that's trickier. But you would
need a very good reason to complicate your
life that much...

Or do you really want a class level property?
In which case refer to Peter's comment.

-- 
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] Tkinter and canvas question

2017-04-18 Thread Phil
On Mon, 17 Apr 2017 22:57:41 -0500
boB Stepp  wrote:

> I have yet to do much class writing with tkinter, but if I am
> understanding things correctly, in your Sudoku class where you
> instantiate a Canvas instance, you assign it to the name "the_canvas".
> This will be local to the __init__ method's namespace.  I think you
> need to precede each of those "the_canvas" with "self." to get
> "self.the_canvas".  This way your solve method will be able to access
> it.

Thank you Bob. I was fixated on the error being elsewhere and didn't think 
about the_canvas being just another attribute of the class. It seem obvious now 
that I've been shown.

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


Re: [Tutor] classproperty for Python 2.7 (read-only enough)

2017-04-18 Thread Peter Otten
Thomas Güttler wrote:

> I would like to have read-only class properties in Python.
> 
> I found this
> http://stackoverflow.com/questions/128573/using-property-on-classmethods
> But there are a lot of discussions of things which I don't understand.
> 
> I want to be a user of class properties, not an implementer of the
> details.
> 
> I found this: https://pypi.python.org/pypi/classproperty
> 
> But above release is more then ten years old. I am unsure if it's dead or
> mature.
> 
> I am using Python 2.7 and attribute getters would be enough, no attribute
> setter is needed.
> 
> My use case is configuration, not fancy algorithms or loops.

Like this?

$ cat classproperty.py
class classproperty(object):
def __init__(self, fget):
self.fget = fget
def __get__(self, inst, cls):
return self.fget(cls)


class Foo(object):
FOO = 42
@classproperty
def foo(cls):
return cls.FOO

print "Foo.foo =", Foo.foo
print "Foo().foo =", Foo().foo
$ python2 classproperty.py 
Foo.foo = 42
Foo().foo = 42


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


[Tutor] classproperty for Python 2.7 (read-only enough)

2017-04-18 Thread Thomas Güttler

I would like to have read-only class properties in Python.

I found this 
http://stackoverflow.com/questions/128573/using-property-on-classmethods
But there are a lot of discussions of things which I don't understand.

I want to be a user of class properties, not an implementer of the details.

I found this: https://pypi.python.org/pypi/classproperty

But above release is more then ten years old. I am unsure if it's dead or 
mature.

I am using Python 2.7 and attribute getters would be enough, no attribute 
setter is needed.

My use case is configuration, not fancy algorithms or loops.

Regards,
  Thomas Güttler

--
Thomas Guettler http://www.thomas-guettler.de/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 making a spurious duplicate?

2017-04-18 Thread Peter Otten
Marilyn Davis wrote:

> #!/usr/bin/env python3
> """
> Hello Tutors,
> 
> I can't figure out why the FillWithStars() function puts Canopus in the db
> twice.
> 
> What am I missing?
> 
> Thank you for any help.
> 
> Marilyn Davis
> 
> p.s. That Reset(db_name) is in there so that you can run it over and over
> if you want.
> 
> ---
> """
> import os, sqlite3, subprocess
> 
> def Reset(db_name):
> os.system("rm " + db_name)
> db_process = subprocess.Popen(("sqlite3", db_name),
>stdin=subprocess.PIPE,
>stdout=subprocess.PIPE,
>stderr=subprocess.PIPE)
> for send in (b'.tables', ):
> returned = db_process.communicate(send)
> assert returned == (b'', b'')
> 
> def FillWithStars():
> 
> with sqlite3.connect("stars.db") as connection:
> 
> connection.executescript("""
> CREATE TABLE brightest(
> name,
> constellation,
> apparent_magnitude,
> absolute_magnitude,
> distance);
> INSERT INTO brightest VALUES("Canopus", "Carina", -0.72, -2.5,
> 74);
> """)
> 
> connection.executemany("INSERT INTO brightest VALUES(?, ?, ?, ?,
> ?)",
> [("Arcturus", "Bootes", -0.04, 0.2, 34),])
> 
> stored_stars = connection.execute("SELECT * FROM BRIGHTEST")

At this point the connection is closed, and it looks like stored_stars is 
still iterable but contains garbage. 

> for star in stored_stars:
> print(star)

Try and indent the lines above one more level so that they are executed 
inside the with-suite.

> 
> def main():
> Reset("stars.db")
> FillWithStars()
> 
> if __name__ == '__main__':
> main()
> 
> """Output:
> 
> bash-3.2$ ./why3.py
> ('Canopus', 'Carina', -0.72, -2.5, 74)
> ('Canopus', 'Carina', -0.72, -2.5, 74)
> ('Arcturus', 'Bootes', -0.04, 0.2, 34)
> bash-3.2$
> """
> 
> ___
> 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


Re: [Tutor] Tkinter and canvas question

2017-04-18 Thread Alan Gauld via Tutor
On 18/04/17 00:13, Phil wrote:
> Thank you for reading this.
> 
> How do I reference the_canvas from my solve() method? 

> class Sudoku(Frame):
> def __init__(self, parent):
> Frame.__init__(self, parent)
> self.parent = parent
> 
> parent.title("Sudoku solver")
> 
>   #create canvas
> the_canvas = Canvas(width = 300, height = 300)
>  
> the_canvas.pack(side = TOP, anchor = NW, padx = 10, pady = 10)

You need to store the_canvas as an instance attribute so you need to
precede it with self:

self.the_canvas = Canvas(width=300, height=300


>   #create grid
> 
> #create solve button
> solve_button = Button(the_canvas, text = "Solve", command = 
> self.solve,
> anchor = W)

Similarly with the button

self.solve_button = 

> solve_button.configure(width = 5, activebackground = "#33B5E5",
> relief = FLAT)
> solve_button.pack(side = TOP)
> solve_button_window = the_canvas.create_window(250, 250, anchor=NW, 
> window=solve_button)

Without the self your widgets are attached to local variables
that go out of scope as soon as init() ends. You could find
them by traversing the child widget tree of self.parent,
but that's just making life difficult for the sake of it!


-- 
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] bracket issue

2017-04-18 Thread Cameron Simpson

On 15Apr2017 14:05, Alan Gauld  wrote:

On 15/04/17 03:17, Palm Tree wrote:


s="2*3+3(8-4(5+6+9))+2+3+3(4/4)"

i want to find expression enclosed in brackets.

i want outputs to be like:
(8-4(5+6+9))
(5+6+9)
(4/4)



You probably could do it with some fancy regex but personally
I'd investigate a proper text parser.


The nested brackets thing is something that regexps generally won't do fully, 
because they don't recurse. You could write a regexp to handle up-to-n nestings 
for some finite n, but as soon as that gets above something like 2 you're 
probably into the "don't use regexps for this" domain.


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