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
3
  • 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. Commented Jan 31, 2017 at 9:15
  • 2
    I suggest you do use a file, pickle it Commented Jan 31, 2017 at 9:16
  • 1
    Possible duplicate of Keep persistent variables in memory between runs of Python script Commented Jan 31, 2017 at 9:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.