Skip to content

Instantly share code, notes, and snippets.

View wrunk's full-sized avatar

Warren Runk wrunk

View GitHub Profile
@wrunk
wrunk / simple_urllib2_example.py
Created February 27, 2012 19:11
Simple python web page get using urllib2
#! /usr/bin/env python
'''
Super dumb example of getting the contents of a web page.
'''
import urllib2
page_contents = urllib2.urlopen('http://www.python.org/').read()
print page_contents
@wrunk
wrunk / httplib2_post_json.py
Created February 17, 2012 20:49
Python httplib2 POST json body with auth example
#! /usr/bin/env python
'''
This is more of a personal reference as I use json very often.
The httplib2 examples are VERY good, and you should refer to them:
http://code.google.com/p/httplib2/wiki/Examples
'''
from httplib2 import Http
try:
@wrunk
wrunk / simplejson2.3.0_encode_issue.py
Created December 29, 2011 16:53
Simplejson 2.3.0 object dictionary encode issue
from simplejson import JSONEncoder
class ObjectDictWorks(dict):
""" Makes a dictionary behave like an object. """
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)
@wrunk
wrunk / del_files_older_than.py
Created December 22, 2011 02:14
Python clean files from a directory
'''
Pass a time in seconds, and a base directory.
I will nuke anything older than now - seconds in that directory. Optionally
recursively
'''
from datetime import datetime
import os
from optparse import OptionParser
@wrunk
wrunk / opt_parse.py
Created December 22, 2011 00:29
Python option parse example (optparse OptionParser)
from optparse import OptionParser
import sys
def example_function():
print "Hello world!"
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
@wrunk
wrunk / haproxy_stats_cmds.c
Created December 20, 2011 01:46
Haproxy stats commands
const char stats_sock_usage_msg[] =
"Unknown command. Please enter one of the following commands only :\n"
" clear counters : clear max statistics counters (add 'all' for all counters)\n"
" help : this message\n"
" prompt : toggle interactive mode with prompt\n"
" quit : disconnect\n"
" show info : report information about the running process\n"
" show stat : report counters for each proxy and server\n"
" show errors : report last request and response errors for each proxy\n"
" show sess [id] : report the list of current sessions or dump this session\n"
@wrunk
wrunk / path.py
Created October 28, 2011 00:18
Get current python file path
#!/usr/bin/env python
# Stupid gist by warren runk
import os
if __name__ == "__main__":
full_file_path = os.path.realpath(__file__)
print 'Full path to this current file is', full_file_path
@wrunk
wrunk / jinja2_file_less.py
Last active September 19, 2023 15:05
python jinja2 examples
#!/usr/bin/env/python
#
# More of a reference of using jinaj2 without actual template files.
# This is great for a simple output transformation to standard out.
#
# Of course you will need to "sudo pip install jinja2" first!
#
# I like to refer to the following to remember how to use jinja2 :)
# http://jinja.pocoo.org/docs/templates/
#
@wrunk
wrunk / logging.conf
Created September 26, 2011 02:34
Getting setup with proper python logging
# ---------------------------------------------------------------------------- #
# Standard setup stuff. In general you will want to alter the formatters
#
[formatters]
keys: detailed,simple
[handlers]
keys: console
[formatter_simple]
@wrunk
wrunk / simple.py
Created June 24, 2011 18:32
Simple python script for bustin
#!/usr/bin/env python
# The above line will cause this script to be run using the program 'python'
# Lines that start with a hash are comments for fun
# The interpreter wont read these.
def print_function():
print '''
******* * *
* * *