2020年信号与系统课程批改工作处理程序

本文集成了相应的处理应用程序,便于之后的参见:

作者:卓晴博士,清华大学自动化系
更新时间:2020-07-26 Sunday

01 BASIC INFORMATION


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY                     -- by Dr. ZhuoQing 2020-06-18
#
# Note:
#============================================================

from headm import *
from inforsub               import *

si = text2infor(ssau)
printff(len(si), si)

si = text2infor(ssxy)
printff(len(si), si)

#------------------------------------------------------------
#        END OF FILE : TEST1.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# INFORSUB.PY                  -- by Dr. ZhuoQing 2020-06-18
#
# Note:
#============================================================

from headm import *
import pandas as pd

#------------------------------------------------------------
ssau = 2
ssxy = 3

#------------------------------------------------------------
def text2infor(fileid):
    filename = tspgetdopfile(fileid)
#    printf(filename)

    studentdict = {}

    with open(filename, 'r', encoding='gbk') as f:
        for l in (f.readlines()):
            l.rstrip('\n')
            lsect = l.split()
            if lsect[0] == '学号':
                continue

            studentdict[lsect[0]] = lsect[1]

    return studentdict

#------------------------------------------------------------
def setexcelcell(filename, sid, col, num):
    excelfile = pd.read_excel(filename, sheet_name='Exam')

    listdata = excelfile.values.tolist()
    rowid = -1
    for id,l in enumerate(listdata):
        if l[0] == sid:
            rowid = id
            printff(id, listdata[id])
            break

    if rowid < 0:
        printf("ERROR: Can not find %d in %s.\a"%(sid, filename))
        return

    excelfile.iat[rowid, col+1] = num
    listdata = excelfile.values.tolist()
    printff(rowid, listdata[rowid], sum(listdata[rowid][2:]))

    excelfile.to_excel(filename, sheet_name='Exam', index=False)

#------------------------------------------------------------
def showexcelcell(filename, sid):
    excelfile = pd.read_excel(filename, sheet_name='Exam')

    listdata = excelfile.values.tolist()

    if sid == 0:
        for l in listdata:
            s = '%s %d'%(str(l), sum(l[2:]))
            ss = s.replace('\'', '')
            ss = ss.replace('[', '')
            ss = ss.replace(']', '')
            ss = ss.replace(',', '')
            printf(ss)

        return

    #--------------------------------------------------------

    rowid = -1
    for id,l in enumerate(listdata):
        if l[0] == sid:
            rowid = id
            printff(id, listdata[id],sum(listdata[id][2:]))
            break

    if rowid < 0:
        printf("ERROR: Can not find %d in %s.\a"%(sid, filename))
        return

#------------------------------------------------------------
def setexcelcells(filename, sid, num):
    excelfile = pd.read_excel(filename, sheet_name='Exam')

    listdata = excelfile.values.tolist()
    rowid = -1
    for id,l in enumerate(listdata):
        if l[0] == sid:
            rowid = id
            printff(id, listdata[id])
            break

    if rowid < 0:
        printf("ERROR: Can not find %d in %s.\a"%(sid, filename))
        return

    for id, n in enumerate(num):
        excelfile.iat[rowid, id+2] = n

    listdata = excelfile.values.tolist()
    printff(rowid, listdata[rowid], sum(listdata[rowid][2:]))

    excelfile.to_excel(filename, sheet_name='Exam', index=False)

#------------------------------------------------------------

def setpaperscore(filename, sid, score=None, paper=None):
    excelfile = pd.read_excel(filename, sheet_name='Paper')

    listdata = excelfile.values.tolist()
    rowid = -1
    for id,l in enumerate(listdata):
        if l[0] == int(sid):
            rowid = id
            printff(id, listdata[id])
            break

    if rowid < 0:
        printf("ERROR: Can not find %s in %s.\a"%(sid,filename))
        return

    if score == None:
        return

    excelfile.iat[rowid, 2] = score
    excelfile.iat[rowid, 3] = paper

    listdata = excelfile.values.tolist()
    printff(rowid, listdata[rowid])
    excelfile.to_excel(filename, sheet_name="Paper", index=False)

def showpapernone(filename):
    count = 0
    excelfile = pd.read_excel(filename, sheet_name='Paper')
    listdata = excelfile.values.tolist()
    for id,l in enumerate(listdata):
        if l[2] == 0:
            printff(id , listdata[id])
            count = count + 1

    printf('No Paper :%d'%count)

