[Tutor] Why is an OrderedDict not sliceable?

2016-01-20 Thread Albert-Jan Roskam
Hi,

Like the subject says: Why is an OrderedDict not sliceable? (From the 
collections library). Was that an intentional omission, or a mistake? [1]

Background: I do not use OrderedDict very often, but I thought I could use it 
to look up street and city names using postcodes ([0-9]{4} [a-z]{2} format). I 
needed it to be ordered because I also wanted to be able to use bisect, which 
is needed when the postcode letters are missing. In short: a fast dict lookup 
for complete postcodes and less fast bisect lookup for in complete postcodes.

[1] http://stackoverflow.com/questions/30975339/slicing-a-python-ordereddict

Thanks!

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


[Tutor] import cv, not cv2

2016-01-20 Thread Rheeya Uppaal
After successfully installing dlib and cv2, I have now been asked to
install the cv library on Ubuntu.

>From what I have read, it seems OpenCV has discontinued support for cv and
only cv2 is used now. Am I correct? Regardless, does anyone know how I can
get this library?

I apologize in advance if my question is foolish.

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


Re: [Tutor] import cv, not cv2

2016-01-20 Thread Alan Gauld
On 20/01/16 10:55, Rheeya Uppaal wrote:
> After successfully installing dlib and cv2, I have now been asked to
> install the cv library on Ubuntu.
> 
> From what I have read, it seems OpenCV has discontinued support for cv and
> only cv2 is used now. Am I correct? Regardless, does anyone know how I can
> get this library?

This isn't really a Python language or standard library question.
You should try asking on the openCV support forum:

http://answers.opencv.org/questions/

They should be able to help you.

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


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-20 Thread Steven D'Aprano
On Wed, Jan 20, 2016 at 01:33:17PM +, Albert-Jan Roskam wrote:
> Hi,
> 
> Like the subject says: Why is an OrderedDict not sliceable? (From the 
> collections library). Was that an intentional omission, or a mistake? 
> [1]

Because slicing a dict makes no sense. A dict is a mapping, not a 
sequence.

d = OrderedDict()
d["cow"] = "a"
d["fox"] = "b"
d["ape"] = "c"
d["dog"] = "d"
d["bee"] = "e"

I can do a dict lookup on a key:

d["cow"]

What would a slice even mean? d[1:4] but 1, 2, 3 are not keys of the 
dict.



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


[Tutor] Why is the name "self" optional instead of mandatory?

2016-01-20 Thread boB Stepp
I'm whizzing along in "Python Crash Course" and am in the chapter on
classes.  Just to satisfy my suspicion that "self" is just a
placeholder for creating an object instance, I tried the following:

>>> class Dog(object):
def __init__(this, name, age):
this.name = name
this.age = age
def bark(this):
print("Woof!  Woof!  Grrr!!!")
def whoami(this):
print("My name is", this.name.title(), "and I am",
 this.age, "years old.")

>>> mydog = Dog('Spotty', 50)
>>> mydog.bark()
Woof!  Woof!  Grrr!!!

>>> mydog.whoami()
My name is Spotty and I am 50 years old.

And just to be really silly:

>>> class Cat(object):
def __init__(MEO, name, age):
MEO.name = name
MEO.age = age
def happy_cat(MEO):
print("Zzzz ... pu ... ")
def whoami(MEO):
print("My name is", MEO.name, "and I am", MEO.age,
 "years old.  Now leave me be!  I'm very sleepy!!!")

>>> mycat = Cat('Callie', 7)
>>> mycat.happy_cat()
Zzzz ... pu ... 
>>> mycat.whoami()
My name is Callie and I am 7 years old.  Now leave me be!  I'm very sleepy!!!

So I really only have one question:  Why not make Python's
*traditional* name, "self", mandatory?  Why give the programmer this
kind of choice?  [OK, that was two questions.]

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


[Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

2016-01-20 Thread boB Stepp
My intent was to deliberately introduce an error into my class definition:

>>> class Hmm(object):
def __init__(self, sigh_type, sigh_strength):
self.sigh_type = sigh_type
self.sigh_strength = sigh_strength
def snort(self):
if self.sigh_strength == 'low':
print("snort")
elif self.sigh_strength == 'med':
print("Snort!")
elif self.sigh_strenght == 'high':
print("SNORT!!!")
else:
print("Hmm...")
def sigh():
if self.sigh_type == 'quiet':
print("pss")
elif self.sigh_type == 'annoying':
print("Whoosh!")
elif self.sigh_type == 'loud':
print("HEAVY SIGH!!!")
else:
print("HMM!!!")

I omitted "self" from the sigh() method to see what would happen plus
some other things.

>>> humdrum = Hmm('quiet', 'low')
>>> humdrum.snort()
snort
>>> humdrum.sigh_strength = 'med'
>>> humdrum.snort()
Snort!
>>> humdrum.sigh_strenght = 'high'
>>> humdrum.snort()
Snort!

At this point I wondered why my output was not "SNORT!!!".  Then I
noticed my typo.  But now I wonder why I did not get an error from
this typo?

>>> humdrum.sigh_strength = 'high'
>>> humdrum.snort()
SNORT!!!
>>> humdrum.sigh()
Traceback (most recent call last):
  File "", line 1, in 
humdrum.sigh()
TypeError: sigh() takes 0 positional arguments but 1 was given

This was my original point in doing all of this, to see what would
result if I omitted "self".  I am pretty sure the error is because the
object instance gets automatically passed to the sigh() method, but by
leaving the "self" parameter out in the method definition, I have a
mismatch between what was defined (0 parameters) and what was passed
to the method (1 argument).

>>> humdrum.sigh_strenght
'high'

But what about this?  It seems like I can use the humdrum arguments
outside of the Hmm class and merrily define new variables.  Why is
this?  Is this potentially useful behavior?

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


Re: [Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

2016-01-20 Thread Cameron Simpson

On 20Jan2016 22:34, boB Stepp  wrote:

My intent was to deliberately introduce an error into my class definition:


class Hmm(object):

   def __init__(self, sigh_type, sigh_strength):
   self.sigh_type = sigh_type
   self.sigh_strength = sigh_strength
   def snort(self):
   if self.sigh_strength == 'low':
   print("snort")
   elif self.sigh_strength == 'med':
   print("Snort!")
   elif self.sigh_strenght == 'high':
   print("SNORT!!!")
   else:
   print("Hmm...")
   def sigh():
   if self.sigh_type == 'quiet':
   print("pss")
   elif self.sigh_type == 'annoying':
   print("Whoosh!")
   elif self.sigh_type == 'loud':
   print("HEAVY SIGH!!!")
   else:
   print("HMM!!!")

I omitted "self" from the sigh() method to see what would happen plus
some other things.


Well... You've bound a function accepting no arguments to the "sigh" attribute 
of the class. Legal. Nonsensical perhaps, but legal.



humdrum = Hmm('quiet', 'low')
humdrum.snort()

snort

humdrum.sigh_strength = 'med'
humdrum.snort()

Snort!

humdrum.sigh_strenght = 'high'
humdrum.snort()

Snort!

At this point I wondered why my output was not "SNORT!!!".  Then I
noticed my typo.  But now I wonder why I did not get an error from
this typo?


Because your "if" statement matched the "med". So it never tried to look up 
"self.sigh_strenght".



humdrum.sigh_strength = 'high'
humdrum.snort()

SNORT!!!


Again, as you expected, yes?


humdrum.sigh()

Traceback (most recent call last):
 File "", line 1, in 
   humdrum.sigh()
TypeError: sigh() takes 0 positional arguments but 1 was given

This was my original point in doing all of this, to see what would
result if I omitted "self".  I am pretty sure the error is because the
object instance gets automatically passed to the sigh() method, but by
leaving the "self" parameter out in the method definition, I have a
mismatch between what was defined (0 parameters) and what was passed
to the method (1 argument).


Correct.


humdrum.sigh_strenght

'high'

But what about this?  It seems like I can use the humdrum arguments
outside of the Hmm class and merrily define new variables.  Why is
this?  Is this potentially useful behavior?


"humdrum" is just an object. You can assign attibutes to it at any time.

The code executing inside the class is no different to the code outside the 
class; Python is a dynamic language and you can do this stuff at any time.


It isn't _specificly_ useful to assign an attribute long after its normal 
initialisation, but it can be quite useful. But consider your initialiser:



   def __init__(self, sigh_type, sigh_strength):
   self.sigh_type = sigh_type
   self.sigh_strength = sigh_strength


By the time __init__ is called, the object already exists with a type/class and 
everything. All __init__ is doing is what you find unexpected later; defining 
new attribute values not there before. The only thing special about __init__ is 
that it is called automatically after an object is created. But that is all.


This is not a static language, and __init__ is not defining what 
fields/attributes the object possesses. It is merely setting some of them. It 
is executable code, not a static type definition.


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


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-20 Thread Cameron Simpson

On 20Jan2016 21:42, boB Stepp  wrote:

I'm whizzing along in "Python Crash Course" and am in the chapter on
classes.  Just to satisfy my suspicion that "self" is just a
placeholder for creating an object instance,


No, it is a placeholder for a _preexiting_ object instance.

[...]

So I really only have one question:  Why not make Python's
*traditional* name, "self", mandatory?  Why give the programmer this
kind of choice?  [OK, that was two questions.]


Why make it mandatory? What benefit would it bring? Remember, one can write 
nonsense or impossible to read gibberish in any language; most people don't try 
to. So to repeat my question: why make it mandatory?


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


Re: [Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

2016-01-20 Thread Danny Yoo
 humdrum.sigh_strenght = 'high'
 humdrum.snort()
> Snort!
>
> At this point I wondered why my output was not "SNORT!!!".  Then I
> noticed my typo.  But now I wonder why I did not get an error from
> this typo?


Hi boB,


Just to be explicit: you are pointing out that:

 humdrum.sigh_strenght = 'high'

was a typo, and it would have been nice if it could be caught as an error.

If that's the case, then yes, I agree.  Unfortunately, this isn't a
runtime error because Python allows us to add arbitrary attributes to
objects: it's a deliberate design feature.  One justification for this
appears to be similar to that of dynamic typing: if we have good unit
tests, we should be able to catch these problems with our unit tests,
so why add a restriction?

(As a personal note: I'm not entirely convinced of the merit of this
particular freedom.  Misspelling is a really easy mistake to make, and
I want the language to catch my simple mistakes, if it's not too
onerous to do so.)


That being said, there are tools available that should be able to
catch many of these problems before runtime.

http://www.pylint.org/
http://pychecker.sourceforge.net/

These should raise warnings if you run it on your program.  You might
want to take a look at these!


Furthermore, there actually *is* a way to turn it into a runtime
error, though this is probably not beginner material.  If you're
interested, see material on "__slots__".
https://docs.python.org/3/reference/datamodel.html#slots.  I don't
think it's intended to be used to catch typos though: its main purpose
is to reduce memory usage, and it's use to catch misspellings is
incidental.


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


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-20 Thread Danny Yoo
> So I really only have one question:  Why not make Python's
> *traditional* name, "self", mandatory?  Why give the programmer this
> kind of choice?  [OK, that was two questions.]


There are situations where it might not be mandatory.


This is definitely not beginner Python material, so I don't want to
delve into it too deeply.  Roughly: we can do things like "metaclass"
programming where your programs modify the rules that construct
classes.  In that way, it's possible to do fairly crazy things.  We
might even be able to make it optional to say "self" all the time, if
we modify the rules radically enough.

If you are really interested in this, see:
https://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/
for an primer.

Basically, Python does almost everything at run-time.  The sorts of
things we'd expect to be static properties aren't static in Python.
Other languages tend to lock things down a bit more firmly, or at the
very least, do more things at compile-time.

Python programmers have enormous flexibility at run-time, at the cost
of making it really easy to make mistakes that aren't caught until
run-time.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-20 Thread Ben Finney
boB Stepp  writes:

> So I really only have one question: Why not make Python's
> *traditional* name, "self", mandatory? Why give the programmer this
> kind of choice?

For the same reason that four-space indentation is not mandatory, yet
anyone who chooses a different indentation size needs to produce good
reasons why.

In both cases: Because nothing is broken, nor made especially ambiguous,
by choosing differently. And because the Python community of programmers
can be relied upon to enforce the convention, as with most other
conventions.

-- 
 \  “When I was a little kid we had a sand box. It was a quicksand |
  `\   box. I was an only child... eventually.” —Steven Wright |
_o__)  |
Ben Finney

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


Re: [Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

2016-01-20 Thread Ben Finney
boB Stepp  writes:

> My intent was to deliberately introduce an error into my class definition:
>
> >>> class Hmm(object):
> def __init__(self, sigh_type, sigh_strength):
> self.sigh_type = sigh_type
> self.sigh_strength = sigh_strength
> def snort(self):
> if self.sigh_strength == 'low':
> print("snort")
> elif self.sigh_strength == 'med':
> print("Snort!")
> elif self.sigh_strenght == 'high':
> print("SNORT!!!")
> else:
> print("Hmm...")
> def sigh():
> if self.sigh_type == 'quiet':
> print("pss")
> elif self.sigh_type == 'annoying':
> print("Whoosh!")
> elif self.sigh_type == 'loud':
> print("HEAVY SIGH!!!")
> else:
> print("HMM!!!")
>
> I omitted "self" from the sigh() method to see what would happen

What would happen, when? At the time of class definition, there are no
errors that I can see.

Python doesn't run every possible branch of your code ahead of time,
just to find out what it will do. Most errors will be discovered only by
encountering the combination of inputs that will trigger them at run
time.

For this and other reasons, it is highly recommended to develop unit
tests with your code, to make explicit assertions about exactly what
will trigger each branch of the code.

This, in turn, will lead to making your code simpler and less-branching
:-)

-- 
 \ “Do unto others twenty-five percent better than you expect them |
  `\  to do unto you. (The twenty-five percent is [to correct] for |
_o__)error.)” —Linus Pauling's Golden Rule |
Ben Finney

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