Re: [Tutor] PyCountry currency formatting woes

2014-05-06 Thread Sithembewena Lloyd Dube
Thanks for this response, this is exactly what I needed to know.


On Mon, May 5, 2014 at 6:26 AM, Marc Tompkins wrote:

> On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube  > wrote:
>
>> Thanks, i was actually getting the error information to update the post.
>> Apoligies to waste your time posting here - I could not find an appropriate
>> PyCountry discussion list and my next best bet seemed to be a Python users'
>> list.
>>
>>
> You also posted on StackOverflow; I just answered you there.  In short:
> the currency numeric is not guaranteed to be the same as the country
> numeric (in the case of the Euro, how could it possibly be?)  The numeric
> for DE is 276; the numeric for the Euro is 978.  Obviously they don't match.
>
> PyCountry is a wrapper around some tables provided by Debian; those tables
> don't include a country/currency mapping.  You can find those mapping
> tables at
>  http://www.currency-iso.org/en/home/tables/table-a1.html
> and roll your own wrapper; I'm sure it's been done a thousand times
> before, but I'm not aware of a Python package that does this.
>



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


Re: [Tutor] Final review

2014-05-06 Thread Scott Dunning

On May 1, 2014, at 5:30 AM, Steven D'Aprano  wrote:
> 

Awesome, thanks everyone!  I understand lists a lot better now.  

I have another question.  I don’t understand why below would give an error?

>>> greeting = 'Hello World'
>>> greeting [len(greeting)]


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


Re: [Tutor] attributes vs globals (was Re: global list)

2014-05-06 Thread Albert-Jan Roskam



> From: Alan Gauld 
>To: tutor@python.org 
>Sent: Friday, April 25, 2014 12:15 AM
>Subject: Re: [Tutor] attributes vs globals (was Re: global list)
> 
>
>On 24/04/14 21:48, Mark Lawrence wrote:
>> On 24/04/2014 21:30, Albert-Jan Roskam wrote:
>>>
>>> As a side not, I find that variables (attributes) defined in __init__
>>> are also much like globals, but that's probably a different discussion.
>>
>> Would you please be kind enough to explain your logic.
>
>I can't speak for Albert but I do tend to agree that class
>(or instance) variables are a lot like globals. They are
>slightly better controlled because they are unique to an
>instance but they do result in code(methods) that are
>dependant on side effects. You are changing data that
>is neither passed into the method or returned by it.
>So its not obvious which instance attributes are being
>modified by which methods.
>
>Now OOP theory says that with information hiding you
>shouldn't care, so long as the external behaviour is
>the correct, but in Python its  common to directly
>access attributes. And if you inherit a class you
>often do care about its internal state.
>
>So instance variables carry many of the same negative
>characteristics that globals do, albeit confined to a
>single entity and a single, constrained, set of functions..


Hi all,

Sorry for the delayed reply, I was on holiday. Yes, Alan said what I meant with 
my original (somewhat vague) description.
The snippet below further illustrates this (orignally I had only pseudo_global2 
in mind, but the pseudo_global1 is another option). self.pseudo_global*'s value 
can be accessed everywhere, even though it is not an argument of the function 
or method.


class Foo(object):
    pseudo_global1 =  "I am all over the place, if I want to"
    def __init__(self):
    self.pseudo_global2 = "I am also all over the place, if I want to"
    print self.pseudo_global2
    def some_method(self):
    print self.pseudo_global2
    def some_function():
    print self.pseudo_global2
    some_function()
    print Foo.pseudo_global11

if __name__ == "__main__":
    Foo().some_method()


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


Re: [Tutor] Final review

2014-05-06 Thread Danny Yoo
On Mon, May 5, 2014 at 9:04 PM, Scott W Dunning  wrote:
>
> On May 1, 2014, at 5:30 AM, Steven D'Aprano  wrote:
>
> Awesome, thanks everyone!  I understand lists a lot better now.
>
> I have another question.  I don’t understand why below would give an error?
>
  greeting = 'Hello World’
  greeting [len(greeting)]


Hi Scott,

Question: what do you expect to see?

Also, you may want to try a smaller example that still demonstrates
the problem.  For example, consider the staccato greeting "hi", and
work through the scenario.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append vs list addition

2014-05-06 Thread Dave Angel
C Smith  Wrote in message:
> Sorry.
> 
> I meant for example:
> list1 = [1,2,3]
> list2 = [3,4,5]
> 
> newList = list1 + list2
> 
> versus
> 
> for x in list2:
> list1.append(x)
> 
> Which is the preferred way to add elements from one list to another?

Thank you for switching to text mail.

These examples still aren't equivalent.  But in any similar
 example,  if list2 is type list, then avoid the list. Use either
 extend, or the equivalent += . And if you aren't permitted to
 change list1, you should use += .

