I want to setup a local network printer for me and my roomates, so they can print papers remotely without sending file to me then I have to do the operation.

At the very beginning, I installed Windows Server 2016 on a miniPC, it works perfectly fine but the CPU (Atom D425 1.8GHz) is really a pain in the ass when I try to do other jobs(Remote Display, IIS Hosting, etc). So I change the OS to Ubuntu and the performace becomes better.

Sharing a printer on Ubuntu is not that hard(it’s more complicated than its on Windows which has a GUI configuration and you just need to click some buttons), we just need to configure the printer in the system then install CUPS.

Environment

  • Ubuntu 16.04

Steps

  1. Plug the printer in and set it up.
    • In Ubuntu desktop, go to Settings -> Printers, then click the Add button. The left panel shows all the ports that already connected to the machine, choose the printer then choose the right driver, the system will take care of the rest configurations.
    • You can print a test page to check whether the printer is working when it’s done.
  2. Install CUPS
    1. Open Terminal(you can open it by pressing CTRL+ALT+T on desktop).
    2. Execute commands:
      sudo apt update
      sudo apt install cups

      After the installation, CUPS Web Page will listening on port 631, visit http://127.0.0.1:631 to get access to CUPS.

    3. 19-04-13 Update:
      I checked the settings again and realized that you don’t have to setup a reverse proxy, CUPS(version 2.1.3) will automatically listen on the external IP. But you still have to disable https if you don’t have a SSL certification or using a IP address.
      Open CUPS’ config file, located at /etc/cpus/cpusd.conf and add this line(the full config file documentation is here):

      DefaultEncryption Never

      Then restart CPUS service:

      sudo service cups restart
      # or use this
      sudo systemctl restart cups.service

      Visit http://[IP address or domain]:631 to see the web interface.
      Old stuff:

      If you want to visit CUPS web page on your other devices, you can install nginx to setup a simple reverse proxy.

      sudo apt install nginx

      Open a file named [your name].conf under /etc/nginx/conf.d/, then paste:

      server {
          listen 632;
          server_name cups;
          location / {
               proxy_pass http://127.0.0.1:631;
          }
      }

      Save changes, quit, execute commands:

      sudo nginx -t # test config file
      sudo nginx -s reload

      Then visit http://[your machine’s ip]:632 to get access to CUPS.

  3. Once you open the CUPS webpage, go to Administration, check the Share printers connected to this system box, then click Change Settings.
    • Type in your system account if it asks authentication.

If you successfully configured the printer in the system, CUPS will automatically detect the printer and share it in your local network. You just need to add the network printer on the other devices.

 

接触一下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()