Re: [Tutor] self.name is calling the __set__ method of another class

2019-05-02 Thread Arup Rakshit

On 30/04/19 6:34 AM, David L Neil wrote:
As Steven explained, this is a complex environment where only those 
with a good understanding of the meta abstractions would even want to 
play (IMHO). Perhaps you would be better served by actually writing 
some Python applications, and with such experience under-your-belt, 
adding these 'advanced knowledge' ideas at some later time, if/when 
needed?)


Thanks David for suggesting this. I am going to do that, writing an web 
application using Python Flask. It feels more easier than battling with 
such frustrated abstractions. :) I don't know why python exposed so many 
things, and getter/setter goes wild in that respect. It was not that 
hard in the other languages(Ruby, JS). I think Python object model and 
how python interpreter executes code is the foundation. I'll try to hunt 
that slowly, because that's where I think I hit the wall everytime. 
Python doesn't work the way I am thinking it, and I am far behind of this.



--
Thanks,

Arup Rakshit

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


Re: [Tutor] self.name is calling the __set__ method of another class

2019-05-02 Thread Arup Rakshit

On 30/04/19 5:11 AM, Steven D'Aprano wrote:

On Tue, Apr 30, 2019 at 12:47:02AM +0530, Arup Rakshit wrote:


I really didn't write that code by myself. The day I'll you will not see
me here everyday :) . I was watching a PyCon video
https://youtu.be/81S01c9zytE?t=8172 where the author used this code. But
his explanation is not clear to me. The main problem is that the guy who
was recorded it far away from the projector, so what speaker were
showing there is not clear. So thought to ask here as usual. Because I
felt so lost with this trick.

Okay, the short, SIMPLIFIED (and therefore inaccurate) summary of
descriptors:

Descriptors are the "magic" used by Python whenever it does an attribute
lookup. When you do any sort of attribute lookup or assignment:

 x = spam.eggs

 spam.eggs = value

Python looks at spam and spam's class for an attribute called "eggs",
and if that attribute is an object with a __set__ or __get__ method, it
calls that method:

 x = spam.eggs
 => x = spam.eggs.__get__()

 spam.eggs = value
 => spam.eggs.__set__(value)

For the gory details of what *precisely* happens, see the Howto Guide:

https://docs.python.org/3/howto/descriptor.html


All answers in the thread with the howto link above helped me to 
understand this at least. I did't masters yet, but atleast now can 
reason about what is going on when I meet such code examples.





Python has a few common descriptors built in:

- ordinary methods
- classmethod
- staticmethod
- property

Apart from staticmethod, they're all pretty common in code. But writing
your own custom descriptors is fairly rare. I've only done it once, in
25+ years of using Python.



Thank you very much.

--
Thanks,

Arup Rakshit

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


Re: [Tutor] self.name is calling the __set__ method of another class

2019-05-02 Thread Mats Wichmann
On 5/2/19 9:25 AM, Arup Rakshit wrote:
> On 30/04/19 6:34 AM, David L Neil wrote:
>> As Steven explained, this is a complex environment where only those
>> with a good understanding of the meta abstractions would even want to
>> play (IMHO). Perhaps you would be better served by actually writing
>> some Python applications, and with such experience under-your-belt,
>> adding these 'advanced knowledge' ideas at some later time, if/when
>> needed?)
> 
> Thanks David for suggesting this. I am going to do that, writing an web
> application using Python Flask. It feels more easier than battling with
> such frustrated abstractions. :) I don't know why python exposed so many
> things, and getter/setter goes wild in that respect.  It was not thathard in 
> the other languages(Ruby, JS).

So let me make this comment: some of us (yeah, I'm guilty of this) like
to expose the underlying details when talking about something.  In my
case, I do that because it's how I myself learn - if the details make
beautiful sense, then the "normal way" complete with syntactic sugar
doesn't feel like a mystery and I'm much happier. Does that mean
everybody needs those details up front? Absolutely not. PyCon talks have
a high risk of doing this - great material, usually, but they got
accepted as talks because they are "revealing secrets". Attendees
already knew how to use the feature but come back from the conference
going "now I understand so much better" and so got their money's worth.

In normal everyday use, getters and setters in Python are simple.

(a) don't use them unless you actually need them, which is rarely - this
isn't C++, Java, etc. where you're supposed to encapsulate everything.
(b) if you do need them, "wrap" a regular attribute access in your class
definition.

So I wouldn't write this:

class C:
def __init__(self, x):
self.__x = x

def get_x(self):
return self.__x

def set_x(self, x):
self.__x = x

# But this:

class C:
def __init__(self, x):
self.x = x


Now if you later found that 'x' needed to be more complex (in this
trivial example, x is only allowed to be between 0 and 100), we can add
property decorators which provide some sort of encapsulation, but leaves
the same API - so the retrofit doesn't break clients:

class C:
def __init__(self, x):
self.x = x

@property
def x(self):
return self.__x

@x.setter
def x(self, x):
if x < 0:
self.__x = 0
elif x > 100:
self.__x = 100
else:
self.__x = x

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


Re: [Tutor] self.name is calling the __set__ method of another class

2019-05-02 Thread Alan Gauld via Tutor
On 02/05/2019 16:25, Arup Rakshit wrote:

> such frustrated abstractions. :) I don't know why python exposed so many 
> things,

