Re: [Tutor] Paper Rock Scissors game - User's choice not returned properly

2011-11-01 Thread Peter Otten
Alan Gauld wrote:

> On 31/10/11 20:22, Peter Otten wrote:
>> Alan Gauld wrote:
>>
>>> if choice.lower() not in ('prs'): # NB use a single string
>>
>> That's not a good idea. If a user accidentally enters PR (for example)
>> your version will mistake that for a valid choice.
> 
> Good point, although  you could test the first character only...
> 
> if choice[0].lower() not in ('prs'): # NB use a single string

What Steven says, plus you may run into an IndexError if choice is the empty 
string. If you absolutely want to test against a single string you have to 
check the length first.


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


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Alexander Etter
On Oct 31, 2011, at 15:31, Tim Johnson  wrote:

> * Rinu Boney  [111031 07:03]:
>> I Use Windows.I Already Know C/C++ which makes python syntax seem very easy.
>> Maybe Setting Up Emacs With Python Will Make Me Productive.
>> I Have Eclipse With PyDev.
>> Why Is There Not A Pythonic Emacs?
>  Rinu, by this time I believe that Alan has addressed your question
>  above. He has also (wisely) provided caveats regarding the
>  difficulty of learning emacs itself. 
> 
>  I'll take a reverse of Alan's comments, not to contradict him, but
>  to give a possible different perspective:
> 
>  If you learn to use emacs with python, you will essentially be
>  learning *two* programming languages: Python _and_ elisp, which is
>  the internal programming language of emacs. Emacs is essentially
>  an elisp interpreter. There may be advantages to learning two
>  languages simultaneously. 
> 
>  This will take time. A lot of time. Do you have the time? Will you
>  be compensated for the time? :) having two additional programming
>  languages "under your belt" may be considered compensation.
> 
>  In case you do not know this: Emacs has the ability to run the
>  python or language-your-choice interpreter asynchronous within the
>  editor, in it's own window. There could be great advantages to
>  this. I have in the past, written elisp code that allows me two
>  write code in one window and have it evaluated in the 'python
>  window' or 'language-of-your-choice window'.
> 
>  I'll reiterate what I said earlier, I no longer use emacs, but
>  have great respect for it. I use vim linked against the python
>  binary so that I can use python code to enhance my (hand-rolled)
>  "IDE".  I much prefer python code to elisp code.
> 
>  I hope my comments are of some help. I'm sure that you have been
>  well informed as to what you would be getting youself into. :)
>  regards
> -- 
> Tim 

Rinu, I use emacs. I use Python and C++. I'm also a university student. Last 
semester I learned python 2.7 using IDLE, and continued with IDLE while I 
searched for alternatives over the summer. I didn't find what I was looking 
for. Say, just a few weeks ago I started my C++ course and switched to emacs 
since the professor was using it. I tried it, read the easy to understand 
documentation, and I am so productive, jubilant, and satisfied with GNU Emacs. 
It's extensible beyond immediate comprehension; like a sunflower it starts as a 
seed, sprouts leaves, etc; I'm elaborating the infinite usability of emacs. 

There is a learning curve. One may find a learning curve with everything in 
existence, whereas I repudiate one discouraging another for the aforementioned. 
Those who desire the power of emacs seek it. 
Tim, do you use GNU Emacs?
>From what literature I've encountered including a wikipedia page I believe 
>there is a satiric starwars-like cold-war feud between users of vi and emacs. 
I'm neutral and won't judge an entity or patronize one for their use of free 
will. 
I'm happy. Forgive me if I appear too anything. 
Good Day. 
Alexander Etter


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


Re: [Tutor] Help

2011-11-01 Thread Dave Angel
(Pleas put your reply after the part you're quoting.  What you did is 
called top-posting, and makes reading the messages very confusing)

On 11/01/2011 12:10 AM, Chris Kavanagh wrote:
I'm going to thank Steven once again, and answer my own question in 
the 2nd paragraph directly below (Steven hasn't had a chance to 
respond yet).


I just learned that the Function definitions are not read by the 
interpreter UNTIL they are called. I was reading them and assuming 
they were executed from the top down. Obviously I was getting somewhat 
confused because of this. This 'dragon' program makes much more sense 
to me now that I understand that. . .Thanks again Steven & everyone!!!

Happy Halloween!!!

That's not correct either.  The  interpreter definitely reads the file 
from top down.  But sometimes the effect of the code in this phase is to 
build some structures for later use.  For example the def keyword says 
that the line and some following ones are to be compiled into an object 
form representing a function.  Default arguments are exececuted at this 
time, but none of the rest.  it's compiled into a function object, and 
saved in the current namespace (typically the global one) for later 
use.  Then when the interpreter encounters a function call, it looks up 
the name, and if it's "callable"  (as a function object is), it gets its 
parameters filled in and gets used.


Then when that function returns, the interpreter continues interpreting 
till the end of file.


But notice that when you are executing inside a function, you may 
encounter references to the same or other functions.  At this point they 
do need to have been compiled into callable objects.  So while the order 
of the function definitions doesn't matter for calls made from the end 
of the file, you can get into trouble if some function is called which 
tries to call another one not yet compiled.  To avoid this possibility, 
the standard paradigm is to put all top-level code at the end of file 
(and inside an  if __name__ == "__main__"clause) .




On 10/31/2011 11:07 PM, Chris Kavanagh wrote:

Yes Steven, that solved my question(s). It also cleared up what was to
be my next question! Thanks so much. You might not realize how grateful
I am to be able to have you & others on the list answer my questions.
Just trust me when I say, I am grateful. And I apologize for the code
being mangled on your end. I'm not sure why this happened, but you were
correct in your assumptions. . .

One thing I'm curious about. If the code is read by the Interpreter or
Compiler from the top down, why in this case, does it not get confused
(cause an error) when it hits line 30, if it doesn't yet know that
{caveNumber=chooseCave()} and checkCave(caveNumber)}?? It seems to me in
my limited experience, those last two lines should've come before line
30. It seems as though it would've made more sense to somehow put them
before. In other languages, ala C++, don't global variables have to be
declared at the 'top' of the code??

The main reason that C and C++ require declarations before use is that 
different kinds of variables take differing spaces, and the compiler 
figures out the memory layout before generating the code.  The compiler 
would have to be much more complex if it didn't have those 
restrictions.  In python, symbols ( colloquailly called "variables") 
don' t have any type, and can refer to different kinds of objects at 
different times.




--

DaveA

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


[Tutor] A question about sys.argv

2011-11-01 Thread Jefferson Ragot
In a Vista command prompt if I typed this:

>>> python  somescript.py  filename

Will sys.argv[1] return a valid path or just the filename?
If it just returns the filename, is there a simple way to get the path?

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


Re: [Tutor] A question about sys.argv

2011-11-01 Thread Joel Goldstick
On Tue, Nov 1, 2011 at 9:48 AM, Jefferson Ragot  wrote:

> In a Vista command prompt if I typed this:
>
> >>> python  somescript.py  filename
>
> Will sys.argv[1] return a valid path or just the filename?
> If it just returns the filename, is there a simple way to get the path?
>
> --
> Jefferson B. Ragot
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
sysargv[1] returns the text following your script.

You can find the current working directory with this:

http://docs.python.org/library/os.html#os.getcwd



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


Re: [Tutor] A question about sys.argv

2011-11-01 Thread Hugo Arts
On Tue, Nov 1, 2011 at 2:48 PM, Jefferson Ragot  wrote:
> In a Vista command prompt if I typed this:
>
>     >>> python  somescript.py  filename
>
> Will sys.argv[1] return a valid path or just the filename?
> If it just returns the filename, is there a simple way to get the path?
>

sys.argv contains exactly what you typed in on the command line.
Nothing more, nothing less. It doesn't care whether what you typed is
actually a filename or a plain number or some string, it just passes
the commands as they are.

You can use the os.path.abspath() function on your argument, that will
probably do what you want.

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


[Tutor] improve the code

2011-11-01 Thread lina
Hi,

The following code (luckily) partial achieved what I wanted, but I
still have few questions:


#!/usr/bin/python3

import os.path

INFILEEXT=".txt"
OUTFILEEXT=".new"
DICTIONARYFILE="dictionary.pdb"
orig_dictionary={}
new_dictionary={}
abetaABresidues={}

def processonefiledata(infilename):
with open(infilename,"r") as f:
for line in f:
parts=line.strip().split()
orig_dictionary[parts[0]]=parts[1]


def build_abetadictionary(infilename,olddict):
with open(infilename,"r") as f:
for line in f:
parts=line.strip().split()
if parts[0] != "85CUR" and (parts[0] not in abetaABresidues.keys()):
abetaABresidues[parts[0]]=0
for residues, numbers in abetaABresidues.items():
if residues in olddict.keys():
new_dictionary[residues]=olddict[residues]
else:
new_dictionary[residues]=0
with open(base+OUTFILEEXT,"w") as f:
for residues, numbers in new_dictionary.items():
print(residues,numbers,file=f)
## Q1: How can I sort the results, like the effect of | sort -g

from something like:
84ALA 12
:
:
83ILE 28
:
:

to
:
83ILE 28
84ALA 12
:


if __name__=="__main__":
for filename in os.listdir("."):
base, ext =os.path.splitext(filename)
if ext == INFILEEXT:
print(filename)
processonefiledata(filename)
build_abetadictionary(DICTIONARYFILE,orig_dictionary)

Thanks for any comments or suggestions you may give.

The relevant testing file are attached below links:

https://docs.google.com/open?id=0B93SVRfpVVg3YjdhNjlmYTAtMTdkYy00ZTNjLThkOWEtOGMyNTM1YTBiMmU4
https://docs.google.com/open?id=0B93SVRfpVVg3OWNiZmUwODktMDU4Ny00ZDUyLWExYzQtM2E2ZmY5NGJhNzgz
https://docs.google.com/open?id=0B93SVRfpVVg3MTBmNTM3M2UtYjdiMC00N2UwLWE1YTQtNmU3OGQzOGYwNDc3

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


Re: [Tutor] A question about sys.argv

2011-11-01 Thread Jose Amoreira
HiOn Tuesday, November 01, 2011 01:55:18 PM Joel Goldstick wrote:
> On Tue, Nov 1, 2011 at 9:48 AM, Jefferson Ragot  wrote:
> > In a Vista command prompt if I typed this:
> > >>> python  somescript.py  filename
> > 
> > Will sys.argv[1] return a valid path or just the filename?
> > If it just returns the filename, is there a simple way to get the path?
> > 
Here's the contents of my somescript.py:
-
import sys
for index,arg in enumerate(sys.argv):
print index, arg
---

Here is its output:

mu:python$ python somescript.py match.py
0 somescript.py
1 match.py

mu:python$ python somescript.py somescript.py stripaccents.py
0 somescript.py
1 somescript.py
2 stripaccents.py

mu:python$ python somescript.py Hello, how do you do?
0 somescript.py
1 Hello,
2 how
3 do
4 you
5 do?

mu:python$ python somescript.py /home/amoreira/public_html/index.php 
0 somescript.py
1 /home/amoreira/public_html/index.php

mu:python$ python somescript.py /unexistent/directory/unexistent_file.txt
0 somescript.py
1 /unexistent/directory/unexistent_file.txt

So, sys.argv has nothing to do with files or paths, it just stores whatever 
you write in the command line. I don't have a vista system on wich to try 
things, but I'm pretty sure it's the same.
 
> sysargv[1] returns the text following your script.
> 
> You can find the current working directory with this:
> 
> http://docs.python.org/library/os.html#os.getcwd

No. sys.argv[1:] (note the colon) does return (not quite "return", since it's 
not a function call but ok) the text following your script. sys.argv[1] only 
"returns" the *first* word after your script (in the invocation command)

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


Re: [Tutor] A question about sys.argv

2011-11-01 Thread Dave Angel

On 11/01/2011 10:05 AM, Hugo Arts wrote:

On Tue, Nov 1, 2011 at 2:48 PM, Jefferson Ragot  wrote:

In a Vista command prompt if I typed this:

 >>>  python  somescript.py  filename

Will sys.argv[1] return a valid path or just the filename?
If it just returns the filename, is there a simple way to get the path?


sys.argv contains exactly what you typed in on the command line.
Nothing more, nothing less. It doesn't care whether what you typed is
actually a filename or a plain number or some string, it just passes
the commands as they are.

You can use the os.path.abspath() function on your argument, that will
probably do what you want.

HTH,
Hugo


That's very helpful, but let me add a caveat:

The operating system shell may modify that command line before handing 
it to Python.  i don't believe that happens in Vista, however.  Watch 
out for quotes and such, they may be eaten by the c runtime.  And if you 
were on Linux, a filename with wildcards could get expanded into several 
argv[] items.


--

DaveA

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


Re: [Tutor] A question about sys.argv

2011-11-01 Thread Dave Angel

On 11/01/2011 10:19 AM, Jose Amoreira wrote:

HiOn Tuesday, November 01, 2011 01:55:18 PM Joel Goldstick wrote:

On Tue, Nov 1, 2011 at 9:48 AM, Jefferson Ragot  wrote:

In a Vista command prompt if I typed this:
 >>>  python  somescript.py  filename

Will sys.argv[1] return a valid path or just the filename?
If it just returns the filename, is there a simple way to get the path?


Here's the contents of my somescript.py:
-
import sys
for index,arg in enumerate(sys.argv):
 print index, arg
---

Here is its output:

mu:python$ python somescript.py match.py
0 somescript.py
1 match.py

mu:python$ python somescript.py somescript.py stripaccents.py
0 somescript.py
1 somescript.py
2 stripaccents.py

mu:python$ python somescript.py Hello, how do you do?
0 somescript.py
1 Hello,
2 how
3 do
4 you
5 do?

mu:python$ python somescript.py /home/amoreira/public_html/index.php
0 somescript.py
1 /home/amoreira/public_html/index.php

mu:python$ python somescript.py /unexistent/directory/unexistent_file.txt
0 somescript.py
1 /unexistent/directory/unexistent_file.txt

So, sys.argv has nothing to do with files or paths, it just stores whatever
you write in the command line. I don't have a vista system on wich to try
things, but I'm pretty sure it's the same.


sysargv[1] returns the text following your script.

You can find the current working directory with this:

http://docs.python.org/library/os.html#os.getcwd

No. sys.argv[1:] (note the colon) does return (not quite "return", since it's
not a function call but ok) the text following your script. sys.argv[1] only
"returns" the *first* word after your script (in the invocation command)

Cheers
Ze Amoreira

More precisely,  sys.argv[1:] yields a list whose items are the words on 
the command line.  it does not reproduce the command line as a single 
string.


--

DaveA

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


Re: [Tutor] improve the code

2011-11-01 Thread Dave Angel

On 11/01/2011 10:11 AM, lina wrote:

Hi,

The following code (luckily) partial achieved what I wanted, but I
still have few questions:


#!/usr/bin/python3

import os.path

INFILEEXT=".txt"
OUTFILEEXT=".new"
DICTIONARYFILE="dictionary.pdb"
orig_dictionary={}
new_dictionary={}
abetaABresidues={}

def processonefiledata(infilename):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 orig_dictionary[parts[0]]=parts[1]


def build_abetadictionary(infilename,olddict):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 if parts[0] != "85CUR" and (parts[0] not in 
abetaABresidues.keys()):
 abetaABresidues[parts[0]]=0
 for residues, numbers in abetaABresidues.items():
 if residues in olddict.keys():
 new_dictionary[residues]=olddict[residues]
 else:
 new_dictionary[residues]=0
 with open(base+OUTFILEEXT,"w") as f:
 for residues, numbers in new_dictionary.items():
 print(residues,numbers,file=f)
## Q1: How can I sort the results, like the effect of | sort -g

from something like:
84ALA 12
:
:
83ILE 28
:
:

to
:
83ILE 28
84ALA 12
:


Just use the sort() method of the list object.  In particular, items() 
returns an unordered list, so it's ready to be sorted.


for residues, numbers in new_dictionary.items().sort():

That will sort such that residues are in sorted order.

--

DaveA

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


Re: [Tutor] Tutor Digest, Vol 93, Issue 4

2011-11-01 Thread Rinu Boney
Alexander Etter ,
can u help me setup the emacs as python ide - like code refactoring and
stuff?
can i get to you by email?
Thanks.

