Skip to content

Commit

Permalink
Correct python typo for data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
heqin-zhu committed Apr 4, 2019
2 parents b3f8af6 + 6d8c780 commit 218b3fd
Show file tree
Hide file tree
Showing 34 changed files with 2,491 additions and 1,805 deletions.
48 changes: 20 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
language: python3
sudo: required
python:
- "3.6"
language: python
python: "3.6"

notifications:
email:
recipients:
- zhuheqin1@gmail.com
on_success: change # default: change
on_failure: always # default: always
branches:
only:
- master

env:
# Github Pages
- GH_REF: github.com/USTC-Resource/USTC-Course

install:
- sudo apt-get install python3 -y
- sudo apt-get install python3-pip -y
- sudo pip3 install markdown
- sudo pip3 install pypinyin
- pip install --upgrade pip
- pip install flake8 markdown pypinyin

script:
- python3 utils/genReadme.py
- python3 utils/genIndex.py
- flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
- python utils/genReadme.py
- python utils/genIndex.py

after_script:
# Build Master Repository(Coding Pages)
Expand All @@ -31,15 +29,9 @@ after_script:
- git commit -m "Travis-CI Update pages with build $TRAVIS_BUILD_NUMBER"
- git push -f "https://${GH_TOKEN}@${GH_REF}" master:gh-pages

addons:
apt:
update: true

branches:
only:
- master
env:
global:
# Github Pages
- GH_REF: github.com/USTC-Resource/USTC-Course

notifications:
email:
recipients:
- zhuheqin1@gmail.com
on_success: change # default: change
on_failure: always # default: always
26 changes: 16 additions & 10 deletions utils/checkBigFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-p','--path',help='path to check',default='.')
parser.add_argument('-s','--size',help='max size of file to be removed',default=2**20*100) # 100Mb
parser.add_argument('-p', '--path', help='path to check', default='.')
parser.add_argument(
'-s', '--size', help='max size of file to be removed',
default=2**20 * 100) # 100Mb
args = parser.parse_args()

PATH = args.path
SIZE = args.size
def checkBigFile(path,size):


def checkBigFile(path, size):
big = '.bigFile'
if not os.path.exists(big):
os.mkdir(big)
gen = os.walk(os.path.abspath(path))
for path,dirs,files in gen:
for path, dirs, files in gen:
li = path.strip(os.sep).split(os.sep)
if any([i[0]=='.' and i!='.' for i in li]):continue
if any([i[0] == '.' and i != '.' for i in li]): continue
for file in files:
filePath = os.path.join(path,file)
filePath = os.path.join(path, file)
sz = os.path.getsize(filePath)
if sz > size:
print('[BIG]: {} is bigger than 100mb'.format(filePath))
try:
shutil.move(filePath,big)
shutil.move(filePath, big)
except Exception as e:
print(e,path)
print(e, path)
os.remove(filePath)
if __name__=='__main__':
checkBigFile(PATH,SIZE)


if __name__ == '__main__':
checkBigFile(PATH, SIZE)
32 changes: 20 additions & 12 deletions utils/codecogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,47 @@
import sys
from translate import Translator as TR

FORMULA = re.compile(r'\${1,2}(?P<formula>.+?)\${1,2}',re.DOTALL)
FORMULA = re.compile(r'\${1,2}(?P<formula>.+?)\${1,2}', re.DOTALL)
Chinese = re.compile(u"(?P<chinese>[\u4e00-\u9fa5]+)")
API = 'https://latex.codecogs.com/gif.latex?'


def codecog(f):
if os.path.exists(f) and f.endswith('.md'):
with open(f) as fp:
txt = fp.read()
with open(f,'w') as fp:
fp.write(re.sub(FORMULA,covert,txt))
with open(f, 'w') as fp:
fp.write(re.sub(FORMULA, covert, txt))
else:
s = re.sub(FORMULA,covert,f)
s = re.sub(FORMULA, covert, f)
print(s)


def covert(matched):
s = matched.group('formula').strip('$ ')
s = re.sub(Chinese,zh2en,s)
s = re.sub(r'\r+|\n+|\\n',' ',s)
s = re.sub(' +','&space;',s)
return '![]({})'.format(API+s)
s = re.sub(Chinese, zh2en, s)
s = re.sub(r'\r+|\n+|\\n', ' ', s)
s = re.sub(' +', '&space;', s)
return '![]({})'.format(API + s)


