0
\$\begingroup\$

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
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

So some minor comments to kick things off:

Your method names are not PEP8 compliant. Usually camel case is avoided in Python. Names like checkArgumentsLength should really be more along the lines of check_arguments_length.

It is bad practice to put sys.argv reads in the middle of your classes. Imagine a case where someone imports your code from Ipython, or from another script. This will cause it to break when no command line arguments are found (or the wrong arguments are found). Good practice would be to have those parameters passed as arguments to your class, with the sys.argv parameters only parsed under a conditional if __name__=="__main__" statement. This will add flexibility to the code. I realise that in the case of your code, a lot of what is going on is dependent on the command line arguments, but you might be better off having them parsed by a separate object that then feeds the arguments into your ControlArguments class (perhaps in a dictionary).

New style classes should inherit from object. e.g. class ControlArguments: should be class ControlArguments(object):

Your docstrings are not PEP257 compliant. They should go inside the functions that they relate to, not before them.

\$\endgroup\$
1
  • \$\begingroup\$ again great advice. agree with each of the points \$\endgroup\$
    – codious
    Commented Apr 3, 2014 at 12:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.