Transferring information from one script to another

I have two python scripts, scriptA and scriptB that work on Unix systems. scriptA takes 20 seconds to run and generates an X. scriptB needs X when it starts and takes about 500 ms. I need to run scriptB every day, but scriptA only once a month. Therefore, I do not want to run scriptA from scriptB. I also do not want to manually edit scriptB every time I run scriptA. I thought about updating the file through scriptA, but I'm not sure where such a file can be placed perfectly so that scriptB can read it later; regardless of the location of these two scripts. What is the best way to store this X value on a Unix system so that it can be used later by the B script?

+4
source share
5 answers

Many Linux / Unix programs save the configuration in /etc/and use the subfolder in /var/for other files.
But you may need root privileges.

If you run the script in your home folder, than you can create a file ~/.scripB.rcor folder ~/.scriptB/or~/.config/scriptB/

See also wikipedia File System Hierarchy Standard

+3
source

It looks like you want to serialize the results of ScriptA, save it in a file or database somewhere, then try ScriptB to read these results (maybe also modify the file or update the database record to indicate that these results were processed).

, ScriptA ScriptB, ... , , ScriptB , , ScriptA (, , ScriptA , , ScriptB ).

, ScriptA ScriptB , . DRY. , . (, ... , , import ... - , / ( , , , ) . , ( ), script.

, ... .

, , SQLite3. , over-kill SQL- . SQLite3 Python .

pickle JSON YAML ( )... , - struct. , , . JSON . , , , ScriptA ScriptB (, , , - ) .

SQLite3 , , concurrency . (, ScriptA "--initdb" , ). , :

#!/usr/bin/python
import sqlite3
db = sqlite3.connect('./foo.db')
cur = db.cursor()
results = cur.execute(SELECT value, MAX(date) FROM results').fetchone()[0]

... :

#!/usr/bin/python
# (Same import, db= and cur= from above)
with db:
    cur.execute('INSERT INTO results (value)  VALUES (?)', (myvalue,))

, - (foo.db ) - :

#!/usr/bin/python
# (Same import, db= and cur= from above)
with db:
    cur.execute('CREATE TABLE IF NOT EXISTS results (value INTEGER NOT NULL, date TIMESTAMP DEFAULT current_timestamp)')

( , , ).

, , JSON. SQLite3 ACID () , .

, . , , . , , "" :

#!/usr/bin/python
# (Same import, db= and cur= from above)
with db:
    cur.execute('DELETE FROM results where date < ?', cur.execute('SELECT MAX(date) from results').fetchone())

, , INSERT UPDATE, :

#!/usr/bin/python
# (Same import, db= and cur= from above)
with db:
    cur.execute(cur.execute('UPDATE results SET value=(?)', (mynewvalue,))

( , (mynewvalue,) . DBAPI , , , , ).

, UPDATE, "date" "results" MAX(data) .

, . , , ScriptB , ScriptA , , ).

+3

, , (pickle ) , , . :

import pickle  # or import cPickle as pickle

# Create a python object like a dictionary, list, etc.
favorite_color = { "lion": "yellow", "kitty": "red" }

# Write to file ScriptA
f_myfile = open('C:\\My Documents\\My Favorite Folder\\myfile.pickle', 'wb')
pickle.dump(favorite_color, f_myfile)
f_myfile.close()

# Read from file ScriptB
f_myfile = open('C:\\My Documents\\My Favorite Folder\\myfile.pickle', 'rb')
favorite_color = pickle.load(f_myfile)  # variables come out in the order you put them in
f_myfile.close()
+1

Change / run crontab -e:

# this will run every month on the 25th at 2am
0 2 25 * * python /path/to/scriptA.py > /dev/null

# this will run every day at 2:10 am
10 2 * * * python /path/to/scriptB.py > /dev/null

Create an external file for both scripts:

In scriptA:

>>> with open('/path/to/test_doc','w+') as f:
...     f.write('1')
... 

In scriptB:

>>> with open('/path/to/test_doc','r') as f:
...     v = f.read()
...
>>> v
'1'
+1
source

You can take a look at PyPubSub. This is a python package that provides the Python API for publishing a subscription, which facilitates event-based programming.

It will provide you with an OS-independent solution to your problem and require only a few extra lines of code for both A and B.

Also you do not need to process messy files!

+1
source

Source: https://habr.com/ru/post/1546634/


All Articles