def zh2en(txt):
s = txt.group('chinese').strip()
tran = TR(to_lang='en',from_lang='zh')
tran = TR(to_lang='en', from_lang='zh')
en = tran.translate(s)
return re.sub(' +','-',en)
return re.sub(' +', '-', en)


def handle(path):
if os.path.isdir(path):
for p,ds,fs in os.walk(path):
for p, ds, fs in os.walk(path):
for f in fs:
if f.endswith('.md'):
codecog(os.path.join(p,f))
codecog(os.path.join(p, f))
else:
codecog(path)


if __name__ == '__main__':
args = sys.argv[1:]
if not args:
Expand Down
15 changes: 9 additions & 6 deletions utils/genReadme.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@

parser = ArgumentParser()

parser.add_argument('-p','--path',default='.',help='path to walk')
parser.add_argument('-f','--fileinclude',action='store_true',help='if has, list files and dirs, else only dirs')
parser.add_argument('-d','--depth', type = int, default = 1)
parser.add_argument('-p', '--path', default='.', help='path to walk')
parser.add_argument(
'-f',
'--fileinclude',
action='store_true',
help='if has, list files and dirs, else only dirs')
parser.add_argument('-d', '--depth', type=int, default=1)
#获取参数
args = parser.parse_args()
FILE = args.fileinclude
PATH = args.path
DEPTH = args.depth


idxs = tree(PATH,DEPTH,FILE)
idxs = tree(PATH, DEPTH, FILE)
s = README.format(index='\n'.join(idxs))
with open('README.md','w') as f:
with open('README.md', 'w') as f:
f.write(s)
45 changes: 30 additions & 15 deletions utils/genZipFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,62 @@
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-r','--rewrite',help='rewrite zip file',action='store_true')
parser.add_argument(
'-r', '--rewrite', help='rewrite zip file', action='store_true')
args = parser.parse_args()
REWRITE = args.rewrite


def checkZip(name):
'''check if this file should be added to the zip'''
li = [name.startswith('.') ,name.endswith('.zip'),name.lower()=='readme.md']
li = [
name.startswith('.'),
name.endswith('.zip'),
name.lower() == 'readme.md'
]
return not any(li)

def isIgnore(li,files):
return 'index.html' in files or any((i[0]=='.' and i!='.') or i.startswith('__') or i in IGNORE for i in li)
def genZipFile(tar = WALKDIR,rewrite=False):

def isIgnore(li, files):
return 'index.html' in files or any(
(i[0] == '.' and i != '.') or i.startswith('__') or i in IGNORE
for i in li)


def genZipFile(tar=WALKDIR, rewrite=False):
os.chdir(tar)
n = len(tar)
gen = os.walk(tar)
pwd = os.path.abspath('.')
for path, dirs, files in gen:
li = path.strip(os.sep).split(os.sep)
if isIgnore(li,files):continue
if isIgnore(li, files): continue
ziplst = []
for i in files:
if i.endswith('个文件.zip'):
if rewrite:
os.remove(os.path.join(path,i))
else:break
os.remove(os.path.join(path, i))
else:
break
elif checkZip(i):
ziplst .append(i)
ziplst.append(i)
else:
if len(ziplst)<3:continue
if len(ziplst) < 3: continue
ziplst.sort()
tmp = os.path.abspath(path) \
.replace(pwd,'')\
.replace(os.sep,'-')
name = '{tmp}目录下的{length}个文件.zip'.format(tmp=tmp,length =len(ziplst))
zipName = os.path.join(path,name)
name = '{tmp}目录下的{length}个文件.zip'.format(
tmp=tmp, length=len(ziplst))
zipName = os.path.join(path, name)
try:
with ZipFile(zipName,'w') as z:
with ZipFile(zipName, 'w') as z:
os.chdir(path)
for i in ziplst: z.write(i)
for i in ziplst:
z.write(i)
except Exception as e:
print(e,path)
print(e, path)


if __name__ == '__main__':
genZipFile(rewrite=REWRITE)
17 changes: 10 additions & 7 deletions utils/getSize.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# coding: utf-8
import os
import sys