#------------------------------------------------------------
if __name__ == "__main__":
    filename = r'D:\Teaching\SignalsSystems\SS2020S\Examination\Excel\SSXY-SCORE.xlsx'
    setexcelcell(filename, 2017013661, 1, 10)

'''
    si = text2infor(ssau)
    printff(len(si), si)

    si = text2infor(ssxy)
    printff(len(si), si)

'''
#------------------------------------------------------------
#        END OF FILE : INFORSUB.PY
#============================================================

02FileName Change


ssau:
D:\Teaching\SignalsSystems\SS2020S\Examination\Excel\SSAU-SCORE.xlsx
ssxy:
D:\Teaching\SignalsSystems\SS2020S\Examination\Excel\SSXY-SCORE.xlsx

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# FILENAMECHANGE.PY            -- by Dr. ZhuoQing 2020-06-19
#
# Note:
#============================================================

from headm import *
from inforsub               import *

#------------------------------------------------------------
siau = text2infor(ssau)
sixy = text2infor(ssxy)

#------------------------------------------------------------
filedir = tspstring2text('ssau')
printf(filedir)

filedim = os.listdir(filedir)
filenew = [(f.split('_')[0], os.path.splitext(f)[-1]) for f in filedim]

fileout = []
changecount = 0
for id,f in enumerate(filenew):
    name = siau[f[0]]
    fo = '%s_%s%s'%(f[0],name,f[1])

    fold = os.path.join(filedir, filedim[id])
    fnew = os.path.join(filedir, fo)

    if os.path.isfile(fold):
        try:
            os.rename(fold, fnew)
            changecount = changecount + 1
        except:
            pass

printf('Change:%d\a'%changecount)

#------------------------------------------------------------
#        END OF FILE : FILENAMECHANGE.PY
#============================================================

03分数录入


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# SCORE.PY                     -- by Dr. ZhuoQing 2020-06-19
#
# Note:
#============================================================

from head import *
from inforsub               import *

#------------------------------------------------------------
xyinfor = text2infor(ssxy)
auinfor = text2infor(ssau)
xyexcel = r'D:\Teaching\SignalsSystems\SS2020S\Examination\Excel\SSXY-SCORE.xlsx'
auexcel = r'D:\Teaching\SignalsSystems\SS2020S\Examination\Excel\SSAU-SCORE.xlsx'

if len(xyinfor) == 0:
    printf("No student infor.\a")
    exit()
if len(auinfor) == 0:
    printf("No student infor.\a")
    exit()

if os.path.isfile(xyexcel) == False:
    printf("No output excel file.%s"%excelfile)
    exit()

if os.path.isfile(auexcel) == False:
    printf("No output excel file.%s"%excelfile)
    exit()

#------------------------------------------------------------
printf('')

#------------------------------------------------------------
idstr = ''
namestr = ''
excelfile = ''
idsamecount = 0

title = tspgetwindowtitle()
for t in title:
    ts = t.split('_')
    if len(ts) < 2: continue

    if ts[0] in xyinfor:
        idstr = ts[0]
        namestr = xyinfor[idstr]
        excelfile = xyexcel
        idsamecount = idsamecount + 1
        printf('%s_%s:%s\a'%(idstr, namestr, excelfile))
#        break

    if ts[0] in auinfor:
        idstr = ts[0]
        namestr = auinfor[idstr]
        excelfile = auexcel
        idsamecount = idsamecount + 1
        printf('%s_%s:%s\a'%(idstr, namestr, excelfile))
#        break

if idsamecount != 1:
    printf("ERROR: idsame count : %d.\a"%idsamecount)
    exit()

#if len(idstr) == 0:
#    printf("ERROR: No find id string.\a")
#    exit()

#printf('%s_%s:%s\a'%(idstr, namestr, excelfile))
#------------------------------------------------------------

if len(sys.argv) < 3:
#    printf("Usage: score s1 s2 s3....")
    showexcelcell(excelfile, int(idstr))
    exit()

if len(sys.argv) == 3:
    scoreid = int(sys.argv[1])
    score = float(sys.argv[2])

    if scoreid < 1 or scoreid > 8:
        printf("Score id ERROR.\a")
        exit()

    setexcelcell(excelfile, int(idstr), scoreid, score)
    printf('\a')
    exit()