Because by exposing it those few who do need to play with
the internals can(*). Most of us just ignore it

> Python doesn't work the way I am thinking it, and I am far behind of this.

Most languages don't really work the way most users think
they do, but Python allows you to discover that more easily
than most. (Lisp and Smalltalk are other exceptions that
expose their inner workings to anyone who cares to look)


(*)One area where these meta-programming features are
used is in frameworks, either web, GUI or networking.
Django being one case in point where meta-classes are
used effectively to make Python behave slightly differently
to normal, which, in turn, makes web programming slightly
easier...

-- 
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] pip issue

2019-05-02 Thread Anil Duggirala
hello,
I executed the pip3 install --user -r contrib/requirements/requirements.txt (I 
actually did sudo before that). I then interrupted the process with Ctrl-C. 
Now, when I execute the same command I get:
Collecting aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt 
(line 5))
  Could not find a version that satisfies the requirement aiorpcX<0.18,>=0.17.0 
(from -r contrib/requirements/requirements.txt (line 5)) (from versions: )
No matching distribution found for aiorpcX<0.18,>=0.17.0 (from -r 
contrib/requirements/requirements.txt (line 5))

I suspect this has something to do with me interrupting the install process, 
because I interrupted it precisely when it was fetching the package that it 
can't find now. 

Can anyone please help me, 
Let me know if I should be asking this elsewhere.
thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pip issue

2019-05-02 Thread Cameron Simpson

On 02May2019 17:24, Anil Duggirala  wrote:
I executed the pip3 install --user -r 
contrib/requirements/requirements.txt (I actually did sudo before 
that).


Please don't use sudo for this. The notion "install" does not imply 
being root.


The whole point of --user is to install packages in your personal 
account without troubling with the system packages. Doing that as root 
only installs the packages for root, generally a useless thing as you 
shouldn't be running normal stuff as root.


Just do pip3 as yourself.

I then interrupted the process with Ctrl-C. Now, when I execute the 
same command I get:

Collecting aiorpcX<0.18,>=0.17.0 (from -r contrib/requirements/requirements.txt 
(line 5))
 Could not find a version that satisfies the requirement aiorpcX<0.18,>=0.17.0 
(from -r contrib/requirements/requirements.txt (line 5)) (from versions: )
No matching distribution found for aiorpcX<0.18,>=0.17.0 (from -r 
contrib/requirements/requirements.txt (line 5))


That is odd, though I've seen this kind of complaint from pip before for 
some packages.


Try this (as _yourself_, not as root):

 pip3 install --verbose --user 'aiorpcX<0.18,>=0.17.0'

For me, this just worked.

Also, the output includes the location where pip is installing stuff.  
You could always just clean that area out and retry.


Also, try the --ignore-installed option and/or the --force-reinstall, 
which may cause pip3 to ignore any partial/damaged install and just do 
it all from scratch.



I suspect this has something to do with me interrupting the install process, 
because I interrupted it precisely when it was fetching the package that it 
can't find now.
Let me know if I should be asking this elsewhere.


This is a fine place to ask this question.

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


Re: [Tutor] pip issue

2019-05-02 Thread Mats Wichmann
On 5/2/19 6:14 PM, Cameron Simpson wrote:
> On 02May2019 17:24, Anil Duggirala  wrote:
>> I executed the pip3 install --user -r
>> contrib/requirements/requirements.txt (I actually did sudo before that).
> 
> Please don't use sudo for this. The notion "install" does not imply
> being root.

> The whole point of --user is to install packages in your personal
> account without troubling with the system packages. Doing that as root
> only installs the packages for root, generally a useless thing as you
> shouldn't be running normal stuff as root.
> 
> Just do pip3 as yourself.

also, despite the large volume of old text that says otherwise, it's
better not to invoke pip as a command, but as a module. Thus, instead of

pip3 install --user blah

do:

python3 -m pip install --user blah

assuming you wanted it to work for "python3". the latter way makes sure
the package ends up in a place that matches the Python you're going to
use - and you are going to use this code from another Python program,
no?  (that change is not going to fix your problem, it's just general
advice). Most Linux systems these days have several Python versions, and
the more you use Python the more likely it is you got something that set
up another Python version (virtualenv, bundled Python, etc). Mac users
often have two: the system one, and the one you actually use for your
own work.  Even Windows users end up with several Pythons, e.g. one you
installed, one that got installed with Visual Studio, etc.

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