>
> Rinu, I use emacs. I use Python and C++. I'm also a university student.
> Last semester I learned python 2.7 using IDLE, and continued with IDLE
> while I searched for alternatives over the summer. I didn't find what I was
> looking for. Say, just a few weeks ago I started my C++ course and switched
> to emacs since the professor was using it. I tried it, read the easy to
> understand documentation, and I am so productive, jubilant, and satisfied
> with GNU Emacs. It's extensible beyond immediate comprehension; like a
> sunflower it starts as a seed, sprouts leaves, etc; I'm elaborating the
> infinite usability of emacs.
>
> There is a learning curve. One may find a learning curve with everything
> in existence, whereas I repudiate one discouraging another for the
> aforementioned.
> Those who desire the power of emacs seek it.
> Tim, do you use GNU Emacs?
> >From what literature I've encountered including a wikipedia page I
> believe there is a satiric starwars-like cold-war feud between users of vi
> and emacs.
> I'm neutral and won't judge an entity or patronize one for their use of
> free will.
> I'm happy. Forgive me if I appear too anything.
> Good Day.
> Alexander Etter
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-01 Thread lina
On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:
> On 11/01/2011 10:11 AM, lina wrote:
>>
>> Hi,
>>
>> The following code (luckily) partial achieved what I wanted, but I
>> still have few questions:
>>
>>
>> #!/usr/bin/python3
>>
>> import os.path
>>
>> INFILEEXT=".txt"
>> OUTFILEEXT=".new"
>> DICTIONARYFILE="dictionary.pdb"
>> orig_dictionary={}
>> new_dictionary={}
>> abetaABresidues={}
>>
>> def processonefiledata(infilename):
>>     with open(infilename,"r") as f:
>>         for line in f:
>>             parts=line.strip().split()
>>             orig_dictionary[parts[0]]=parts[1]
>>
>>
>> def build_abetadictionary(infilename,olddict):
>>     with open(infilename,"r") as f:
>>         for line in f:
>>             parts=line.strip().split()
>>             if parts[0] != "85CUR" and (parts[0] not in
>> abetaABresidues.keys()):
>>                 abetaABresidues[parts[0]]=0
>>         for residues, numbers in abetaABresidues.items():
>>             if residues in olddict.keys():
>>                 new_dictionary[residues]=olddict[residues]
>>             else:
>>                 new_dictionary[residues]=0
>>         with open(base+OUTFILEEXT,"w") as f:
>>             for residues, numbers in new_dictionary.items():
>>                 print(residues,numbers,file=f)
>> ## Q1: How can I sort the results, like the effect of | sort -g
>>
>> from something like:
>> 84ALA 12
>> :
>> :
>> 83ILE 28
>> :
>> :
>>
>> to
>> :
>> 83ILE 28
>> 84ALA 12
>> :
>
> Just use the sort() method of the list object.  In particular, items()
> returns an unordered list, so it's ready to be sorted.
>
>            for residues, numbers in new_dictionary.items().sort():
>
> That will sort such that residues are in sorted order.

Thanks, but still something went wrong here,

Traceback (most recent call last):
  File "fill-gap.py", line 41, in 
build_abetadictionary(DICTIONARYFILE,orig_dictionary)
  File "fill-gap.py", line 31, in build_abetadictionary
for residues, numbers in new_dictionary.items().sort():
AttributeError: 'dict_items' object has no attribute 'sort'

I have another concerns,
is it possible to append the output file content as a sing one,
such as a.new is
A 1
B 3

b.new is
A 3
B 5

I wish the final one like:

A 1 3
B 3 5

I will think about it. Thanks,
> --
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-01 Thread Peter Otten
lina wrote:

> On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:
>> On 11/01/2011 10:11 AM, lina wrote:

>> Just use the sort() method of the list object.  In particular, items()
>> returns an unordered list, so it's ready to be sorted.
>>
>> for residues, numbers in new_dictionary.items().sort():
>>
>> That will sort such that residues are in sorted order.
> 
> Thanks, but still something went wrong here,
> 
> Traceback (most recent call last):
>   File "fill-gap.py", line 41, in 
> build_abetadictionary(DICTIONARYFILE,orig_dictionary)
>   File "fill-gap.py", line 31, in build_abetadictionary
> for residues, numbers in new_dictionary.items().sort():
> AttributeError: 'dict_items' object has no attribute 'sort'

Dave didn't realize that you are using Python 3 where items() no longer 
returns a list. You need to change

new_dictionary.items().sort()

to 

sorted(new_dictionary.items())

as sorted() will accept an arbitrary iterable.

> I have another concerns,
> is it possible to append the output file content as a sing one,
> such as a.new is
> A 1
> B 3
> 
> b.new is
> A 3
> B 5
> 
> I wish the final one like:
> 
> A 1 3
> B 3 5
> 
> I will think about it. Thanks,

Sorry, I can't make sense of that.

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


[Tutor] beginner question

2011-11-01 Thread Mayo Adams
When writing a simple for loop like so:

 for x in f

where f is the name of a file object, how does Python "know" to interpret
the variable x as a line of text, rather than,say, an individual character
in the file? Does it automatically
treat text files as sequences of lines?

-- 
Mayo Adams


287 Erwin Rd.
Chapel Hill, NC 27514
(919)-968-7889
mayoad...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginner question

2011-11-01 Thread Steve Willoughby

On 01-Nov-11 08:34, Mayo Adams wrote:

When writing a simple for loop like so:

  for x in f

where f is the name of a file object, how does Python "know" to interpret
the variable x as a line of text, rather than,say, an individual
character in the file? Does it automatically
treat text files as sequences of lines?


Every object defines what its behavior will be when asked to do 
something.  In this case, file objects know that they are capable of 
iterating over a list of their contents by being used in a "for x in f" 
loop construct.  The file object knows to respond to that by yielding up 
a line from the file for every iteration.


You could, theoretically, write a variation of the file object class 
which iterated over characters, or logical blocks (records of some sort) 
within files, and so forth.


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-01 Thread lina
On Tue, Nov 1, 2011 at 11:28 PM, Peter Otten <__pete...@web.de> wrote:
> lina wrote:
>
>> On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:
>>> On 11/01/2011 10:11 AM, lina wrote:
>
>>> Just use the sort() method of the list object.  In particular, items()
>>> returns an unordered list, so it's ready to be sorted.
>>>
>>> for residues, numbers in new_dictionary.items().sort():
>>>
>>> That will sort such that residues are in sorted order.
>>
>> Thanks, but still something went wrong here,
>>
>> Traceback (most recent call last):
>>   File "fill-gap.py", line 41, in 
>>     build_abetadictionary(DICTIONARYFILE,orig_dictionary)
>>   File "fill-gap.py", line 31, in build_abetadictionary
>>     for residues, numbers in new_dictionary.items().sort():
>> AttributeError: 'dict_items' object has no attribute 'sort'
>
> Dave didn't realize that you are using Python 3 where items() no longer
> returns a list. You need to change
>
> new_dictionary.items().sort()
>
> to
>
> sorted(new_dictionary.items())

Thanks, it works, but there is still a minor question,

can I sort based on the general numerical value?

namely not:
:
:
83ILE 1
84ALA 2
8SER 0
9GLY 0
:
:

rather 8 9 ...83 84,

Thanks,


>
> as sorted() will accept an arbitrary iterable.
>
>> I have another concerns,
>> is it possible to append the output file content as a sing one,
>> such as a.new is
>> A 1
>> B 3
>>
>> b.new is
>> A 3
>> B 5
>>
>> I wish the final one like:
>>
>> A 1 3
>> B 3 5
>>
>> I will think about it. Thanks,
>
> Sorry, I can't make sense of that.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-01 Thread Dave Angel

On 11/01/2011 11:11 AM, lina wrote:

On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:

On 11/01/2011 10:11 AM, lina wrote:


Hi,

The following code (luckily) partial achieved what I wanted, but I
still have few questions:


#!/usr/bin/python3

import os.path

INFILEEXT=".txt"
OUTFILEEXT=".new"
DICTIONARYFILE="dictionary.pdb"
orig_dictionary={}
new_dictionary={}
abetaABresidues={}

def processonefiledata(infilename):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 orig_dictionary[parts[0]]=parts[1]


def build_abetadictionary(infilename,olddict):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 if parts[0] != "85CUR" and (parts[0] not in
abetaABresidues.keys()):
 abetaABresidues[parts[0]]=0
 for residues, numbers in abetaABresidues.items():
 if residues in olddict.keys():
 new_dictionary[residues]=olddict[residues]
 else:
 new_dictionary[residues]=0
 with open(base+OUTFILEEXT,"w") as f:
 for residues, numbers in new_dictionary.items():
 print(residues,numbers,file=f)
## Q1: How can I sort the results, like the effect of | sort -g

from something like:
84ALA 12
:
:
83ILE 28
:
:

to
:
83ILE 28
84ALA 12
:


Just use the sort() method of the list object.  In particular, items()
returns an unordered list, so it's ready to be sorted.

for residues, numbers in new_dictionary.items().sort():

That will sort such that residues are in sorted order.


Thanks, but still something went wrong here,

Traceback (most recent call last):
   File "fill-gap.py", line 41, in
 build_abetadictionary(DICTIONARYFILE,orig_dictionary)
   File "fill-gap.py", line 31, in build_abetadictionary
 for residues, numbers in new_dictionary.items().sort():
AttributeError: 'dict_items' object has no attribute 'sort'



Peter fixed that one.  Actually my code wasn't even right in Python 2.x, 
as the sort() method sorts in place, and doesn't return a value.  His 
code will work in both 2.x and 3.x, and is thus a much better answer.  I 
frequently confuse the sort method and the sorted function, and judging 
from this list, so do many others.  I'm pretty good at spotting that 
error if someone else makes it ;-)




I have another concerns,
is it possible to append the output file content as a sing one,
such as a.new is
A 1
B 3