#------------------------------------------------------------
if len(sys.argv) != 9:
    printf("Usage: score s1 s2 ...s8\a")
    exit()

scoredim = []
for s in sys.argv[1:]:
    scoredim.append(float(s))

printf(scoredim)
setexcelcells(excelfile, int(idstr), scoredim)

printf('Total:%5.2f\a'%sum(scoredim))

#------------------------------------------------------------
#        END OF FILE : SCORE.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# INQUIRY.PY                   -- by Dr. ZhuoQing 2020-06-19
#
# Note:
#============================================================

from headm import *
from inforsub               import *

xyexcel = r'D:\Teaching\SignalsSystems\SS2020S\Examination\Examination\Excel\SSXY-SCORE.xlsx'
auexcel = r'D:\Teaching\SignalsSystems\SS2020S\Examination\Examination\Excel\SSAU-SCORE.xlsx'

printf('2020信号与系统XY卷面分数')
printf(' ')
printf('学号: $1')
printf('姓名: $2')
printf('1.选择题1: $3  2.判断题: $4')
printf('3.填空题: $5  4.简答题: $6 ')
printf('5.计算题: $7  6.计算题:$8')
printf('7.系统分析:$9  8.系统分析:$10')
printf('卷面总分:$11')
printf('-------------------')
showexcelcell(auexcel, 0)

tspmsgcopy()
printf('\a')

#------------------------------------------------------------
#        END OF FILE : INQUIRY.PY
#============================================================

04 Paper


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PAPERSCORE.PY                -- by Dr. ZhuoQing 2020-06-22
#
# Note:
#============================================================

from headm import *
from inforsub               import *

paperau = tspstring2text('paperau')
paperxy = tspstring2text('paperxy')
#printf(paperau, paperxy)

siau = text2infor(ssau)
sixy = text2infor(ssxy)

#------------------------------------------------------------
def id4existnum(idstr):
    count = 0
    paperstr = ''
    global paperau, paperxy
    id = 0

    for k in siau.keys():
        if k[-len(idstr):] == idstr:
            count = count+1
            paperstr = paperau
            id = k

    for k in sixy.keys():
        if k[-len(idstr):] == idstr:
            count = count+1
            paperstr = paperxy
            id = k

    return count, paperstr, id

#------------------------------------------------------------
def name2existnum(namestr):
    count = 0
    paperstr = ''
    global paperau, paperxy
    id = 0

    for k,name in siau.items():
        if name == namestr:
            count = count + 1
            paperstr = paperau
            id = k

    for k,name in sixy.items():
        if name == namestr:
            count = count + 1
            paperstr = paperxy
            id = k

    return count, paperstr, id

#------------------------------------------------------------
'''
notone = 0
for k in siau.keys():
    idstr = k[-4:]
    num,paper,id = id4existnum(idstr)
    if num != 1:
        notone = notone + 1
        printff(idstr, num, k, id, paper)

for k in sixy.keys():
    idstr = k[-4:]
    num,paper,id = id4existnum(idstr)
    if num != 1:
        notone = notone + 1
        printff(idstr, num, k, id,paper)

printf("Not One:%d"%notone)
'''

#------------------------------------------------------------

if len(sys.argv) < 2:
#    printf("Usage: paperscore paper_name score id1 id2")
#    exit()
    printf('AU Paper None:')
    showpapernone(paperau)
    printf("XY Paper None:")
    showpapernone(paperxy)

    exit()

#------------------------------------------------------------

if len(sys.argv) == 2:
    idstr = sys.argv[1]
    num,paperfile,id = id4existnum(idstr)

    if num != 1:
        printf("ID exists is not one:%s,%s"%(num, id))
        exit()

    setpaperscore(paperfile,id)
    exit()

#------------------------------------------------------------
if len(sys.argv) == 3:

    idstr = ''
    idsamecount = 0

    title = tspgetwindowtitle()
    for t in title:
        ts = t.split('_')
        if len(ts) < 2: continue

        if ts[0] in sixy:
            idstr = ts[0]
            idsamecount = idsamecount + 1

        if ts[0] in siau:
            idstr = ts[0]
            idsamecount = idsamecount + 1

    if idsamecount != 1:
        printf("IDSTR error:%d"%idsamecount)
        exit()

    num, paperfile, id = id4existnum(idstr)

    if num != 1:
        printf('ID %s exists error : %s,%s'%(idstr, num, id))
        exit()

    score = int(sys.argv[2])
    papername = sys.argv[1]
    printff(paperfile, id, score, papername)

    setpaperscore(paperfile, id, score, papername)

    exit()

