nathan virgil | 1 Jan 2009 04:51
Picon

Re: Creating sub-menus?

Okay, so I changed line 84 from:

current_menu

to:

choice = current_menu, and the menu prtion seems to work fine. Only problem now is, once I select a formula, it repeats that function over and over again, never going back to a menu. Should I just add something like:

choice = current_menu

to each formula function?

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
ALAN GAULD | 1 Jan 2009 11:58

Fw: Creating sub-menus?

Forwarding to the list....
Please use ReplyAll when responding.


On Wed, Dec 31, 2008 at 4:10 AM, Alan Gauld <alan.gauld <at> btinternet.com> wrote:
"nathan virgil" <sdragon1984 <at> gmail.com> wrote


Each menu is a function that prints out options, saves a raw_input as the
variable choice, and returns choice. In the main menu, each option leads to
a sub-menu. After choice is defined, however, the sub-menu "tags" the value
of choice.

Yes that can all work.


Then create a loop of while choice !=q, run current_menu, and include a
bunch of statements along the lines of:

if choice == <value that leads to first sub-menu>:
      current_menu = <function name for first sub-menu>

Consider using a dictionary keyed by your combined choice values.
Then the big if/elif chain shrinks to

returnValue = FuncDict[choice](params)

The only challenge with this route is making all the functions
take a single input argument. But that argument can be a tuple :-)

Dictionaries? Tuples? I just figured out functions, so I'm very new. I'm still working on understanding lists. I know what I have probably isn't the best solution for what I'm trying to do, but I'm trying to work with the little that I know.


This seems like it would work, but for some reason, every time I run the
code, it freezes after I give input from the main menu. Can anybody help? I
can show my source code, but indentation doesn't seem to copy/paste very
well, so it may be a bit hard to read...

Try putting it on the pastebin web site and sending us the URL.
That gives us colour coding of syntax too which helps read it!


http://pastebin.com/m252eea7a

It's a simple formula calculator, so the point isn't really the content. I don't have all the formulas coded in yet, but the way I see it, if I can't access the sub-menus that lead to the formulas, then it doesn't do me any good to have the formulas.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
spir | 1 Jan 2009 13:57
Picon
Favicon

Re: Fw: Creating sub-menus?

On Thu, 1 Jan 2009 10:58:44 +0000 (GMT)
ALAN GAULD <alan.gauld <at> btinternet.com> wrote:

> Forwarding to the list....
> Please use ReplyAll when responding.
> 
> 
> 
> 
> On Wed, Dec 31, 2008 at 4:10 AM, Alan Gauld <alan.gauld <at> btinternet.com> wrote:
> 
> "nathan virgil" <sdragon1984 <at> gmail.com> wrote
> 
> 
> 
> Each menu is a function that prints out options, saves a raw_input as the
> variable choice, and returns choice. In the main menu, each option leads to
> a sub-menu. After choice is defined, however, the sub-menu "tags" the value
> of choice.
> 
> 
> Yes that can all work.
> 
> 
> 
> Then create a loop of while choice !=q, run current_menu, and include a
> bunch of statements along the lines of:
> 
> if choice == <value that leads to first sub-menu>:
>       current_menu = <function name for first sub-menu>
> 
> 
> Consider using a dictionary keyed by your combined choice values.
> Then the big if/elif chain shrinks to
> 
> returnValue = FuncDict[choice](params)
> 
> The only challenge with this route is making all the functions
> take a single input argument. But that argument can be a tuple :-)
> 
> Dictionaries? Tuples? I just figured out functions, so I'm very new. I'm still working on
> understanding lists. I know what I have probably isn't the best solution for what I'm trying to
> do, but I'm trying to work with the little that I know. 
> 
> 
> 
> 
> This seems like it would work, but for some reason, every time I run the
> code, it freezes after I give input from the main menu. Can anybody help? I
> can show my source code, but indentation doesn't seem to copy/paste very
> well, so it may be a bit hard to read...
> 
> 
> Try putting it on the pastebin web site and sending us the URL.
> That gives us colour coding of syntax too which helps read it!
> 
> 
> http://pastebin.com/m252eea7a

Below a copy of the menu control loop of your code, with some corrections.
denis