b.new is
A 3
B 5

I wish the final one like:

A 1 3
B 3 5

I will think about it. Thanks,



No idea how you came up with a.new or b.new.  Or even what they contain. 
 When you say a.new is "> A 1\n> B 3\n"  I've got to guess you're 
misrepresenting it.


But if you have two dictionaries that use EXACTLY the same keys, and you 
want to print out approximately that way, consider


def  printme(dict1, dict2):
for key in sorted(dict1.keys()):
 print(key, dict1[key], dict2[key])


But notice that it won't print any value which has a key in dict2, but 
not in dict1.  And it'll get an exception if there's a key in dict1 
which is not in dict2.


So what's your real problem?  There are better ways to accomodate 
multiple sets of related data, and my answer doesn't help if there are 
3, or 4, or 42 dictionaries.


By the way, it's usually better to separate outputting the data from 
inputting.  Factoring code into separate functions makes the code more 
flexible when requirements change.


--

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


Re: [Tutor] improve the code

2011-11-01 Thread Dave Angel

On 11/01/2011 11:11 AM, lina wrote:

On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:

On 11/01/2011 10:11 AM, lina wrote:


Hi,

The following code (luckily) partial achieved what I wanted, but I
still have few questions:


#!/usr/bin/python3

import os.path

INFILEEXT=".txt"
OUTFILEEXT=".new"
DICTIONARYFILE="dictionary.pdb"
orig_dictionary={}
new_dictionary={}
abetaABresidues={}

def processonefiledata(infilename):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 orig_dictionary[parts[0]]=parts[1]


def build_abetadictionary(infilename,olddict):
 with open(infilename,"r") as f:
 for line in f:
 parts=line.strip().split()
 if parts[0] != "85CUR" and (parts[0] not in
abetaABresidues.keys()):
 abetaABresidues[parts[0]]=0
 for residues, numbers in abetaABresidues.items():
 if residues in olddict.keys():
 new_dictionary[residues]=olddict[residues]
 else:
 new_dictionary[residues]=0
 with open(base+OUTFILEEXT,"w") as f:
 for residues, numbers in new_dictionary.items():
 print(residues,numbers,file=f)
## Q1: How can I sort the results, like the effect of | sort -g

from something like:
84ALA 12
:
:
83ILE 28
:
:

to
:
83ILE 28
84ALA 12
:


Just use the sort() method of the list object.  In particular, items()
returns an unordered list, so it's ready to be sorted.

for residues, numbers in new_dictionary.items().sort():

That will sort such that residues are in sorted order.


Thanks, but still something went wrong here,

Traceback (most recent call last):
   File "fill-gap.py", line 41, in
 build_abetadictionary(DICTIONARYFILE,orig_dictionary)
   File "fill-gap.py", line 31, in build_abetadictionary
 for residues, numbers in new_dictionary.items().sort():
AttributeError: 'dict_items' object has no attribute 'sort'



Peter fixed that one.  Actually my code wasn't even right in Python 2.x, 
as the sort() method sorts in place, and doesn't return a value.  His 
code will work in both 2.x and 3.x, and is thus a much better answer.  I 
frequently confuse the sort method and the sorted function, and judging 
from this list, so do many others.  I'm pretty good at spotting that 
error if someone else makes it ;-)




I have another concerns,
is it possible to append the output file content as a sing one,
such as a.new is
A 1
B 3

b.new is
A 3
B 5

I wish the final one like:

A 1 3
B 3 5

I will think about it. Thanks,



No idea how you came up with a.new or b.new.  Or even what they contain. 
 When you say a.new is "> A 1\n> B 3\n"  I've got to guess you're 
misrepresenting it.


But if you have two dictionaries that use EXACTLY the same keys, and you 
want to print out approximately that way, consider [untested]


def  printme(dict1, dict2):
for key in sorted(dict1.keys()):
 print(key, dict1[key], dict2[key])


But notice that it won't print any value which has a key in dict2, but 
not in dict1.  And it'll get an exception if there's a key in dict1 
which is not in dict2.


So what's your real problem?  There are better ways to accomodate 
multiple sets of related data, and my answer doesn't help if there are 
3, or 4, or 42 dictionaries.


By the way, it's usually better to separate outputting the data from 
inputting.  Factoring code into separate functions makes the code more 
flexible when requirements change.


--

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


Re: [Tutor] beginner question

2011-11-01 Thread Peter Otten
Steve Willoughby wrote:

> On 01-Nov-11 08:34, Mayo Adams wrote:
>> When writing a simple for loop like so:
>>
>>   for x in f
>>
>> where f is the name of a file object, how does Python "know" to interpret
>> the variable x as a line of text, rather than,say, an individual
>> character in the file? Does it automatically
>> treat text files as sequences of lines?
> 
> Every object defines what its behavior will be when asked to do
> something.  In this case, file objects know that they are capable of
> iterating over a list of their contents by being used in a "for x in f"
> loop construct.  The file object knows to respond to that by yielding up
> a line from the file for every iteration.
> 
> You could, theoretically, write a variation of the file object class
> which iterated over characters, or logical blocks (records of some sort)
> within files, and so forth.
 
Here's a simple example of a class derived from file that iterates over 
characters (bytes) instead of lines.

Standard file:

>>> for line in open("tmp.txt"):
... print repr(line)
...
'this\n'
'is but an\n'
'example\n'

Modified file class:

>>> class MyFile(file):
... def next(self):
... c = self.read(1)
... if not c:
... raise StopIteration
... return c
...
>>> for c in MyFile("tmp.txt"):
... print repr(c)
...
't'
'h'
'i'
's'
'\n'
'i'
's'
' '
'b'
'u'
't'
' '
'a'
'n'
'\n'
'e'
'x'
'a'
'm'
'p'
'l'
'e'
'\n'


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


Re: [Tutor] improve the code

2011-11-01 Thread Peter Otten
lina wrote:

>> sorted(new_dictionary.items())
> 
> Thanks, it works, but there is still a minor question,
> 
> can I sort based on the general numerical value?
> 
> namely not:
> :
> :
> 83ILE 1
> 84ALA 2
> 8SER 0
> 9GLY 0
> :
> :
> 
> rather 8 9 ...83 84,
> 
> Thanks,

You need a custom key function for that one: 

>>> import re
>>> def gnv(s):
... parts = re.split(r"(\d+)", s)
... parts[1::2] = map(int, parts[1::2])
... return parts
...
>>> items = [("83ILE", 1), ("84ALA", 2), ("8SER", 0), ("9GLY", 0)]
>>> sorted(items, key=lambda pair: (gnv(pair[0]), pair[1]))
[('8SER', 0), ('9GLY', 0), ('83ILE', 1), ('84ALA', 2)]


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


Re: [Tutor] beginner question

2011-11-01 Thread Steven D'Aprano

Mayo Adams wrote:

When writing a simple for loop like so:

 for x in f

where f is the name of a file object, how does Python "know" to interpret
the variable x as a line of text, rather than,say, an individual character
in the file? Does it automatically
treat text files as sequences of lines?


Nice question! But the answer is a little bit complicated. The short 
answer is:


File objects themselves are programmed to iterate line by line rather 
than character by character. That is a design choice made by the 
developers of Python, and it could have been different, but this choice 
was made because it is the most useful.


The long answer requires explaining how for-loops work. When you say

for x in THINGY: ...

Python first asks THINGY to convert itself into a iterator. It does that 
by calling the special method THINGY.__iter__(), which is expected to 
return an iterator object (which may or may not be THINGY itself). If 
there is no __iter__ method, then Python falls back on an older sequence 
protocol which isn't relevant to files. If that too fails, then Python 
raises an error.


So what's an iterator object? An iterator object must have a method 
called "next" (in Python 2), or "__next__" (in Python 3), which returns 
"the next item". The object is responsible for knowing what value to 
return each time next() is called. Python doesn't need to know anything 
about the internal details of the iterator, all it cares about is that 
when it calls THINGY.next() or THINGY.__next__(), the next item will be 
returned. All the "intelligence" is inside the object, not in Python.


When there are no more items left to return, next() should raise 
StopIteration, which the for loop detects and treats as "loop is now 
finished" rather than as an error.


So, the end result of all this is that Python doesn't care what THINGY 
is, so long as it obeys the protocol. So anyone can create new kinds of 
data that can be iterated over. In the case of files, somebody has 
already done that for you: files are built into Python.


Built-in file objects, like you get from f = open("some file", "r"), 
obey the iterator protocol. We can run over it by hand, doing exactly 
what Python does in a for-loop, only less conveniently.


Suppose we have a file containing "fee fi fo fum" split over four lines. 
Now let's iterate over it by hand. File objects are already iterators, 
so in Python 3 they have their own __next__ method and there's no need 
to call __iter__ first:


