OpenMediaVault在执行apt update/upgrade后会报如下错误

Traceback (most recent call last):
File “/usr/lib/python3.5/weakref.py”, line 117, in remove
TypeError: ‘NoneType’ object is not callable

问题讨论和解决方法地址:Github

解决方法一:

直接执行指令

wget -O /usr/lib/python3.5/weakref.py https://raw.githubusercontent.com/python/cpython/9cd7e17640a49635d1c1f8c2989578a8fc2c1de6/Lib/weakref.py

解决方法二:

打开/usr/lib/python3.5/weakref.py,将109行修改为

def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):

再将117行修改为

_atomic_removal(d, wr.key)

 

接触一下python plotly这个数据可视化的模块,万万没想到,走出去的第一步就踩进泥坑,描述一下我想要实现的功能:

用柱状图显示部分省份2018年的GDP以及GDP增长速度。也就是以城市为X坐标,GDP和增长率为Y坐标(左右各一个),每个城市两个柱形,显示10组这样的数据。

万万没想到,第一步就遇到了官方bug。

环境

  • python 3.6.6
  • plotly 3.7.1

参考

plotly的使用

  1. pip安装plotly
    pip install plotly
  2. 项目中引入plotly模块
    import plotly, json
    from plotly import graph_objs as go
  3. 通过官方文档熟悉其用法后,可以简单绘制出 y=x x∈[1, 4] 的函数图
    import plotly
    from plotly import graph_objs as go
    
    trace1 = go.Scatter(
        x=[1, 2, 3, 4],
        y=[1, 2, 3, 4]
    )
    
    data = [trace1]
    
    plotly.offline.plot(data, auto_play=True)

从陷入泥潭到实现目标

首先去收集了一下2018年部分省份的GDP信息,并以json格式存储到Data文件中,长这样:

{
    "cities":[{
        "name": "广州",
        "gdp": 21705.29,
        "speed": 7
    },
    {
        "name": "江苏",
        "gdp": 21093.3,
        "speed": 7.1
    },
    {
        "name": "山东",
        "gdp": 18900.6,
        "speed": 6.7
    },
    {
        "name": "浙江",
        "gdp": 11691,
        "speed": 7.4
    },
    {
        "name": "河南",
        "gdp": 10611,
        "speed": 7.9
    },
    {
        "name": "四川",
        "gdp": 8590.2,
        "speed": 8.2
    },
    {
        "name": "湖北",
        "gdp": 8188.84,
        "speed": 7.6
    },
    {
        "name": "上海",
        "gdp": 7863.40,
        "speed": 6.8
    },
    {
        "name": "湖南",
        "gdp": 7777.65,
        "speed": 8
    },
    {
        "name": "河北",
        "gdp": 7564,
        "speed": 6
    }]
}

Continue reading “python plotly数据可视化(踩坑纪实)”

同学发来一编程题目,图:

上来就一张图,实不相瞒,前八眼硬是没看懂要干嘛,一番说明后才略懂题意:

前面的选择题也是题干,目的是给出十道题的答案,判断给出的答案是否合法(符合10道选择题的描述)。

第二问则是编写程序给出合法的答案。

思路:

问题一:把10个问题的文字描述转换为代码,然后用输入的答案与每题符合规则的答案比对,如果出现不同答案则不合法。

问题二:基于问题一的代码,10个循环暴力解决。

以上解决方案确实不需要很长的思考时间和深厚的代码功底,所以性能上肯定是最差的。期待一个更好的解决方案。

以下是python3代码:

ANS = list([])


def Q2(a5):
    if a5 == 1:
        return 3
    elif a5 == 2:
        return 4
    elif a5 == 3:
        return 1
    elif a5 == 4:
        return 2
    else:
        return 0


def Q3(a3, a6, a2, a4):
    if a3 == a6 == a2:
        return 4
    elif a3 == a6 == a4:
        return 3
    elif a3 == a2 == a4:
        return 2
    elif a6 == a2 == a4:
        return 1
    else:
        return 0


