UP | HOME

Class 13: python and control structures

Table of Contents

Introduction

Using if    if

if True:
    print 'yes'
if False:
    print 'yes'

else

if True:
    print 'yes'
else:
    print 'no'
if False:
    print 'yes'
else:
    print 'no'

A special one line if

'equal' if 1==0 else 'really not-equal'
print 'equal' if 1==0 else 'really not-equal'
answer = 'equal' if 1==0 else 'really not-equal'

While loops

count = 0
while count < 10:
    print count
    count += 2

Making a function    function

You will want to break you problem down into sections. One way to do that is to write functions.

def hello():
    print 'hello world function'

# Call it
hello()
def add_one(number):
    new_number = number + 1
    return new_number

# Calling our function.  Pass in the number 9
add_one(9)

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

Getting data!    socat

sudo apt-get install socat
socat TCP4:datalogger1.ccom.nh:36000 - | head
socat TCP4:datalogger1.ccom.nh:36000 - | head
$WIMDA,30.0261,I,1.0168,B,13.0,C,,,,,,,14.1,T,29.5,M,1.8,N,0.9,M*2A,rccom-airmar,1318512281.75
$WIMWD,14.9,T,30.3,M,1.8,N,0.9,M*56,rccom-airmar,1318512281.83
$HCHDT,26.0,T*1D,rccom-airmar,1318512281.87
$WIMWV,348.9,T,1.8,N,A*2A,rccom-airmar,1318512281.92
$WIMWV,347.2,R,1.9,N,A*29,rccom-airmar,1318512281.98
$HCHDT,26.0,T*1D,rccom-airmar,1318512282.27
$GPVTG,339.3,T,354.7,M,0.1,N,0.2,K,D*2A,rccom-airmar,1318512282.35
$GPZDA,132442,13,10,2011,00,00*4B,rccom-airmar,1318512282.42
$WIMWV,347.8,R,1.9,N,A*23,rccom-airmar,1318512282.48
$GPGGA,132442,4308.1264,N,07056.3757,W,2,9,0.9,35.8,M,,,,*00,rccom-airmar,1318512282.6
2011/10/13 09:24:42 socat[82807] E write(1, 0x802e00, 95): Broken pipe

http://gpsd.berlios.de/NMEA.html

Save a bunch to a file:

socat TCP4:datalogger1.ccom.nh:36000 - | head -1000 > ccom-weather.log

Author: Kurt Schwehr

Date: <2011-10-13 Thu>

HTML generated by org-mode 7.4 in emacs 23