Re: [Tutor] Tkinter layout question

2017-04-19 Thread boB Stepp
I don't know (now) how to solve your challenges below.  But if I were
trying to figure this out, I would try to find more complex tkinter
example applications that contain some of the issues I'd be interested
in.  Then I would dig into the code, find the relevant pieces, and
then start playing with them until I understood them.  I know Alan
suggested some resources in one of your earlier threads.  One of the
books he suggested was, "Python and Tkinter Programming" by John E.
Grayson.  It is a fairly old book from the Python 2 days, copyrighted
2000.  However, I own this book and have gotten a lot of use out of
it.  He has _lots_ of complete examples, including such things as a
scientific calculator that looks like something HP sells!  The other
book, "Programming Python" by Mark Lutz has a very large section on
tkinter with plenty of examples.  I have gotten much good use out this
book as well.  It is much more recent, covering Python 3 (and mentions
Python 2 differences as well).  And then there is the Internet.
Surely you could find relevant material out there.  Maybe even the
source code for IDLE would be helpful.  I understand it is written
using tkinter.

On Wed, Apr 19, 2017 at 5:48 PM, Phil  wrote:
> I'm looking for ideas here.
>
> A working solution for my sudoku solver is a 9 x 9 grid of entry boxes but it 
> looks a bit basic. So I created a 9 x 9 grid on a canvas which looks much 
> better. I can display digits in the centre of the squares but entering the 
> digits from the keyboard seems to be beyond me. I experimented with entering 
> a digit at the mouse location but it all seems to be too complicated. Perhaps 
> someone can offer a repetitively simple solution?
>

Surely doing the keyboard entries isn't a horribly difficult problem?
It sounds like it breaks down to two different types of key bindings:
(1) Binding some keys (perhaps the arrow keys) to change focus from
cell to cell; and, (2) binding keys to do the actual numerical data
entry in each cell.

> A second experiment involved the earlier grid of entry boxes but with a space 
> between every third row and column. This seems to be more achievable, 
> eventually.
>

I think I have done some of this type of "spacing" stuff using empty
narrow frames.  I don't know if would be helpful or not for you in
your use case, but perhaps it is something to think about.  In the
Grayson book he has lots of examples of achieving very precise spacing
and placement of widgets, etc.

Just some thoughts.  Perhaps they might inspire a creative thought or
two.  Of course one of the experienced tkinter pros out there might
chime in with very detailed, specific help.


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


[Tutor] Tkinter layout question

2017-04-19 Thread Phil
I'm looking for ideas here.

A working solution for my sudoku solver is a 9 x 9 grid of entry boxes but it 
looks a bit basic. So I created a 9 x 9 grid on a canvas which looks much 
better. I can display digits in the centre of the squares but entering the 
digits from the keyboard seems to be beyond me. I experimented with entering a 
digit at the mouse location but it all seems to be too complicated. Perhaps 
someone can offer a repetitively simple solution?

A second experiment involved the earlier grid of entry boxes but with a space 
between every third row and column. This seems to be more achievable, 
eventually.

Something along these lines:

for i in range(9):
if i % 4 == 0:
place a blank text label
else:
place an entry box

So, how might I enter digits into a grid on a canvas or how could I create a 
space between a grid entry boxes?

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


Re: [Tutor] Need help with code

2017-04-19 Thread Joel Goldstick
You most likely have no file named words.txt in the directory from
which you are running your code.

On Mon, Apr 17, 2017 at 10:12 PM, Tyler Seacrist  wrote:
> Hello,
>
>
> How do I avoid this error message everytime I utilize wordlist = 
> open("words.text") ?
>
 wordlist = open("words.txt")
> Traceback (most recent call last):
>   File "", line 1, in 
> wordlist = open("words.txt")
> FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
 def calculate_abecedarian():
> total_count = 0
> for word in wordlist:
> if is_abecedarian(word):
> print (word)
> total_count += 1
> return total_count
>
> or
>
 if __name__ == '__main__':
> fin = open('words.txt', 'r')
> print (no_contain(fin))
>
>
> Traceback (most recent call last):
>   File "", line 2, in 
> fin = open('words.txt', 'r')
> FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with code

2017-04-19 Thread Alan Gauld via Tutor
On 18/04/17 03:12, Tyler Seacrist wrote:

