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?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • You say it's third party but I don't understand why that stops you from adding code to it. – Alex Hall May 28 '16 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. – Nico Schlömer May 28 '16 at 11:24
  • 1
    http://stackoverflow.com/questions/1506010/how-to-use-export-with-python-on-linux – strangeqargo May 28 '16 at 11:25

1 Answers1

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.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89