一个很水的论坛体格式生成脚本

一个很水的论坛体格式生成脚本

!Warning
特别声明:本博客公开的源代码,一律禁止肖战团队及其粉丝使用。

先看效果:

闲得蛋疼

也不能说闲得蛋疼,写论坛体的时候每一层楼都要复制粘贴改时间更蛋疼……

实在受不了手动调整,本着“能交给计算机的事情就不要人工做”的原则,琢磨着写了个Python脚本,直接生成任意楼高的格式文档,然后我往里填内容,就很爽了。

核心思想是每一楼都在上一楼的基础上,在分钟数上加一个随机数。

说干就干,开始写脚本。

基础版:生成时间信息

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 25 11:01:32 2017
@author: matrixk
"""
import random

f = open('forum_format.md', 'w')

def forum_format(date, hour_start, min_start, sec_start, floor):
    #set initial
    floor_number = 0
    date_show = date
    time_hour = int(hour_start)
    time_min = int(min_start)
    time_sec = int(sec_start)
    standard = 35
    
    for i in range(int(floor)+1):
        f.write (u"\u2116".encode("utf-8")+str(floor_number)+u"\u0020\u2606\u2606\u2606=\u0020=\u0020于".encode("utf-8"))
        f.write (str(date_show) + u"\u0020".encode("utf-8"))
        f.write (str(time_hour).zfill(2) + ":" + str(time_min).zfill(2) + ":" + str(time_sec).zfill(2) +u"留言\u2606\u2606\u2606".encode("utf-8"))
        f.write ("\n\n- - - - -\n\n")
        floor_number = floor_number + 1
        time_sec = time_sec + random.randint(1,59)
        if time_sec >= 60:
            time_sec = time_sec - 60
            time_min = time_min + 1
            
        time_min = time_min + random.randint(1,7)
        
        if time_min >= 60:
            time_hour = time_hour + 1
            time_min = time_min - 60
            
        if time_hour >= 24:
            time_hour = time_hour - 24
            date_day = int(date_show[8:10]) + 1
            date_show = date_show[0:8]+str(date_day).zfill(2)

    f.close()



date_input = raw_input("date: ")
time_start = raw_input("publish time of the first floor: ")
hour_input = time_start[0:2]
min_input = time_start[3:5]
sec_input = time_start[6:8]
floor_input = raw_input("floor: ")
forum_format(date_input, hour_input, min_input, sec_input, floor_input)

思路还是很清晰的,在time_min = time_min + random.randint(1,7)这个地方试了几个不同的值,最后感觉7是一个比较合适的值,楼层之间的时间间隔比较合理。

不过……我是这么容易满足的人吗,显然不是啊(什么)。

因为如果是深夜树洞贴,那回复肯定很少;如果是大家都在上网的时间段,楼肯定盖得很快,但是如果是加一个随机数的话,所有的时间段的流量都是差不多的,那我还得手动调整楼层,还是很蛋疼……

于是琢磨着做一下流量-时间修正。

流量-时间修正

要做流量-时间修正,首先要知道大家都什么时候上网……

于是我找到了这个帖子:流量研究院:网民上网时间分布报告

很好,里面给出了精确到小时的流量比例统计,真是太好了,省了不少事。


所以琢磨一下,怎么在原来的基础上体现这个流量差别呢。

直观来看,其实就是,在流量大的时候,上下楼之间的时间间隔应该更小,而流量小的时候,上下楼之间的时间间隔会很大(长时间没有人回帖)。

所以我们只要把time_min = time_min + random.randint(1,7)里面,randint的部分,在流量小的时候距离拉的很开(可以间隔很长时间)这样就OK了。

所以我们根据这个流量比例,把最大值“7”替换为变量standard,然后让standard随小时的变化而变化起来:

def fluctuation(hour_flu):
    standard = 35
    if int(hour_flu) == 0:
        standard = standard / 2.73
    elif int(hour_flu) == 1:
        standard = standard / 1.66
    elif int(hour_flu) == 2:
        standard = standard / 1.10
    elif int(hour_flu) == 3:
        standard = standard / 0.80
    elif int(hour_flu) == 4:
        standard = standard / 0.66
    elif int(hour_flu) == 5:
        standard = standard / 0.65
    elif int(hour_flu) == 6:
        standard = standard / 0.91
    elif int(hour_flu) == 7:
        standard = standard / 1.64
    elif int(hour_flu) == 8:
        standard = standard / 3.32
    elif int(hour_flu) == 9:
        standard = standard / 4.81
    elif int(hour_flu) == 10:
        standard = standard / 5.56
    elif int(hour_flu) == 11:
        standard = standard / 5.64
    elif int(hour_flu) == 12:
        standard = standard / 5.53
    elif int(hour_flu) == 13:
        standard = standard / 5.57
    elif int(hour_flu) == 14:
        standard = standard / 5.58
    elif int(hour_flu) == 15:
        standard = standard / 6.00
    elif int(hour_flu) == 16:
        standard = standard / 6.13
    elif int(hour_flu) == 17:
        standard = standard / 5.81
    elif int(hour_flu) == 18:
        standard = standard / 5.68
    elif int(hour_flu) == 19:
        standard = standard / 6.39
    elif int(hour_flu) == 20:
        standard = standard / 6.79
    elif int(hour_flu) == 21:
        standard = standard / 6.59
    elif int(hour_flu) == 22:
        standard = standard / 5.77
    elif int(hour_flu) == 23:
        standard = standard / 4.25
    else:
        standard = standard
    return standard

这样的话,如果是没什么人上网的四点到五点,上下楼之间的时间差是一分钟到53分钟(35 / 0.66),可以说是很冷清了;在热闹的时候(晚八点),上下楼之间的时间差只有一分钟到五分钟,也算符合大家回帖的手速吧。

最后还考虑了一下如果standard人为调整得更高一点的话可能会突破60分钟,所以把最初版对于分钟的if判断改成了while判断。

经评论区LanternDSnow 学长提醒,其实可以用一个数组来代掉上面的if-elif的……我写的时候没想那么多Orz大概我就是那种靠Vim一拖暴力输出的人(喂)。

修改完以后fluctuation函数变成:

def fluctuation(hour_flu):
    standard = 60
    flufluc = [2.73, 1.66, 1.10, 0.80, 0.66, 0.65, 0.91, 1.64, 3.32, 4.81, 5.56, 5.64, 5.53, 5.57, 5.83, 6.00, 6.13, 5.81, 5.68, 6.39, 6.79, 6.59, 5.77, 4.25]
    standard = standard / flufluc[int(hour_flu)]
    return standard

一下子就短了不少,嗯……

最终脚本

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 25 11:01:32 2017

@author: matrixk
"""
import random