> How do I avoid this error message everytime I utilize wordlist = 
> open("words.text") ?
> 
 wordlist = open("words.txt")
> Traceback (most recent call last):
>   File "", line 1, in 
> wordlist = open("words.txt")
> FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'

You need to tell Python where the file lives. It is evidently not
in the same folder where you run the program(*) so you need to
provide the path.

(*)You can determine which folder python considers its home folder by
including this code in your program;

import os
print( os.getcwd() )  # get Current Working Directory

and you can see its contents with

print( os.listdir() )

So you need to provide the path. If you are using
Windows you should put an 'r' in front of the path,
like so:

fin = open( r"C:\Path\to\words.txt" )

or use forward slashes:

fin = open( "C:/Path/to/words.txt" )

Either technique will avoid python interpreting the '\'
as an escape character.

-- 
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] Need help with code

2017-04-19 Thread Tyler Seacrist
Hello,


How do I avoid this error message everytime I utilize wordlist = 
open("words.text") ?

>>> wordlist = open("words.txt")
Traceback (most recent call last):
  File "", line 1, in 
wordlist = open("words.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
>>> def calculate_abecedarian():
total_count = 0
for word in wordlist:
if is_abecedarian(word):
print (word)
total_count += 1
return total_count

or

>>> if __name__ == '__main__':
fin = open('words.txt', 'r')
print (no_contain(fin))


Traceback (most recent call last):
  File "", line 2, in 
fin = open('words.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'


___
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-19 Thread eryk sun
On Wed, Apr 19, 2017 at 10:19 AM, Peter Otten <__pete...@web.de> wrote:
> Steven D'Aprano wrote:
>
>> As I said, I haven't had a chance to try Peter's code, so it's possible
>> that he's solved all these problems. I'm judging by previous
>
> No, my simple code only "works" for read-only properties and only as long as
> you don't overwrite the property by assigning to the attribute. To disallow
> writing you can define a
>
> def __set__(*args):
> raise AttributeError
>
> method, but that would only be invoked for instances of the class, not the
> class itself. For the same reason the setter of a read/write class property
> following that design would only be invoked in an instance, not in the
> class.
>
> The other simple solution, defining a normal property in the metaclass,
> works for the class (as expected, remember that the class is an instance of
> the metaclass), but is invisible to the instance:
>
 class T(type):
> ... @property
> ... def foo(self): return 42
> ...
 class Foo:
> ... __metaclass__ = T
> ...
 Foo.foo
> 42
 Foo.foo = "bar"
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: can't set attribute
 Foo().foo
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'Foo' object has no attribute 'foo'

This is a bit awkward, but you can take advantage of property() being
a data descriptor. When the attribute is looked up on the class, the
metaclass __getattribute__ or __setattr__ first searches the metaclass
MRO. If it finds a data descriptor, then it uses it unconditionally.
This means you can add a foo property to the class as well and have it
chain to the metaclass property. For example:

class T(type):
@property
def foo(self):
return 42

class C(object):
__metaclass__ = T
@property
def foo(self):
return type(self).foo

>>> C.foo
42
>>> C.foo = 0
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
>>> o = C()
>>> o.foo
42
>>> o.foo = 0
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Write Data to Multiple Files

2017-04-19 Thread Alan Gauld via Tutor
On 19/04/17 11:35, Mohanad Ismail via Tutor wrote:

> Read data from serial and write to file 1 for 15 sec, 
> after the 15 seconds continue writing the data on to
> file 2 for 15 seconds
>... each file should write for 15 seconds and stop for 15 seconds

> Code:
> #!/usr/bin/env python
> from datetime import datetimeimport serial
> outfile='/home/bogie/Documents/Data/file1.txt'#outfile='/home/bogie/Documents/Data/file2.txt'ser
>  = serial.Serial(port='/dev/ttyS4',baudrate=9600,
> parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,
> bytesize=serial.EIGHTBITS,
> )
> while ser.isOpen():d = open(outfile,'a') dataString = ser.read(44)
> d.write(datetime.utcnow().isoformat()+ '\t' + dataString + '\n')d.flush() 
> #included to force the system to write to disk
> 
> ser.close()

This is a text only list and as such html formatting tends to
get wrecked so we can't read your code. Please use plain text
in future posts please?

Meanwhile, the hardest bit of what you want to do is checking
that 15s have elapsed. Actually opening two files and writing
to them is straightforward.

For the time bit I'd probably use the simpler time module
and its time() function rather than datetime, since you
are only interested in seconds which is what time() gives you.

Set an initial start counter using time.time() and on every
write iteration set a now value. When now-start >= 15 swap
to the other file and set start to now. repeat until done.

hth
-- 
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] classproperty for Python 2.7 (read-only enough)

2017-04-19 Thread Alan Gauld via Tutor
On 19/04/17 08:43, Alan Gauld via Tutor wrote:

> Probably because it is such a rare use case and because
> its not that hard to do yourself if you really need it.

Having read Steven's post I'll retract the bit about
it being not that hard! :-)

