Codejam之初章 python decorator 版

然后用函数修饰符把Codejam模版又写了一遍。哈哈哈哈

 更新:用带参数的函数修饰符重写了。


 

'''
Created on 2012-12-11

@author: festony
'''

import sys
import time

__all__ = [ \
           'rec_proc_time', \
           'multi_cases', \
           'redir_stdin', \
           'output_into_file', \
           'read_n_strs'\
           ]

default_input_file_path = '..\\inout\\testin.txt'
default_output_file_path = '..\\inout\\testout.txt'

# decorators.

def rec_proc_time(func_name=None):
    def real_rec_proc_time(func):
        def modified_func(*arg, **args):
            start_time = time.clock()
            r = func(*arg, **args)
            case_num = ''
            if arg:
                case_num = ' ' + str(arg[0])
            print "Function {%s%s} process time: %g sec(s)" % \
            (func_name, case_num, time.clock() - start_time)
            return r
        return modified_func
    return real_rec_proc_time

def multi_cases(func):
    def modified_func(*arg, **args):
        case_total_num = int(sys.stdin.readline().rstrip('\r\n'))
        r = ''
        for i in range(case_total_num):
            r += func(i+1) + '\n'
        return r
    return modified_func

def redir_stdin(input_file_path=default_input_file_path):
    def real_redir_stdin(func):
        def modified_func(*arg, **args):
            oldstdin = sys.stdin
            inputfile = open(input_file_path, 'r')
            sys.stdin = inputfile
            r = func(*arg, **args)
            sys.stdin = oldstdin
            return r
        return modified_func
    return real_redir_stdin

def output_into_file(output_file_path=default_output_file_path):
    def real_output_into_file(func):
        def modified_func(*arg, **args):
            outputfile = open(output_file_path, 'w')
            r = func(*arg, **args)
            outputfile.write(r)
            print r
            return r
        return modified_func
    return real_output_into_file

# commonly used functions

def read_n_strs():
    """
    Read strings from stdin.
    
    The number of strings read in is the first input.
    e.g. input:
    3
    aaaa
    bbbbb b
    ccc
    dd dddd
    result in read first 4 strings then returns them.
    """
    n = int(sys.stdin.readline().rstrip('\r\n'))
    r = list()
    for i in range(n):
        r.append(sys.stdin.readline().rstrip('\r\n'))
    return r

# Test
if __name__ == '__main__':
    @rec_proc_time('Whole') # Record process time of the whole case runs
    @redir_stdin()          # Redirect std input
    @output_into_file()     # Copy the output into the output file.
    @multi_cases            # Run it N cases.
    @rec_proc_time('Case')  # Record process time of each single case runs
    def process_func(case_num):
        print 'Round', case_num
        return 'Case #%d: %s' % (case_num, '0')
    
    # Run it!
    process_func()

        


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值