The time when append is preferred over + is when you HAVE to loop
 over the items,  like when they each need computing or when
 they're output from a generator. 



-- 
DaveA

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


Re: [Tutor] PyCountry currency formatting woes

2014-05-06 Thread Alan Gauld

On 04/05/14 21:25, Sithembewena Lloyd Dube wrote:


currency = pycountry.currencies.get(numeric=country.numeric)



Have you tried using locales?

import locale as loc
loc.setlocale(loc.LC_ALL,'')  # selects default - do this at start
...
print(loc.currency(myNum_here))

I don't know the module you reference but locale is the standard library 
option...


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

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


Re: [Tutor] append vs list addition

2014-05-06 Thread Denis Heidtmann
On Sun, May 4, 2014 at 6:44 PM, Dave Angel  wrote:
> C Smith  Wrote in message:
>> Sorry.
>>
>> I meant for example:
>> list1 = [1,2,3]
>> list2 = [3,4,5]
>>
>> newList = list1 + list2
>>
>> versus
>>
>> for x in list2:
>> list1.append(x)
>>
>> Which is the preferred way to add elements from one list to another?
>
> Thank you for switching to text mail.
>
> These examples still aren't equivalent.  But in any similar
>  example,  if list2 is type list, then avoid the list. Use either
>  extend, or the equivalent += . And if you aren't permitted to
>  change list1, you should use +=

> DaveA
>

Lurking here.  I am confused by "avoid the list".  What does that mean?
 Also, you say extend and += are equivalent, yet say " if you aren't
permitted to change list1, you should use +="  If they are equivalent,
why choose one over the other?  Doesn't += always change the left-hand
side?

Not being critical--just confused.

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


Re: [Tutor] Final review

2014-05-06 Thread Alan Gauld

On 06/05/14 04:43, Scott Dunning wrote:

I have another question.  I don’t understand why below would give an error?


greeting = 'Hello World'
greeting [len(greeting)]



Because list indexing starts at zero but len() returns the actual 
length. So the last element of a list is


mylist[len(mylist)-1]

or, more simply:

mylist[-1]

Aside:
len() does work with range() and slicing so you can write

myList[:len(mylist)]

to get a copy of your list...

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

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


Re: [Tutor] Keeping change-in-place vs. copy methods straight

2014-05-06 Thread Alan Gauld

On 04/05/14 13:54, Dave Angel wrote:

Alan Gauld  Wrote in message:



