This code connects to a mongodb database and returns collection objects. I am concerned about the exception handling, variable names and comments.
import config_files
import tweepy
import csv
import warnings
import importlib
import sys
from MongodbConnections import MongodbConnections
#Disable warning messages
warnings.filterwarnings("ignore")
#import parameters
class ControlArguments:
"""
method description: confirms mongodb client connection
input: None
output: Boolean(True/False)
"""
def checkDBconnection(self):
obj = MongodbConnections(self.getEnvironment())
try:
obj.getClient()
return True
except:
return False
"""
method description: returns LR user twitter screen name
input: command line argument
output: String
"""
def getUserScreenName(self):
return sys.argv[2]
"""
method description: returns User key token
input: None
output: String
"""
def getUserKeyToken(self):
return sys.argv[5]
"""
method description: return user secret token
input: None
output: String
"""
def getUserSecretToken(self):
return sys.argv[6]
"""
method description: return congif file object
input: None
output: importlib module
"""
def getConfig(self):
return importlib.import_module('config_files.config_'+self.getEnvironment())
"""
method description: return application consumer key
input: None
output: String
"""
def getApplicationConsumerKey(self):
return self.getConfig().application_consumer_key
"""
method description: return application consumer secret
input: None
output: String
"""
def getApplicationConsumerSecret(self):
return self.getConfig().application_consumer_secret
"""
method description: confirms if 6 argumets are sent
input: None
output: Boolean
"""
def checkArgumentsLength(self):
if len(sys.argv) == 7:
return True
else:
return False
"""
method description: returns keyword
input: None
output: String
"""
def getKeywordsIds(self):
return sys.argv[3].split('%')
"""
method description: returns environment of execution
input: None
output: String
"""
def getEnvironment(self):
return sys.argv[1]
"""
method description: returns debugger object
input: None
output: importlib object: debugger
"""
def getDebugger(self):
return importlib.import_module('config_files.debugger')
"""
method description: returns keyword from keyword ids
input: None
output: List of Strings
"""
def getKeywords(self):
keywords = []
obj = MongodbConnections(self.getEnvironment())
for keyid in self.getKeywordsIds():
try:
word = obj.getKeywordIndexCol().find_one({'keyid':keyid})['keyword']
self.getDebugger().info([keyid, word], self.getConfig().debug)
keywords.append(word)
except:
return False
return keywords
"""
method description: confirms user oAuth authentication
input:None
output: Boolean
"""
def twitterAuth(self):
try:
auth = tweepy.OAuthHandler(self.getApplicationConsumerKey(), self.getApplicationConsumerSecret())
auth.set_access_token(self.getUserKeyToken(), self.getUserSecretToken())
return [True,auth]
except:
return [False,0]
"""
method description: confirms, connects and returns twitter api object
input: None
output: api connection object
"""
def connectTwitterAPI(self):
try:
auth = self.twitterAuth()
api = tweepy.API(auth[1])
api.rate_limit_status()
return [True,api]
except:
return [False,0]
#extract keywords
"""
method description: returns autofavorite mode of user
input: None
output: integer(1/0)
"""
def getFavoriteMode(self):
try:
auto = int(sys.argv[4])
if auto == 1 or auto == 0:
return [True,auto]
else:
return [False,0]
except:
return False