Re: Code Snippets

2017-11-02 Thread Rhodri James

On 01/11/17 18:57, Stefan Ram wrote:

Ned Batchelder  writes:

You should not optimize for the shortest time to paste a line of code.Â
You should take time and care writing your code, so that it reads best
and runs best.  If you needed another os function, would you have two
__import__("os") in your code? Ugh.


   I make a distinction between two kinds of code:

   1.) Quick-and-dirty code (rapid prototyping)

   One just wants to find out something using Python code.
   The code might be written into the Shell and not be saved,
   or only be saved temporarily. For example, what the sum of
   5412 and 2141 is, or whether an idea for a program code
   works at all.

   The snippets are also intended for such code.

   2.) Library-grade code

   This is code that might be around for a longer time and
   might be maintained in the future. It even is possible that
   it will become part of a library.

   It is possible that qnd-code might evolve into lg-code.
   In this case, it is still possible to remove all »__imports__«.

   Your comments might apply more to lg-code than to qnd-code.


The bad thing here is that you are training yourself in a coding style 
(quick and dirty) that ought to be rejected in any code that isn't 
completely ephemeral.  And in my experience, most "throw-away" code 
isn't thrown away.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Steve D'Aprano
On Thu, 2 Nov 2017 08:02 am, Ben Bacarisse wrote:

> r...@zedat.fu-berlin.de (Stefan Ram) writes:
> 
>> Wolfgang Maier  writes:
>>>If you're worried bout having things on separate lines, you could write:
>>>import os; os.getcwd()
>>>,etc., which is actually saving a few characters :)
>>
>>   Yes, but there still is the risk of the identifier »os«
>>   already being used in the sorrounding code. While
>>
>> __import__( "os" ).getcwd()
>>
>>   does not seem to "leak" names into the enclosing scope.
> 
> Also it's an expression which may be important in your "quick and dirty"
> scripts.

No script is so quick or so dirty to justify calling __import__ with a string
literal argument instead of import.

We've all written quick and dirty throw-away scripts where we don't care too
much about best practices. But this isn't so much less-than-best practices as
worst-practices: optimizing to save a few seconds during the initial editing
run, by using the copy-and-paste anti-pattern.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Steve D'Aprano
On Thu, 2 Nov 2017 05:57 am, Stefan Ram wrote:

> I also have heard that there was a module cache, so I
> was hoping that a second import of the same module might
> not be such an effort for the implementation.


There is: sys.modules.

Although `import spam` is cheap when spam is in the cache, its not free, and
`__import__("spam")` is even less cheap (more costly). Its a function call,
not a statement, so it requires a runtime name lookup and a function call on
top of the same process of checking the cache and importing the module.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Steve D'Aprano
On Thu, 2 Nov 2017 04:25 am, Stefan Ram wrote:

>   I started to collect some code snippets:
[...]

> __import__( "random" ).random()
>
>   And so on. You get the idea.
>
>   However, reportedly, all those snippets are anti-patterns
>   because they use »__import__«.

Correct. Nearly all dunder functions and methods are reserved for use by the
interpreter.

It isn't an outright error to call __import__ directly, but you should avoid
it unless absolutely necessary.


[...]
>   What I'm supposed to do instead, I guess, is:
[...]

> import random
> ...
> random.random()
> 
>   Now, the user has to cut the import, paste it to the top
>   of his code, then go back to the list of snippets, find
>   the same snippet again, copy the expression, go to his code,
>   then find the point where he wanted to insert the snippet again,
>   and finally insert the snippet.

The ellipsis do nothing, why are they there? And surely you are capable of
copying two lines at a time.


import random
random.random()


requires only one copy operation.

There is no outright requirement to collect all the imports at the top of your
module. That's merely a very good convention to follow. If you don't mind
breaking the convention, you can simply paste the result where you want the
random number:

# code here
# more code
# paste here >>
import random
random.random()  # and edit as needed


This has saved you ten seconds of editing time while writing the code. It will
probably cost you ten minutes, when you come back to maintain the program in
six months and the imports are scattered all through the module, inside
functions and classes, but that's your decision to make.


>   And still there now is a 
>   risk of name collisions. So, it seems to me that __import__
>   is just so much better!

Only if you wish to write ugly, inefficient code.

The idea of code snippets is that they are intended as *templates*, not that
you repeat them over and over again. That would be the worst sort of copy and
paste programming, an anti-pattern.

If you need four random numbers, would you write this?


a = __import__( "random" ).random()
b = __import__( "random" ).random()
c = __import__( "random" ).random()
d = __import__( "random" ).random()


Or a list of them?


numbers = [__import__( "random" ).random() for i in range(1000)]


Do I need to explain how terrible that code is?

Code snippets are not an alternative to actually thinking about your code and
writing the best code you can.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Chris Angelico
On Thu, Nov 2, 2017 at 8:02 AM, Ben Bacarisse  wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> Wolfgang Maier  writes:
>>>If you're worried bout having things on separate lines, you could write:
>>>import os; os.getcwd()
>>>,etc., which is actually saving a few characters :)
>>
>>   Yes, but there still is the risk of the identifier »os«
>>   already being used in the sorrounding code. While
>>
>> __import__( "os" ).getcwd()
>>
>>   does not seem to "leak" names into the enclosing scope.
>
> Also it's an expression which may be important in your "quick and dirty"
> scripts.

