Re: [Tutor] looking for a Python feature for computer teaching

2014-12-15 Thread Danny Yoo
On Dec 14, 2014 3:35 PM, "Pi Po"  wrote:
>
> As a teacher I find python simple and effective.
>
> However, appreciate feedback from anyone who knows
> of a Python version with this feature:
>
> Want each interpreted line of code to introduce as a cells on
> a spreadsheet window each new variable (or array) with its initialized
> value,
> and show the updated contents of each previously defined variable (or
> array).
>

Have you seen the visualization provided by
http://pythontutor.com/visualize.html#mode=edit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] about multiprocessing performance

2014-12-15 Thread Luis San Martin
I made a silly mistake as Jerry points its working above the same performance.

Kind regards

On Fri, Dec 12, 2014 at 8:27 PM, Alan Gauld  wrote:
> On 12/12/14 14:20, Luis San Martin wrote:
>>
>> Dear fellows,
>>
>> I'm learning about on multiprocessing module on python. So far I've
>> enjoyed
>> it though regarding performance I got some doubts. There is not that much
>> difference[0] when running it on Mac OS X on the contrary to Linux.
>
>
> They are both Unix based although different variants.
> Why would you expect much difference?
> Or are you just informing us of your findings?
>
> I'm not sure if you have a question?
> And if so what is it you want to know?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Is there an easily or shorter way?

2014-12-15 Thread Ken G.

I am sure there is a better way to refine the following lines.

Letting x equal a number from 1 to 28, go through 28 separate 'if'
statements to print a resulting value that equaled the value of x.

For example:

x = 8

if x = 1, print 'one'
if x = 2, print 'two'
...
...
if x = 8, print 'eight'
...
...
if x = 28, print 'twenty eight'

Would a single line using list or dictionary be shorter?

Thanks,

Ken



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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Danny Yoo
On Mon, Dec 15, 2014 at 1:25 PM, Ken G.  wrote:
> I am sure there is a better way to refine the following lines.
>
> Letting x equal a number from 1 to 28, go through 28 separate 'if'
> statements to print a resulting value that equaled the value of x.


Yes, the repetitive nature of those statements indicate that putting
the varying part in a data structure, like a list or dictionary, would
be greatly preferable.


As a side note: if we were to talk about how we'd do this in a
professional context, I think we'd recommend a library such as
"humanize", which has functions to go from numbers to human-friendly
string descriptions.

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Danny Yoo
> As a side note: if we were to talk about how we'd do this in a
> professional context, I think we'd recommend a library such as
> "humanize", which has functions to go from numbers to human-friendly
> string descriptions.
>
> https://pypi.python.org/pypi/humanize


Whoops: wrong library.  Humanize is a good one, but not exactly the
one I was supposed to cite.  I should have cited num2words, which is a
cardinal number library:

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Bod Soutar
On 15 December 2014 at 21:25, Ken G.  wrote:
> I am sure there is a better way to refine the following lines.
>
> Letting x equal a number from 1 to 28, go through 28 separate 'if'
> statements to print a resulting value that equaled the value of x.
>
> For example:
>
> x = 8
>
> if x = 1, print 'one'
> if x = 2, print 'two'
> ...
> ...
> if x = 8, print 'eight'
> ...
> ...
> if x = 28, print 'twenty eight'
>
> Would a single line using list or dictionary be shorter?
>
> Thanks,
>
> Ken
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


If you wanted to do this without additional libraries, consider how
many *unique* words are actually required if you were to write them
all out. You could use a dictionary to map the numbers to their words

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Steven D'Aprano
On Mon, Dec 15, 2014 at 04:25:42PM -0500, Ken G. wrote:
> I am sure there is a better way to refine the following lines.
> 
> Letting x equal a number from 1 to 28, go through 28 separate 'if'
> statements to print a resulting value that equaled the value of x.

Since you only care about the first 28 values, a list or dict is the way 
to go. The two look remarkably similar:

# Using a dict. Replace the dots ... with the rest of the values.
names = {1: "one", 2: "two", 3: "three", ... 28: "twenty-eight"}
print(names[x])

# Using a list. Again, replace the dots.
names = ["zero", "one", "two", "three", ... "twenty-eight"]
print(names[x])


