Re: [Tutor] Tkinter Entry field text

2011-09-10 Thread Wayne Werner
On Thu, Sep 8, 2011 at 9:31 PM, brandon w  wrote:

> How do you display text in a Entry field and have it disappear when a
> person clicks in it?
>  To get text into this box the person must first delete
> what is already in there.
>
> Python 2.6.6
>

 Think about the process. You're already mostly there: you're displaying
data already, and you know what you want to do.

You'll want to take a look at binding events, and if you Google for "tkinter
events" (http://www.google.com/search?q=tkinter+events) then your first
result takes you here:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

To point you in the right direction, you want to take a look at the
 event. If you're not familiar with what a callback is, you should
also read this page: http://effbot.org/zone/tkinter-callbacks.htm

If you still have problems after that, show us what else you've tried and
we'll be happy to give you more pointers in the right direction.



Of course, while this answers your questions, I would be remiss if I didn't
suggest a few more things about your program in general.

> label1 = Label(root, text="Enter you password: ")

label1 isn't a terribly descriptive name. That's fine if you don't intend to
actually do anything with the label. However, you can make this even more
explicit by chaining the commands together. Since Label() returns a label,
you can add a dot to the end and treat it just like you would the variable:

Label(root, text="Enter you password: ").grid(sticky=W, row=0, column=0)

That will create your label and stick it in the grid in one step, and makes
it clear to anyone reading your code (including yourself down the road!)
that you don't care to do anything with that label.

Next:

> enter_data1 = Entry(root, bg = "pale green")

enter_data1 also suffers from the same naming problem. It doesn't describe
what the variable is or does very well, aside from entering data. You could
change it to something like "password_entry" - which tells anyone reading
your program that the variable should contain something that lets you do
some password entry. Just naming it password would also be better than
enter_data1.

One other issue that you should consider - with the options you have set,
anyone could see what you typed in as your password. If you're just using
this as a testing program to play around with, that's probably OK, but
what's even better is to change what's shown in the box. You can do this by
setting the "show" option, either in the constructor or somewhere later:

from Tkinter import *

root = Tk()
entry = Entry(root)
entry.pack()
entry.insert(0, "My Cool Password")
entry.config(show="*")
root.mainloop()

The nice thing about the config() method is that it allows you to change
config attributes later. You can combine this knowledge with some of what I
mentioned earlier to show 'password' until they navigate to the field, and
then just show asterisks.

For bonus points, if they leave the password field without typing a
password, can you make it show 'password' again?

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
So I've done quite a bit more work. With phone_book.py the user can
not only access phone numbers by the person's initials, but can add
items to the data file. I've also solved the problem of adding a
person who's initials have already been used in a key.

I've pasted phone_book_for_pasting.py at .

I'd appreciate any comments, instructive criticism, etc.

Some have suggested using the shelve module. I looked at it but
couldn't see in detail how to use it. If someone could write a short
demo script, or send me to one that pretty much does what my
phone_book.py does, that would be terrific.

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


[Tutor] databases

2011-09-10 Thread Andre' Walker-Loud
Hi All,

I am completely new to databases as well as using python to access/create 
databases.  I started googling about it and found so much info, I wasn't sure 
where to begin to answer my first question.  So I thought I would query this 
group by asking a question I am sure has been asked before - but you all are so 
friendly, I thought I would give it a try.

I have databases (many of them) which I want to manipulate, averaging data, 
merging databases, etc.

Do I need to install separate modules to access the databases?

Do I need to know the specific style the databases were created in to open 
manipulate them with python (2.7)?  I ask this because with xml files, I was 
able to just use 

from xml.dom import minidom

and then by trial and error in an interactive session, I could figure out how 
to walk through the xml file to find what I wanted.  I am wondering if I can do 
something similar with a database, or if there are more pre-defined formats.  I 
do not actually know what format my databases are in (someone else wrote the 
c-code to create them).


While waiting for suggestions, I have started to read Alan Gauld's tutorial on 
the subject

http://www.alan-g.me.uk/tutor/index.htm



Thanks,

Andre


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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Jack Trades
On Sat, Sep 10, 2011 at 1:08 PM, Richard D. Moores wrote:

