0

Let's say in a file A.py and I am computing value of some variable x by some lengthy procedure, such that code takes around 15-30 seconds to execute.

I want to use value of x in another program B.py.

I have thought of importing x to another file B.py but when B executes, it executes A again in order to calculate x. B takes another 1-2 minutes to execute, so it's essential that I just use the output x. I tried to put it under if __name__ == "main", but how will I compute x then?

Also, I have tried copying the value of x to a text file as mentioned here, but x is a very large list (around 10000 length), so output just shows "..." and omits printing most of it.

ab123
  • 203
  • 4
  • 12
  • @meow I don't get it. Even when I run C, everytime it will compute x again when the function is called, even though it's in if name == condition. – ab123 Jun 08 '18 at 12:50

2 Answers2

2

I think you want to use pickle.

import pickle

very_long_list = [1,2,3]
file_name = "/tmp/very_long_list.pickle"

pickle.dump(very_long_list, open(file_name, "wb"))

del very_long_list

very_long_list = pickle.load(open(file_name, "rb"))
print(very_long_list)  # prints [1,2,3], we did it!
nicoco
  • 1,421
  • 9
  • 30
  • It still prints "..." for me – ab123 Jun 08 '18 at 12:15
  • This is for saving the variable. Use `for el in in list: print(el)` if you really want to print the whole list to the screen but I thought the problem was saving the result… – nicoco Jun 08 '18 at 12:24
  • Sorry for a late reply but `pickle` also doesn't seem to work. I have tried printing the list row wise (it's a 2D array) but it still prints `[array([a b x ... c d y]), array([e f g ... h i j], and so on ... ` – ab123 Jun 11 '18 at 05:13
  • I think nobody really understood your problem then. Is it to save the result of a long computation or to display the content of a long list? You're mentioning an array. Are you using `numpy`? Are you looking for [numpy print options](https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html)? – nicoco Jun 11 '18 at 07:00
  • The problem is to save a long (in terms of memory) result, of a long (in terms of execution time) computation. The result is a list of lists. I want to save it to a file, so that it it doesn't need to be computed again, and it can be easily loaded from that file. I am using `numpy` in the code but the final result is not a `numpy.ndarray` its type is ``. At present, using `pickle` to save to a file and then loading and printing it like `for i in range(row): print(x[i])` prints the form I mentioned in previous comment – ab123 Jun 11 '18 at 07:38
  • If printing the whole array is crucial to you, then follow the numpy docs links I provided before. – nicoco Jun 11 '18 at 08:29
0

When you don't need to run A.py for any other reason except to produce x (to be used i B.py), then you could simply have the code that returns x in a function called compute_x() and then in B.py write from A import compute_x and call the function in B.py.

Dronir
  • 866
  • 7
  • 10
  • But will compute_x not take some time to execute everytime I execute B? – ab123 Jun 08 '18 at 12:13
  • Ah, yes it will. If x never needs to be recomputed then saving it to a file is the way to go. There's various ways for that, and pickle as described by nicoco is maybe the most straightforward. – Dronir Jun 08 '18 at 12:24