I stress that neither version *quite* works yet. You have to replace the 
dots ... with the rest of the values, which is tedious but not hard.

In the case of the list version, the reason that I add an entry for zero 
is that lists are indexed from zero. That is, given the list:

L = ['spam', 'eggs', 'cheese', 'toast']

the first entry is written L[0], the second entry L[1] and so forth. 
Although it takes a bit of getting used to, there actually are good 
reasons for that. One advantage of dicts over lists is that you can 
leave gaps while a list must have placeholders for every position:

{2: "two", 4: "four", 8: "eight"}
["", "", "two", "", "four", "", "", "", "eight"]


Of course, as Danny suggested, if you're going to be using this 
seriously for arbitrary numbers, you can't possibly list every single 
one in advance. What if somebody asks for the name of 9274810276523? In 
that case, we need a function that turns a number into an name digit by 
digit:

nine trillion, two hundred and seventy-four billion, 
eight hundred and ten million, two hundred and 
seventy-six thousand, five hundred and twenty-three

Doing this makes a nice little programming exercise, so I will leave it 
to you :-)


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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Dave Angel

On 12/15/2014 04:25 PM, Ken G. wrote:

I am sure there is a better way to refine the following lines.

Letting x equal a number from 1 to 28, go through 28 separate 'if'
statements to print a resulting value that equaled the value of x.

For example:

x = 8

if x = 1, print 'one'
if x = 2, print 'two'
...
...
if x = 8, print 'eight'
...
...
if x = 28, print 'twenty eight'

Would a single line using list or dictionary be shorter?



If this is an assignment, and you quoted it correctly, then you're 
required to go through 28 if statements.  On the other hand, if that 
paragraph is just a description of the way you solved it, then yes, it 
can be improved.  Just making all but the first if statement an elif 
will make it faster, because once it finds a value, it won't continue 
checking the remaining ones.


Still faster would be testing first for fourteen, and making a tree out 
of the if statements, using > and < comparisons instead of only == 
comparisons.  Worst case would be about 5 tests.  This would not be more 
compact, just quicker to execute.


Faster yet, and somewhat more compact would be to make a tuple or list 
of 29 items (0 through 28), and just index into it.


Slower, but more compact, would be to write the kind of library that 
Danny pointed you to, or the code that Ken/Bod alluded to.


But the real question is what's your goal.   Your original code isn't 
legal Python, so you're presumably in a learning mode.  If so, you want 
to keep it simple, not use 3rd party libraries for something you could 
do yourself.  And when you're given an assignment, you should do it 
exactly the way they want it, and only after that's correct (and 
running), do it also in other ways.  Those other ways could be to 
improve performance, reduce size, make it more readable (or less, to 
enter it in obfuscation contests), to make it independent of any loaded 
libraries, to look like some sample in another language, ...



My second suggestion, untested:
if x < 14:
if x < 7:
 if x < 3:
 if x == 1:
   print ("one")
 else:
   print ("two")
 elif x < 5:
 if x == 3:
   print ("three")
 else:
   print ("four")




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


Re: [Tutor] looking for a Python feature for computer teaching

2014-12-15 Thread Steven D'Aprano
On Mon, Dec 15, 2014 at 11:12:04AM +1100, Steven D'Aprano wrote:
> On Sun, Dec 14, 2014 at 11:29:08AM -0800, Pi Po wrote:
> 
> > Want each interpreted line of code to introduce as a cells on
> > a spreadsheet window each new variable (or array) with its initialized
> > value,
> > and show the updated contents of each previously defined variable (or
> > array).
> 
> 
> I haven't used any of these, but you can try them and see if they do 
> what you want:
> 
> http://manns.github.io/pyspread/

A better link to pyspread is here:

https://pypi.python.org/pypi/pyspread

Coincidentally a new release has just come out.


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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Dave Angel

On 12/15/2014 05:49 PM, Steven D'Aprano wrote:

On Mon, Dec 15, 2014 at 04:25:42PM -0500, Ken G. wrote:

I am sure there is a better way to refine the following lines.

Letting x equal a number from 1 to 28, go through 28 separate 'if'
statements to print a resulting value that equaled the value of x.





