Re: Persisting functions typed into the shell

2022-11-12 Thread Chris Angelico
On Sun, 13 Nov 2022 at 05:48, Stefan Ram  wrote:
>   So much for the topic of "In Python, /everything/ is an
>   object"! There seem to be first and second-class objects:
>   Shelveable and non-shelveable objects.
>

That's a bit unfair. Everything IS an object, but not all objects can
be treated the same way. Complex numbers can't be compared for
greater-than/less-than, but they are still first-class objects.

Is it acceptable to you if the reconstituted function doesn't have
source code (which will affect tracebacks and such), as long as it
behaves the same way? If so, save the __code__.co_code attribute, and
build a new function from that.

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


Re: Persisting functions typed into the shell

2022-11-12 Thread Weatherby,Gerard
Sounds like Jupyter Notebooks: https://jupyter.org


From: Python-list  on 
behalf of Stefan Ram 
Date: Saturday, November 12, 2022 at 1:48 PM
To: python-list@python.org 
Subject: Persisting functions typed into the shell
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

  Many readers here know interactive Python sessions with
  prompts like ">>>". But a "session" could be something else.
  One could imagine that when starting a new session, one
  still sees all the variables and constants defined in
  preceding sessions.

  I have implemented something like a "restore()" and a "save()"
  call. "restore()" will restore the names from the last "save()".
  "save()" will look for user-defined names (it excludes certain
  standard names and certain other names from my software) and
  save them using the "shelve" package from the standard library.

  I you know "shelve" or have read the subject line, you can
  guess what comes now:

  I cannot save user-defined functions this way!

  When a user types into the console:

|>>> def f():
|...print( "example" )
|...

  he gives source code to shell and hopes that the shell will
  cherish the memory of that function. But instead it acts as
  if from now on it does not know the source code of "f"!

  So, there seems to be no way now to persist this function
  to a file? as if by "save( f )" or something similar?
  If not the source code, then maybe some other form?

  So much for the topic of "In Python, /everything/ is an
  object"! There seem to be first and second-class objects:
  Shelveable and non-shelveable objects.


--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kVssvGre00pk5iMVIWUbuGXUwcZ8veRBEuSiX-VLlkRVlJoQ96fl6CZG9zQ72Hky8qqofZhhhA5qZpkneyEz4-4$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Persisting functions typed into the shell

2022-11-12 Thread dn

On 13/11/2022 05.05, Stefan Ram wrote:

Wayne Harris  writes:

Wow.  Could the dis module help at all?


   Thank you for this idea! I think this should work, but only
   under CPython and not necessarily across different Python
   versions. Still, I might use dis.


Was constructing a two-part tutorial. When it came to the preparing for 
the second meeting, was surprised to discover that PyCharm retained code 
and values in its Python Console (its REPL tab/window) - I'm presuming, 
within its 'project' structure.


Was more interested in ensuring all-parties would be able to re-create 
the ending/re-starting position, regardless of IDE. Accordingly, never 
looked any further.


Depending upon the full range of criteria, may be worth a look...


Disclaimer: JetBrains sponsor our local PUG with a one-year 'pro' 
license, as a monthly 'door prize'. Apart from using the product, have 
no other connection!


Similarly, have not looked at such within VS-Codium or other editors/IDEs...
--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Persisting functions typed into the shell

2022-11-12 Thread Wayne Harris via Python-list
On 12/11/2022 10:01, Stefan Ram wrote:
>   Many readers here know interactive Python sessions with
>   prompts like ">>>". But a "session" could be something else. 
>   One could imagine that when starting a new session, one
>   still sees all the variables and constants defined in 
>   preceding sessions.
> 
>   I have implemented something like a "restore()" and a "save()"
>   call. "restore()" will restore the names from the last "save()".
>   "save()" will look for user-defined names (it excludes certain
>   standard names and certain other names from my software) and
>   save them using the "shelve" package from the standard library.
> 
>   I you know "shelve" or have read the subject line, you can
>   guess what comes now:
> 
>   I cannot save user-defined functions this way!
> 
>   When a user types into the console:
> 
> |>>> def f():
> |...print( "example" )
> |...
> 
>   he gives source code to shell and hopes that the shell will
>   cherish the memory of that function. But instead it acts as
>   if from now on it does not know the source code of "f"!
> 
>   So, there seems to be no way now to persist this function
>   to a file? as if by "save( f )" or something similar?
>   If not the source code, then maybe some other form?
> 
>   So much for the topic of "In Python, /everything/ is an
>   object"! There seem to be first and second-class objects:
>   Shelveable and non-shelveable objects.

Wow.  Could the dis module help at all?  Say by getting the bytes of the
compiled function and then saving them to a file, then reading them off
later and rebuilding the function with dis again?
-- 
https://mail.python.org/mailman/listinfo/python-list