Re: Python script accessing own source code

2021-05-12 Thread Mirko via Python-list
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

2021-05-12 Thread 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
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python script accessing own source code

2021-05-12 Thread Stestagg
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

2021-05-12 Thread MRAB

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

2021-05-12 Thread Michael F. Stemper

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: The importance of mental health

2021-05-12 Thread tommy yama
I do not know you and your past but guess destigmatization of mental
illnesses is an important agenda to people,


On Tue, May 11, 2021 at 8:34 AM Mirko via Python-list <
python-list@python.org> wrote:

> Am 10.05.2021 um 15:16 schrieb Kyle Stanley:
> > Hey all,
> >
> > In these last few months, I have been in the process of healing from some
> > pretty heavy past trauma. And now that I am on the road to recovery, I
> want
> > to share my journey with the Python community in hopes that it may reach
> > those that are struggling with their own mental health battles, as many
> of
> > us are during these dark and difficult times.
> >
> > Trigger warning that it includes a decent amount of highly personal
> > content, so only check it out if you are okay with that:
> > https://discuss.python.org/t/break-from-open-source/6372/7?u=aeros.
> >
> > To anyone that would limit my employment opportunities as a result of
> > having had these struggles, *that's perfectly okay*. I kept the post in
> the
> > private section because I was originally in fear of discriminate.
> However,
> > I have reached an important conclusion: *I would not want to work for
> your
> > company if you discriminate against people who have overcome past
> struggles*
> > .
> >
>
>
> Thank you for your courage! This is what we need. True, genuine and
> honest stories from people with mental health issues. To demystify
> and destigmatize the entire topic. Everybody happily runs to the
> doctors to get antibiotics/antivirals for their simple cold and
> everybody literally "loves* to complain about how bad they feel when
> having a cold. But even today in the year 2021, very few people dare
> to go public with their "mental cold" or their "mental cancer". The
> danger of mockery or discrimination is still far to high.
>
>
> All the best, Kyle and good luck! :-)
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list