0

I have a small Python file from a third-party package with

$ cat a.py
a = 3.14

I would now like to make the value of a available on the command line. I initially thought that I just grep/sed/awk my way out of this, but perhaps there's a more pythonic approach.

Any hints?

3
  • You say it's third party but I don't understand why that stops you from adding code to it. Commented May 28, 2016 at 11:19
  • I have to fetch the source from upstream regularly and I need a clean source after reading the version. Perhaps one could programmatically insert code, execute, and remove the code again, but a little over-the-top. Commented May 28, 2016 at 11:24
  • 1
    stackoverflow.com/questions/1506010/… Commented May 28, 2016 at 11:25

1 Answer 1

3

Here's a simple crude solution:

$ cat a.py
a = 3.14
a *= 2
$ python <(cat a.py && echo '\nprint(a)') 
6.28

There are various ways this might not work, but the constraints of your situation are not clear.

The \n is there to prevent a syntax error in the case that a.py does not end in a newline.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.