If your quick-and-dirties are needing these kinds of imports all the
time, the best solution might be to slap something into site.py that
"pre-imports" those into the builtins. Then you can just use
os.getcwd() without worrying about the import, and without calling a
dunder.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Ben Bacarisse
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Wolfgang Maier  writes:
>>If you're worried bout having things on separate lines, you could write:
>>import os; os.getcwd()
>>,etc., which is actually saving a few characters :)
>
>   Yes, but there still is the risk of the identifier »os«
>   already being used in the sorrounding code. While
>
> __import__( "os" ).getcwd()
>
>   does not seem to "leak" names into the enclosing scope.

Also it's an expression which may be important in your "quick and dirty"
scripts.

-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Irmen de Jong
On 11/01/2017 06:25 PM, Stefan Ram wrote:

> import random
> ...
> random.random()
> 
>   Now, the user has to cut the import, paste it to the top
>   of his code, then go back to the list of snippets, find
>   the same snippet again, copy the expression, go to his code,
>   then find the point where he wanted to insert the snippet again,
>   and finally insert the snippet. And still there now is a
>   risk of name collisions. So, it seems to me that __import__
>   is just so much better!

..or suggest them to use an IDE instead? For instance in PyCharm you can type:
random.random()   (squiggle appears under random)

(it suggests: Import This name)  
(it suggests: from random) 
done, an import random has been added at the top of my module.


Irmen


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Chris Angelico
On Thu, Nov 2, 2017 at 7:17 AM, Stefan Ram  wrote:
> Wolfgang Maier  writes:
>>If you're worried bout having things on separate lines, you could write:
>>import os; os.getcwd()
>>,etc., which is actually saving a few characters :)
>
>   Yes, but there still is the risk of the identifier »os«
>   already being used in the sorrounding code. While
>
> __import__( "os" ).getcwd()
>
>   does not seem to "leak" names into the enclosing scope.

If you're using the name "os" for something else, you need to be aware
of that anyway. Leaking names of core modules shouldn't normally be a
problem.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Wolfgang Maier

On 01.11.2017 18:25, Stefan Ram wrote:

   I started to collect some code snippets:

   Sleep one second

__import__( "time" ).sleep( 1 )

   Get current directory

__import__( "os" ).getcwd()

   Get a random number

__import__( "random" ).random()

   And so on. You get the idea.

   However, reportedly, all those snippets are anti-patterns
   because they use »__import__«.

   But what I like about them: You just paste them in where
   you want them to be, and your done.

   What I'm supposed to do instead, I guess, is:

   Sleep one second

import time
...
time.sleep( 1 )

   Get current directory

import os
...
os.getcwd()

   Get a random number

import random
...
random.random()

   Now, the user has to cut the import, paste it to the top
   of his code, then go back to the list of snippets, find
   the same snippet again, copy the expression, go to his code,
   then find the point where he wanted to insert the snippet again,
   and finally insert the snippet. And still there now is a
   risk of name collisions. So, it seems to me that __import__
   is just so much better!



I'm not sure why you think this has to do with import vs __import__.
If you're worried bout having things on separate lines, you could write:

import os; os.getcwd()

,etc., which is actually saving a few characters :)



--
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Ned Batchelder

On 11/1/17 1:25 PM, Stefan Ram wrote:

   I started to collect some code snippets:

   Sleep one second

__import__( "time" ).sleep( 1 )

   Get current directory

__import__( "os" ).getcwd()

   Get a random number

__import__( "random" ).random()

   And so on. You get the idea.

   However, reportedly, all those snippets are anti-patterns
   because they use »__import__«.

   But what I like about them: You just paste them in where
   you want them to be, and your done.

   What I'm supposed to do instead, I guess, is:

   Sleep one second

import time
...
time.sleep( 1 )

   Get current directory

import os
...
os.getcwd()

   Get a random number

import random
...
random.random()

   Now, the user has to cut the import, paste it to the top
   of his code, then go back to the list of snippets, find
   the same snippet again, copy the expression, go to his code,
   then find the point where he wanted to insert the snippet again,
   and finally insert the snippet. And still there now is a
   risk of name collisions. So, it seems to me that __import__
   is just so much better!



You should not optimize for the shortest time to paste a line of code.  
You should take time and care writing your code, so that it reads best 
and runs best.  If you needed another os function, would you have two 
__import__("os") in your code? Ugh.


--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Code Snippets

2017-11-01 Thread Neil Cerutti
On 2017-11-01, Stefan Ram  wrote:
>   I started to collect some code snippets:
>
>   Sleep one second
>
> __import__( "time" ).sleep( 1 )
>
>   What I'm supposed to do instead, I guess, is:
>
>   Sleep one second
>
> import time
> ...
> time.sleep( 1 )
>
>   Get current directory
>
> import os
> ...
> os.getcwd()
>
>   Get a random number
>
> import random
> ...
> random.random()
>
>   Now, the user has to cut the import, paste it to the top
>   of his code, then go back to the list of snippets, find
>   the same snippet again, copy the expression, go to his code,
>   then find the point where he wanted to insert the snippet again,
>   and finally insert the snippet. And still there now is a
>   risk of name collisions. So, it seems to me that __import__
>   is just so much better!

You can import wherever you like--only good style requires you to
put them at the top of your file.

Moreover, snippets could be a library, with each snippet a
function, with the import inside the function. That would keep
the module name out of your global namespace.

-- 
Neil Cerutti

-- 
https://mail.python.org/mailman/listinfo/python-list