Skip to content

Instantly share code, notes, and snippets.

@carlynorama
carlynorama / lpthw_ex17_syserrors.py
Last active August 29, 2015 14:19
Based on LPTHW Example 17. Only talks to you if there was a mistake. Uses try.
#http://learnpythonthehardway.org/book/ex17.html
from sys import argv, exit
from os.path import exists
script, from_file, to_file = argv
try:
with open(from_file) as file:
pass
except IOError as e:
#http://learnpythonthehardway.org/book/ex18.html
# this one is like your scripts with argv
#What does the * in *args do?
#That tells Python to take all the arguments to the function and then put them in args as a list. It's like argv that you've been using, but for functions. It's not normally used too often unless specifically needed.
def print_two(*args):
arg1, arg2 = args
print "arg1: %r, arg2: %r" % (arg1, arg2)
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
@carlynorama
carlynorama / LPTHW_call_a_song.py
Created April 19, 2015 23:11
LPTHW exercise 40, working with Classes and Modules. Wanted to be able to create instance variables with a dictionary. This is not populating class variables. It's begging for trouble, but good to know how to do.
from song import Song
happy_b_day_dict = {
'name' : "Happy Birthday",
'lyrics': ["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"]
}
happy_b_day = Song(happy_b_day_dict)
@carlynorama
carlynorama / jokes.txt
Created April 20, 2015 17:43
Knock Knock Joke Teller, Draft messing around with python. Didn't want to write a text game. Rough state.
Canoe Canoe come out and play with me today?
Lettuce Lettuce in, it's cold out here.
Honey bee Honey bee a dear and get me some juice.
Wooden shoe Wooden shoe like to hear another joke?
A broken pencil Oh never mind it's pointless.
Cow says No silly, a cow says Mooooo!
Double W!
Mikey Mikey doesn't fit in the keyhole!
Atch Bless you!
I am You don't know who you are?
@carlynorama
carlynorama / motor_serial_test01.ino
Last active August 29, 2015 14:20
Software Serial control of Motors Example for Art Project. Written without access to the IDE.
#include <SoftwareSerial.h>
#include <SyRenSimplified.h>
SoftwareSerial motor01_Serial(NOT_A_PIN, 11); // RX, TX
SoftwareSerial motor02_Serial(NOT_A_PIN, 10); // RX, TX
//LED status pin, refecting state of system incase of motor failure.
const int ledPin = 5; // the pin that the LED is attached to
SyRenSimplified Motor01(motor01_Serial); // Use SWSerial as the serial port.
@carlynorama
carlynorama / winston_dance.js
Created July 17, 2015 19:27
Test code for flying ducks (khan academy example)
var move = 0;
var jump = function(faceX, faceY) {
fill(255, 255, 0);
ellipse(faceX, faceY, 180, 180); // face
fill(46, 46, 41);
ellipse(faceX - 30, faceY - 50, 30, 30); // eyes
ellipse(faceX + 44, faceY - 55, 30, 30);
fill(252, 65, 65);
ellipse(faceX + 19, faceY + 32, 82, 81); // mouth
};
@carlynorama
carlynorama / swift_stings.playground
Created February 6, 2016 14:28
Learning how to print strings in Swift using the C++ stdout printf format specifiers
// Working with numbers in strings
import UIKit
var str = "Hello, playground"
let myInt:Int = 7;
let myDouble:Double = 8.4;
// basic
print("The result of \(myInt) multiplied by \(myDouble) is \(Double(myInt) * myDouble)");
// formating fancy
@carlynorama
carlynorama / JavascriptPlaytime.xcplaygroundpage
Created February 6, 2016 15:34
Including Javascript in a Swift File, Simple and Clean.
//: Embedding JavaScript in Swift
import UIKit
import JavaScriptCore
var str = "Hello, playground"
//from http://nshipster.com/javascriptcore/
let context1 = JSContext()
@carlynorama
carlynorama / dictionary_sorting.swift
Last active April 10, 2021 04:31
One line sorting of dictionaries Swift 2.1, both by key and by value examples.
let myDictionary = [
"20" : "banna",
"60" : "apple",
"30" : "cucumber",
"10" : "starfruit"
]
//assume that sort() kinda takes 2 arguments i.e. sort(dictionary element a, dictionary element b)
//woriking its way down the array (http://www.sorting-algorithms.com/bubble-sort)
//compare the 0th assumed parameter(a) with the 1st assumed parameter (b), using the key (0 after decimal)
@carlynorama
carlynorama / reduce_basics.swift
Last active February 7, 2016 16:04
Summing arrays and dictionaires with reduce in Swift 2.1
//Concat strings
print(["one","two","three"].reduce("This string comes before: ",combine:{$0 + $1}))
//Sum an Int array
let numbers = [Int](0..<10)
let total = numbers.reduce(0, combine: +)
//same as numbers.reduce(0, combine: {$0 + $1})
//Dictionary values