Show Menu
Cheatography

Python for Raspberry Pi Cheat Sheet (DRAFT) by

Memory for often used code

This is a draft cheat sheet. It is a work in progress and is not finished yet.

datetime strftime selectors

%a
Weekday name (Sat)
%A
Weekday name (Saturday)
%b
Month name (Oct)
%B
Month name (October)
%c
%d
Day of month (01...31)
%H
Hour (24h)
%I
Hour (12h)
%j
Day of year (001...366)
%m
Month (01...12)
%M
Minute (00...59)
%p
AM / PM
%S
Second (00...61)
%U
Week number (00...53) (Sun)
%w
Weekday number (0(Sun­day­)...6)
%W
Week number (00...53) (Mon)
%x
%X
%y
Year (00...99)
%Y
Year (1990...2099)
%Z
Time zone name

time

from datetime import datetime
i = datetime.now()
print i.strftime("%H:%M:%S")
 

new object

#!/usr/bin/env python
# -- coding: utf-8 --

class SampleObject(object):
    def __init__(self, a):
      self.a = a

   def staticMethod():
      # This is a static method

   def objectMethod(self, b):
      # needs to be instance of class
      b += 1
      return b

if __name__ == "__main__":
   myObject = SampleObject(42)

if clauses

if a > 5 and b < 3:
   # condition 1
elif a < 5 and b < 3:
   # condition 2
else:
   # condition 3

for loop

for i in range(4):
   # do something

Lists

myList = []
myList.append("Another entry")
entry = myList[0]

JSON parsing

import requests
r = requests.get("http://test.com/test.json")
data = r.json()
entry = data['day1']['room2']['temp3']
r.close()