But I still think its a fairly rare scenario. Until
somebody really has a burning need it will probably
remain a "nice to have"

-- 
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] Write Data to Multiple Files

2017-04-19 Thread Mohanad Ismail via Tutor
Hello,
I have recently started using python and i have come to a halt while writing a 
code and i was hoping someone out there can help me.
I am attempting to store a stream of data directly from a serial port onto 
multiple files in sequence after x amount of time is elapsed.
for example :
Read data from serial and write to file 1 for 15 sec, after the 15 seconds 
continue writing the data on to file 2 for 15 seconds and vice versa. in 
essence each file should write for 15 seconds and stop for 15 seconds while the 
data is written to the other file
at the moment i have managed to write a code to read and write the data to one 
file (see below). can someone please assist me.
Code:
#!/usr/bin/env python
from datetime import datetimeimport serial
outfile='/home/bogie/Documents/Data/file1.txt'#outfile='/home/bogie/Documents/Data/file2.txt'ser
 = serial.Serial(    port='/dev/ttyS4',    baudrate=9600,    
parity=serial.PARITY_NONE,    stopbits=serial.STOPBITS_ONE,    
bytesize=serial.EIGHTBITS,
)
while ser.isOpen():    d = open(outfile,'a')     dataString = ser.read(44)    
d.write(datetime.utcnow().isoformat()+ '\t' + dataString + '\n')    d.flush() 
#included to force the system to write to disk

ser.close()

Thanks,Mohanad 
___
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-19 Thread Peter Otten
Steven D'Aprano wrote:

> As I said, I haven't had a chance to try Peter's code, so it's possible
> that he's solved all these problems. I'm judging by previous

No, my simple code only "works" for read-only properties and only as long as 
you don't overwrite the property by assigning to the attribute. To disallow 
writing you can define a

def __set__(*args):
raise AttributeError

method, but that would only be invoked for instances of the class, not the 
class itself. For the same reason the setter of a read/write class property 
following that design would only be invoked in an instance, not in the 
class.

The other simple solution, defining a normal property in the metaclass,
works for the class (as expected, remember that the class is an instance of 
the metaclass), but is invisible to the instance:

>>> class T(type):
... @property
... def foo(self): return 42
... 
>>> class Foo:
... __metaclass__ = T
... 
>>> Foo.foo
42
>>> Foo.foo = "bar"
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
>>> Foo().foo
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'Foo' object has no attribute 'foo'


___
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-19 Thread Steven D'Aprano
On Wed, Apr 19, 2017 at 09:28:26AM +0200, Thomas Güttler wrote:

[code for a classproperty]

> Nice, if it is that simple.
> 
> Is there a reason why this is not in the standard library?

I haven't had a chance to test Peter's classproperty code yet, but I 
don't expect it to be that simple. People have asked for it before, and 
even Guido himself (the inventor of Python) has agreed that if it 
existed he'd use it, but the proposals have (so far) always stumbled on 
two factors:

- there are not a lot of uses for classproperty that ordinary property 
  isn't "good enough" for;

- its hard to get classproperty to work right.


The *easy* part is to do something like this:


class Spam(object):
@classproperty
def x(cls):
return "some value"


Now you can say:

Spam.x

and it will return "some value". BUT if you say:

Spam.x = "hello world"

the class property doesn't run, and Python just overrides x with the new 
value, and you lose the class property and get just a regular attribute. 
That's bad.