>>> f = open('temp.txt', 'r')
>>> f.__next__()
'fee\n'
>>> f.__next__()
'fi\n'
>>> f.__next__()
'fo\n'
>>> f.__next__()
'fum\n'
>>> f.__next__()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration



So the file object itself keeps track of how much of the file has been 
read, and the Python interpreter doesn't need to know anything about 
files. It just needs to know that the file object is iterable. I already 
know this, so I took a short-cut, calling f.__next__() directly. But 
Python doesn't know that, it performs one extra step: it calls 
f.__iter__ to get an iterator object:


>>> f.__iter__()
<_io.TextIOWrapper name='temp.txt' encoding='UTF-8'>

In this case, that iterator object is f itself, and now the Python 
interpreter goes on to call __next__() repeatedly.


File objects are actually written in C for speed, but if they were 
written in pure Python, they might look something vaguely like this:



class File(object):
def __init__(self, name, mode='r'):
self.name = name
if mode == 'r':
... # open the file in Read mode
elif mode == 'w':
... # open in Write mode
else:
# actually there are other modes too
raise ValueError('bad mode')

def __iter__(self):
return self  # I am my own iterator.

def read(self, n=1):
# Read n characters. All the hard work is in here.
...

def readline(self):
# Read a line, up to and including linefeed.
buffer = []
c = self.read()
buffer.append(c)
while c != '' and c != '\n':
c = self.read()  # Read one more character.
buffer.append(c)
return ''.join(buffer)

def __next__(self):
line = self.readline()
if line == '':
# End of File
raise StopIteration
else:
return line


--
Steven

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


[Tutor] login window using Tk

2011-11-01 Thread Chris Hare

I am working on a python Tk program which involves a login window and I am 
looking for some advice.

Currently the code I have creates a window (Toplevel) where the login controls 
are and I am running that using a main loop for the window.  The root window is 
hidden.  The objective is that when the user ha successfully authenticated, the 
login window is closed or the main loop os exited and then the root window is 
shown and the main loop started for the actual application.

Questions:
1.  Is this the best way of doing this or is there a better way?
2.  How do I exit the main loop when the user has authenticated?

Thanks

Chris Hare
ch...@labr.net
http://www.labr.net



Chris Hare
ch...@labr.net
http://www.labr.net

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Steve Willoughby

On 01-Nov-11 09:47, Chris Hare wrote:

Questions:
1. Is this the best way of doing this or is there a better way?
2. How do I exit the main loop when the user has authenticated?


Why stop the main loop and restart it?  Typically you'd setup the app, 
and start the main loop running for the duration.  Everything else is 
handled by the application's logic.


For example, you'd display the login toplevel window, and when it's 
satisfied, it can trigger the functionality which creates (or displays 
the pre-created but hidden) application window and dismisses the login 
toplevel.



--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] login window using Tk

2011-11-01 Thread Joel M.
I am also intrested in this topic.

Chris were you thinking of using the window.hide() method?

-Joel M
On Nov 1, 2011 1:21 PM, "Chris Hare"  wrote:

>
> I am working on a python Tk program which involves a login window and I am
> looking for some advice.
>
> Currently the code I have creates a window (Toplevel) where the login
> controls are and I am running that using a main loop for the window.  The
> root window is hidden.  The objective is that when the user ha successfully
> authenticated, the login window is closed or the main loop os exited and
> then the root window is shown and the main loop started for the actual
> application.
>
> Questions:
> 1.  Is this the best way of doing this or is there a better way?
> 2.  How do I exit the main loop when the user has authenticated?
>
> Thanks
>
> Chris Hare
> ch...@labr.net
> http://www.labr.net
>
>
>
> Chris Hare
> ch...@labr.net
> http://www.labr.net
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help

2011-11-01 Thread Alan Gauld

On 01/11/11 04:10, Chris Kavanagh wrote:


before. In other languages, ala C++, don't global variables have to be
declared at the 'top' of the code??


No, that's just common usage.
You can declare a variable anywhere in C/C++ provided it's before
it is used. But that can lead to hard to read code (hunting for variable 
definitions to see their type, say). One place where it is common to 
define variables away from the top of yourcode is in a for loop:


for (int i = 0;i<10;i++){
 // pass
}

printf("%d\n", i);

is valid C++(*) and defines the loop variable i inside the loop 
condition, then uses it after the loop.


But apart from that its not that common.


(*)Oops, I just tried it and got a obsolescence warning from g++ so
that may have changed recently! But it worked ok with -fpermissive 
defined...


HTH,

Alan G.

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 01/11/11 16:47, Chris Hare wrote:


I am working on a python Tk program which involves a login window and I
am looking for some advice.

Currently the code I have creates a window (Toplevel) where the login
controls are and I am running that using a main loop for the window.


Don't do this, it is frought with difficulties. Have a single
mainloop for the program. Everything else should be driven by
events.

So you initialise your program including creating both the main and 
login windows. You show the login window. When the user logs in you
validate the login and if not valid return control to the login window. 
If it is valid close the login window and show the main window.


You then proceed to process all events for the main window.

Trying to synchronize and control multiple mainloops in a GUI 
environment is incredibly hard to get right.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alexander Etter
On Nov 1, 2011, at 12:47, Chris Hare  wrote:

> 
> I am working on a python Tk program which involves a login window and I am 
> looking for some advice.
> 
> Currently the code I have creates a window (Toplevel) where the login 
> controls are and I am running that using a main loop for the window.  The 
> root window is hidden.  The objective is that when the user ha successfully 
> authenticated, the login window is closed or the main loop os exited and then 
> the root window is shown and the main loop started for the actual application.
> 
> Questions:
> 1.  Is this the best way of doing this or is there a better way?
> 2.  How do I exit the main loop when the user has authenticated?
> 
> Thanks
> 
> Chris Hare
> ch...@labr.net
> 
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Hi, hopefully a more experience hacker can provide clarity, but how secure does 
this login need to be? I dont much about python in DRAM but your login sounds 
like it could be easily hacked. 
Alexander___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-01 Thread Alan Gauld

On 01/11/11 14:33, Dave Angel wrote:


Just use the sort() method of the list object. In particular, items()
returns an unordered list, so it's ready to be sorted.

for residues, numbers in new_dictionary.items().sort():


I don't think this would work since sort works in place. You would need 
to use sorted()


for residues, numbers in sorted(new_dictionary.items()):

And sorted() also works on the Set type object that items returns...


HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Paper Rock Scissors game - User's choice not returned properly

2011-11-01 Thread Alan Gauld

On 01/11/11 09:46, Peter Otten wrote:

Alan Gauld wrote:



Good point, although  you could test the first character only...

if choice[0].lower() not in ('prs'): # NB use a single string


What Steven says, plus you may run into an IndexError if choice is the empty
string. If you absolutely want to test against a single string you have to
check the length first.


Hmm, yeah, ok you win :-)

Use the list form, even though it does involve a few more keystrokes and 
a lot more screen space. The extra typing to avoid the problems are not 
worth it! :-)



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 01/11/11 18:09, Alexander Etter wrote:


Hi, hopefully a more experience hacker can provide clarity, but how
secure does this login need to be? I dont much about python in DRAM but
your login sounds like it could be easily hacked.


That depends entirely on how the user is authenticated.
(assuming basic things like blanked password fields in the UI etc)

For all we know the authentication could be against a central
Single Sign On authentication server someplace.
The OP didn't say.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Tim Johnson
* Alexander Etter  [01 03:36]:
 
> Rinu, I use emacs. I use Python and C++. I'm also a university
> student. Last semester I learned python 2.7 using IDLE, and
> continued with IDLE while I searched for alternatives over the
> summer. I didn't find what I was looking for. Say, just a few
> weeks ago I started my C++ course and switched to emacs since the
> professor was using it. I tried it, read the easy to understand
> documentation, and I am so productive, jubilant, and satisfied
> with GNU Emacs. It's extensible beyond immediate comprehension;
> like a sunflower it starts as a seed, sprouts leaves, etc; I'm
> elaborating the infinite usability of emacs. 

  My niece graduated in Computer Science at UC Berkeley. I believe
  that emacs was the default programming editor in many of her
  classes. Elisp  employs docstrings for subroutines, constants and
  variables. Emacs has a system of extracting those docstrings,
  which makes any 'plugins' self documenting. I'm sure that these
  features make emacs very attractive for team work, regardless of
  the environment whether academic or professional.

> There is a learning curve. One may find a learning curve with
> everything in existence, whereas I repudiate one discouraging
> another for the aforementioned. 
  Yup. Bigtime.

> Those who desire the power of emacs seek it. 
> Tim, do you use GNU Emacs?
  I did use GNU emacs. I also used Xemacs, but prefered GNU Emacs.

