Skip to content

Instantly share code, notes, and snippets.

View stuartmyles's full-sized avatar

Stuart Myles stuartmyles

View GitHub Profile
@stuartmyles
stuartmyles / SNSTopic.py
Last active February 6, 2020 22:36
A complete example of how to use Amazon Web Services Simple Notification Services from Python. This code uses the boto library https://github.com/boto/boto to create a topic, wait for a confirmation and then send a success message. Simply plug in your AWS access and secret keys, plus your email and mobile phone number.
# An example of how to use AWS SNS with Python's boto
# By Stuart Myles @smyles
# http://aws.amazon.com/sns/
# https://github.com/boto/boto
#
# Inspired by parts of the Ruby SWF SNS tutorial http://docs.aws.amazon.com/amazonswf/latest/developerguide/swf-sns-tutorial-implementing-activities-poller.html
# And the Python SNS code in http://blog.coredumped.org/2010/04/amazon-announces-simple-notification.html and http://awsadvent.tumblr.com/post/37531769345/simple-notification-service-sns
import boto.sns as sns
import json
@stuartmyles
stuartmyles / Sports in JSON Experiment - "Flattened Players"
Created June 11, 2014 13:56
One early experiment to represent a sports event in JSON - this one using "flattened" team and player structures
{
"competionid": "123456",
"eventid": "78901",
"competionname": "FIFA World Cup 2014",
"eventname": "FIFA World Cup 2014 Final",
"competitionstartdate": "2014-12-06",
"compeitionenddate": "2014-13-07",
"eventstartdate": "2014-07-13T19:00Z",
"eventtimeelapsed": "54",
"eventvenueid": "111668",
@stuartmyles
stuartmyles / Sports in JSON Experiment - Players in Teams
Created June 11, 2014 14:10
One early experiment to represent a sports event in JSON - this one using players "inside" teams
{
"competionid": "123456",
"eventid": "78901",
"competionname": "FIFA World Cup 2014",
"eventname": "FIFA World Cup 2014 Final",
"competitionstartdate": "2014-12-06",
"compeitionenddate": "2014-13-07",
"eventstartdate": "2014-07-13T19:00Z",
"eventtimeelapsed": "54",
"eventvenueid": "111668",
import ast
class Greeter:
def __init__(self):
#m = "Hello world!"
assignment = ast.Module(body=[ ast.Assign(targets = [
ast.Name(id = 'm', ctx = ast.Store())],
value = ast.Str(s="Hello world!"))
# Use ast to generate an abstract syntax tree to assign an empty tuple to a variable named "nothing"
# i.e. equivalent to
# nothing = ()
import ast
emptyness = ast.Module(body=[ ast.Assign(targets = [
ast.Name(id = 'nothing', ctx = ast.Store())],
value = ast.Tuple(elts=[], ctx = ast.Load()))
])
@stuartmyles
stuartmyles / astCallAFunction.py
Created August 9, 2015 20:52
Python ast code to call a function _foo() and assign the returned value to a variable called "result", i.e. equivalent to result = foo()
import ast
# ast code to call a function _foo() and assign the returned value to a variable called "result", i.e. equivalent to
# result = _foo()
def _foo():
return "It worked!"
assignresult = ast.Module(body=[ ast.Assign(targets = [
@stuartmyles
stuartmyles / asCallAFunctionWithAParameter.py
Created August 9, 2015 21:07
Python ast code to call a function _bar(), pass in a value and assign the returned value to a variable called "result", i.e. equivalent to result = _bar("theResult")
import ast
# ast code to call a function _bar(), pass in a value and assign the returned value to a variable called "result", i.e. equivalent to
# result = _bar("theResult")
def _bar(theStr):
return theStr
assignresult = ast.Module(body=[ ast.Assign(targets = [
ast.Name(id = 'result', ctx = ast.Store())],
value = ast.Call(func = ast.Name(id='_bar', ctx = ast.Load()), ctx = ast.Load(), args=[ast.Name(id="theResult", ctx = ast.Load())], keywords=[]))
@stuartmyles
stuartmyles / astCallAnInstanceObjectMethod.py
Created August 9, 2015 21:46
Python ast code to call an instance object method and pass in a parameter, i.e. equivalent to result = self._baz(theResult)"
import ast
# ast code to call an instance object method and pass in a parameter, i.e. equivalent to
# result = self._baz(theResult)"
class Greeter:
def _baz(self, theStr):
return theStr
def baz(self, theResult):
@stuartmyles
stuartmyles / astParseAndDump.py
Created August 9, 2015 22:03
Python ast code to figure out how to call an instance object method and pass in a parameter, i.e. equivalent to result = self._baz(theResult)"
import ast
# ast code to figure out how to call an instance object method and pass in a parameter, i.e. equivalent to
# result = self._baz(theResult)"
class Greeter:
def _baz(self, theStr):
return theStr
def baz(self, theResult):