Posts


Aug. 5, 2019

8/4 15.1~15.2 C++ 友元、嵌套类

想了好久还是用这种方式水一水了。其实C++ Primer Plus并不适合有编程基础之后去读,不过懒得再找书了。学C++的过程中很尴尬的一点是实践机会不多,这样也能尽可能避免过目就忘的情况,权当笔记,会摘抄书中的一些代码。

More...

Jul. 29, 2019

自制一个简易的电脑状态监视器

效果如图:

这是效果图:

 

前端用js,后端用Python,实现还是比较简单的。

后端用psutil库获得电脑的各项信息然后用WebSocket发给前端,前端再处理到页面上。

前端用Bootstrap框架做出进度条来表示CPU和内存的占用百分比。缓存连续10次的数据,计算出硬盘和网络的流量速度,用echarts显示成折线图。

More...

Jul. 19, 2019

Python3的字节类型(bytes)

Python3在处理一些底层应用时(比如socket编程)会用到字节类型(bytes)。

首先Python2与Python3的字节字符串大有不同,如果不幸看错了教程,那就悲剧了。以下内容均指Python3.

More...

Jul. 8, 2019

Python高级特性 断言 assert

如果觉得自己并不聪明到轻松理解技术书上的内容(尤其是译本),可以在读完一本进阶书籍后,练习一段时间,然后再找另一本相关的书或者其它权威的资料,再读,直至自己的理解与权威的说法一致,写出的代码足够Pythonic同时又不会滥用特性。

More...

Jun. 22, 2019

使用Matplotlab.pyplot绘图时的中文字体问题

在使用Matplotlab绘制图表时,如果标签中包含中文,会显示初方块,这是因为默认的字体中没有包含中文。

可以在代码中手动指定字体:

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['STZhongsong']

STZhongsong(华文中宋)即是我们所指定的支持中文的字体。你可以使用自己喜欢的字体。

More...

Jun. 16, 2019

暑假终于开始了!

从今天开始才算是真正地完事了。去了一趟中传,去了一趟山师。

有时候会感觉很不是滋味,因为我原本以为能在最后一段时间多提升一些,结果发现三轮还没开始就结束了。再后悔也晚了,无论如何只是证明我的水平而已,没什么好说的。

More...

Mar. 21, 2019

我失眠了

没有征兆,没有什么特别的地方。11:50左右上床睡觉,在床上躺了几十分钟后感觉到不对劲,毫无困意,一点也没有,熟悉的感觉不在了。

挣扎了三个小时后,现在是凌晨三点。我打开电脑,稍微有点头疼,感觉还好。也许是老天故意要给我一个写点什么东西的机会,否则我可能在很长一段时间之内都不会再打开WP了。

More...

Mar. 16, 2019

Python Websocket服务端

用Python做了个Websocket的服务端,虽然知道相关的库应该早就有了,但是还是想自己做出来。

当然也参考了不少网上的教程,琢磨了好久的Websocket协议的格式。

内含一个WebsocketServer类,可以实现握手、建立连接、心跳(ping)、传输数据、上下文管理器。可以接收任意类型的数据,发送字符串(稍微改一下也可以发送二进制数据),支持数据分片(传输和发送都可以)。

More...

Feb. 10, 2019

将歌词处理成电子书