> From what literature I've encountered including a wikipedia page I
> believe there is a satiric starwars-like cold-war feud between
> users of vi and emacs. 
  A feud for those who have nothing better to do. The acrimony
  around the 'fork' of Xemacs is greater and more serious.
  
  I believe that there are 'plugins' that can make emacs easier to
  use for the beginner. There are also a number of emacs 'Cheat Sheets'
  available, perhaps even with the distro.

  There is a physical Reference Card available here:
  http://shop.fsf.org/product/emacs-reference-cards-v-22/
  I highly recommend it.
  cheers
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare
Here is a code snippet I have pulled out of the project.  It is as bare bones 
as I can make it to get the point across.

the problems I am having:

1.  I would really like the window to be centered in the user's screen, but 
setting the geometry doesn't place it there.  (that isn't included here)
2.  When I click the Login button, nothing happens.  I know I am missing 
something but it just isn't obvious what it is.
3.  Finally, I would like to be able to hide the root window until the 
authentication is performed, but root.hide() gets me a getattr error.  
root.withdraw() works, but I can't get the root window back 

Thanks for your help.  

import sys
from Tkinter import *
import tkMessageBox
import tkFont

class Login:
def __init__(self,parent):
self.window = parent

def show(instance): 
window = Toplevel()
frame = Frame(window,bg=backColor)
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command="sys.exit()")
menubar.add_cascade(label="File", menu=filemenu)
window.config(menu=menubar)
programName = Label(frame, text = "test")
list = Listbox(frame)
list.insert( END,"test")
label1 = Label(frame, text = "Organization", fg="Blue", 
bg=backColor)
label2 = Label(frame, text = "Username", fg="Blue", 
bg=backColor)
label3 = Label(frame, text = "Password", fg="Blue", 
bg=backColor)
login_userid = Entry(frame,bg=outFocusColor)
login_passwd = Entry(frame,bg=outFocusColor,show="*")
login_userid.bind("", login_passwd.focus_set())
btnLogin = Button(frame, text="Login", command="print button 
pressed",highlightbackground=backColor)

frame.title = "Login to application" 
list.focus_set()
frame.grid()
programName.grid(row=0, column=0,columnspan=5,sticky=W)
label1.grid(row=1, column=0,columnspan=3, sticky=W)
list.grid(row=1, column=6, columnspan=5, sticky=W)
label2.grid(row=2, column=0,columnspan=3, sticky=W)
login_userid.grid(row=2, column=6, columnspan=5,sticky=W)
label3.grid(row=3, column=0,columnspan=3, sticky=W)
login_passwd.grid(row=3, column=6, columnspan=5,sticky=W)
btnLogin.grid(row=4, column=4, sticky=W)

if __name__ == "__main__":
backColor = "Gray"
entryColor = "Cyan"
okColor = "Green"
warnColor = "Red"
inFocusColor = "Cyan"
outFocusColor = "White"

root = Tk()
root.withdraw()
l = Login(root)
l.show()

root.mainloop()___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare
Okay - that makes sense.  The login window uses the show="*" for the password 
field and is authenticated against a database where the passwords are 
encrypted.  I have this working in a text only environment, just struggling to 
get it right for the GUI

Thanks

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 1:02 PM, Alan Gauld wrote:

> 

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Wayne Werner
On Tue, Nov 1, 2011 at 1:57 PM, Chris Hare  wrote:

> Here is a code snippet I have pulled out of the project.  It is as bare
> bones as I can make it to get the point across.
>
> the problems I am having:
>
> 1.  I would really like the window to be centered in the user's screen,
> but setting the geometry doesn't place it there.  (that isn't included here)
>

Take a look at the winfo_screenwidth/height methods.


> 2.  When I click the Login button, nothing happens.  I know I am missing
> something but it just isn't obvious what it is.
>

In your code you have "print button pressed" as the command - this is a
string, and certainly wont do anything useful - you want to put a function
here, e.g. command=self.login or something to that effect. You don't want
parenthesis, or it will try to login as soon as python creates your button.


> 3.  Finally, I would like to be able to hide the root window until the
> authentication is performed, but root.hide() gets me a getattr error.
>  root.withdraw() works, but I can't get the root window back
>

root.deiconify() is the method you're looking for here.

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


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Wayne Werner
On Tue, Nov 1, 2011 at 6:31 AM, Alexander Etter wrote:

> There is a learning curve.


Yes, and for a graphical comparison of learning curves:
http://jeetworks.org/files/images/emacs_learning_curves.png

;)


> One may find a learning curve with everything in existence, whereas I
> repudiate one discouraging another for the aforementioned.
> Those who desire the power of emacs seek it.
> Tim, do you use GNU Emacs?
> >From what literature I've encountered including a wikipedia page I
> believe there is a satiric starwars-like cold-war feud between users of vi
> and emacs.
> I'm neutral and won't judge an entity or patronize one for their use of
> free will.


I think these days a lot more people have become more pragmatic (or maybe I
just hang around more levelheaded people now ;) but there are few better
ways to start a flame war on IRC or USENET than ask the question which is
better - vi or emacs.

I "grew up" using vim, and I personally prefer modal editing - something
about my brain prefers the clear distinction between writing my code and
editing my code. For a while I tried emacs (mainly because I started
learning Lisp, and I was working at a .NET shop and they had some horribly
basic emacs shortcuts), and got lots of wrist cramps using chords for
everything, even after I changed the caps key to control like it should be.

My only recommendation is that you should learn emacs, vim, (or both, if
you're crazy like I was ;) because you will be a *much* more productive
programmer, simply because you can do things with both emacs and vim that
you cannot do in more basic editors.

Anyhow, just my two-bits.
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Alexander Etter

On Nov 1, 2011, at 16:06, Wayne Werner  wrote:

> On Tue, Nov 1, 2011 at 6:31 AM, Alexander Etter  wrote:
> There is a learning curve.
> 
> Yes, and for a graphical comparison of learning curves: 
> http://jeetworks.org/files/images/emacs_learning_curves.png
> 
> ;)
>  
> One may find a learning curve with everything in existence, whereas I 
> repudiate one discouraging another for the aforementioned.
> Those who desire the power of emacs seek it.
> Tim, do you use GNU Emacs?
> >From what literature I've encountered including a wikipedia page I believe 
> >there is a satiric starwars-like cold-war feud between users of vi and emacs.
> I'm neutral and won't judge an entity or patronize one for their use of free 
> will.
> 
> I think these days a lot more people have become more pragmatic (or maybe I 
> just hang around more levelheaded people now ;) but there are few better ways 
> to start a flame war on IRC or USENET than ask the question which is better - 
> vi or emacs.
> 
> I "grew up" using vim, and I personally prefer modal editing - something 
> about my brain prefers the clear distinction between writing my code and 
> editing my code. For a while I tried emacs (mainly because I started learning 
> Lisp, and I was working at a .NET shop and they had some horribly basic emacs 
> shortcuts), and got lots of wrist cramps using chords for everything, even 
> after I changed the caps key to control like it should be. 
> 
> My only recommendation is that you should learn emacs, vim, (or both, if 
> you're crazy like I was ;) because you will be a *much* more productive 
> programmer, simply because you can do things with both emacs and vim that you 
> cannot do in more basic editors.
> 
> Anyhow, just my two-bits.
> -Wayne

I like than .png image! It does appear vi biased though!
Alexander___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Steve Willoughby

On 01-Nov-11 13:19, Alexander Etter wrote:

I like than .png image! It does appear vi biased though!


Not quite, notice the initial steep climb.  :)  Yes, it's 
tongue-in-cheek, but feels about right, once you master vi (or emacs) you're

able to be amazingly productive with complex operations involving
text files.

You think emacs is bad, though? Try TECO.


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Wayne Werner
On Tue, Nov 1, 2011 at 3:24 PM, Steve Willoughby  wrote:

> On 01-Nov-11 13:19, Alexander Etter wrote:
>
>> I like than .png image! It does appear vi biased though!
>>
>
> Not quite, notice the initial steep climb.  :)  Yes, it's tongue-in-cheek,
> but feels about right, once you master vi (or emacs) you're
> able to be amazingly productive with complex operations involving
> text files.
>
> You think emacs is bad, though? Try TECO.


Or better yet, ed: http://www.gnu.org/fun/jokes/ed.msg.html

Interesting to note though, ed's manpage has got quite an update in the
past few years to something that's actually useful (and there are several
ed tutorials). I learned to use a little bit of ed, and it was fun to see
where a lot of vim commands came from.

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 01/11/11 18:57, Chris Hare wrote:

Here is a code snippet I have pulled out of the project. It is as bare
bones as I can make it to get the point across.


I think you could have dropped a lot more to be honst - like all the 
menu code for a start...



1. I would really like the window to be centered in the user's screen,
but setting the geometry doesn't place it there. (that isn't included here)


OK, But thats a problem for another day.
And some of these things are OS/UI dependent so you need to tell us what 
OS you are on.




