2

Currently I'm working on tweepy and I need to store tweet id in a variable and want it to retain this value when the script is made to run again. I know I can do this using files and db but I want to do it using environment variable. Looking for a nudge in the right direction. I have set the environment variable from the terminal using the command >export en=1 Here is the code that I'm using :

#!/usr/bin/env python
# encoding: utf-8

import tweepy 
import time
import random
import os
t=time.time()

#my api keys 
consumer_key = "xxx"
consumer_secret = "xxx"
access_key = "xxx"
access_secret = "xxx"


toReply="xxx"
rt=['hello','hi','okay','bye']
#rt=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','you','suck','bruh',':P']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ts=api.user_timeline(screen_name=toReply,count=1)
#g=open('id.txt','r')
#x=g.read()
#g.close()
x=os.environ["en"]
print x
l=[]
#print dir(ts)
#print ts.max_id
#print type(ts.max_id)
def unique(y,l):
    for i in range(0,len(l)):
        if l[i]==y:
            return 0
if int(x)!=ts.max_id:        
    for tweet in ts:
        for i in range(0,len(rt)+1):
            y=random.choice(rt)
            #print y
            if(unique(y,l)!=0):
                    #print("unique")
                    api.update_status("@" + toReply + " "+y, in_reply_to_status_id = tweet.id)
                    #print y
                    l.append(y)

    os.environ["en"]=str(ts.max_id)
    print os.environ["en"]
    '''
    f=open('id.txt','w')
    f.write(str(ts.max_id))
    f.close()
    ''' 
t1=time.time()-t
print t1
Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
Shashwat Siddhant
  • 411
  • 2
  • 5
  • 17
  • 2
    Environment variables are one directional: they go from the command interpreter (or other execution environment) into a program. They are also inherited by descendants of the process. But a program cannot alter its parent's environment. – wallyk Jan 31 '17 at 09:15
  • 2
    I suggest you do use a file, `pickle` it – Chris_Rands Jan 31 '17 at 09:16
  • 1
    Possible duplicate of [Keep persistent variables in memory between runs of Python script](http://stackoverflow.com/questions/6687660/keep-persistent-variables-in-memory-between-runs-of-python-script) – iFlo Jan 31 '17 at 09:17

1 Answers1

1

Pickle is suggested in a comment, but it has the large drawback that you cannot edit a pickle from outside a the Python program.

If you want something like a Windows .ini file, then the Python library module you want is ConfigParser as per this answer How to read and write INI file with Python3?

Another possibility is to use JSON (module json). This can be extremely complex, but if you stick to a single dictionary of key:value pairs it isn't.

Yet another possibility is YAML.

In all cases you may also want atexit to register an exit handler so that the state of your configuration variables will get saved whenever the program exits sensibly.

Community
  • 1
  • 1
nigel222
  • 7,582
  • 1
  • 14
  • 22