#------------------------------------------------------------
papername = sys.argv[1]
score = int(sys.argv[2])
for idstr in sys.argv[3:]:

    if idstr.isnumeric():
        num, paperfile, id = id4existnum(idstr)
    else: num,paperfile,id = name2existnum(idstr)

    if num != 1:
        printf('ID %s exists error: %s,%s'%(idstr, num, id))
        continue

    printff(paperfile, id, score, papername)

    setpaperscore(paperfile, id, score, papername)

#------------------------------------------------------------

#------------------------------------------------------------
#        END OF FILE : PAPERSCORE.PY
#============================================================

05处理微信平台


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# WCCOMB.PY                    -- by Dr. ZhuoQing 2020-06-23
#
# Note:
#============================================================

from head import *

pnid = 24
pndata = 25

idfile = tspgetdopfile(pnid)
datafile = tspgetdopfile(pndata)

printf(idfile, datafile)

#------------------------------------------------------------

iddim = []
namedim = []
wcdim = []
recdim = []

with open(idfile, 'r', encoding='gbk') as f:
    for l in (f.readlines()):
        l.rstrip('\n')
        lsect = l.split()
        if len(lsect) < 3: continue

        iddim.append(lsect[2])
        namedim.append(lsect[1])
        wcdim.append(lsect[0])
        recdim.append((0,0,0,0,0))

for d in zip(namedim, iddim, wcdim):
    if len(d[1]) < 5:
        printf(d)

#printf(iddim, namedim, wcdim)
#printf(len(iddim))

#------------------------------------------------------------

with open(datafile, 'r', encoding='gbk') as f:
    for l in (f.readlines()):
        l.rstrip('\n')
        lsect = l.split()

        if len(lsect) < 20:
            printf(l)
            continue

        id = namedim.index(lsect[0])
        if id < 0:
            printf(l)
            continue

        recdim[id] = (int(lsect[4]), int(lsect[5]), int(lsect[6]), int(lsect[8]), int(lsect[9]))

tspsave('normalrect', name=namedim, id=iddim, rec=recdim)

printf('\a')

#------------------------------------------------------------
#        END OF FILE : WCCOMB.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# WC2EXCEL.PY                  -- by Dr. ZhuoQing 2020-06-23
#
# Note:
#============================================================

from headm import *
from inforsub               import *

name, id, rec = tspload('normalrect', 'name', 'id', 'rec')
rec = [((d[0] + d[1] + d[3]) - 150)/(285-150)*50+50 for d in rec]
rect = [min(max(x,50),100) for x in rec]

wcau = tspstring2text('wcau')
wcxy = tspstring2text('wcxy')

excelfile = pd.read_excel(wcau, sheet_name='Wechat')
listdata = excelfile.values.tolist()

id = list(id)

#------------------------------------------------------------
for n,l in enumerate(listdata):

    if str(l[0]) not in id:
        printf('%s not in record.'%l)
        continue

    index = id.index(str(l[0]))
    if index >= 0:
        excelfile.iat[n, 2] = rect[index]
    else:
        excelfile.iat[n, 2] = 0
        printf(l)

excelfile.to_excel(wcau, sheet_name='Wechat', index=False)

#------------------------------------------------------------
excelfile = pd.read_excel(wcxy, sheet_name='Wechat')
listdata = excelfile.values.tolist()

id = list(id)

#------------------------------------------------------------
for n,l in enumerate(listdata):

    if str(l[0]) not in id:
        printf('%s not in record.'%l)
        continue

    index = id.index(str(l[0]))
    if index >= 0:
        excelfile.iat[n, 2] = rect[index]
    else:
        excelfile.iat[n, 2] = 0
        printf(l)

excelfile.to_excel(wcxy, sheet_name='Wechat', index=False)

printf('\a')

#------------------------------------------------------------
#        END OF FILE : WC2EXCEL.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# DRAWREC.PY                   -- by Dr. ZhuoQing 2020-06-23
#
# Note:
#============================================================

from headm import *