#In development
choice = "start"			# rather use None or "" as not-yet-setvalue
current_menu = main_menu()		# current_menu = main_menu : see below
while choice != "q":
      current_menu			# current_menu() : it's a call
      #Main Menu results
      if choice == "1":
         current_menu = temp_conv_menu()# idem: rather no '()' for consistency
      elif choice == "2":
           current_menu = area_menu()
      elif choice == "3":
           current_menu = perim_menu()
	### inside sub-menus, you simply forget to process "back to main menu" choices
	### in all active branches below, you also forget to go back up a menu level
	###	after execution of the terminal choice
	## so that, once reached, the user is stick in a terminal branch
      elif choice == "t1":
         temp = input("Celsius temperature: ")
         print "Fahrenheit:", celsius_to_fahrenheit(temp)
		* current_menu = temp_conv_menu / main_menu
		* [possibly with "x = raw_input("ok?")" before]
      elif choice == "t2":
        temp = input("Fahrenheit temperature: ")
        print "Celsius:", fahrenheit_to_celsius(temp)
	* elif "t3":
		* current_menu = main_menu
      elif choice =="a1":
         s = input("Width:")
         print "Square area:", area_s(s)
      elif choice =="a2":
         w = input("Width:")
         h = input("Height:")
         print "Rectangle area:", area_r(w, h)
      elif choice =="a3":
          none
      elif choice =="a4":
         r=input("Radius of circle: ")
         print "Circle area:", area_c(r)

In the sub-menus, I would also give a distinctive key for "back to main menu" (eg 'b' or 'm')
rather than a number. [all comments / changes / additions untested]

------
la vida e estranya
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Christopher Mutel | 1 Jan 2009 15:07
Picon
Gravatar

Distinction between tuples and lists

Hello all-

I stumbled across some discussion of why the fundamental difference
between lists and tuples is not mutability, but hetero- versus
homogeneous data, e.g.

http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/

http://pyre.third-bit.com/blog/archives/000450.html

However, after reading the cited discussions, my python books, etc., I
have to admit I don't really understand this idea. What does it mean
that "lists are intended for homogeneous sequences"? What is different
about lists that would make them more suited for homogeneous sequences
than heterogeneous sequences (or vice-versa for tuples)? In the end,
from what I understand, the idea of homo/heterogeneity seems
orthogonal to mutability, which is the main distinction emphasized by
e.g. Learning Python.

I would greatly appreciate any help provided,

-Chris Mutel
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 1 Jan 2009 15:34
Gravatar

Top posters to tutor list for 2008

For several years I have been using a simple script to find the top 20
posters to the tutor list by web-scraping the archive pages. I thought
others might be interested so here is the list for 2008 and the script
that generates it. The lists for previous years (back to 2003) are at
the end so everyone on the list doesn't hit the archives to find out
:-)

The script gives a simple example of datetime, urllib2 and
BeautifulSoup. It consolidates names that vary by case but other
variations are not detected.

Alan, I thought you might have passed me this year but we are both off
a little :-) Somehow I have posted an average of 2.8 times per day for
the last four years...

Happy New Year everyone!

Kent

2008
====
Kent Johnson 931
Alan Gauld 820
bob gailer 247
Dick Moores 191
W W 142
Wayne Watson 106
John Fouhy 97
Steve Willoughby 91
Lie Ryan 88
bhaaluu 85
Marc Tompkins 83
Michael Langford 71
Tiger12506 70
Andreas Kostyrka 64
Dinesh B Vadhia 64
wesley chun 58
Tim Golden 57
Chris Fuller 54
Ricardo Ar&#225;oz 53
spir 53

#####################################

''' Counts all posts to Python-tutor by author'''
# -*- coding: latin-1 -*-
from datetime import date, timedelta
import operator, urllib2
from BeautifulSoup import BeautifulSoup

today = date.today()

for year in [2008]:
    startDate = date(year, 1, 1)
    endDate = date(year, 12, 31)
    thirtyOne = timedelta(days=31)
    counts = {}

    # Collect all the counts for a year by scraping the monthly author
archive pages
    while startDate < endDate and startDate < today:
        dateString = startDate.strftime('%Y-%B')

        url = 'http://mail.python.org/pipermail/tutor/%s/author.html'
% dateString
        data = urllib2.urlopen(url).read()
        soup = BeautifulSoup(data)

        li = soup.findAll('li')[2:-2]

        for l in li:
            name = l.i.string.strip()
            counts[name] = counts.get(name, 0) + 1

        startDate += thirtyOne

    # Consolidate names that vary by case under the most popular spelling
    nameMap = dict() # Map lower-case name to most popular name
    for name, count in sorted(counts.iteritems(),
key=operator.itemgetter(1), reverse=True):
       lower = name.lower()
       if lower in nameMap:
          # Add counts for a name we have seen already
          counts[nameMap[lower]] += count
       else:
          nameMap[lower] = name

    print
    print year
    print '===='
    for name, count in sorted(counts.iteritems(),
key=operator.itemgetter(1), reverse=True)[:20]:
        print name.encode('latin-1', 'xmlcharrefreplace'), count
    print

