Re: [Tutor] str.replace error

2019-04-26 Thread Alan Gauld via Tutor
On 26/04/2019 05:29, Steven D'Aprano wrote:

> (deliberately? accidently?) run into one of the slightly advanced 
> corners of Python: unbound methods.

Ooh, good catch. I completely forgot that the string class'
name is str...

That's why he didn't get a name error...

-- 
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] When you think to setup the __class__ of a module object to a subclass of ModuleType

2019-04-26 Thread Arup Rakshit

On 26/04/19 11:10 AM, Steven D'Aprano wrote:

On Thu, Apr 25, 2019 at 02:52:07PM +0530, Arup Rakshit wrote:


Here it is: *3.3.2.1. Customizing module attribute access*
(https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access)

Oh! That's brand new in 3.7, no wonder I didn't know about it.

I did see the core developers talking about adding this feature, but I
didn't know that they had done so.

Your original question was:


In the simple code like what are the advantages we get from? Is this
so that we can implement more special methods than just __getattr__
and __dir__ in the module level?

Yes, that's what the documentation says. I don't see any reason not to
believe it.



Well I believed it, but I was not sure if I understood it correctly. So 
I was trying to confirm myself by those who understand the language.



BTW, one thing I would like to know about this list is that, everytime I 
send an email I see it the in list after 2 hours approx. Is this for me 
or everybody? I am just curious.





Oh, this is cool! I'm going to enjoy playing with this...


py> from types import ModuleType
py> class Magic(ModuleType):
... count = 0
... @property
... def spam(self):
... self.count += 1
... return ' '.join(['spam']*self.count)
...
py> import sys
py> sys.modules['__main__'].__class__ = Magic
py> import __main__
py> __main__.spam
'spam'
py> __main__.spam
'spam spam'
py> __main__.spam
'spam spam spam'





--
Thanks,

Arup Rakshit

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


Re: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType

2019-04-26 Thread Alan Gauld via Tutor
On 26/04/2019 13:48, Arup Rakshit wrote:

> BTW, one thing I would like to know about this list is that, everytime I 
> send an email I see it the in list after 2 hours approx. Is this for me 
> or everybody? I am just curious.

Just for you! ...And every other new poster :-)

It stays on moderation until the volume increases
to the point where I get to recognise the email address.
Around which point I get motivated to change the moderation
setting. I've just done that for you so they should
come through a little quicker now.

-- 
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] When you think to setup the __class__ of a module object to a subclass of ModuleType

2019-04-26 Thread Steven D'Aprano
On Fri, Apr 26, 2019 at 06:18:32PM +0530, Arup Rakshit wrote:

> BTW, one thing I would like to know about this list is that, everytime I 
> send an email I see it the in list after 2 hours approx. Is this for me 
> or everybody? I am just curious.

Just you, I think.

Looking at the headers of your post, I see when you sent the post:

Received: from arup.local ([117.194.106.70]) by smtp.gmail.com
 Fri, 26 Apr 2019 05:48:35 -0700 (PDT)

and then a few hops through google, to mail.python.org, and then there 
seems to be a long delay before mail.python.org sends it back out again.

Perhaps you are stuck in the moderator queue?



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


Re: [Tutor] str.replace error

2019-04-26 Thread Mats Wichmann
On 4/25/19 10:29 PM, Steven D'Aprano wrote:
> On Thu, Apr 25, 2019 at 10:46:31AM -0700, Roger Lea Scherer wrote:
> 
>> with open('somefile') as csvDataFile:
>> csvReader = csv.reader(csvDataFile)
>> for row in range(100):
>> a = "Basic P1"
>> str.replace(a, "")
>> print(next(csvReader))
> 
> 
> I'm not quite sure what you expected this to do, but you've 
> (deliberately? accidently?) run into one of the slightly advanced 
> corners of Python: unbound methods. 

accidentally, I believe.

notice that the way the Python 3 page on string methods is written, you
_could_ read it as you are to use the literal 'str' but in fact you are
expected to substitute in the name of your string object.

For this specific case:
===
str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old
replaced by new. If the optional argument count is given, only the first
count occurrences are replaced.
===

So for the example above you're expected to do (without changing the
range call, which has been commented on elsewhere: you should just
iterate directly over the reader object, that's the way it's designed):

for row in range(100):
a = "Basic P1"
row.replace(a, "")

and then hopefully actually do something with the modified 'row', not
just go on to the next iteration and throw it away...

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


Re: [Tutor] When you think to setup the __class__ of a module object to a subclass of ModuleType

2019-04-26 Thread Arup Rakshit

On 26/04/19 8:58 PM, Alan Gauld via Tutor wrote:

On 26/04/2019 13:48, Arup Rakshit wrote:


BTW, one thing I would like to know about this list is that, everytime I
send an email I see it the in list after 2 hours approx. Is this for me
or everybody? I am just curious.

Just for you! ...And every other new poster :-)

It stays on moderation until the volume increases
to the point where I get to recognise the email address.
Around which point I get motivated to change the moderation
setting. I've just done that for you so they should
come through a little quicker now.


Thanks Alan Gauld.

As my post was taking hours to come to this, I asked a beginner question 
to the other list 
https://mail.python.org/pipermail/python-list/2019-April/740629.html . 
:) Next time I will be here only.



--
Thanks,

Arup Rakshit

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


Re: [Tutor] module import from a packager works inconsistent between REPL and command line

2019-04-26 Thread Arup Rakshit

On 26/04/19 10:58 AM, Steven D'Aprano wrote:

On Thu, Apr 25, 2019 at 05:40:18PM +0530, Arup Rakshit wrote:


I have a small app like this:

Please simplify your code to the minimum needed to demonstrate the
problem you are asking about. This bit is excellent:


pizza-shop$ tree .
.
└── pizzapy
 ├── __init__.py
 ├── menu.py
 └── pizza.py

1 directory, 3 files

Nicely shown!

But we don't need to see all the gory details of menu.py and especially
not of pizza.py, all those methods in pizza.Pizza are irrelevant to the
problem. Please read this:

http://www.sscce.org/

for a guide. It is written for Java programmers, but it applies to any
language.

All we need in menu.py is a single line:

# menu.py
from pizza import Pizza

because that's the line that fails.

And for pizza.py, all we need is:

# pizza.py
Pizza = None



Now when I call the menu.py from command like it works as expected.

When you ask Python to import a module, it doesn't search the entire
hard drive, that could take hours if the drive is big enough. It only
looks in the search-path. At runtime, you can see the paths searched
like this:

import sys
print(sys.path)

which will show you where Python is looking for modules.

When you directly call a module:

 python path/to/menu.py

the directory holding that module is included in the search path, so if
you also have path/to/pizza.py the "import pizza" will work.

But in the REPL, only the default search path is used.

In your case, the fix is, I think, to change menu.py to do this:

# was: from pizza import Pizza
from pizzapy.pizza import Pizza

which I *think* will solve the problem, but I haven't tested it.
Thanks for explaining this. I got it, it is terrible confusing for a 
beginner untill spent some months and use Python daily. Every beginner 
will be frustated, and probably think to give up the whole Python 
journey.. haha. Some StackOverflow posts shown modifying sys.path by 
inserting at position 0, it felt to me as a hacked workaround.. So came 
here to know how to cook it. :)


For what it is worth, importing problems are sometimes annoying to
solve. What works as a good design for importing libraries doesn't
always make a good design for scripts that you call directly from the
command line, and visa versa, so the import system is a bit of a
compromise between the two.




Yes, yes.

--
Thanks,

Arup Rakshit

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