Skip to content

Instantly share code, notes, and snippets.

View wzpan's full-sized avatar
:octocat:
Focusing

潘伟洲 wzpan

:octocat:
Focusing
View GitHub Profile
@wzpan
wzpan / lxml.py
Created August 2, 2013 13:49
Python - xml parser
try:
from lxml import etree
except ImportError:
import xml.etree.ElementTree as etree
tree = lxml.etree.parse('file.xml')
@wzpan
wzpan / pickle.py
Created August 3, 2013 06:34
Python - pickcle I/O
import pickle
# write object to file
with open('entry.pickle', 'wb') as f:
pickle.dump(entry, f)
# read object from file
with open('entry.pickle', 'rb') as f:
entry = pickle.load(f)
@wzpan
wzpan / pickleversion.py
Created August 3, 2013 07:12
Python - pickle version
# usage:
# >>> import pickleversion
# >>> with open('entry.pickle', 'rb') as f:
# ... v = pickleversion.protocol_version(f)
# >>> v
import pickletools
def protocol_version(file_object):
maxproto = -1
@wzpan
wzpan / http.py
Created August 3, 2013 14:30
Python - http web
# get
import httplib2
h = httplib2.Http('.cache')
response, content = h.request('http://diveintopython3.org/examples/feed.xml')
# post
from urllib.parse import urlencode
import httplib2
@wzpan
wzpan / rc.lua
Created August 6, 2013 04:01
My Awesome Config
-- Standard awesome library
local gears = require("gears")
awful = require("awful")
awful.rules = require("awful.rules")
require("awful.autofocus")
-- Widget and layout library
local wibox = require("wibox")
-- Theme handling library
local beautiful = require("beautiful")
-- Notification library
@wzpan
wzpan / raise.py
Created August 17, 2013 04:07
Python - raise
#!/usr/bin/python
# Filename: raising.py
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
@wzpan
wzpan / doctest.py
Last active December 21, 2015 06:48
Python - doctest
# add this to the end of the module
if __name__ == "__main__":
import doctest
doctest.testmod()
@wzpan
wzpan / setup.py
Created August 19, 2013 08:28
Python - setup.py
from distutils.core import setup
setup(
name = "fibonacci",
packages = ["fib"],
version = "1.0",
description = "An generator for fibonacci series",
author = "Joseph Pan",
author_email = "cs.wzpan@gmail.com",
url = "http://hahack.com/",
#!/usr/bin/python
####
# 02/2006 Will Holcomb <wholcomb@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
@wzpan
wzpan / upload.py
Created August 20, 2013 09:47
Python - upload a file
import MultipartPostHandler, urllib2, cookielib
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler)
params = {"username": "bob", "password": "riviera", "file": open("filename", "rb")}
opener.open("http://wwww.bobsite.com/upload/", params)