Skip to content

Instantly share code, notes, and snippets.

#netstate-anov_deadloop.py
# works for windows XP SP-2, python2.5
from subprocess import call
import time
import sys
default_netstat_arglist = "-anov"
@photonxp
photonxp / Through BASEServer to TCPServer to HTTPServer
Created March 27, 2014 14:24
Design discussions on inheritance and HTTPServer design
python2.6.5
help("BaseHTTPServer")
It seems that HTTPServer become heavy after it inherits many methods from TCPServer.
Through BASEServer to TCPServer to HTTPServer, in my view it's better to be a pipe-lined structure. I hope HTTPServer could be something like a plugin for TCPServer or BASEServer, because I think the registration mechanism seems to be better than inheritance.
Statistically, the model for Parent Classes are probably much stabler than Child classes. Parent Classes are like infrastrcture. Usually, infrastructure tends to change less frequently than Child classes. The Child Classes don't need to get himself bound with the Parent classes too much, which limit their freedom.
A nice way to judge python version by import statement:
https://github.com/facebook/tornado/blob/master/tornado/web.py
try:
from io import BytesIO # python 3
except ImportError:
from cStringIO import StringIO as BytesIO # python 2
The following lines could be moved to a separate function from def clear
line 248:
if (not self.request.supports_http_1_1() and
getattr(self.request, 'connection', None) and
not self.request.connection.no_keep_alive):
conn_header = self.request.headers.get("Connection")
if conn_header and (conn_header.lower() == "keep-alive"):
self._headers["Connection"] = "Keep-Alive"
@photonxp
photonxp / optparse_exampe_1.py
Last active August 29, 2015 13:58
shows a simple example on how to use optparse module
#!/usr/bin/python
# shows a simple example on how to use optparse module
# works under python2.6.5
# usage 1:
# python optparse_exampe_1.py -o output.txt
# usage 2:
# python optparse_exampe_1.py --help
# usage 3:
#!/usr/bin/python
# usage:
# python optparse_getoptcomparison.py -o output.txt
# works under python2.6.5
# the source of this snippet is from Doug Hellmann's Python Module of The Week
# http://pymotw.com/2/optparse/index.html
import optparse
coverage.py usage command
coverage help classic
========
coverage -rm api/* (show statistics result for files under the path of api)
name@namevirtual:~/.jenkins/jobs/test_coverage/workspace$ coverage -rm namell/api/*
Name Stmts Miss Cover Missing
------------------------------------------------------------
@photonxp
photonxp / transform_unittest.py
Created April 5, 2014 01:04
a demo url test
#!/usr/bin/env python
# Copyright 2008 Brett Slatkin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@photonxp
photonxp / get_company_facebook_info_test.py
Last active August 29, 2015 13:58
CrucnBase api demo usage
#!/usr/bin/python
# simplistic demo
# refer to the api doc of the website for more api interfaces and usage
import urllib
developer_key = "something_you_should_change_here"
REQUEST_URL = "http://api.crunchbase.com/v/1/company/facebook.js?api_key=%s" % developer_key
print "CHECK REQUEST URL:", REQUEST_URL
@photonxp
photonxp / xlsxwriter_demo_usage.py
Last active August 29, 2015 13:58
demo usage for xlsxwriter
#!/usr/bin/python
# the demo code is from https://pypi.python.org/pypi/XlsxWriter
import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()