> So I've done quite a bit more work. With phone_book.py the user can
> not only access phone numbers by the person's initials, but can add
> items to the data file. I've also solved the problem of adding a
> person who's initials have already been used in a key.
>
> I've pasted phone_book_for_pasting.py at .
>
> I'd appreciate any comments, instructive criticism, etc.
>
>
It looks pretty good overall, though I didn't examine it too closely.  IMHO
there are some awkward bits which I think come from your representation of
the data.

I would probably make the phonebook itself a list, with each entry being a
dict.  Something like:

book = [
{'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msand...@stanfordalumni.org'},
{'name':'AAA', 'phone':'575-3992', 'phone2':'1-800-472-4630',
'notes':'Membership #422 260 0131863 00 8'},
#...
]


Then you can easily search your phone book by name, email, type of contact,
relation, etc.  A search by name would look like this:

def find_by_name(name):
  for entry in book:
if entry['name'] == name:
  return entry

find_by_name('Mark Sanders')
#==> {'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msand...@stanfordalumni.org}

or a more general procedure for doing searches on your book could be:

def find(criteria, term):
  for entry in book:
if entry[criteria] == term:
  return entry

find('name', 'Mark Sanders')
#==> {'name':'Mark Sanders', 'cell':'422-318-2346', '
email':'msand...@stanfordalumni.org}


Similarly you could search for initials by providing a to_initials
procedure:

def to_initials(name):
  return ''.join([i[0] for i in name.split(' ')])

def find_by_initials(initials):
  for entry in book:
if to_initials(entry['name']) == initials:
  return entry

find_by_initials('MS')
#==> {'cell': '422-318-2346', 'name': 'Mark Sanders', 'email': '
msand...@stanfordalumni.org'}


Adding a new entry would then be as simple as:

def add_new_entry(entry, book):
  book.append(entry)


For storing data I would probably use Pickle, which would look something
like this:

from cPickle import load, dump

f = open('book.pk', 'w')
dump(book, f)
f.close()

and loading your book is similar

f = open('book.pk', 'r')
book = load(f)
f.close()


If you want a human readable storage format I would look into json, but
pickle has served me well for most purposes.  Surely you can store your
phonebook as plain text and parse it the way you have, but it's not
necessary to do that with all the tools that exist for that purpose.


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


Re: [Tutor] databases

2011-09-10 Thread James Reynolds
On Sat, Sep 10, 2011 at 3:39 PM, Andre' Walker-Loud wrote:

> Hi All,
>
> I am completely new to databases as well as using python to access/create
> databases.  I started googling about it and found so much info, I wasn't
> sure where to begin to answer my first question.  So I thought I would query
> this group by asking a question I am sure has been asked before - but you
> all are so friendly, I thought I would give it a try.
>
> I have databases (many of them) which I want to manipulate, averaging data,
> merging databases, etc.
>
> Do I need to install separate modules to access the databases?
>
> Do I need to know the specific style the databases were created in to open
> manipulate them with python (2.7)?  I ask this because with xml files, I was
> able to just use
>
> from xml.dom import minidom
>
> and then by trial and error in an interactive session, I could figure out
> how to walk through the xml file to find what I wanted.  I am wondering if I
> can do something similar with a database, or if there are more pre-defined
> formats.  I do not actually know what format my databases are in (someone
> else wrote the c-code to create them).
>
>
> While waiting for suggestions, I have started to read Alan Gauld's tutorial
> on the subject
>
> http://www.alan-g.me.uk/tutor/index.htm
>
>
>
> Thanks,
>
> Andre
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


What type of databases? sql server, mysql, sqllite?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] databases

2011-09-10 Thread Andre' Walker-Loud
> 
> What type of databases? sql server, mysql, sqllite?

Hi James,

well this already helps.  I don't even know.  Do I have to know ahead of time?  
Or is there a general database package that can open a databases without 
knowing there format?


Thanks,

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
Thanks so much, Jack. You've given me much to chew on.

I began phone_book.py without much need for it -- I already had an RTF
file with 786 lines that I "grepped" using a script I wrote with Tutor
help long ago. I used an RTF file instead of a text file so that any
URLs in it would be live. But I wanted to refresh what little I used
to know about dicts and see where I could go with it. It turns out to
be something I'll actually use for quickly looking up  phone numbers
of people (friends, relatives, doctors, etc.) and some businesses, and
the occasional address. For adding key=value items to the data file,
values can be copied as is from the RTF file.  It'll probably have
fewer than 100 entries.  Your idea doesn't seem efficient for me --
lots of typing and editing. But very interesting! I'll probably have
fewer than 100 entries.

