Tuesday, December 18, 2012

Python: apply method or function in string format to a string

StackOverflow came in very handy for this: http://stackoverflow.com/questions/3061/calling-a-function-from-a-string-with-the-functions-name-in-python

If you wish to apply a method or function call that is currently in string format (e.g., you got an option with optparse) and wish to apply that method or function to another string, use getattr.
getattr(your_string, method_you_want_to_apply)()
The parentheses at the end apply the function or method rather than merely trying to obtain the attribute. 


Check for Kerberos ticket or authentication

Some commands require a valid Kerberos ticket, but if you want to integrate a check for a ticket yourself, try this:
/usr/kerberos/bin/klist -5 -c -s 2