# Results as of 12/31/2008:
'''
2003
====
Danny Yoo 617
Alan Gauld 421
Jeff Shannon 283
Magnus Lycka 242
Bob Gailer 195
Magnus =?iso-8859-1?Q?Lyck=E5?= 166
alan.gauld <at> bt.com 161
Kirk Bailey 155
Gregor Lingl 152
Lloyd Kvam 142
Andrei 118
Sean 'Shaleh' Perry 117
Magnus Lyckå 113
Michael Janssen 113
Erik Price 100
Lee Harr 88
Terry Carroll 87
Daniel Ehrenberg 78
Abel Daniel 76
Charlie Clark 74

2004
====
Alan Gauld 699
Danny Yoo 530
Kent Johnson 451
Lloyd Kvam 146
Dick Moores 145
Liam Clarke 140
Brian van den Broek 122
Karl Pfl&#228;sterer 109
Jacob S. 101
Andrei 99
Chad Crabtree 93
Bob Gailer 91
Magnus Lycka 91
Terry Carroll 88
Marilyn Davis 84
Gregor Lingl 73
Dave S 73
Bill Mill 71
Isr Gish 71
Lee Harr 67

2005
====
Kent Johnson 1189
Danny Yoo 767
Alan Gauld 565
Alan G 317
Liam Clarke 298
Max Noel 203
Nathan Pinno 197
Brian van den Broek 190
Jacob S. 154
jfouhy at paradise.net.nz 135
Alberto Troiano 128
Bernard Lebel 119
Joseph Quigley 101
Terry Carroll 93
Andrei 79
D. Hartley 77
John Fouhy 73
bob 73
Hugo Gonz&#225;lez Monteverde 72
Orri Ganel 69

2006
====
Kent Johnson 913
Alan Gauld 815
Danny Yoo 448
Luke Paireepinart 242
John Fouhy 187
Chris Hengge 166
Bob Gailer 134
Dick Moores 129
Asrarahmed Kadri 119
Terry Carroll 111
Python 94
Mike Hansen 74
Liam Clarke 72
Carroll, Barry 67
Kermit Rose 66
anil maran 66
Hugo Gonz&#225;lez Monteverde 65
wesley chun 63
Christopher Spears 53
Michael Lange 51

2007
====
Kent Johnson 1052
Alan Gauld 938
Luke Paireepinart 260
Dick Moores 203
Eric Brunson 164
Terry Carroll 128
Tiger12506 112
John Fouhy 105
Bob Gailer 97
Ricardo Ar&#225;oz 93
Rikard Bosnjakovic 93
bhaaluu 88
elis aeris 83
Andreas Kostyrka 77
Michael Langford 68
shawn bright 63
Tim Golden 62
Dave Kuhlman 62
wormwood_3 53
wesley chun 53

2008
====
Kent Johnson 931
Alan Gauld 820
bob gailer 247
Dick Moores 191
W W 142
Wayne Watson 106
John Fouhy 97
Steve Willoughby 91
Lie Ryan 88
bhaaluu 85
Marc Tompkins 83
Michael Langford 71
Tiger12506 70
Andreas Kostyrka 64
Dinesh B Vadhia 64
wesley chun 58
Tim Golden 57
Chris Fuller 54
Ricardo Ar&#225;oz 53
spir 53

'''
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

jadrifter | 1 Jan 2009 15:43
Picon

Re: Distinction between tuples and lists

On Thu, 2009-01-01 at 15:07 +0100, Christopher Mutel wrote:
> Hello all-
> 
> I stumbled across some discussion of why the fundamental difference
> between lists and tuples is not mutability, but hetero- versus
> homogeneous data, e.g.
> 
> http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
> 
> http://pyre.third-bit.com/blog/archives/000450.html
> 
> However, after reading the cited discussions, my python books, etc., I
> have to admit I don't really understand this idea. What does it mean
> that "lists are intended for homogeneous sequences"? What is different
> about lists that would make them more suited for homogeneous sequences
> than heterogeneous sequences (or vice-versa for tuples)? In the end,
> from what I understand, the idea of homo/heterogeneity seems
> orthogonal to mutability, which is the main distinction emphasized by
> e.g. Learning Python.
> 
> I would greatly appreciate any help provided,
> 
> -Chris Mutel
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> http://mail.python.org/mailman/listinfo/tutor

I read it and don't get their emphasis or perhaps level of concern.  As
explanatory pieces these two blogs aren't very helpful.  They provide a
few assertions with no apparent (to me) attempt to support or explain
them.  

Both data types are indexed and both can contain homogeneous (same as)
or heterogeneous (different than) data.  I get that lists are analogous
to a C linked lists as tuples are to C structs.  I get that the
flexibility of one and the stability of the other makes them useful in
different situations.  Being able to use struct notation (employee.age
instead of employee[4]) would be nice but also not that difficult to
implement as a class either.