Your pickle examples give me a start on using the cPickle module.

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
Thanks so much, Jack. You've given me much to chew on.

I began phone_book.py without much need for it -- I already had an RTF
file with 786 lines that I "grepped" using a script I wrote with Tutor
help long ago. I used an RTF file instead of a text file so that any
URLs in it would be live. But I wanted to refresh what little I used
to know about dicts and see where I could go with it. It turns out to
be something I'll actually use for quickly looking up  phone numbers
of people (friends, relatives, doctors, etc.) and some businesses, and
the occasional address. For adding key=value items to the data file,
values can be copied as is from the RTF file.  It'll probably have
fewer than 100 entries.  Your idea doesn't seem efficient for me --
lots of typing and editing. But very interesting! I'll probably have
fewer than 100 entries.

Your pickle examples give me a start on using the cPickle module.

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
Jack Trades (actually Nick Zarczynski) just sent me this link to a
"Simple phone book app", and has agreed to let me post it to this
thread: 

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


Re: [Tutor] databases

2011-09-10 Thread Rafael Durán Castañeda

On 10/09/11 21:44, Andre' Walker-Loud wrote:

What type of databases? sql server, mysql, sqllite?

Hi James,

well this already helps.  I don't even know.  Do I have to know ahead of time?  
Or is there a general database package that can open a databases without 
knowing there format?


Thanks,

Andre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
You might look at http://www.sqlalchemy.org/, since it works with most 
databases. However if you are new on databases, I think you should start 
learning databases basics, choose a database management system (DBMS) 
that fit your needs and learn as much as you can about that specific 
DBMS. Then you can use SQLAlchemy or specific packagas (MySQL-Python, 
PyGreSQL,...) to acces to that DBMS. In addition if you are going to use 
frameworks, most of them already have their own tools for DB manipulation.

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Jack Trades
On Sat, Sep 10, 2011 at 4:36 PM, Richard D. Moores wrote:

> Your idea doesn't seem efficient for me --
> lots of typing and editing.
>

Not sure what you mean by that?  I've updated the gist with a quick 5min
implementation of a GUI using Tkinter and the approach I outlined.  I think
using a GUI is best way to minimize "typing and editing" in an app like
this.  You can find it here:

https://gist.github.com/1208786#file_book.py

If you're talking about re-entering all your data from your file, you would
write a script to do that.  This program assumes that you are starting from
scratch with a blank phone book.  If you would like help converting your
existing file, I'm sure I or others can help, but I'd need to see the
original file.

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


Re: [Tutor] databases

2011-09-10 Thread Alan Gauld

On 10/09/11 20:44, Andre' Walker-Loud wrote:

What type of databases? sql server, mysql, sqllite?

well this already helps.  I don't even know.
Do I have to know ahead of time?  Or is there a general database

> package that can open a databases without knowing there format?

The Python DB API is pretty good at covering all the common databases 
but sadly everyone has some slight variances so you do need to know 
which product you will be using.


As an example the SQLite package that comes in the standard library - 
and is a good starter - doesn't require login credentials but Oracle, 
MySQL etc do. Also Sqllite is stored in a single file accessed via a 
code library whereas most other SQL databases use multiple files and a 
server frontend. (That's why there's a connect() function - to connect 
to the server... in SQLite connect just opens the file!)


If you are a database noob I'd keep it simple and stick with SQLite, 
it's powerful enough for most beginner type projects and misses out some 
of the more complex features of the other packages. Provided you aren't 
expecting to scale up to 10's of millions of records it will do just 
fine. Once you understand SQLite moving to MySQL or Firebird or whatever 
will be an easy next step.


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

2011-09-10 Thread Alejandro Companioni

On Sep 10, 2011, at 6:15 PM, tutor-requ...@python.org wrote:

> You might look at http://www.sqlalchemy.org/, since it works with most 
> databases. However if you are new on databases, I think you should start 
> learning databases basics, choose a database management system (DBMS) 
> that fit your needs and learn as much as you can about that specific 
> DBMS. Then you can use SQLAlchemy or specific packagas (MySQL-Python, 
> PyGreSQL,...) to acces to that DBMS. In addition if you are going to use 
> frameworks, most of them already have their own tools for DB manipulation.
> 

Just wanted to chime in, because I wish someone had told me this sooner, but if 
you're using a Mac, try to steer clear of mysql-python. Setting it up on a Mac 
is absolutely infuriating.

If your databases are MySQL-based and you're using a Mac, I'd recommend setting 
up a Linux VM to access them with Python (or not using Python at all). Good 
luck!

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Alan Gauld

On 10/09/11 19:08, Richard D. Moores wrote:


Some have suggested using the shelve module. I looked at it but
couldn't see in detail how to use it.


Did you read the help page? It says:

import shelve
d = shelve.open(filename) # open, with (g)dbm filename -- no suffix

d[key] = data   # store data at key
data = d[key]   # retrieve a COPY of the data at key
del d[key]  # delete data stored at key
flag = d.has_key(key)   # true if the key exists
list = d.keys() # a list of all existing keys (slow!)
d.close()   # close it


So you open the file and from that point on treat it exactly like a 
dictionary.


Then close the file at the end.

Now which part don't you understand?
The ony bit that migt confuse is the mention of gdbm filename which just 
means give it a filename without any suffix...just ignore the gdbm 
reference.


--
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] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
On Sat, Sep 10, 2011 at 15:32, Alan Gauld  wrote:
> On 10/09/11 19:08, Richard D. Moores wrote:
>
>> Some have suggested using the shelve module. I looked at it but
>> couldn't see in detail how to use it.
>
> Did you read the help page?

I did. I can see it would be a useful reference once I learned from
elsewhere how to use shelve.

> It says:
>
> import shelve
> d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
>
> d[key] = data   # store data at key
> data = d[key]   # retrieve a COPY of the data at key
> del d[key]      # delete data stored at key
> flag = d.has_key(key)   # true if the key exists
> list = d.keys() # a list of all existing keys (slow!)
> d.close()       # close it
>
>
> So you open the file and from that point on treat it exactly like a
> dictionary.

I'm still a bit shaky about dictionaries.

> Then close the file at the end.
>
> Now which part don't you understand?

Much of what comes after that is beyond me.

> The ony bit that migt confuse is the mention of gdbm filename which just
> means give it a filename without any suffix...just ignore the gdbm
> reference.

Thanks for your encouragement Alan, but I'm still looking among my
Python books for a good exposition of shelve.

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


Re: [Tutor] databases

2011-09-10 Thread Andre' Walker-Loud
> > package that can open a databases without knowing there format?
> 
> The Python DB API is pretty good at covering all the common databases but 
> sadly everyone has some slight variances so you do need to know which product 
> you will be using.
> 
> As an example the SQLite package that comes in the standard library - and is 
> a good starter - doesn't require login credentials but Oracle, MySQL etc do. 
> Also Sqllite is stored in a single file accessed via a code library whereas 
> most other SQL databases use multiple files and a server frontend. (That's 
> why there's a connect() function - to connect to the server... in SQLite 
> connect just opens the file!)
> 
> If you are a database noob I'd keep it simple and stick with SQLite, it's 
> powerful enough for most beginner type projects and misses out some of the 
> more complex features of the other packages. Provided you aren't expecting to 
> scale up to 10's of millions of records it will do just fine. Once you 
> understand SQLite moving to MySQL or Firebird or whatever will be an easy 
> next step.

So, in case I wasn't clear, the databases are already made by someone else, and 
the format is beyond my control.  I need/want to learn to manipulate them.  
Most likely they are similar to the Berkeley database (but I don't know what 
this means yet).

Thanks for the help,

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


Re: [Tutor] databases

2011-09-10 Thread Andre' Walker-Loud
>> You might look at http://www.sqlalchemy.org/, since it works with most 
>> databases. However if you are new on databases, I think you should start 
>> learning databases basics, choose a database management system (DBMS) 
>> that fit your needs and learn as much as you can about that specific 
>> DBMS. Then you can use SQLAlchemy or specific packagas (MySQL-Python, 
>> PyGreSQL,...) to acces to that DBMS. In addition if you are going to use 
>> frameworks, most of them already have their own tools for DB manipulation.
>> 
> 
> Just wanted to chime in, because I wish someone had told me this sooner, but 
> if you're using a Mac, try to steer clear of mysql-python. Setting it up on a 
> Mac is absolutely infuriating.
> 
> If your databases are MySQL-based and you're using a Mac, I'd recommend 
> setting up a Linux VM to access them with Python (or not using Python at 
> all). Good luck!