2. When I click the Login button, nothing happens. I know I am missing
something but it just isn't obvious what it is.


You don't provide a command in the command parameter. You provide a 
string. When you click the button Python will try to call the string, 
which won't work. You should get an error message in the console -= how 
are you running this? Is it from a command prompt? If not you may not be 
seeing all the error messages Python is sending you...



3. Finally, I would like to be able to hide the root window until the
authentication is performed, but root.hide() gets me a getattr error.
root.withdraw() works, but I can't get the root window back


Really? You want the Login window to disappear even before you have 
authenticated the user? Thats very unusual behaviour. Usually the login 
window only goes away once you have been authenticated.




import sys
from Tkinter import *
import tkMessageBox
import tkFont

class Login:
def __init__(self,parent):
self.window = parent

def show(instance):
window = Toplevel()
frame = Frame(window,bg=backColor)

> ...

label2 = Label(frame, text = "Username", fg="Blue", bg=backColor)
label3 = Label(frame, text = "Password", fg="Blue", bg=backColor)
login_userid = Entry(frame,bg=outFocusColor)
login_passwd = Entry(frame,bg=outFocusColor,show="*")
login_userid.bind("", login_passwd.focus_set())
btnLogin = Button(frame, text="Login", command="print button
pressed",highlightbackground=backColor)


you need to provide the name of a function to the command parameter - or 
use a lambda expression if its just a one liner



frame.grid()
label2.grid(row=2, column=0,columnspan=3, sticky=W)
login_userid.grid(row=2, column=6, columnspan=5,sticky=W)
label3.grid(row=3, column=0,columnspan=3, sticky=W)
login_passwd.grid(row=3, column=6, columnspan=5,sticky=W)
btnLogin.grid(row=4, column=4, sticky=W)




if __name__ == "__main__":
root = Tk()
root.withdraw()
l = Login(root)
l.show()

root.mainloop()


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Joel Montes de Oca

On 11/01/2011 02:18 PM, Alan Gauld wrote:

On 01/11/11 18:09, Alexander Etter wrote:


Hi, hopefully a more experience hacker can provide clarity, but how
secure does this login need to be? I dont much about python in DRAM but
your login sounds like it could be easily hacked.


That depends entirely on how the user is authenticated.
(assuming basic things like blanked password fields in the UI etc)

For all we know the authentication could be against a central
Single Sign On authentication server someplace.
The OP didn't say.



Question, once the code is compiled to a binary, can someone inject code 
to cause the hidden window to show, skipping the login altogether?


--
-Joel M.

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


[Tutor] Problem in running script

2011-11-01 Thread Narguess Dadfar
I want to prepare a script in python that updates the attributes of the
crime incidents falling within the patrol zone. I have  a point feature
class of crime incidents and a polygon feature class of patrol zone.

I used the getcount. method to take a count of the incidents in the current
layer as the script goes the each loop, returns an integer value, and adds
1. The update cursor then updates the “INCIDENT” field. The calculations to
be performed in order to determine the incidents per square mile are:
taking the Shape_area in meters and converting it to miles by dividing it
by the constant provided and then dividing the number of incident by the
converted shape area(in miles). The incidents per square mile will then be
used to rank the priority of graffiti incidents in each zone. This was done
with an” if/elif/else” statement where the first “if” is the “Top Priority”
and the rest of the ranks follow as they meet the conditions in the
consecutive statement. An update cursor is written into each of the
“if/elif/else” parts of the statement to update the “PRIORITY “ field while
the script is running through the current zone each time it loops.But I ran
to problem. Please let me know what I should change.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare

Good feedback Alan, thanks.

I wasn't using the root window to hold the login form, although I suppose I 
could.  I guess where I am stuck is the login to control displaying the login 
window, and hiding it to display the actual application window once the user 
has authenticated.  

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 3:49 PM, Alan Gauld wrote:

> 

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


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Steve Willoughby

On 01-Nov-11 13:24, Steve Willoughby wrote:

On 01-Nov-11 13:19, Alexander Etter wrote:

I like than .png image! It does appear vi biased though!


Not quite, notice the initial steep climb. :) Yes, it's tongue-in-cheek,


Oops, my mistake. If the y axis is productivity and x is time using the 
tool, then yes, a bit vi-biased.  And that makes the Visual Studio graph

make more sense as well :)



--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GNU Emacs and Python

2011-11-01 Thread Alan Gauld

On 01/11/11 20:24, Steve Willoughby wrote:


You think emacs is bad, though? Try TECO.


Thats why Mr Stallman wrote emacs! :-)

I once used Teco for a month, just for a dare.
The best thing about it was taking a test file and typing your name into 
it, and seeing if you could guess what the final result would be...


(For the uninitiated Teco was driven by characters on the keyboard, but 
it didn't bother to wait till you hit enter it would just do stuff as 
soon as it had enough data to operate on. A bit like vi in edit mode but 
without the :   And every key seemed to do something!)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Problem in running script

2011-11-01 Thread Steven D'Aprano

Narguess Dadfar wrote:
[...]

But I ran to problem. Please let me know what I should change.



Would you like us to guess what problem you had?

My guess is... you got a SyntaxError, because you forgot to put a 
closing bracket on the previous line.


Am I close?



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


Re: [Tutor] Problem in running script

2011-11-01 Thread Hugo Arts
On Tue, Nov 1, 2011 at 10:47 PM, Narguess Dadfar
 wrote:
> I want to prepare a script in python that updates the attributes of the
> crime incidents falling within the patrol zone. I have  a point feature
> class of crime incidents and a polygon feature class of patrol zone.
>
> I used the getcount. method to take a count of the incidents in the current
> layer as the script goes the each loop, returns an integer value, and adds
> 1. The update cursor then updates the “INCIDENT” field. The calculations to
> be performed in order to determine the incidents per square mile are: taking
> the Shape_area in meters and converting it to miles by dividing it by the
> constant provided and then dividing the number of incident by the converted
> shape area(in miles). The incidents per square mile will then be used to
> rank the priority of graffiti incidents in each zone. This was done with an”
> if/elif/else” statement where the first “if” is the “Top Priority” and the
> rest of the ranks follow as they meet the conditions in the consecutive
> statement. An update cursor is written into each of the “if/elif/else” parts
> of the statement to update the “PRIORITY “ field while the script is running
> through the current zone each time it loops.But I ran to problem. Please let
> me know what I should change.
>

I'm sorry, but did you say you "ran to problem?" I don't mean to
offend, but that's by far the worst problem description I've ever
encountered. Please, include relevant code, errors given by the python
interpreter, expected results, and make sure to let us know if you're
doing a homework assignment.

Also, consider reading "How to ask questions the smart way." It's a
long read but you'll help yourself and everyone on the internet for
the rest of your life by reading it:
http://catb.org/~esr/faqs/smart-questions.html

We can't magically access your computer and see your code. I have no
idea what you should change if all I'm given is a paragraph of
ambiguous, incomplete English description of your code and the words
"I ran to problem."

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Wayne Werner
On Nov 1, 2011 4:17 PM, "Joel Montes de Oca"  wrote:
>
> On 11/01/2011 02:18 PM, Alan Gauld wrote:
>>
>> On 01/11/11 18:09, Alexander Etter wrote:
>>
>>> Hi, hopefully a more experience hacker can provide clarity, but how
>>> secure does this login need to be? I dont much about python in DRAM but
>>> your login sounds like it could be easily hacked.
>>
>>
>> That depends entirely on how the user is authenticated.
>> (assuming basic things like blanked password fields in the UI etc)
>>
>> For all we know the authentication could be against a central
>> Single Sign On authentication server someplace.
>> The OP didn't say.
>>
>
> Question, once the code is compiled to a binary, can someone inject code
to cause the hidden window to show, skipping the login altogether?

Technically speaking, you could do that with /any/ program in any language.
Good security is hard, and some things are just not worth spending that
much time on. If good security were easy then photoshop wouldn't be pirated
so much.

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


[Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread Joel Montes de Oca
I just discovered that it is a bad idea to complete uninstall Python 2.7 
on Ubuntu 11.10. If you do, expect a lot of things not to work, mainly 
your system. haha


I just reinstalled Python 2.7 and I hope things are not so bad now when 
I reboot.


--
-Joel M.

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Justin Straube

On 11/1/2011 3:28 PM, Chris Hare wrote:


Good feedback Alan, thanks.

I wasn't using the root window to hold the login form, although I
suppose I could. I guess where I am stuck is the login to control
displaying the login window, and hiding it to display the actual
application window once the user has authenticated.

Chris Hare
ch...@labr.net 
http://www.labr.net


Hi Chris,

Have you looked into using a Frame to hold you input fields, and then 
using .destroy() to remove it upon successful login?


This would allow you to not have to worry about hiding windows, as you 
can just reuse the same root window.


Im just a hobbyist, so if there are reasons not to use this approach, 
I'd be interested in why.


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


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread Joel Montes de Oca

On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:

Heh, yeah.  It's usually a bad idea to do stuff like that (I know a guy 
(Windows) who deleted his OS of his system).

