Skip to content

Instantly share code, notes, and snippets.

View ZhigangPu's full-sized avatar

Ayron ZhigangPu

  • Hongkong university of science and techonology
  • Hongkong
View GitHub Profile
@ZhigangPu
ZhigangPu / C
Created October 29, 2019 11:36
file operation
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fp;
char ch;
char str[10];
if((fp = fopen("test.csv", "r"))==NULL){
printf("文件打开失败!\n");
exit(1);
@ZhigangPu
ZhigangPu / gist:c1ec5e8d429630a321042c03990dbc3b
Created March 19, 2019 08:31
change file name from png to jpg
import os
for filenames in os.listdir():
if filenames.endwith('png'):
os.rename(filename, filename[:-3]+'jpg')
with open('OceanCoder_SNSD_P2 (5).jpg', 'rb') as f:
pic1 = f.read()
for i in range(1000):
with open('1'+str(i), 'wb') as f:
f.write(pic1)
with open('OceanCoder_SNSD_P2 (133).jpg', 'rb') as f:
pic2 = f.read()
# 日志模块,带滚动策略
def create_logger():
if not os.path.exists('./log/'):
os.makedirs('./log/')
# 日志文件
rotateHandler=RotatingFileHandler('./log/log.log', maxBytes=100*1024*1024, backupCount=2) # 单个文件100M
rotateHandler.setLevel(logging.INFO)
rotateHandler.setFormatter(logging.Formatter("%(asctime)s %(filename)s [line:%(lineno)d] [%(levelname)s'] %(message)s"))
# 屏幕输出
consoleHandler=logging.StreamHandler()
# 生成时间戳
def get_timestamp(string, mode=1):
if mode==1:
return int(time.mktime(time.strptime(string,"%Y%m%d")))
elif mode==2:
return int(time.mktime(time.strptime(string,'%Y-%m-%d %H:%M:%S')))
# 时间戳转换为日期 mode1:‘年月日’,mode2:'年-月-日 小时:分钟:秒'
def get_dateString(timestamp,mode=1):
# dict
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} = {1}".format(key, value))
# list
def test_var_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv:", arg)
# join strings
letters = ['h', 'e', 'l', 'l', 'o']
''.join(letters)
# filter in a list
a = next((i for i in range(1, 10) if not i % 4), -1)
# replace too many ==
foo = 'bar'
if foo in ('bar1', 'bar2', 'bar3'):
pass
@ZhigangPu
ZhigangPu / gist:29e4f511185dacf30a910d7285a94ec1
Created March 6, 2019 02:26
string compatibility of python3 and python2
PY2 = sys.version_info[0] == 2
if PY2:
text_type = unicode
string_types = (unicode, str)
from cStringIO import StringIO as NativeBytesIO
else:
text_type = str
string_types = (str,)
from io import BytesIO as NativeBytesIO