Wednesday, May 9, 2012

Using Static Methods on a Class

Static Methods are functions that can be called from a class without creating that object.  From python.org - "A static method does not receive an implicit first argument"
This means basically that it is not dependent on the class from within it resides, but it is an associated function to the overall class. 

Take the example below:
import arcpy
import os
class StaticExample(object):

    def __init__(self,*argv):
        '''
        Constructor
        '''
        pass
    @staticmethod
    def createSDEFromConnectionInfo(folderName,
                           fileName,
                           serverName,
                           serviceName,
                           databaseName,
                           username,
                           password,
                           versionName,
                           saveUserInfo,
                           saveVersionInfo,
                           authType,
                           overwriteExisting=True):
        """
            Static Method To Create an SDE Connection File
            Inputs:
                folderName - The folder path where the .sde file will be stored.
                fileName -  The name of the ArcSDE Connection file. Use the .sde file extension.
                serverName - The ArcSDE Server machine name.
                serviceName - The ArcSDE Service name or TCP port number.
                databaseName - For non-Oracle databases only. The DBMS database to connect to.
                username - Database username to connect with using Database Authentication.
                password - The database user password when using Database Authentication.
                versionName -  The Geodatabase version to connect to.
                saveUserInfo - SAVE_USERNAME —Save the username and password in the connection file.
                               DO_NOT_SAVE_USERNAME —Do not save the username and password in the file.
                               Every time you attempt to connect using the file you will be prompted for
                               the username and password.
                saveVersionInfo - SAVE_VERSION —Save the version name in the connection file.
                                  DO_NOT_SAVE_VERSION —Do not save the version name in the connection file.
                                  Without the version name being saved with the file you will be prompted
                                  for the version to connect to each time you access the connection file.
                authType - Either DATABASE_AUTH or OPERATING_SYSTEM_ATUH - forms of authentication
                overwriteExisting - Tells the function if it can overwrite the .sde file.
        """
        if overwriteExisting == True and \
           os.path.isfile(folderName + os.sep + fileName) == True:
            os.remove(folderName + os.sep + fileName)
        arcpy.CreateArcSDEConnectionFile_management (folderName, fileName,
                                                     serverName, serviceName,
                                                     databaseName, authType,
                                                     username, password,
                                                     saveUserInfo, versionName,
                                                     saveVersionInfo)


To declare your static method, use the descriptor tag @staticmethod.  This tells the class that this function does not need to know about itself.  Using the function is easy:

print StaticExample.createSDEFromConnectionInfo(parm1,param2,....)
>>> c:\temp\mysde.sde


It sometimes is helpful to have functions that are associated with a class be accessible without creating the class object. Some would say just put the function in the .py file outside the class, but then you lose the ability of inheritance on that function.

Happy Coding

2 comments:

Allan said...

I think this would be better implemented as just a function. In the case of putting the function "outside the class", you'd still have access to the function when you import the python module that contains it.

In this case it's being used as a function, so the extra lines of code only adds overhead.

Andrew said...

I understand what you are saying, but if a function belongs to a specific class and is associated with the functionality of the class as shown, then it should be inside the class as a static method.
Also if you are building collections of classes, functions, etc.. and you don't want just that functionality to be imported but rather the whole class, then that is another reason to do it that way.