I assumed (never assume!) that it returned a reference to the original.
I really, really, hate the way Python handles this :-(


It's not clear to me what you would change. Would you only provide
  methods (like sort) that mangle their object?


No, I'd not provide methods that work on the object and return None.
I much prefer the Smalltalk model where if all else fails you return 
self. Since Python has objects everywhere they could easily have adopted 
that model, it is so much more consistent. And it also allows method 
chaining... But just the consistency of always getting a useful

object (I don't mean None!)  back makes a big difference to programming.

It does mean you need to make copy semantics obvious in the naming
but that's not too onerous. So sorted() would become sortedCopy() or 
some such.


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

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


Re: [Tutor] Alice_in_wonderland Question

2014-05-06 Thread Alan Gauld

On 05/05/14 04:13, Jake Blank wrote:


I did have one more question though.

import os
from wordtools import extract_words

source_filepath=input("Enter the path to the source file:")
dest_filepath =input("Enter the path to the destination file:")

I'm wondering how I can make it so the program can tell if the
source/dest_filepath the user entered is actually a program on the computer.


Do you really mean is it a program? - ie an executable file? Or do you 
just want to know if the file (any kind of file) exists?


There are ways to do what you want but you need to be much more specific.



Also i have to ask the user if they would like to "process another
file(Y/N)?" and I'm not sure where to put that.


You need a loop. It will look something like(pseudo code)

while True:
   you existing copy code here
   ask the user if they want to do more
   if they don't:
  break  # exit the loop

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

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


Re: [Tutor] PyCountry currency formatting woes

2014-05-06 Thread Peter Otten
Sithembewena Lloyd Dube wrote:

> Thanks, i was actually getting the error information to update the post.
> Apoligies to waste your time posting here - I could not find an
> appropriate PyCountry discussion list and my next best bet seemed to be a
> Python users' list.
> 
> For those who care to look, the error is as follows (a concise example
> from an interactive shell:
> 
> import pycountry
> country = pycountry.countries.get(alpha2='DE')
> currency = pycountry.currencies.get(numeric=country.numeric)
> Traceback (most recent call last):
> File "", line 1, in
> File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
> return self.indices[field][value]
> KeyError: '276'
> 
> The obvious issue here is that the pycountry.countries collection does not
> contain a currency with a numeric of 276 (Germany's numeric) - yet it does
> contain the Euro. Any ideas as to what the way around this may be?

It looks like the development version of babel

http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies

can do what you want:

$ LANG=en_US.UTF-8 python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import babel.numbers as bn
>>> bn.get_territory_currencies("DE")
['EUR']
>>> print bn.format_currency(1.234, "EUR")
€1.23
>>> print bn.format_currency(1.234, "EUR", locale="DE")
1,23 €
>>> import babel
>>> babel.__version__
'2.0-dev'

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


Re: [Tutor] Alice_in_wonderland Question

2014-05-06 Thread Peter Otten
Jake Blank wrote:

> I finally got it.
> This was the code:
> for k in sorted(word_count, key=lambda x:word_count[x], reverse=True):
> print (k, word_count[k])
> 
> The only question i have now is how to limit the amount of returns the
> program runs to the first 15 results.

Hint: you can make a list of the first N items of a list with

some_list = some_list[:N]

The list in question is

sorted(word_count, key=lambda x:word_count[x], reverse=True)

You might also try the heapq.nlargest() function:

>>> word_count
{'pewter': 12, 'rawboned': 2, 'accolade': 2, 'Messiah': 15, 'summoning': 17, 
'sleeking': 6, 'parse': 14, 'Freya': 8, 'chroniclers': 13, 'faith': 1}
>>> import heapq
>>> for word in heapq.nlargest(5, word_count, key=lambda w: word_count[w]):
... print(word, word_count[word])
... 
summoning 17
Messiah 15
parse 14
chroniclers 13
pewter 12

See also 

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


Re: [Tutor] sending email via SMTP: code review requested

2014-05-06 Thread Alan Gauld

On 05/05/14 18:53, Steven D'Aprano wrote:


And, as I side note, could anyone explain why changing a first world
of a body line 'From' to '>From' is the preferred standard?


Because it's a dirty, nasty hack invented by somebody who wasn't
thinking very carefully at the time, and now everybody does it. Bleh.


Just to add to that.

You need to remember with email (and a few other old
favourites like ftp) that these things were invented at the
very dawn of networking. They predate TCP/IP and what we know
as the internet by quite a long ways. So when they came to "standardize" 
things, it was a case of trying to make the

crud that already existed look as if it was a "modern"
standard. Also when there were multiple, slightly incompatible,
mail tools/formats out there they tended to take a vote
by committee - so everybody's favourite hack got thrown
into the pot.

The older the technology the more likely it is to have
horrible hacks embedded in it from the dawn of
(computing) time.

Its one reason that new email/messaging standards keep coming
out. Somebody decides they need to get rid of the crud and
do it right. But trying to get a zillion email users
to adopt a new standard is not an easy task.

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

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


Re: [Tutor] append vs list addition

2014-05-06 Thread Dave Angel
Denis Heidtmann  Wrote in message:
> On Sun, May 4, 2014 at 6:44 PM, Dave Angel  wrote:
>> C Smith  Wrote in message:
>>> Sorry.
>>>
>>> I meant for example:
>>> list1 = [1,2,3]
>>> list2 = [3,4,5]
>>>
>>> newList = list1 + list2
>>>
>>> versus
>>>
>>> for x in list2:
>>> list1.append(x)
>>>
>>> Which is the preferred way to add elements from one list to another?
>>
>> Thank you for switching to text mail.
>>
>> These examples still aren't equivalent.  But in any similar
>>  example,  if list2 is type list, then avoid the list. Use either
>>  extend, or the equivalent += . And if you aren't permitted to
>>  change list1, you should use +=
> 
>> DaveA
>>
> 
> Lurking here.  I am confused by "avoid the list".  What does that mean?
>  Also, you say extend and += are equivalent, yet say " if you aren't
> permitted to change list1, you should use +="  If they are equivalent,
> why choose one over the other?  Doesn't += always change the left-hand
> side?
> 
> Not being critical--just confused.



You're right to be confused;  my fingers were confused typing my
 last sentence.  It should have ended:
   ... you should use + .

Likewise the previous thought should have said:
 
 But in any similar
 example,  if list2 is type list, then
 avoid the loop. 

I've been trying to use a tablet to run a newsreader,  and between
 bugs in the reader and an over enthusiastic spell correction, 
 I'm ready to give up.

-- 
DaveA

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


Re: [Tutor] append vs list addition

2014-05-06 Thread Denis Heidtmann
On Tue, May 6, 2014 at 1:21 PM, Dave Angel  wrote:


> You're right to be confused;  my fingers were confused typing my
>  last sentence.  It should have ended:
>... you should use + .
>
> Likewise the previous thought should have said:
>
>  But in any similar
>  example,  if list2 is type list, then
>  avoid the loop.
...
> DaveA

Thanks.  That makes me feel better.

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