def Q4(a1, a2, a5, a6, a7, a9, a10):
    if a1 == a5:
        return 1
    elif a2 == a7:
        return 2
    elif a1 == a9:
        return 3
    elif a6 == a10:
        return 4
    else:
        return 0


def Q5(a5, a8, a4, a9, a7):
    if a5 == a8:
        return 1
    elif a5 == a4:
        return 2
    elif a5 == a9:
        return 3
    elif a5 == a7:
        return 4
    else:
        return 0


def Q6(a8, a2, a4, a1, a6, a3, a10, a5, a9):
    if a8 == a2 == a4:
        return 1
    elif a8 == a1 == a6:
        return 2
    elif a8 == a3 == a10:
        return 3
    elif a8 == a5 == a9:
        return 4
    else:
        return 0


def Q7(most_chose):
    if most_chose == 1:
        return 3
    elif most_chose == 2:
        return 2
    elif most_chose == 3:
        return 1
    elif most_chose == 4:
        return 4
    else:
        return 0


def Q8(a1, a7, a5, a2, a10):
    if (a7 + 1 != a1) or (a7 - 1 != a1):
        return 1
    elif (a5 + 1 != a1) or (a5 - 1 != a1):
        return 2
    elif (a2 + 1 != a1) or (a2 - 1 != a1):
        return 3
    elif (a10 + 1 != a1) or (a10 - 1 != a1):
        return 4
    else:
        return 0
    

def Q9(a1, a6, a5, a10, a2, a9):
    if (a1 == a6) != (a5 == a6):
        return 1
    elif (a1 == a6) != (a5 == a10):
        return 2
    elif (a1 == a6) != (a5 == a2):
        return 3
    elif (a1 == a6) != (a5 == a9):
        return 4
    else:
        return 0


def Q10(diff):
    if diff == 1:
        return 4
    elif diff == 2:
        return 2
    elif diff == 3:
        return 1
    elif diff == 4:
        return 3
    else:
        return 0


def most_chose_diff(ans):
    dic = {1: 0, 2: 0, 3: 0, 4: 0}
    for each in ans:
        dic[each] += 1
    lst = sorted(dic, reverse=True)
    lst2 = sorted(dic.values(), reverse=True)
    return lst[0], lst2[0]-lst2[3]


def validate(ans):
    mostchose, diff = most_chose_diff(ans)
    if ans[1] != Q2(ans[4]):
        return False, 2
    if ans[2] != Q3(ans[2], ans[5], ans[1], ans[3]):
        return False, 3
    if ans[3] != Q4(ans[0], ans[1], ans[4], ans[5], ans[6], ans[8], ans[9]):
        return False, 4
    if ans[4] != Q5(ans[4], ans[7], ans[3], ans[8], ans[6]):
        return False, 5
    if ans[5] != Q6(ans[7], ans[1], ans[3], ans[0], ans[5], ans[2], ans[9], ans[4], ans[8]):
        return False, 6
    if ans[6] != Q7(mostchose):
        return False, 7
    if ans[7] != Q8(ans[0], ans[6], ans[4], ans[1], ans[9]):
        return False, 8
    if ans[8] != Q9(ans[0], ans[5], ans[4], ans[9], ans[1], ans[8]):
        return False, 9
    if ans[9] != Q10(diff):
        return False, 10
    return True, 0


def get_ans():
    for a1 in range(1, 5):
        for a2 in range(1, 5):
            for a3 in range(1, 5):
                for a4 in range(1, 5):
                    for a5 in range(1, 5):
                        for a6 in range(1, 5):
                            for a7 in range(1, 5):
                                for a8 in range(1, 5):
                                    for a9 in range(1, 5):
                                        for a10 in range(1, 5):
                                            is_val, num = validate([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])
                                            if is_val:
                                                return [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]


def main():
    print("Correct answer is " + str(get_ans()))
    ANS = input().split(" ")
    ANS = [int(x) for x in ANS]
    isval, number = validate(ANS)
    if isval:
        print("OK")
    else:
        print("Wrong " + str(number))


if __name__ == "__main__":
    main()