Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

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

Wednesday, August 11, 2010

Creating Private Methods in Python

Python, as you may or may not know, is a fully object oriented language and with the newly created ArcPy module for ESRI's ArcGIS Suite, users should start creating programs following some sort of OO design. 
One design pattern to follow is the singleton pattern, which can be found here

Once you have started diving into the world of classes, name spaces, and methods, it's time to figure out how to make methods private.  It's surprisingly simple to do this.  To make something private in python, just put two underscores in front of your method.  That's it, now only that class can access that method. 

Here is a small example:
This class contains two functions.  One that will make the self.foo text upper case, and the other lower case.  The capital method has the two __ in front of the method, and should not be seen by the user, and lower() has no underscores that therefore is public. Using the idle's we can easily show that __capital cannot be used publicly by a user:

Enjoy
A