There is a solution to that: use a custom metaclass. And yes, that is 
not just advanced, but Black Magic and Voodoo advanced. But if you do 
it, you can solve the above problem.

However, the metaclass solution creates a *new* problem. We'd like this 
to work too:

obj = Spam()
obj.x  # should call the classproperty and return "some value"


but with the metaclass solution, that doesn't work.

As I said, I haven't had a chance to try Peter's code, so it's possible 
that he's solved all these problems. I'm judging by previous 
discussions.

On the bug tracker there's currently a request for classproperty, but 
its languishing:

https://bugs.python.org/issue24941

See Nick Coghlan's comments in particular:

https://mail.python.org/pipermail/python-ideas/2011-January/008959.html


Here's a previous request that was closed for lack of progress and 
interest:

https://bugs.python.org/issue23586


Here's Guido expressing some interest:

https://mail.python.org/pipermail/python-ideas/2011-January/008955.html


-- 
Steve
___
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-19 Thread Alan Gauld via Tutor
On 19/04/17 03:21, Marilyn Davis wrote:

> connection.execute("SELECT * FROM BRIGHTEST")
> 
> returns a , not a regular python
> sequence.  

Pretty much everything inn  SQL land uses cursor objects.
It's why best practice creates an explicit cursor for
executing statements rather than the impl8icit one on
the connection. Also of course because you can then
have more than one query active at a time.

A connection based query is fine where you just want
to jump in and pull out some data, in that case a
single, implicit, cursor doesn't cause any issues.

> But starting a new context, instead, before the executemany does not fix
> it!  We still get the duplicate Canopus in the iterable, even though it
> was inserted into the data a different context.
> 
> I suspect that this is not an intended behavior.

Can you show what you mean there?
Provided you use the cursor inside the context
it should still work.

> Another question: is it a good principle to try to only put SQL in the
> context, and then regular python outside that context?  It suits my
> instinct  but maybe it is just my superstition?

No, you often need to put the python that manipulates the
data inside the context, especially if the database is
large. You could (should?) of course put the manipulation
into a function that is defined outside the context and
called from within, but what you probably should not do
is try to pull all your data into memory within the context
then manipulate it outside. This would be particularly
dangerous in a multi-user database context, where the
data may be changing independently of your program.

-- 
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] classproperty for Python 2.7 (read-only enough)

2017-04-19 Thread Alan Gauld via Tutor
On 19/04/17 08:28, Thomas Güttler wrote:

> Nice, if it is that simple.
> 
> Is there a reason why this is not in the standard library?

Probably because it is such a rare use case and because
its not that hard to do yourself if you really need it.

But the standard library, like any open source project,
develops as people need things. If nobody needs something
it will never be built and therefore never be added to
the library. I'd guess this falls into that category.


-- 
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] sqlite3 making a spurious duplicate?

2017-04-19 Thread Marilyn Davis
Thank you Alan, Steven and Peter,

So, this call:

connection.execute("SELECT * FROM BRIGHTEST")

returns a , not a regular python
sequence.  I did not know that.  And, the connection must still be alive
when you iterate it.

That is a very important tidbit of info.

The fix is to listify the Cursor object, or iterate while still in the
context.  Interesting.  Closing the context and opening a new one fixes it
too:

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

with sqlite3.connect("stars.db") as connection:
stored_stars = connection.execute("SELECT * FROM BRIGHTEST")

for star in stored_stars:
print(star)

---
But starting a new context, instead, before the executemany does not fix
it!  We still get the duplicate Canopus in the iterable, even though it
was inserted into the data a different context.

I suspect that this is not an intended behavior.

Another question: is it a good principle to try to only put SQL in the
context, and then regular python outside that context?  It suits my
instinct  but maybe it is just my superstition?

Thank you so much.

Marilyn

p.s.  Thank you, Steven, for liking the post.  You guys prove what I try
to pound into students: pretty code that does not work is much better than
hard-to-read code, even if it works -- because with pretty code, people
are happy to help you, and with working bad code, when it must be
modified, you have a mess.




___
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-19 Thread Thomas Güttler

Am 18.04.2017 um 13:17 schrieb 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


Nice, if it is that simple.

Is there a reason why this is not in the standard library?

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