Skip to content

Instantly share code, notes, and snippets.

View benhosmer's full-sized avatar

Ben Hosmer benhosmer

View GitHub Profile
@benhosmer
benhosmer / intro-to-python-lists-variables.py
Created August 10, 2012 19:04
Introduction to Python - Applying variables, user input, and lists with the list.append
# A simple friend game score keeper
# This is used to demonstrate variables, user input, and using lists to
# track values. It is meant as a starting point.
username = raw_input("Enter your name: ")
userage = raw_input("Enter your age: ")
friendsname = raw_input("Enter your friends's name: ")
friendsage = raw_input("Enter your friend's age: ")
print "Hello " + username + ", you are " + userage + " years old.\n"

An Introduction to Python

Introduction

What is Python?

  • A programming language that is compiled when you run it. (Explain JIT vs. Compiled languages like C)

How can You use Python?

  • Sort files on your computer
  • Do complex math
  • Create websites
  • Make games
@benhosmer
benhosmer / install-saltstack.sh
Created August 14, 2012 13:19
A simple BASH script to install salt.
#!/bin/bash
# A shell script to install saltstack on Ubuntu
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:saltstack/salt
sudo apt-get update
# You may want both salt-master and salt-minion, uncomment one or both depending on what you need
@benhosmer
benhosmer / web-server.sls
Created August 15, 2012 14:43
A SALT .sls file to install Apache, PHP5, and MariaDB on Ubuntu
apache2:
pkg:
- installed
php5:
pkg:
- installed
mariadb-server-5.5:
cmd.run:
@benhosmer
benhosmer / gsutilmysqlbackup.py
Created August 20, 2012 09:01
A python script that uses Google Sync Utilities to back up a mysql database.
#!/usr/bin/env python
import os
backup_dir = os.getcwd() + "/backups"
os.chdir(backup_dir)
files = filter(os.path.isfile, os.listdir(backup_dir))
files = [os.path.join(backup_dir, f) for f in files] # add path to each file
#files.sort(key=lambda x: os.path.getmtime(x))
@benhosmer
benhosmer / tarit.sh
Created August 20, 2012 09:23
A shell script to tar and zip all directories.
#!/bin/bash
for dir in */
do dir=`echo $dir | tr -d '/'`
echo $dir
tar czf $dir.tar.gz $dir
done
@benhosmer
benhosmer / bottle-server.py
Created September 9, 2012 00:49
Using bottle.py to print a list with line breaks
from bottle import route, run
@route('/')
def index(name=['hello', 'World', 'me']):
return '<br>'.join(name)
run(host='localhost', port=9090)
@benhosmer
benhosmer / config-sys-argv.py
Created September 11, 2012 01:14
Read command-line arguments and a configuration file using python.
#!/usr/bin/env python
import ConfigParser
import sys
options = sys.argv
print "Sys Args: ", options[1]
@benhosmer
benhosmer / yaml-read-parse.py
Created September 11, 2012 09:24
Parsing yaml into a python dictionary.
f = open('user.yaml')
dataMap = yaml.load(f)
f.close()
print ""
print "=-----------="
print "dataMap is a ", type(dataMap), dataMap
print "=-----------="
print "main items are", type(dataMap['main']), dataMap['main']
@benhosmer
benhosmer / unique-list-items.py
Created September 11, 2012 09:27
Get the unique items from a python list using set()
mylist = ['apples', 'bananas', 'oranges', 'strawberries', 'apples']
print "Not Unique List: ", mylist
print "Unique List Using set: ", list(set(mylist)