name, id, rec = tspload('normalrect', 'name', 'id', 'rec')
printf(id)

rec = [((d[0] + d[1] + d[3]) - 150)/(285-150)*50+50 for d in rec]
rect = [min(max(x,50),100) for x in rec]

plt.hist(rect, 50, alpha=0.75)
plt.grid(True)
plt.xlabel('Value')
plt.ylabel('Number')
plt.show()

#------------------------------------------------------------
#        END OF FILE : DRAWREC.PY
#============================================================

06HOMEWORK


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# ALLSTUDENT.PY                -- by Dr. ZhuoQing 2020-06-03
#
# Note:
#   Get all the students name and ID from the text file.
#
#============================================================

from headm import *
import hmwasub

#------------------------------------------------------------
hmdir = tspstring2text('hmwxy')
hmfile = [os.path.join(hmdir, f) for f in os.listdir(hmdir) if f.find('.txt') >= 0]

for f in hmfile:
    hmwasub.addhwtextfile(f)

tspmsgcopy()

hmwasub.savealldata('hmwxy')

printf("\a")

#------------------------------------------------------------
#        END OF FILE : ALLSTUDENT.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HMWASUB.PY                   -- by Dr. ZhuoQing 2020-06-03
#
# Note:
#============================================================

from headm import *

#------------------------------------------------------------
idall = []
nameall = []
hwall = []

#------------------------------------------------------------
def addidhw(id, name):
    global idall, nameall, hwall

    if idall.count(id) > 0:
        index = idall.index(id)
        hwall[index][-1] = 1
    else:
        idall.append(id)
        nameall.append(name)

        newhw = []

        if len(hwall) > 0:
            for i in range(len(hwall[0]) - 1):
                newhw.append(0)

        newhw.append(1)
        hwall.append(newhw)

#------------------------------------------------------------
def addidhwall0():
    global hwall

    for hw in hwall:
        hw.append(0)

def nohomeworkstring(hwma):
    str = ''
    for id,h in enumerate(hwma):
        if h == 0:
            str = str + '[%d]'%(id+1)

    if len(str) == 0: str='NULL'
    return str

#------------------------------------------------------------
def savealldata(filename):
    global idall, nameall, hwall
    tspsave(filename, id=idall, name=nameall, hw=hwall)

    printf('Total Students:%d'%len(idall))
    totalwork = len(hwall[0])

    for i, id in enumerate(idall):
        nostr = nohomeworkstring(hwall[i])
        printf('%s %s %d %s %s'%(id, nameall[i], sum(hwall[i]), nostr, str(hwall[i])))

#------------------------------------------------------------
def addhwtextfile(filename):
    with open(filename, 'r', encoding='gbk' ) as f:
        addidhwall0()

        for l in (f.readlines()):
            l.rstrip('\n')
            ls = l.split()

            if ls[0] != '全体':
                continue

            addidhw(ls[1], ls[2])

#------------------------------------------------------------
#        END OF FILE : HMWASUB.PY
#============================================================
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HMWEXCEL.PY                  -- by Dr. ZhuoQing 2020-06-23
#
# Note:
#============================================================

from head import *
from inforsub               import *

hmwau = tspstring2text('hmwexcelau')
hmwxy = tspstring2text('hmwexcelxy')

id0, name0, hw0 = tspload('hmwau', 'id', 'name', 'hw')
id1, name1, hw1 = tspload('hmwxy', 'id', 'name', 'hw')

#------------------------------------------------------------
def hmw2excel(filename, id, hw):
    excelfile = pd.read_excel(filename, sheet_name='HMW')
    listdata = excelfile.values.tolist()

    id = list(id)

    for n,l in enumerate(listdata):

        if str(l[0]) not in id:
            printf('%s not in record.'%l)
            continue

        index = id.index(str(l[0]))

        if index >= 0:
            excelfile.iat[n, 2] = sum(hw[n])
        else:
            excelfile.iat[n, 2] = 0
            printf(l)

    excelfile.to_excel(filename, sheet_name='HWM', index=False)

#------------------------------------------------------------
hmw2excel(hmwau, id0, hw0)
hmw2excel(hmwxy, id1, hw1)

printf("\a")

#------------------------------------------------------------
#        END OF FILE : HMWEXCEL.PY
#============================================================

猜你喜欢

转载自blog.csdn.net/zhuoqingjoking97298/article/details/107589328