Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 2.18 KB

File metadata and controls

115 lines (84 loc) · 2.18 KB

Research Tools Video 9 - Python part 3 - structure

Introduction

This video describes the parts that we will use to construct software with Python.

http://www.rvdata.us/operators/profiles

Assignment statements

depth = 25.2 # meters
shipname = 'R/V Flip'

Dictionaries

calibration_parameters = {
    'soundspeed': [ 1509, 1498, 1501.1, ],
    'shipname':'R/V Revelle',
    'sonar':'EM122',
    'adcp':'NB-150',
    'length' : 83.5,
    'width' : 16
    }
  • Lookup values with [key]
  • List all the keys with .keys()
  • Add key with value: some_dict[key] = value

Function

def hello():
    print 'hello'
# Function with return
def soundspeed():
    return 1501.01 + 0.12
def get_area(length, width):
    return length * width
import math
def distance(x1,y1, x2,y2):
    'coordinates must be in a rectancular coordinate frame for this to work'
    return math.sqrt( (x1-x2)**2 + (y1-y2)**2 )

classes

containers that work together

import math

class Circle(object):
    def __init__(self, radius):
        self.radius = radius

    def get_area(self):
        return 2 * math.pi * self.radius

    def __str__(self):
        return 'Circle of radius ' + str(self.radius)
        
a_circle = Circle(10.2)
print 'area is:', a_circle.get_area()
print str(a_circle)

modules

composed of

  • variables
  • functions
  • classes

The final code

import math

shipname = 'R/V Flip'

def distance(x1,y1, x2,y2):
    'coordinates must be in a rectancular coordinate frame for this to work'
    return math.sqrt( (x1-x2)**2 + (y1-y2)**2 )

class Circle(object):
    def __init__(self, radius):
        self.radius = radius

    def get_area(self):
        return 2 * math.pi * ( self.radius ** 2 )

    def __str__(self):
        return 'Circle of radius ' + str(self.radius)