On re-re-re-reading it (I hate it when I don't get the point) I think
they're arguing for a more structured "You must use this properly"
approach to be built into the language.  And that makes some sense.  Who
would want a database call (say to an employee database table) that
returned  a record as a list?  On the other hand who would want a record
set (group of records) returned as a tuple?  There's no reason to assume
the third record will always be that of John Smith but the 3rd field of
John Smiths's record better be his phone number and not his social
security number or else wackiness will ensue.

Still, aint it great to work with a language that lets us bounce back
and forth between structures as we please?

John Purser

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

jadrifter | 1 Jan 2009 16:23
Picon

Re: Top posters to tutor list for 2008

On Thu, 2009-01-01 at 09:34 -0500, Kent Johnson wrote:
> For several years I have been using a simple script to find the top 20
> posters to the tutor list by web-scraping the archive pages. I thought
> others might be interested so here is the list for 2008 and the script
> that generates it. The lists for previous years (back to 2003) are at
> the end so everyone on the list doesn't hit the archives to find out
> :-)
> 
> The script gives a simple example of datetime, urllib2 and
> BeautifulSoup. It consolidates names that vary by case but other
> variations are not detected.

Kent,

Thank you for this.  I've been thinking about a web scraping script but
didn't  have a clue how to go about it.  Seeing someone else's practical
implementation is a huge help!

A little serendipity to start 2009 off with.

Happy New Year to all.

John

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Andre Engels | 1 Jan 2009 18:33
Picon

Re: Distinction between tuples and lists

On Thu, Jan 1, 2009 at 3:07 PM, Christopher Mutel <cmutel <at> gmail.com> wrote:
> Hello all-
>
> I stumbled across some discussion of why the fundamental difference
> between lists and tuples is not mutability, but hetero- versus
> homogeneous data, e.g.
>
> http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
>
> http://pyre.third-bit.com/blog/archives/000450.html
>
> However, after reading the cited discussions, my python books, etc., I
> have to admit I don't really understand this idea. What does it mean
> that "lists are intended for homogeneous sequences"? What is different
> about lists that would make them more suited for homogeneous sequences
> than heterogeneous sequences (or vice-versa for tuples)? In the end,
> from what I understand, the idea of homo/heterogeneity seems
> orthogonal to mutability, which is the main distinction emphasized by
> e.g. Learning Python.
>
> I would greatly appreciate any help provided,

Lists do have all kinds of operations that tuples do not have. Many of
those will change the order of the elements in the list, for example
by adding or removing an element, or exchanging some elements. Because
of this, many of those operations are only useful on homogenous
sequences - which means that the second element could also be the
first or the fourth, and would then still mean the same thing, though
in another place.

On the other hand, if I have a heterogenous sequence, that is, if what
is in the second position could either not appear on the fourth
position (because the second is an integer and the fourth a string,
for example), or would mean something completely different there (for
example if the second and fourth were both floats, but the second was
a price in dollars and the fourth a mass in kilograms), then many of
those operations make no sense (in fact, the only one I think _would_
make sense for heterogenous sequences is changing the value of the
element at a specified position). Thus, if you have a heterogenous
sequence, all those nifty operations on lists have no use, and
dropping them for getting the niceties of immutability (like usage as
the key in a dictionary) is getting something for almost nothing, thus
you might as well use a tuple.

--
André Engels, andreengels <at> gmail.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Andre Engels | 1 Jan 2009 18:33
Picon

Re: Distinction between tuples and lists

On Thu, Jan 1, 2009 at 3:43 PM, jadrifter <jadrifter <at> gmail.com> wrote:

> Both data types are indexed and both can contain homogeneous (same as)
> or heterogeneous (different than) data.  I get that lists are analogous
> to a C linked lists as tuples are to C structs.  I get that the
> flexibility of one and the stability of the other makes them useful in
> different situations.  Being able to use struct notation (employee.age
> instead of employee[4]) would be nice but also not that difficult to
> implement as a class either.

The similar construct in Python to this struct notation would be to
use a dictionary rather than a tuple (or list); you could then use
employee['age'] etcetera.

--
André Engels, andreengels <at> gmail.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 1 Jan 2009 19:42
Gravatar

Re: Distinction between tuples and lists

On Thu, Jan 1, 2009 at 9:43 AM, jadrifter <jadrifter <at> gmail.com> wrote:
> Being able to use struct notation (employee.age
> instead of employee[4]) would be nice but also not that difficult to
> implement as a class either.

Python 2.6 added collections.namedtuple() which is a factory for
classes like this:
http://docs.python.org/dev/library/collections.html#collections.namedtuple

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane