"""Saves a copy of the portfolio every day at 9:00. """

import time
import simplejson as json
import schedule
import portfolio
import config


def save_portfolio():
    """Saves a copy of the portfolio. """

    positions = portfolio.init_portfolio(config.POSITIONS_FILE)
    positions_json = json.dumps(positions, use_decimal=True)

    with open('positions.json', 'w') as f:
        f.write(positions_json)


save_portfolio()
schedule.every().day.at('9:00').do(save_portfolio)

while True:
    schedule.run_pending()
    time.sleep(1)

@@ -50,16 +50,26 @@
 0. %22%22%22%0A%0A
+import os%0A
 import t
@@ -338,16 +338,251 @@
 =True)%0A%0A
+    if os.path.isfile('positions.json'):%0A        try:%0A            os.makedirs('archive')%0A        except OSError:%0A            pass%0A        os.rename('positions.json',%0A                  'archive/positions.json.%25s' %25 (int(time.time()),))%0A
     with

backup positions.json