f = open('forum_format.md', 'w')

def fluctuation(hour_flu):
    standard = 60
    flufluc = [2.73, 1.66, 1.10, 0.80, 0.66, 0.65, 0.91, 1.64, 3.32, 4.81, 5.56, 5.64, 5.53, 5.57, 5.83, 6.00, 6.13, 5.81, 5.68, 6.39, 6.79, 6.59, 5.77, 4.25]
    standard = standard / flufluc[int(hour_flu)]
    return standard

def forum_format(date, hour_start, min_start, sec_start, floor):
    #set initial
    floor_number = 0
    date_show = date
    time_hour = int(hour_start)
    time_min = int(min_start)
    time_sec = int(sec_start)
    standard = 60
    
    for i in range(floor_number,int(floor)+1):
        standard = fluctuation(time_hour)
        f.write (u"\u2116".encode("utf-8")+str(floor_number)+u"\u0020\u2606\u2606\u2606=\u0020=于".encode("utf-8"))
        f.write (str(date_show) + u"\u0020".encode("utf-8"))
        f.write (str(time_hour).zfill(2) + ":" + str(time_min).zfill(2) + ":" + str(time_sec).zfill(2) +u"留言\u2606\u2606\u2606".encode("utf-8"))
        f.write ("\n\n- - - - -\n\n\n\n")
        floor_number = floor_number + 1
        time_sec = time_sec + random.randint(1,59)
        if time_sec >= 60:
            time_sec = time_sec - 60
            time_min = time_min + 1
            
        time_min = time_min + random.randint(1,int(standard))
        
        while time_min >= 60:
            time_hour = time_hour + 1
            time_min = time_min - 60
            
        while time_hour >= 24:
            time_hour = time_hour - 24
            date_day = int(date_show[8:10]) + 1
            date_show = date_show[0:8]+str(date_day).zfill(2)

    f.close()



date_input = raw_input("date: ")
time_start = raw_input("publish time of the first floor: ")
hour_input = time_start[0:2]
min_input = time_start[3:5]
sec_input = time_start[6:8]
floor_input = raw_input("floor: ")
forum_format(date_input, hour_input, min_input, sec_input, floor_input)

代码和示例都可以在Github上下载。

告别复制粘贴,从今天开始(不是)。

……我觉得教我Python的计算物理老师可能很想打我了,教你们是做计算的,结果一天到晚写奇怪的脚本偷懒(大雾)。

- End -
自制
Python 花式折腾
1,737字 2,899次阅读
Comments
Write a Comment
  • 羡慕会造轮子的人!

    • @水八口 其实Python还挺简单的……至少读起来不难,我有一阵要用C++写东西的时候才是真的痛苦_(:зゝ∠)_

  • def fluctuation(hour_flu)

    里面的switch-case(好吧是if-elif)可以用map弄一个映射,然后就可以减少代码长度了~鉴于从0开始,用一个list也可以啦。

    • @LanternD 有道理……写的时候本着“能用就行”,没太在意代码量,我去改改

  • Snow reply

    见一个数组就好了啦 那么多if else蛇看着会气炸的

    • @Snow 对哦,握草,有道理

      我可能就是蔡老师说的那种程序写得特别简单粗暴vim一拖的那种,特别气死计算机的人(……)

      我去改一改……

      • @矩阵良 蛇是指python😉

        • @Snow 我知道23333@狂蟒 虽然蟒和蛇不是一个种【。