[Code] Python and Houdini Python (2)

Learning Python, so here is jotted-down notes that might be useful, part 2.

LOG 2

* 1 – Strings

  • \n => Enter new line of string; \t => Tab
  • ” \ ” is also used as an escape for some special characters: ‘ can\’t ‘ => can’t
  • r’…’ => turns string into a rawstring. r’some\tasdfj’, write everything out with no special character
  • ”’ (triple quotes) support multi-line strings, or write separate strings joined by “\” at the end of each line.
  • Function Examples:
    Similar string & array uses
    mystring[5] => access 5th char in string, slicing like an array
    mystring.count(‘t’) => check number of ‘t’ in string
    mystring.index(‘a’) / mystring.rindex(‘a’): get the first letter ‘a’ from left for index, from right for rindex.

    Specific string uses
    mystring.strip() => strip blank space at beginning and end
    mystring.strip(‘_’).strip(‘*’)
    mystring.lower() => change to lowercase
    mystring.upper() => change to uppercase
    mystring.capitalize() => capitalize first letter, the rest lowercase
    mystring.title() => capitalize first letter of every word
    mystring.startswith() – mystring.endswith() => returns a Bool, check relative/explixit path or file name type
    mystring.find(‘s’)
    mystring.replace(‘$F3’, hou.frame()) => find what to replace, what to replace with

    mystring.split() => extract each word, drop all space, put into a list
    mystring.split(‘/’) => split, but with a divider
  • Strings can use keyword to add variable to when using the .format() function, or unwrap a dictionary in .format() arguements
  • mystring.format() can do padding a number variable added into the string
    mystring = ‘my var is {0:0>4}’.format(56) => Right-aligned, pad 4 digits
    mystring = ‘my var is {0:0^20}’.format(56) => Center-aligned, pad 20 digits
    mystring = ‘my var is {0:0<4}’.format(56) => Left-aligned, pad 6 digits
  • Join strings together: use string function ‘.join(listname)

* 2 – Modules

  • import modulename
  • from modulename import functionname
  • import modulename as aliasname
  • users import module, then use a variable to reference the module or a module’s function
    foo = random.random => reference random function from random module. random.random() will run function then assign value to variable, we don’t want that here
  • Some useful modules: random, sys, os (operating system), re (regular expression), pprint
  • dir(modulename) => list all function names of the module

* 3 – User-defined Functions

  • def myfunction(): => define function
  • function could have multiple arguments including arbirary arguments, could return or not return anything
  • put all non-defaults arguments before default arguments when defining functions
  • call functions: place all non-keyword arguments before keyword arguments
  • when calling functions that have multiple default arguments, using keywords to call is better, so python will default with the arguments we’re not calling
  • return can be used to check within function, like break used in a loop
  • args is a good name convention for an arbitrary argument. Arbitrary arguments are defined after non-default and default arguments, however they don’t like keywords in calling functions.
  • kargs is a good name convention for dictionary arbitrary keyword argument.

* 4 – Classes

  • Class – properties (variables) – methods (functions)
  • Create class, ‘pass’ to finish
  • Class has ‘magic method’ defined within the class: def __init__():
  • For every method defined inside a class, the zero argument will always be ‘self’
  • __repr__() => magic method to represent when you call a class instance, you can return a string in the method
  • __add__(self, other): => some magic method includes teaching python to do mathematical function like + – between objects or objects and int (or float).
  • a variable defined inside a function does not exist outside of a function unless it was defined outside beforehand.
  • Good coding habit: do not duplicate when you already have variables, code and test as if the users know nothing => idiot-proof is the goal

* 5 – Errors

  • Type of errors: syntax error, logic error, runtime error
  • Python does ‘duck typing’, so sometimes you need to check for TypeErrors
  • Try – Except block: skip errors and move on to perform sth else, however don’t overuse this as a coding habit.

* 6 – Help (Python documentation & Houdini documentation)

  • Be careful with similar spelling and capital letters
  • if a function’s argument has ‘=’ sign, meaning it has default, if you don’t give the argument, it will use the default.
  • if a function’s argument does not have ‘=’ sign, you have to give the argument.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: