Skip to content

Instantly share code, notes, and snippets.

@countrymarmot
countrymarmot / overload_override.py
Last active December 31, 2015 20:09
overload, override
#!/usr/bin/env python
# encoding: utf-8
class Bird(object):
def __init__(self, name):
self.name = name
def fly(self):
print self.name + " can fly"
@countrymarmot
countrymarmot / exit.py
Last active December 31, 2015 20:09
function before exit python or ctr+c
#!/usr/bin/env python
# encoding: utf-8
import atexit
import signal
import time
def shutdown():
"""function when exit, exception
#!/usr/bin/env python
# encoding: utf-8
class decorator(object):
def __init__(self, f):
print("inside decorator.__init__()")
f() # Prove that function definition has completed
def __call__(self):
class MyData:
def __init__(self, max):
self.max = max
self.n = 0
def __iter__(self):
return self
def __str__(self):
return "test"
@countrymarmot
countrymarmot / arg.py
Created December 20, 2013 03:13
argparse test
#!/usr/bin/env python
# encoding: utf-8
import argparse
def parse_args():
parser = argparse.ArgumentParser(description="example")
parser.add_argument('-s', dest='singlevalue', action='store', help='store a value')
@countrymarmot
countrymarmot / logging_test.py
Last active January 1, 2016 07:49
logging to file and print to screen
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import logging
LOG_FILE = 'error.log'
#
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
#
import ctypes
import logging
import os
class ColorizingStreamHandler(logging.StreamHandler):
# color names to indices
color_map = {
@countrymarmot
countrymarmot / timeout.py
Created December 26, 2013 05:10
decorator to calculate python function timeout and elapsed time.
import signal
import time
from functools import wraps
class TimeoutException(Exception):
pass
def timethis(func):
@countrymarmot
countrymarmot / magic_test.py
Created December 31, 2013 06:51
magic method execution sequence
class Test(object):
def __init__(self):
print "hello"
def __enter__(self):
print "enter"
def __call__(self, msg):
print "call " + str(msg)
@countrymarmot
countrymarmot / string_test.py
Created January 6, 2014 04:11
test the basic encypt method
def string_process(string, encrypt):
int_list = []
for s in string:
int_list.append(ord(s))
result = ''
for i in int_list:
if i >= ord('A') and i <= ord('z'):
if encrypt:
result += chr(i - 3)