python小汇总

in #cn-dev6 years ago

输出更完善的异常信息

import sys
def PrintException(msg=''):
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    print('EXCEPTION IN ({}, LINE {} "{}"): {}, {}'.format(filename, lineno, line.strip(), exc_obj, msg))

元组中单元素问题

test = []
test1 = []
test2 = []
for v in range(1,5):
    test.append((v, ))
    test1.append((v, v))
    test2.append((v))
print(test, test1, test2)

单元素添加进元组的时候,需要加逗号。

Sort:  

@ety001 You have earned a random upvote from @botreporter & @bycoleman because this post did not use any bidbots.

你今天过的开心吗?才子,才女,你参加月旦评了吗?这可是我们中文区最大的活动之一啊。倘若你想让我隐形,请回复“取消”。

Congratulations @ety001! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last announcement from @steemitboard!

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

更新异常信息输出函数

import traceback
def PrintException(msg=''):
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    print('EXCEPTION IN ({}, LINE {} "{}"): {}, {}, {}'.format(filename, lineno, line.strip(), exc_obj, msg, traceback.print_exc()))

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.034
BTC 63173.85
ETH 3147.56
USDT 1.00
SBD 3.87