Mailing List Archive

Re: Python script accessing own source code
On 12/05/2021 08.26, Dino wrote:

> Hi, here's my (probably unusual) problem. Can a Python (3.7+) script
> access its own source code?

Here is a fairly simple python program that reads itself:

================================================
#!/usr/bin/python

import sys

with open( sys.argv[0], "rt" ) as myself:
for line in myself:
junk = sys.stdout.write( "%s" % (line) )

sys.exit(0)
================================================

It's not bullet-proof. If you put it in a directory in your $PATH and
run it from somewhere else, it won't work.

--
Michael F. Stemper
I feel more like I do now than I did when I came in.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
On 2021-05-12 15:48, Michael F. Stemper wrote:
> On 12/05/2021 08.26, Dino wrote:
>
>> Hi, here's my (probably unusual) problem. Can a Python (3.7+) script
>> access its own source code?
>
> Here is a fairly simple python program that reads itself:
>
> ================================================
> #!/usr/bin/python
>
> import sys
>
> with open( sys.argv[0], "rt" ) as myself:
> for line in myself:
> junk = sys.stdout.write( "%s" % (line) )
>
> sys.exit(0)
> ================================================
>
> It's not bullet-proof. If you put it in a directory in your $PATH and
> run it from somewhere else, it won't work.
>
How about this:

with open(__file__) as myself:
print(myself.read(), end='')
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
On 2021-05-12 15:48, Michael F. Stemper wrote:

> > On 12/05/2021 08.26, Dino wrote:
> >
> >> Hi, here's my (probably unusual) problem. Can a Python (3.7+) script
> >> access its own source code?
> >
> > Here is a fairly simple python program that reads itself:
> >
> > ================================================
> > #!/usr/bin/python
> >
> > import sys
> >
> > with open( sys.argv[0], "rt" ) as myself:
> > for line in myself:
> > junk = sys.stdout.write( "%s" % (line) )
> >
> > sys.exit(0)
> > ================================================
> >
> > It's not bullet-proof. If you put it in a directory in your $PATH and
> > run it from somewhere else, it won't work.
> >
>
>
Here's a fairly simple option:

==========
import inspect
import sys

print(inspect.getsource(sys.modules[__name__]))
========
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
.......
>
> with open(__file__) as myself:
>     print(myself.read(), end='')

very nice, but accessing code that's already seems quite easy. I think the real problem is to get a python script name
that creates and writes itself. So I would ask if any one has the solution to the self writing script

python find-tomorrows-lotto-numbers.py

since GvR has been shown to have time traveling abilities such a script could paradoxically appear acausally.
--
yrs-not-too-seriously
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
.......
>
> with open(__file__) as myself:
>     print(myself.read(), end='')

very nice, but accessing code that's already seems quite easy. I think the real problem is to get a python script name
that creates and writes itself. So I would ask if any one has the solution to the self writing script

python find-tomorrows-lotto-numbers.py

since GvR has been shown to have time traveling abilities such a script could paradoxically appear acausally.
--
yrs-not-too-seriously
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
Am 12.05.2021 um 20:41 schrieb Robin Becker:
> .......
>>
>> with open(__file__) as myself:
>> print(myself.read(), end='')
>
> very nice, but accessing code that's already seems quite easy. I
> think the real problem is to get a python script name that creates
> and writes itself. So I would ask if any one has the solution to the
> self writing script
>
> python find-tomorrows-lotto-numbers.py
>
> since GvR has been shown to have time traveling abilities such a
> script could paradoxically appear acausally.
> --
> yrs-not-too-seriously
> Robin Becker


Not sure, if that's what you mean, but writing a self-replicating
script is easy too:

import os
import sys

with open(os.path.abspath(__file__)) as myself:
with open(sys.argv[1], "w") as yourself:
yourself.write(myself.read())


Give it a filename as a command-line argument and it will write
itself to that file.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
On 12/05/2021 20:17, Mirko via Python-list wrote:
> Am 12.05.2021 um 20:41 schrieb Robin Becker:
>> .......
>>>
>...........
>> since GvR has been shown to have time traveling abilities such a
>> script could paradoxically appear acausally.
>> --
>> yrs-not-too-seriously
>> Robin Becker
>
>
> Not sure, if that's what you mean, but writing a self-replicating
> script is easy too:

actually I was really joking about self creating scripts that do something useful like finding future lotto numbers.

the artificial programmer servant is some way off

>
> import os
> import sys
>
> with open(os.path.abspath(__file__)) as myself:
> with open(sys.argv[1], "w") as yourself:
> yourself.write(myself.read())
>
>
> Give it a filename as a command-line argument and it will write
> itself to that file.
>
--
Robin Becker
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
On 12/05/2021 20:17, Mirko via Python-list wrote:
> Am 12.05.2021 um 20:41 schrieb Robin Becker:
>> .......
>>>
>...........
>> since GvR has been shown to have time traveling abilities such a
>> script could paradoxically appear acausally.
>> --
>> yrs-not-too-seriously
>> Robin Becker
>
>
> Not sure, if that's what you mean, but writing a self-replicating
> script is easy too:

actually I was really joking about self creating scripts that do something useful like finding future lotto numbers.

the artificial programmer servant is some way off

>
> import os
> import sys
>
> with open(os.path.abspath(__file__)) as myself:
> with open(sys.argv[1], "w") as yourself:
> yourself.write(myself.read())
>
>
> Give it a filename as a command-line argument and it will write
> itself to that file.
>
--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list
Re: Python script accessing own source code [ In reply to ]
On Thu, May 13, 2021 at 5:27 PM Robin Becker <robin@reportlab.com> wrote:
>
> On 12/05/2021 20:17, Mirko via Python-list wrote:
> > Am 12.05.2021 um 20:41 schrieb Robin Becker:
> >> .......
> >>>
> >...........
> >> since GvR has been shown to have time traveling abilities such a
> >> script could paradoxically appear acausally.
> >> --
> >> yrs-not-too-seriously
> >> Robin Becker
> >
> >
> > Not sure, if that's what you mean, but writing a self-replicating
> > script is easy too:
>
> actually I was really joking about self creating scripts that do something useful like finding future lotto numbers.
>
> the artificial programmer servant is some way off
>

Perhaps not so far off as you might think. With import hooks, it's
entirely possible to have a program that creates itself on demand -
for instance, you might transpile your script from some variant form
of the language (maybe you're trying out a proposed new syntax). It
won't help you with the lotto, but it certainly is an AI assistant for
a programmer that creates runnable code when called upon.

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