Skip to content

Instantly share code, notes, and snippets.

View xiantail's full-sized avatar

Ken M. Xu xiantail

  • Tokyo, JAPAN
View GitHub Profile
@xiantail
xiantail / time_lists.py
Created December 28, 2015 23:38
Introducing Python / Chapter 12
from timeit import timeit
def make_list_1():
result = []
for value in range(1000):
result.append(value)
return result
def make_list_2():
result = [value for value in range(1000)]
@xiantail
xiantail / capitals.py
Last active December 28, 2015 22:43
Introducing Python / Chapter12 pdb debugging
def process_cities(filename):
with open(filename, 'rt') as file:
for line in file:
line = line.strip()
if 'quit' in line.lower():
return
country, city = line.split(',')
city = city.strip()
country = country.strip()
print(city.title(), country.title(), sep=',')
@xiantail
xiantail / dump.py
Last active December 28, 2015 22:09
Introducing Python / Chapter 12 dump with decorator
def dump(func):
"Return arguments and outputs"
def wrapped(*args, **kwargs):
print("Function name: %s" % func.__name__)
print("Input arguments: %s" % ' '.join(map(str, args)))
print("Input keyword arguments: %s" % kwargs.items())
output = func(*args, **kwargs)
print("Output:", output)
return output
return wrapped
@xiantail
xiantail / cap_new.py
Created December 28, 2015 14:08
Introducing Python / Chapter 12 unittest - retry no errors
def just_do_it(text):
#Capitalize all words in <text>
return text.title()
@xiantail
xiantail / cap.py
Created December 28, 2015 14:03
Introducing Python / Chapter 12 unittest
def just_do_it(text):
#Capitalize all words in <text>
return text.capitalize()
@xiantail
xiantail / ftoc.py
Created December 28, 2015 13:37
Introducing Python / Chapter 12
F_BOIL_TEMP = 212.0
F_FREEZE_TEMP = 32.0
C_BOIL_TEMP = 100.0
C_FREEZE_TEMP = 0.0
F_RANGE = F_BOIL_TEMP - F_FREEZE_TEMP
C_RANGE = C_BOIL_TEMP - C_FREEZE_TEMP
F_C_RATIO = C_RANGE / F_RANGE
def ftoc(f_temp):
"Convfert Fahrenheit temprature<f_temp> into Celsius "
@xiantail
xiantail / poem_pub.py
Last active December 28, 2015 11:04
Introducing Python / Chap.11 Exercise 11.5
import zmq
import string
import time
host = '127.0.0.1'
port = 6789
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.bind('tcp://%s:%s' % (host, port))
@xiantail
xiantail / redis_choc_supply.py
Last active December 28, 2015 09:58
Introducing Python / Chap.11 Exercise 11.4
import redis
import random
from time import sleep
conn = redis.Redis()
chocolates = ['sweet', 'bitter', 'milk', 'white', 'green tea']
while True:
sleep(random.random())
chocolate = random.choice(chocolates)
conn.rpush('chocolates', chocolate)
@xiantail
xiantail / xmlrpc_server.py
Created December 28, 2015 09:29
Introducing Python / Chap.11 Exercise 11.3
from xmlrpc.server import SimpleXMLRPCServer
from datetime import datetime
def current_time():
now = str(datetime.now())
print('Server sent %s', now)
return now
server = SimpleXMLRPCServer(("localhost", 6789))
server.register_function(current_time, "current_time")
@xiantail
xiantail / zmq_time_client.py
Last active December 28, 2015 09:17
Introducing Python / Chap.11 Exercise 11.2
import zmq
from datetime import datetime
from time import sleep
host = '127.0.0.1'
port = 6789
context = zmq.Context()
client = context.socket(zmq.REQ)
client.connect("tcp://%s:%s" % (host, port))
print("Client started at %s" % datetime.now())