def formatSize(size):
s = 'BKMGTP'
ct = 0
while size>=(1<<ct):
ct+=10
if ct>=10: ct-=10
return '{sz:.2f}{a}'.format(sz=size/(1<<ct),a=s[ct//10])
while size >= (1 << ct):
ct += 10
if ct >= 10: ct -= 10
return '{sz:.2f}{a}'.format(sz=size / (1 << ct), a=s[ct // 10])


def getSize(path='.'):
if os.path.isdir(path):
gen = os.walk(path)
li = []
li = []
for root, dirs, files in gen:
for f in files:
sz = os.path.getsize(os.path.join(root ,f))
sz = os.path.getsize(os.path.join(root, f))
li.append(sz)
#li.insert(('.',sum(i[1] for i in li)),0)
#size = [f'{i[0]}: {formatSize(i[1])}' for i in li]
return formatSize(sum(li))
else:
return formatSize(os.path.getsize(path))


if __name__ == "__main__":
items = sys.argv[1:]
for i in items:
print('{i}: {sz}'.format(i=i,sz =getSize(i)))
print('{i}: {sz}'.format(i=i, sz=getSize(i)))
37 changes: 23 additions & 14 deletions utils/md_tree_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,49 @@
#命令行输入参数处理
parser = ArgumentParser()

parser.add_argument('-p','--path',default='.',help='path to walk')
parser.add_argument('-f','--fileinclude',action='store_true',help='if has, list files and dirs, else only dirs')
parser.add_argument('-d','--depth', type = int, default = 2)
parser.add_argument('-p', '--path', default='.', help='path to walk')
parser.add_argument(
'-f',
'--fileinclude',
action='store_true',
help='if has, list files and dirs, else only dirs')
parser.add_argument('-d', '--depth', type=int, default=2)
#获取参数
args = parser.parse_args()
FILE = args.fileinclude
PATH = args.path
DEPTH = args.depth


def mklink(path):
return '* [{name}]({path})'.format(name=os.path.basename(path),path=path)
return '* [{name}]({path})'.format(name=os.path.basename(path), path=path)


def clean(paths):
ret = []
for path in paths:
name = os.path.basename(path)
if not ( name.startswith('.') or name.startswith('__')):
if not (name.startswith('.') or name.startswith('__')):
ret.append(path)
return ret

def tree(path='.',depth=2,showfile=False):

def tree(path='.', depth=2, showfile=False):
while not os.path.isdir(path):
print('[error]: please input a directory, not file path')
path = input()
li = os.listdir(path)
items = [os.path.join(path,i) for i in li if not i.startswith('.')]
items = [os.path.join(path, i) for i in li if not i.startswith('.')]
items = clean(items)
items = pinyinSort(items)
if not showfile: items = [i for i in items if os.path.isdir(i)]
if depth==1:
return [mklink(path)] + [' '*4 + mklink(i) for i in items]
if depth == 1:
return [mklink(path)] + [' ' * 4 + mklink(i) for i in items]
else:
uls = [tree(i,depth-1,showfile) for i in items]
ret = [' '*4 + li for ul in uls for li in ul]
return [mklink(path)] + ret
uls = [tree(i, depth - 1, showfile) for i in items]
ret = [' ' * 4 + li for ul in uls for li in ul]
return [mklink(path)] + ret


if __name__ =='__main__':
print('\n'.join(tree(PATH,DEPTH,FILE)))
if __name__ == '__main__':
print('\n'.join(tree(PATH, DEPTH, FILE)))
9 changes: 5 additions & 4 deletions utils/mywalk.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os

def mywalk(dire,valid=lambda x:True):

def mywalk(dire, valid=lambda x: True):
if not os.path.isdir(dire):
raise Exception('[Error]: directory excepted')
dirs = []
files = []
for i in os.listdir(dire):
i = os.path.join(dire,i)
i = os.path.join(dire, i)
if valid(i):
if os.path.isdir(i):
dirs.append(i)
else:
files.append(i)
yield dire,dirs,files
yield dire, dirs, files
for d in dirs:
yield from mywalk(os.path.join(dire,d),valid)
yield from mywalk(os.path.join(dire, d), valid)
Loading

0 comments on commit 218b3fd

Please sign in to comment.