On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:


I just discovered that it is a bad idea to complete uninstall Python 2.7 on 
Ubuntu 11.10. If you do, expect a lot of things not to work, mainly your 
system. haha

I just reinstalled Python 2.7 and I hope things are not so bad now when I 
reboot.

--
-Joel M.

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




Yea, It wiped out GNOME and UNITY along with a few other applications. 
It wasn't a big deal tho, I just reinstalled ubuntu-desktop threw 
apt-get. :)



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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 01/11/11 21:28, Chris Hare wrote:


Good feedback Alan, thanks.

I wasn't using the root window to hold the login form, although I
suppose I could. I guess where I am stuck is the login to control
displaying the login window, and hiding it to display the actual
application window once the user has authenticated.


Thats what your command function does. So when the button is pressed 
your event handler authenticates the user details, if valid it closes 
the Login and shows the main window(which could be root...)

In pseudocode:


def doLogin(self):
userid = idField.get()
passwd = pwField.get()
if self.validateUser(userid,passwd):
root.show()
self.window.hide()
else:
self.beep()   # or whatever warning message you want
self.logError("User authentication failed for " + userid)
self.idField.clear()
self.pwField.clear()

Then in creating the button you pass that as the command handler:

btnLogin = Button(self.window, text="Login", command=doLogin)

Now, when the user hits the button the doLogin function will be called.
If the login is ok we show the main window and hide the login dialog.
If the entry is invalid we beep, clear the fields for a retry and log an 
error. We could also add a count so after, say, three attempts we close 
the app.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 01/11/11 21:15, Joel Montes de Oca wrote:

Question, once the code is compiled to a binary, can someone inject code
to cause the hidden window to show, skipping the login altogether?


In general you don't compile Python to a binary, although tools exist 
that give a good approximation to that. But to inject code would need 
access to the source files and if you have been sensible with 
permissions etc that would require admin access to the machine...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Alan Gauld

On 02/11/11 00:16, Justin Straube wrote:


Have you looked into using a Frame to hold you input fields, and then
using .destroy() to remove it upon successful login?


This is a valid approach for some scenarios but its not the norm for 
login dialogs. They usually popup as fairly small standalone windows.
But what you suggest could be done in a banner frame at the top or 
bottom of the main window, then either replaced or removed from the 
geometry.



Im just a hobbyist, so if there are reasons not to use this approach,
I'd be interested in why.


Only user experience I think. Its usually best to make a GUI work like 
the other GUIs that the user is accustomed to. OTOH many web pages use 
the login style that you are suggesting so maybe the fashions will 
change for desktop apps too.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread Max gmail
Heh, yeah.  It's usually a bad idea to do stuff like that (I know a guy 
(Windows) who deleted his OS of his system).

On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:

> I just discovered that it is a bad idea to complete uninstall Python 2.7 on 
> Ubuntu 11.10. If you do, expect a lot of things not to work, mainly your 
> system. haha
> 
> I just reinstalled Python 2.7 and I hope things are not so bad now when I 
> reboot.
> 
> -- 
> -Joel M.
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare

Thanks everyone for all of the help.  I almost have this working.

Everything is written in a class.  I think I have that right, but that remains 
to be seen. :-)

I can create the login window and get all of the controls on it.  My function 
gets called to validate the information in the fields when the user presses the 
button.  

the function called however, can't seem to be able to get the items from the 
fields.  I get the error like list.get(ACTIVE) doesn't have a function for get. 
 (list was defined as a listbox.)  the same is true for the other form fields. 
I opted to use the root window and implement a frame in it for the login.  Once 
the login data has been validated, i can destroy the frame and reuse the 
window.  This may or may not work ;-)  I am a python newbie, biting off a big 
complicated chunk 

The class is defined:
class Login:
def __init__(self, parent):
self.window = parent

When I show the class instance, everything is displayed.  My verification code,

def verifyLogin(self):
farmid = list.get(ACTIVE)
userid = login_userid.get()
login_passwd = login_passwd.get()

gets called, but I get the error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
  File "z.py", line 229, in verifyLogin
farmid = list.get(ACTIVE)
AttributeError: type object 'list' has no attribute 'get'

When the frame controls were added, list is defined as

list = Listbox(frame)

What have I got messed up?  I have poked around the net but I can't find 
anything meaningful to me.

Thanks again


Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 7:50 PM, Alan Gauld wrote:

> On 01/11/11 21:28, Chris Hare wrote:
>> 
>> Good feedback Alan, thanks.
>> 
>> I wasn't using the root window to hold the login form, although I
>> suppose I could. I guess where I am stuck is the login to control
>> displaying the login window, and hiding it to display the actual
>> application window once the user has authenticated.
> 
> Thats what your command function does. So when the button is pressed your 
> event handler authenticates the user details, if valid it closes the Login 
> and shows the main window(which could be root...)
> In pseudocode:
> 
> 
> def doLogin(self):
>userid = idField.get()
>passwd = pwField.get()
>if self.validateUser(userid,passwd):
>root.show()
>self.window.hide()
>else:
>self.beep()   # or whatever warning message you want
>self.logError("User authentication failed for " + userid)
>self.idField.clear()
>self.pwField.clear()
> 
> Then in creating the button you pass that as the command handler:
> 
> btnLogin = Button(self.window, text="Login", command=doLogin)
> 
> Now, when the user hits the button the doLogin function will be called.
> If the login is ok we show the main window and hide the login dialog.
> If the entry is invalid we beep, clear the fields for a retry and log an 
> error. We could also add a count so after, say, three attempts we close the 
> app.
> 
> HTH
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread spawgi
Shouldn't this be treated as a bug then? As a user I should be allowed to
uninstall the software I want to.
Or you uninstalled other things by mistake?

On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca
wrote:

> On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:
>
>> Heh, yeah.  It's usually a bad idea to do stuff like that (I know a guy
>> (Windows) who deleted his OS of his system).
>>
>> On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:
>>
>>  I just discovered that it is a bad idea to complete uninstall Python 2.7
>>> on Ubuntu 11.10. If you do, expect a lot of things not to work, mainly your
>>> system. haha
>>>
>>> I just reinstalled Python 2.7 and I hope things are not so bad now when
>>> I reboot.
>>>
>>> --
>>> -Joel M.
>>>
>>> __**_
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/**mailman/listinfo/tutor
>>>
>>
>>
> Yea, It wiped out GNOME and UNITY along with a few other applications. It
> wasn't a big deal tho, I just reinstalled ubuntu-desktop threw apt-get. :)
>
>
>
> --
> -Joel M.
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread Christian Witts

On 2011/11/02 08:26 AM, spa...@gmail.com wrote:
Shouldn't this be treated as a bug then? As a user I should be allowed 
to uninstall the software I want to.

Or you uninstalled other things by mistake?

On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca 
mailto:joelmonte...@gmail.com>> wrote:


On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:

Heh, yeah.  It's usually a bad idea to do stuff like that (I
know a guy (Windows) who deleted his OS of his system).

On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:

I just discovered that it is a bad idea to complete
uninstall Python 2.7 on Ubuntu 11.10. If you do, expect a
lot of things not to work, mainly your system. haha

I just reinstalled Python 2.7 and I hope things are not so
bad now when I reboot.

-- 
-Joel M.


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



Yea, It wiped out GNOME and UNITY along with a few other
applications. It wasn't a big deal tho, I just reinstalled
ubuntu-desktop threw apt-get. :)



-- 
-Joel M.

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




--
http://spawgi.wordpress.com
We can do it and do it better.


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


It's not a bug. Ubuntu requires Python to be installed for a number of 
it's applications to run, and uninstalling Python will cause them to 
stop working. You could argue that then they should maintain a seperate 
install of Python to handle their core applications, but that would go 
against the grain of how package management is performed and how 
releases would need to be packaged etc.


--

Christian Witts
Python Developer

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


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread Hugo Arts
On Wed, Nov 2, 2011 at 7:26 AM,   wrote:
> Shouldn't this be treated as a bug then? As a user I should be allowed to
> uninstall the software I want to.
> Or you uninstalled other things by mistake?

I don't think this is a bug. Python 2.7 is required software for both
Gnome and Unity. Without it neither would be able to function. Since
they are non-functional anyway, it makes some sense for apt to
automatically remove them. I tried it on my virtual machine, and when
you run `sudo apt-get remove python,` it does tell you that it's about
to remove a metric ass-tonne of packages (including ubuntu-desktop and
basically gnome-everything).

That kind of output should warn most users that something isn't going
according to plan, I'd think :S

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