Of course, as Danny suggested, if you're going to be using this
seriously for arbitrary numbers, you can't possibly list every single
one in advance. What if somebody asks for the name of 9274810276523? In
that case, we need a function that turns a number into an name digit by
digit:




 nine trillion, two hundred and seventy-four billion,
 eight hundred and ten million, two hundred and
 seventy-six thousand, five hundred and twenty-three


That's using the American interpretation for billion and trillion, not 
the British one.




Doing this makes a nice little programming exercise, so I will leave it
to you :-)


"one, two, three, many"

We only need four values, in some societies...;-)



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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Steven D'Aprano
On Mon, Dec 15, 2014 at 06:04:17PM -0500, Dave Angel wrote:
> On 12/15/2014 05:49 PM, Steven D'Aprano wrote:

> > nine trillion, two hundred and seventy-four billion,
> > eight hundred and ten million, two hundred and
> > seventy-six thousand, five hundred and twenty-three
> 
> That's using the American interpretation for billion and trillion, not 
> the British one.

Even the British use the American interpretation of billion these days. 
Mostly. There are probably still a few hold-outs, but I expect that 
"X-ion" meaning powers of 1000 has pretty much won out.


> >Doing this makes a nice little programming exercise, so I will leave it
> >to you :-)
> 
> "one, two, three, many"
> 
> We only need four values, in some societies...;-)

Okay, I'm up to this challenge...

9274810276523 in base 4: "2,012,331,311,301,121,222,223"

two sextilots,
onemany-two quinlots,
three manymany and threemany-one quadlots,
three manymany and onemany-one trilots,
three manymany and one bilots,
one manymany and twomany-one milots,
two manymany and twomany-two lots,
two manymany and twomany-three



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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Ken G.


On 12/15/2014 04:45 PM, Danny Yoo wrote:

As a side note: if we were to talk about how we'd do this in a
professional context, I think we'd recommend a library such as
"humanize", which has functions to go from numbers to human-friendly
string descriptions.

 https://pypi.python.org/pypi/humanize


Whoops: wrong library.  Humanize is a good one, but not exactly the
one I was supposed to cite.  I should have cited num2words, which is a
cardinal number library:

 https://pypi.python.org/pypi/num2words
.

Thank you but actually whatever number I get from either 1 to 28,
each number represent a property name such as "Reading Railroad",
"Judy Avenue", "Pacific Gas and Electric", etc., etc.

For example:

if x = 1 then print "Mediterranean Avenue"
if x = 2 then print "Baltic Avenue"
...
...
if x = 28 then print "Boardwalk"

Yes, I am using the property names from the game, Monopoly. I am
using them in conjunction with of playing a lottery game and using
Python to determine if I won anything from the numbers drawn and
being compared with what numbers I purchased.

Of course, the proper format in Python would be:

if x == "01":
print "Mediterranean Avenue"
...
...
if x == 28:
print "Boardwalk"

Again, thanks for your input.

Ken

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Ken G.


On 12/15/2014 05:49 PM, Steven D'Aprano wrote:

On Mon, Dec 15, 2014 at 04:25:42PM -0500, Ken G. wrote:

I am sure there is a better way to refine the following lines.

Letting x equal a number from 1 to 28, go through 28 separate 'if'
statements to print a resulting value that equaled the value of x.

Since you only care about the first 28 values, a list or dict is the way
to go. The two look remarkably similar:

# Using a dict. Replace the dots ... with the rest of the values.
names = {1: "one", 2: "two", 3: "three", ... 28: "twenty-eight"}
print(names[x])

# Using a list. Again, replace the dots.
names = ["zero", "one", "two", "three", ... "twenty-eight"]
print(names[x])


I stress that neither version *quite* works yet. You have to replace the
dots ... with the rest of the values, which is tedious but not hard.

In the case of the list version, the reason that I add an entry for zero
is that lists are indexed from zero. That is, given the list:

 L = ['spam', 'eggs', 'cheese', 'toast']

the first entry is written L[0], the second entry L[1] and so forth.
Although it takes a bit of getting used to, there actually are good
reasons for that. One advantage of dicts over lists is that you can
leave gaps while a list must have placeholders for every position:

 {2: "two", 4: "four", 8: "eight"}
 ["", "", "two", "", "four", "", "", "", "eight"]


Of course, as Danny suggested, if you're going to be using this
seriously for arbitrary numbers, you can't possibly list every single
one in advance. What if somebody asks for the name of 9274810276523? In
that case, we need a function that turns a number into an name digit by
digit:

 nine trillion, two hundred and seventy-four billion,
 eight hundred and ten million, two hundred and
 seventy-six thousand, five hundred and twenty-three

Doing this makes a nice little programming exercise, so I will leave it
to you :-)



Yes, Steven, it would be a 'nice' programming exercise. If I could just
find the time...sighs.

Thanks,

Ken

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Danny Yoo
>
> Thank you but actually whatever number I get from either 1 to 28,
> each number represent a property name such as "Reading Railroad",
> "Judy Avenue", "Pacific Gas and Electric", etc., etc.
>
> For example:
>
> if x = 1 then print "Mediterranean Avenue"
> if x = 2 then print "Baltic Avenue"


Ah, cool!  Ok, then yes, definitely a list.  There's a sequential-ness
here that we should take advantage of.

   PLACES = ["Is-there-a-place-when-x-is-zero?",
  "Mediterranean Avenue",
  "Baltic Avenue",
  ]# and so on ...

   print PLACES[x]


so that all the conditioning dissolves into a single list lookup.
There might need to be a few more checks to make sure x is in bounds,
but it's worth it here.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Ken G.


On 12/15/2014 05:59 PM, Dave Angel wrote:

On 12/15/2014 04:25 PM, Ken G. wrote:

I am sure there is a better way to refine the following lines.

Letting x equal a number from 1 to 28, go through 28 separate 'if'
statements to print a resulting value that equaled the value of x.

For example:

x = 8

if x = 1, print 'one'
if x = 2, print 'two'
...
...
if x = 8, print 'eight'
...
...
if x = 28, print 'twenty eight'

Would a single line using list or dictionary be shorter?



If this is an assignment, and you quoted it correctly, then you're 
required to go through 28 if statements.  On the other hand, if that 
paragraph is just a description of the way you solved it, then yes, it 
can be improved.  Just making all but the first if statement an elif 
will make it faster, because once it finds a value, it won't continue 
checking the remaining ones.


Still faster would be testing first for fourteen, and making a tree 
out of the if statements, using > and < comparisons instead of only == 
comparisons.  Worst case would be about 5 tests.  This would not be 
more compact, just quicker to execute.


Faster yet, and somewhat more compact would be to make a tuple or list 
of 29 items (0 through 28), and just index into it.


Slower, but more compact, would be to write the kind of library that 
Danny pointed you to, or the code that Ken/Bod alluded to.


But the real question is what's your goal.   Your original code isn't 
legal Python, so you're presumably in a learning mode.  If so, you 
want to keep it simple, not use 3rd party libraries for something you 
could do yourself.  And when you're given an assignment, you should do 
it exactly the way they want it, and only after that's correct (and 
running), do it also in other ways.  Those other ways could be to 
improve performance, reduce size, make it more readable (or less, to 
enter it in obfuscation contests), to make it independent of any 
loaded libraries, to look like some sample in another language, ...



My second suggestion, untested:
if x < 14:
if x < 7:
 if x < 3:
 if x == 1:
   print ("one")
 else:
   print ("two")
 elif x < 5:
 if x == 3:
   print ("three")
 else:
   print ("four")





Oh, it is not an assignment, Dave. It is an actual program I am using
for my benefit. I had to figure out on a fly, what is the name of a piece
of property that each number represent. I had to reckon something out
within a limited amount of time I had. Giving the response given here
so far, I could go with a list, dictionary or tuple.

Thanks for your input.

Ken

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


Re: [Tutor] Is there an easily or shorter way?

2014-12-15 Thread Dave Angel

On 12/15/2014 07:55 PM, Ken G. wrote:


Oh, it is not an assignment, Dave. It is an actual program I am using
for my benefit. I had to figure out on a fly, what is the name of a piece
of property that each number represent. I had to reckon something out
within a limited amount of time I had. Giving the response given here
so far, I could go with a list, dictionary or tuple.



Two other advantages with using a tuple, list or dictionary

1) is that you might someday need to do the reverse lookup, changing a 
string back into the integer.  If you do it from the same data 
structure, you're more likely to get the transformation to be completely 
reversible (for valid values).


2) You might need to load this table from a file, or otherwise generate 
or check it on the fly.  Tough to do that with separate function statements.


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