Thanks for the warning Alejandro.

Turns out, they live on a linux cluster - but I log in via a Mac, and likely 
will copy locally to play around with.

I figured I could either hack the c++ code built already to manipulate them, or 
use this as an excuse to learn about databases via python.


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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Alan Gauld

On 11/09/11 00:18, Richard D. Moores wrote:


So you open the file and from that point on treat it exactly like a
dictionary.


I'm still a bit shaky about dictionaries.


But you started the post with using a dictionary.

Shelve is just a dictionary that lives in a file instead of memory.
If you can put data into or read it out of a dictionary then you can do 
the same with shelve.


The only complexity is you have to open the file before sing it and 
close it when your done.



Much of what comes after that is beyond me.




Thanks for your encouragement Alan, but I'm still looking among my
Python books for a good exposition of shelve.


You probably won't find much in books because shelve has such a specific 
purpose. As a result there isn't much to say about it

if you've already covered dictionaries.

It's a file based dictionary. So read up on dictionaries.
Then just use it.

There are a few limitations with shelve but for most normal
cases you can ignore that and just use it like any normal
dictionary.

--
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] need advice about a dictionary ({})

2011-09-10 Thread Marc Tompkins
On Sat, Sep 10, 2011 at 4:18 PM, Richard D. Moores wrote:

> On Sat, Sep 10, 2011 at 15:32, Alan Gauld 
> wrote:
>
> So you open the file and from that point on treat it exactly like a
> > dictionary.
>
> I'm still a bit shaky about dictionaries.
>
> That right there is the salient bit.  Using shelve is just like using a
dictionary; probably that's why you're finding the documentation sparse:
dictionaries are core Python, so they assume you know how to use them.
("First, catch your rabbit...")

I was about to write an introduction to dictionaries, but I realized it's
been done, and done better than I could.  I really recommend that you learn
them; I think that once you do, you'll find that they're a better fit in all
sorts of places where you've been using lists.  (Speaking from personal
experience...)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Alan Gauld

On 11/09/11 00:18, Richard D. Moores wrote:


Now which part don't you understand?


Much of what comes after that is beyond me.


I meant to add, you can pretty much ignore all the stuff at the end of 
the Help page about class definitions. You only need that if you intend 
to create your own specialised Shelf object. All you need to know is in 
the pseudocode bit that I posted.


open() the file
use the shelf like a dictionary
close() the file

And there are two main caveats given:
1) Don't try to edit mutable data objects (lists) in place.
   Extract them, modify them and replace them
2) Don't use the writeback=True setting when you open large data sets.

That's it.

Alan G.

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


Re: [Tutor] databases

2011-09-10 Thread Alan Gauld

On 10/09/11 23:28, Alejandro Companioni wrote:


Just wanted to chime in, because I wish someone had told me this sooner,
but if you're using a Mac, try to steer clear of mysql-python. Setting
it up on a Mac is absolutely infuriating.


I've never used MySql on a Mac but I'm curious why it should be so 
difficult.


MacOS is just BSD Unix under the GUI so why would be any different
to any other Unix type system? What were the problems that you encountered?


If your databases are MySQL-based and you're using a Mac, I'd recommend
setting up a Linux VM to access them with Python (or not using Python at
all). Good luck!


Now that sounds like it should be much more difficult. You'd effectively 
be running a client server setup to a foreign OS

on your own computer but sharing the physical resources...
There must be something really weird about the MacOS setup
to make that easier!

I'm intrigued.

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

2011-09-10 Thread ALAN GAULD



> > package that can open a databases without knowing there format?
> So, in case I wasn't clear, the databases are already made by someone else, 
> and the format is beyond my control.  I need/want to learn to manipulate 
> them.  
>

OK, That wasn't clear. And it makes a difference. You need to know the format.

> Most likely they are similar to the Berkeley database

And that makes a much bigger difference because most folks assume by 
'database' you mean a SQL database. Berkeley databases are flat file based 
and there is a module to read them in the Python library, but they don't use 
SQL.

They are more like a random access file mechanism than a relational database.
If that's what you are dealing with then it's a whole lot of different 
references 

you need. I'd start with wikipedia and the [g]dbm module documentation.


HTH,

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
On Sat, Sep 10, 2011 at 17:34, Marc Tompkins  wrote:
> On Sat, Sep 10, 2011 at 4:18 PM, Richard D. Moores 
> wrote:
>>
>> On Sat, Sep 10, 2011 at 15:32, Alan Gauld 
>> wrote:
>>
>> > So you open the file and from that point on treat it exactly like a
>> > dictionary.
>>
>> I'm still a bit shaky about dictionaries.
>>
> That right there is the salient bit.  Using shelve is just like using a
> dictionary; probably that's why you're finding the documentation sparse:
> dictionaries are core Python, so they assume you know how to use them.
> ("First, catch your rabbit...")
>
> I was about to write an introduction to dictionaries, but I realized it's
> been done, and done better than I could.  I really recommend that you learn
> them; I think that once you do, you'll find that they're a better fit in all
> sorts of places where you've been using lists.  (Speaking from personal
> experience...)

Well, I wrote "a BIT shaky". I sure learned a lot writing my
phone_book.py, with important input from you . And am still pretty
happy with it. And dictionaries seem to be well-covered in some of the
Python books I have.

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
On Sat, Sep 10, 2011 at 17:34, Alan Gauld  wrote:
> On 11/09/11 00:18, Richard D. Moores wrote:
>
>>> So you open the file and from that point on treat it exactly like a
>>> dictionary.
>>
>> I'm still a bit shaky about dictionaries.
>
> But you started the post with using a dictionary.
>
> Shelve is just a dictionary that lives in a file instead of memory.
> If you can put data into or read it out of a dictionary then you can do the
> same with shelve.
>
> The only complexity is you have to open the file before sing it and close it
> when your done.
>
>> Much of what comes after that is beyond me.
>
>
>> Thanks for your encouragement Alan, but I'm still looking among my
>> Python books for a good exposition of shelve.
>
> You probably won't find much in books because shelve has such a specific
> purpose. As a result there isn't much to say about it
> if you've already covered dictionaries.
>
> It's a file based dictionary. So read up on dictionaries.
> Then just use it.
>
> There are a few limitations with shelve but for most normal
> cases you can ignore that and just use it like any normal
> dictionary.

OK, Alan, I really will give shelve a try.

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


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Richard D. Moores
On Sat, Sep 10, 2011 at 15:15, Jack Trades  wrote:
> On Sat, Sep 10, 2011 at 4:36 PM, Richard D. Moores 
> wrote:
>>
>> Your idea doesn't seem efficient for me --
>> lots of typing and editing.
>
> Not sure what you mean by that?  I've updated the gist with a quick 5min
> implementation of a GUI using Tkinter and the approach I outlined.  I think
> using a GUI is best way to minimize "typing and editing" in an app like
> this.  You can find it here:
>
> https://gist.github.com/1208786#file_book.py

Using Python 2.7 for it, it seems to work fine, except that I can't
see how the GUI helps. It opens only when I use the g option to find
an entry already made. Useful for editing an entry, though.

As for the non-GUI script, I get this error no matter which choice I
make. I'm too dumb, and have forgotten too much of Python 2.x to
debug:

==
What's next:
(q)  Quit
(a)  Add new Entry
(v)  View all Entries
(s)  General Search
(si) Search by Initials
(sn) Search by Name

> q
Traceback (most recent call last):
  File "c:\P32Working\Pickles\nicks_simple_phone_book_app.py", line
171, in 
main_loop()
  File "c:\P32Working\Pickles\nicks_simple_phone_book_app.py", line
94, in main_loop
> """)
  File "", line 1, in 
NameError: name 'q' is not defined
Process terminated with an exit code of 1

>
> If you're talking about re-entering all your data from your file, you would
> write a script to do that.

Ha! I would?

>  This program assumes that you are starting from
> scratch with a blank phone book.  If you would like help converting your
> existing file, I'm sure I or others can help, but I'd need to see the
> original file.

I'll take you up on that. I'll have to edit it some first.

What I have now with "grepping" that 786-line RTF file is maximum
flexibility. A line in the file always begins with a name, with  at
least one phone number, then if a company, the hours they are
reachable by phone, possibly their web address, maybe an email
address, my contact there plus maybe her secretary. If a physician,
there might very well be his specialty, his nurse's name, mention of
who recommended him to me, etc. It seems that the format of info for
your way is set rigidly in advance, or am I wrong?

Dick



>
> --
> Nick Zarczynski
> Pointless Programming Blog
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need advice about a dictionary ({})

2011-09-10 Thread Jack Trades
>
> Using Python 2.7 for it, it seems to work fine, except that I can't
> see how the GUI helps. It opens only when I use the g option to find
> an entry already made. Useful for editing an entry, though.
>

Well the idea would be to build the app as a full-blown GUI.  The GUI search
and edit functionality was just a proof-of-concept to show how that may
work.

I was just having some fun and seeing what I could do in less than an hour.
Turning this into a full blown app is not what I had in mind, though if you
want to build on it to learn I would be willing to help.


>
> As for the non-GUI script, I get this error no matter which choice I
> make. I'm too dumb, and have forgotten too much of Python 2.x to
> debug:
>
> ==
> What's next:
> (q)  Quit
> (a)  Add new Entry
> (v)  View all Entries
> (s)  General Search
> (si) Search by Initials
> (sn) Search by Name
>
> > q
> Traceback (most recent call last):
>  File "c:\P32Working\Pickles\nicks_simple_phone_book_app.py", line
> 171, in 
>main_loop()
>  File "c:\P32Working\Pickles\nicks_simple_phone_book_app.py", line
> 94, in main_loop
>> """)
>  File "", line 1, in 
> NameError: name 'q' is not defined
> Process terminated with an exit code of 1
> 
>

My guess is that this has something to do with Python 3.x not having
raw_input.  Try changing the raw_input calls to input.  I don't get that
error with 2.7.



>
> > If you're talking about re-entering all your data from your file, you
> would
> > write a script to do that.
>
> Ha! I would?
>
>
Well, yeah.


A line in the file always begins with a name, with  at
> least one phone number, then if a company, the hours they are
> reachable by phone, possibly their web address, maybe an email
> address, my contact there plus maybe her secretary. If a physician,
> there might very well be his specialty, his nurse's name, mention of
> who recommended him to me, etc.


Like I said, I'd have to see the file to comment more, but it sounds like it
may be too irregular to make writing a script an easy process.  Though I
can't be sure without seeing it.  You could always enter these things by
hand...

I should probably ask; what is your goal for this app?  Is it to learn some
Python while making a usable app?  Or are you looking for a robust address
book for quick lookups of information?  If it's the latter, I'd recommend
you look around for something that's already made.  It will save you a lot
of trouble in the long run.

If you're looking for a good learning experience this kind of app is a great
starting place.  However expect to run into problems along the way, up to
and possibly beyond possibly hosing all the data you have entered into the
app.  Make regular backups of the data and keep your original around.


> It seems that the format of info for
> your way is set rigidly in advance, or am I wrong?
>
>
Not really.  Key/value pairs can be entered in arbitrary order.  I used = to
seperate key/values because that's what you used in your app and | to
seperate k/v pairs to allow spaces without quoting strings.  You could
easily replace these tokens (= or |) with whatever you want.

If you have another format that differs by more than those characters you
would need to write a different parser.  However there does have to be some
consistency to the format of your data to make parsing easier.  The less
rigid your data the more work you will have to do to parse it.  Since you
said earlier that you will probably have less than 100 entries in your phone
book, you may want to think about restructuring your data by hand to make it
easier to parse before writing your parser.

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


Re: [Tutor] databases

2011-09-10 Thread Alejandro Companioni

On Sep 10, 2011, at 9:29 PM, tutor-requ...@python.org wrote:

> I've never used MySql on a Mac but I'm curious why it should be so 
> difficult.
> 
> MacOS is just BSD Unix under the GUI so why would be any different
> to any other Unix type system? What were the problems that you encountered?

Hey Alan,

I had the same thoughts at first: OS X is just BSD! This can't be too different 
from a Linux installation, right?

There are a number of problems with mysql-python--chiefly its poor maintenance. 
I'll link to a nine (!) step guide on installing mysql-python on Mac as an 
example:

http://friendlybit.com/tutorial/install-mysql-python-on-mac-os-x-leopard/

At Step 9 the author suggests using setuptools even though it will fail, and 
you'd actually have to patch a (old, well-documented) bug yourself. 

I wish I had found that website sooner, as installing mysql-python on my Mac 
took about 5-6 hours of constant frustration. Not a good start for a new Python 
coder, but if at least one novice skips my experience after reading this email 
then I'll be happy.

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


Re: [Tutor] Tkinter Entry field text

2011-09-10 Thread brandon w

On 09/10/2011 10:16 AM, Wayne Werner wrote:
On Thu, Sep 8, 2011 at 9:31 PM, brandon w > wrote:


How do you display text in a Entry field and have it disappear
when a person clicks in it?
 To get text into this box the person must first
delete what is already in there.

Python 2.6.6


 Think about the process. You're already mostly there: you're 
displaying data already, and you know what you want to do.


You'll want to take a look at binding events, and if you Google for 
"tkinter events" (http://www.google.com/search?q=tkinter+events) then 
your first result takes you here: 
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm


To point you in the right direction, you want to take a look at the 
 event. If you're not familiar with what a callback is, you 
should also read this page: http://effbot.org/zone/tkinter-callbacks.htm


If you still have problems after that, show us what else you've tried 
and we'll be happy to give you more pointers in the right direction.




Of course, while this answers your questions, I would be remiss if I 
didn't suggest a few more things about your program in general.


> label1 = Label(root, text="Enter you password: ")

label1 isn't a terribly descriptive name. That's fine if you don't 
intend to actually do anything with the label. However, you can make 
this even more explicit by chaining the commands together. Since 
Label() returns a label, you can add a dot to the end and treat it 
just like you would the variable:


Label(root, text="Enter you password: ").grid(sticky=W, row=0, column=0)

That will create your label and stick it in the grid in one step, and 
makes it clear to anyone reading your code (including yourself down 
the road!) that you don't care to do anything with that label.


Next:

> enter_data1 = Entry(root, bg = "pale green")

enter_data1 also suffers from the same naming problem. It doesn't 
describe what the variable is or does very well, aside from entering 
data. You could change it to something like "password_entry" - which 
tells anyone reading your program that the variable should contain 
something that lets you do some password entry. Just naming it 
password would also be better than enter_data1.


One other issue that you should consider - with the options you have 
set, anyone could see what you typed in as your password. If you're 
just using this as a testing program to play around with, that's 
probably OK, but what's even better is to change what's shown in the 
box. You can do this by setting the "show" option, either in the 
constructor or somewhere later:


from Tkinter import *

root = Tk()
entry = Entry(root)
entry.pack()
entry.insert(0, "My Cool Password")
entry.config(show="*")
root.mainloop()

The nice thing about the config() method is that it allows you to 
change config attributes later. You can combine this knowledge with 
some of what I mentioned earlier to show 'password' until they 
navigate to the field, and then just show asterisks.


For bonus points, if they leave the password field without typing a 
password, can you make it show 'password' again?


HTH,
Wayne

Wayne,

Your advice was extremely helpful. I pointed me into the right 
direction. I got the code to work after some time. I know how to make it 
show  the "password" again. I would have to do this:


from Tkinter import *

root = Tk()
root.title("Show me your Password")
root.geometry("375x100+600+250")
root.grid()

def callback(event):
passwd_entry.delete(0, END) # It would come back because there is 
no .insert() function.

return

[snip...]

#=

Here is the final working code:

#!/usr/bin/python

from Tkinter import *

root = Tk()
root.title("Show me your Password")
root.geometry("375x100+600+250")
root.grid()

def callback(event):
field0 = varText1.get()
passwd_entry.delete(0, END)
passwd_entry.insert(0, field0)
return

def showPasswd():
field1 = varText.get()
passwd_display.delete(0, END)
passwd_display.insert(0, field1)
return

Label(root, text="Type your password in this box: ").grid(sticky=W, 
row=0, column=0)


Label(root, text="Your password is:").grid(sticky=W, row=2, column=0)

varText = StringVar()
varText.set("Enter password")
passwd_entry = Entry(root, textvariable=varText, bg = "pale green")
passwd_entry.bind("", callback)
passwd_entry.config(show="*")
passwd_entry.grid(row=0, column=1)

varText1 = StringVar(None)
passwd_display = Entry(root, textvariable=varText1, bg = "pale green")
passwd_display.grid(row=2, column=1)

Button(root, text="Show Password", width=10, 
command=showPasswd).grid(sticky=W, row=1, column=0)



root.mainloop()


Thanks a lot for your help!


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