后端的最后一部分。

  1. # coding=utf-8  
  2. import json  
  3. import re  
  4.   
  5.   
  6. class Lyric2Book:  
  7.     support_format = ['txt', 'html']  
  8.     support_ts = ['parallel', 'chunk']  
  9.     html_template = """ 
  10.     <section> 
  11.         <h2 class='header'>%(header)s</h2> 
  12.             <div class='info'> 
  13.                 <div class='album'>%(album)s</div> 
  14.                 <div class='artists'>%(artists)s</div> 
  15.             </div> 
  16.         <div class='content'> 
  17.             %(lyrics)s 
  18.         </div> 
  19.     </section> 
  20.         """  
  21.     html_frame = """<!DOCTYPE html> 
  22.     <html lang="zh-cn"> 
  23.     <head>  
  24.     <meta charset="utf-8"/> 
  25.     <meta name="viewport" content="width=device-width, initial-scale=1" /> 
  26.     <meta name="referrer" content="never" /> 
  27.     <title>%s</title> 
  28.     </head> 
  29.     <body> 
  30.     %s 
  31.     </body> 
  32.     </html> 
  33.     """  
  34.     lyrics_template = "<div class='content-%(ver)d' >%(content)s</div>"  
  35.     txt_template = "%(header)s\n专辑:%(album)s\n作者:%(artists)s\n\n%(lyrics)s\n\n"  
  36.     txt_frame = "%s\n\n%s"  
  37.   
  38.     def __init__(self, file_format='html', title='Lyrics', typesetting='parallel'):  
  39.         self.title = title  
  40.         if file_format in Lyric2Book.support_format:  
  41.             self.format = file_format  
  42.         else:  
  43.             raise Exception('Unsupported format: %s.' % file_format)  
  44.         self.res = ""  
  45.         self.data = ""  
  46.         if typesetting in Lyric2Book.support_ts:  
  47.             self.ts = typesetting  
  48.         else:  
  49.             raise Exception('Unsupported typesetting: %s' % typesetting)  
  50.   
  51.     def chunk(self, lyrics):  
  52.         predlyric = []  
  53.         # 传入的是多个版本的歌词  
  54.         for item in lyrics:  
  55.             lines = []  
  56.             if item is not None:  
  57.                 # 分行处理  
  58.                 for line in item.split('\n'):  
  59.                     line = re.sub('\(\d+,\d+\)', '', line)  
  60.                     time_tag = ''.join(re.findall('\[.*?\]', line))  
  61.                     line = [time_tag, line.strip(time_tag)]  
  62.                     lines.append(line)  
  63.                 predlyric.append(lines)  
  64.         output_section = ''  
  65.         if self.format == 'html':  
  66.             i = 1  
  67.             for item in predlyric:  
  68.                 output_section += "<div class='content-%d'>\n" % i  
  69.                 i += 1  
  70.                 for line in item:  
  71.                     output_section += "<p><span class='timetag'>%s</span>%s</p> \n" % (re.sub('[\[\]]', '-', line[0]), line[1])  
  72.                 output_section += "</div>"  
  73.         elif self.format == 'txt':  
  74.             for item in predlyric:  
  75.                 output_section += ""  
  76.                 for line in item:  
  77.                     format_tag = '-%s- ' % re.sub('[\[\]]', '-', line[0]) if line[0] else ''  
  78.                     output_section += format_tag + line[1] + '\n'  
  79.         return output_section  
  80.   
  81.     def parallel(self, lyrics):  
  82.         unpredlyric = []  
  83.         for item in lyrics:  
  84.             lines = {}  
  85.             if item is not None:  
  86.                 for line in item.split('\n'):  
  87.                     line = re.sub('\(\d+,\d+\)', '', line)  
  88.                     time_tag = ''.join(re.findall('\[.*?\]', line))  
  89.                     lines[time_tag] = line.strip(time_tag)  
  90.                 unpredlyric.append(lines)  
  91.         predlyric = []  
  92.         for group_tag in list(unpredlyric[0].keys()):  
  93.             predlyric += [[group_tag] + [i.get(group_tag, unpredlyric[0][group_tag]) for i in unpredlyric]]  
  94.         dparallels = predlyric  
  95.         output_section = ''  
  96.         if self.format == 'html':  
  97.             for item in dparallels:  
  98.                 output_section += "<div class='content'>\n"  
  99.                 dtimetag = item[0]  
  100.                 dcontent = item[1::]  
  101.                 item_div = "<div class='timetag'>\n<p>{timetag}</p>\n</div>\n<div class='single-lyric'>\n{content}</div>\n"  
  102.                 output_spar = ''.join("<p class='ver-%d'>%s</p>\n" % (c[0], c[1]) for c in enumerate(dcontent, 1))  
  103.                 output_section += item_div.format(timetag=re.sub('[\[\]]', ' - ', dtimetag), content=output_spar) + '</div>'  
  104.         elif self.format == 'txt':  
  105.             for item in dparallels:  
  106.                 dtimetag = item[0]  
  107.                 dcontent = item[1::]  
  108.                 item_div = "{timetag}\n{content}\n"  
  109.                 output_spar = ''.join("%s\n" % c[1] for c in enumerate(dcontent))  
  110.                 output_section += item_div.format(timetag=re.sub('[\[\]]', ' - ', dtimetag), content=output_spar)  
  111.         return output_section  
  112.   
  113.     def doconv(self, sections):  
  114.         last_album = ''  
  115.         output = ''  
  116.         for item in sections:  
  117.             header = item['name']  
  118.             album = item['album']  
  119.             artists = ','.join(item['artists'])  
  120.             a = item['lyric']  
  121.             if a is not None:  
  122.                 ly_res = {}  
  123.                 lyrics = [a['0'], a['1'], a['2']]  
  124.                 if self.ts == 'parallel':  
  125.                     ly_res = self.parallel(lyrics)  
  126.                 elif self.ts == 'chunk':  
  127.                     ly_res = self.chunk(lyrics)  
  128.                 if self.format == 'html':  
  129.                     if last_album != album:  
  130.                         last_album = album  
  131.                         output += '<h1>%s</h1>' % album  
  132.                     output += Lyric2Book.html_template % {'header': header, 'album': album, 'artists': artists, 'lyrics': ly_res}  
  133.                 elif self.format == 'txt':  
  134.                     if last_album != album:  
  135.                         last_album = album  
  136.                         output += album + '\n\n'  
  137.                     output += Lyric2Book.txt_template % {'header': header, 'album': album, 'artists': artists, 'lyrics': ly_res}  
  138.         self.res = output  
  139.   
  140.     def output(self):  
  141.         filename = self.title + '.' + self.format  
  142.         with open(filename, 'w+', encoding='utf-8') as f:  
  143.             res = self.res  
  144.             if self.format == 'html':  
  145.                 res = Lyric2Book.html_frame % (self.title, res)  
  146.             elif self.format == 'txt':  
  147.                 res = Lyric2Book.txt_frame % (self.title, res)  
  148.             f.write(res)  
  149.   
  150.   
  151. if __name__ == '__main__':  
  152.     with open('bbc.txt', 'r', encoding='utf-8') as file:  
  153.         t = Lyric2Book(file_format='thjxt', title='BBC Documentary', typesetting='parallel')  
  154.         content = json.loads(file.read())  
  155.         t.doconv(content['result'])  
  156.         t.output()  

Feb. 6, 2019

寻找孤独

2019年的春节只有5天,二十八放假,初三上课。

放假前几分钟,我在想我能够做什么做什么,把计划中的事,未做完的事,未计划的事统统完成,大部分都是课外的事。

我确实完成了一些,但有些我却总也走不下去。歌词工具从十月份开始有想法,十一月份真正开始,到现在有三四个月了。借着这个想法我才真正熟悉了Python,后台逻辑其实一个月以前就结束了,一些改进的想法也决定搁置,没有再重构;前端最关键的部分也测试证明可行,然后我就不走了。

More...