Design a site like this with WordPress.com
Get started

[Code] Houdini Python (6)

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

LOG 6

* 1 – os module

  • os means operating system
    Each system, like Linux or Window, uses different directory syntax, like backslash (“\”) and forward slash (“/”)
  • Example:
    root = …….. (some location)
    txt_doc = …..(location) / filename.txt
  • os.mkdir(‘{0}/{1}’.format(root, ‘something’)) => create directory named ‘something’ at folder root
    os.makedirs(‘{0}/{1}/{2}’.format(root, ‘level2’, ‘level3’)) => recursively create folders

    os.rename(‘{0}/{1}/{2}’.format(root, ‘level2’, ‘level3’), ‘{0}/{1}/{2}’.format(root, ‘level2’, ‘whatever’)) => give full path for old and new names
  • os.access(txt_doc, os.W_OK) #use to check on directories and files for existence and ability to read/write
    4 modes:
    F_OK: check existence and access
    R_OK: readable?
    W_OK: write ok?
    X_OK: executable?os.path.join => take a existing path, join another directory to the end of that
  • os.sep => giving the default separator of the operating system
    os.sep.join(root.split(‘/’)) => split ‘root’ string separated by ‘/’ into list, then joining different componoents of the list by the separator
  • os.path => module designed to work with paths
    os.path.split(root) => split the path and the current working dir
    os.path.splitext(txt_doc) => split the path to file and its extention
    os.path.splitdrive(txt_doc) => split the drive and the path
    os.path.isdir(root) => check if it’s a file or directory
    os.path.dirname(txt_doc) => return the directory path to the current file
  • open(path, ‘access mode’) function
    myfile = open(txt_doc, ‘r+’)
    myfile.write(‘Hahahahahahahahaha’)
    myfile.close()

    python will open it in the background, create a reference in memory, but we have to close it afterward for things you write in to register
    access modes:
    r: read
    r+: read and write, reads first then write
    w: write
    w+: write and read, blank slate & overwrite entirely
    a: append
    a+: append and read

* 2 – json module

  • jsoon: JavaScript Object Notation
  • How json file store data
    json files help store data externally from project file, help storing a hierachy of dictionary and lists.

    Notice the hierachy: dictionary – list – dictionary
    a dictionary with ‘humans’ keyword, value is a list of different humans, within each human is a dictionary of different attributes.
  • Example:

from pprint import pprint
mydata = {
‘humans’: [
{
‘Name’: ‘Tony’,
‘Age’: 22,
‘EyeColor’: ‘Brown’
},
{
‘Name’: ‘Alexis’,
‘Age’: 27,
‘EyeColor’: ‘Black’
}
]
}

mike = {
‘Name’: ‘Mike’,
‘Age’: 30,
‘EyeColor’: ‘Blue’
}

mydata[‘humans’].append(mike) => add ‘mike’ to the human list
pprint(mydata)

  • Write data out to json file
    path = ‘D:/01_PROJECTS/houdini/config/foo.json’
    with json files, if the file does not exist, python will create it for us

    #basic dumping to json
    file = open(path, ‘w’)
    json.dump(mydata, file)
    file.close()

    #dump data to json using with
    with open(path, ‘w’) as file:
    json.dump(mydata, file, sort_keys=True, indent=4) => sort keys and set indent for json file, 4 is commonly used

    #load data from json
    with open(path, ‘r’) as file:
    data = json.load(file)
    print data[‘humans’][0][‘Name’] => Tony
    => using a with block open and automatically closes the file for us
  • More functions:
    js = json.dumps(mydata) => dumps is dump string, we don’t feed a file to store in, it’s just returning a string that’s been formatted in json and store in our variable in our scene
    dt = json.loads(js) => loads is load string, it read in the json formatted string from a variable in our scene, and convert to python format so we can work with it


End of week 6.

Advertisement

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 )

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

%d bloggers like this: