Skip to content

Instantly share code, notes, and snippets.

@swshan
Last active September 16, 2015 08:45
Show Gist options
  • Save swshan/a0e26be596bed2b902d9 to your computer and use it in GitHub Desktop.
Save swshan/a0e26be596bed2b902d9 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
#coding:utf-8
import random
raw = [1,2,3,4,5,6,7,8,9,0]
print raw
b = raw.pop(0)
print b
raw.append(b)
print raw
# ~~~~~~~~另一个练习~~~~ MYSQLdb
import MYSQLdb
db = MYSQLdb.connect(host="localhost", #host
user="john", # username
passwd="megajonhy", #password
db="jonhydb")
# you must create a Cursor object. It will let you execute all the queries you need
cur = db.cursor()
# Use all the SQL you like
cur.execute(SELECT * FROM YOUR_TABLE_NAME)
# print the first cell of all the rows
for row in cur.fetchall():
print row[0]
list = [random.randint(0,100) for i in range(40)]
print list
def add_function(a,b):
c = a + b
print c
add_function(6,6)
def fibs(n):
result = [0, 1]
for i in range(n-2):
result.append(result[-2] + result[-1])
return result
lst = fibs(10)
print lst
def func(x, *arg):
print x
result = x
print arg
for i in arg:
result += i
return result
print func(1)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print fib(10)
# CS11Python
class Animal:
""" Inheritance example """
def __init__(self, weight):
self.weight = weight
def eat(self):
print "I am eating"
def __str__(self):
return "Animal; weight = %d % \
self,weight"
class Super:
def __init__(self, x):
self.x =x
class Sub(Super):
def __init__(self, y):
Super.__init__(self, y)
self.y = y
##############
import eventlet
from eventlet.green import urllib2
urls=["http://www.douban.com",
"http://weibo.com"]
def fetch(url):
return url, urllib2.urlopen(url).info()
pool =eventlet.GreenPool(2)
for url, body in pool.imap(fetch,urls):
print str(body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment