设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 数据 创业者 手机
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

程序员的自我救赎,使用Python开发性格分析工具

发布时间:2019-11-13 16:32 所属栏目:21 来源:王翔
导读:【线上直播】11月21日晚8点贝壳技术总监侯圣文《数据安全之数据库安全黄金法则》 如此不均衡的贫富差距,各行业的领导者如何能管理好公司,让员工们即努力产出,又能安于现状呢?每个领导者必学的一门课程就是职场心理学。只有你充分了解员工心理与对应的行
【线上直播】11月21日晚8点贝壳技术总监侯圣文《数据安全之数据库安全黄金法则》

如此不均衡的贫富差距,各行业的领导者如何能管理好公司,让员工们即努力产出,又能安于现状呢?每个领导者必学的一门课程就是职场心理学。只有你充分了解员工心理与对应的行为表现,才能从容的掌控各类型的人员,从而达到“物尽其用”。

程序员的自我救赎,使用Python开发性格分析工具

那么职场心理学到底学习什么?

九型人格

程序员的自我救赎,使用Python开发性格分析工具

九型人格是一个近年来倍受国际著名大学MBA学员推崇的课程之一,近十几年来已风行欧美学术界及工商界。

全球500强企业的管理阶层均有研习九型性格,并以此培训员工,建立团队,提高执行力。在当代,它对于企业的前期规划、战略确定、教练指导、企业培训等方面,九型人格有很大的优势。

九型人格不仅仅是一种精妙的性格分析工具,更主要的是为个人修养、自我提升和历练提供更深入的洞察力。

俗话说:“龙生九子,子子不同”。通过九型人格的性格分析工具,将性格心理与行为划分为九类。再对不同性格的人群进行研究分析,找到每一类人最适合岗位与职责。这就是领导者们运筹于帷幄之中,决胜于千里之外的筹码。

人为刀俎,我为鱼肉。不想受制于领导者们的掌控,首先要了解自身的人格分类,才能完善自己的不足。这该如何下手?作为程序员,让我们用代码完成自我的救赎吧!

代码改变世界

刚刚过去的华为HR事件,给我印象最深的不是各阶层的矛盾,而是那位HR说的一句话:

在每位开发的心中,都曾有着一个代码改变世界的愿望!

程序员的自我救赎,使用Python开发性格分析工具

那么今天,我们就用Python开发一套九型人格性格分析工具。用以让更多的人,了解自己的性格分类!

既然是九型人格分析,首先我们需要拿到它的测试题。翻了很久,知道了百度文库的测试原题:

https://wenku.baidu.com/view/19455024dd36a32d72758105.html

程序员的自我救赎,使用Python开发性格分析工具

测试题总共36道,通过各场景下的行为表现,最终分析出你最接近的人格分类。现在题有了,如何做出测试题呢?我选择使用Python的tkinter模块,将测试题开发为一个可执行的exe工具,说干就干!

基础准备

为了能将代码打包成单独的可执行文件,我们需要先准备测试题与对应的答案,然后提前存储在代码中。我们需要进行相关拆分,这种苦力活就交给拥有雷锋精神的我来完成吧:

程序员的自我救赎,使用Python开发性格分析工具

界面开发

界面无需太过复杂,提供说明、题目、选项作答、题目切换与操作按钮即可。当然,交卷后,需要显示用户的测试结果,那么开始吧!

30 minutes later…完成!

Main.py

  1.  1from Enneagram_GUI import * 
  2.  2from tkinter import * 
  3.  3 
  4.  4 
  5.  5def center_window(root, width, height): 
  6.  6    screenwidth = root.winfo_screenwidth() 
  7.  7    screenheight = root.winfo_screenheight() 
  8.  8    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) 
  9.  9    root.geometry(size) 
  10. 10 
  11. 11 
  12. 12root = Tk() 
  13. 13 
  14. 14center_window(root, 750, 700) 
  15. 15 
  16. 16root.resizable(width=False, height=False) 
  17. 17root.title('九型人格测试 | 公众号: 清风Python') 
  18. 18ExamPage(root) 
  19. 19root.mainloop() 

Enneagram_GUI.py

  1.  1# -*- coding: utf-8 -*- 
  2.   2# @Author   : 王翔 
  3.   3# @微信号   : King_Uranus 
  4.   4# @公众号    : 清风Python 
  5.   5# @GitHub   : https://github.com/BreezePython 
  6.   6# @Date     : 2019/11/12 23:12 
  7.   7# @Software : PyCharm 
  8.   8# @version  :Python 3.7.3 
  9.   9# @File     : Enneagram_GUI.py 
  10.  10 
  11.  11 
  12.  12# coding:utf-8 
  13.  13from tkinter import * 
  14.  14import Enneagram_Exam 
  15.  15import Enneagram_Result 
  16.  16import tkinter.messagebox 
  17.  17 
  18.  18# 自测说明 
  19.  19Standard = '此份问卷共有36道测试题目,请在每题中选择你认为最恰当或者最接近描述自己的性格行为的句子,\n' \ 
  20.  20           '请全部作答,最高分的项目很可能成为你的基本性格型态。' 
  21.  21 
  22.  22# 人格类型矩阵 
  23.  23Style_Dict = [ 
  24.  24    {3: 2, 6: 2, 10: 2, 15: 2, 19: 1, 22: 2, 28: 2, 32: 2}, 
  25.  25    {1: 1, 6: 1, 12: 1, 17: 2, 20: 1, 23: 1, 29: 1, 33: 1}, 
  26.  26    {4: 1, 7: 1, 10: 1, 14: 2, 23: 2, 26: 2, 30: 1, 34: 1}, 
  27.  27    {2: 1, 8: 2, 12: 2, 16: 1, 21: 2, 24: 1, 28: 1, 34: 2}, 
  28.  28    {1: 2, 4: 2, 13: 1, 16: 2, 19: 2, 25: 1, 31: 1, 36: 1}, 
  29.  29    {5: 1, 9: 2, 14: 1, 18: 1, 21: 1, 25: 2, 29: 2, 32: 1}, 
  30.  30    {2: 2, 7: 2, 11: 2, 18: 2, 22: 1, 27: 2, 33: 2, 36: 2}, 
  31.  31    {3: 1, 9: 1, 13: 2, 17: 1, 24: 2, 27: 1, 20: 2, 35: 2} 
  32.  32] 
  33.  33 
  34.  34 
  35.  35class ExamPage: 
  36.  36    def __init__(self, master=None): 
  37.  37        self.root = master 
  38.  38        # 用户结果集 
  39.  39        self.user_result = {} 
  40.  40        self.status = 1 
  41.  41        self.All_Exam = Enneagram_Exam 
  42.  42        self.normal_choice = IntVar() 
  43.  43        self.start_exam() 
  44.  44 
  45.  45    # 上一题方法 
  46.  46    def before(self): 
  47.  47        if self.normal_choice.get() != 0: 
  48.  48            self.user_result[self.status] = self.normal_choice.get() 
  49.  49            if self.status > 1: 
  50.  50                self.status -= 1 
  51.  51                self.body.grid_forget() 
  52.  52                self.main_exam() 
  53.  53        else: 
  54.  54            tkinter.messagebox.showwarning("提示:", message="请先选择答案!") 
  55.  55 
  56.  56    # 下一题方法 
  57.  57    def after(self): 
  58.  58        if self.normal_choice.get() != 0: 
  59.  59            self.user_result[self.status] = self.normal_choice.get() 
  60.  60            if self.status < len(Enneagram_Exam.Exam): 
  61.  61                self.status += 1 
  62.  62                self.body.grid_forget() 
  63.  63                self.main_exam() 
  64.  64        else: 
  65.  65            tkinter.messagebox.showwarning("提示:", message="请先选择答案!") 
  66.  66 
  67.  67    # 获取当前题目 
  68.  68    def exam_files(self, num): 
  69.  69        return list(map(lambda x: x.split('.'), self.All_Exam.Exam[num - 1].strip().split('\n'))) 
  70.  70 
  71.  71    # 交卷 
  72.  72    def hand_paper(self): 
  73.  73        self.user_result[self.status] = self.normal_choice.get() 
  74.  74        if len(self.user_result) != 36: 
  75.  75            tkinter.messagebox.showwarning("提示:", message="您还有未完成的测试题!") 
  76.  76        else: 
  77.  77            self.exam_result = LabelFrame(self.root, text="测试结果", padx=10, pady=10, fg="red", font=("黑体", '11')) 
  78.  78            self.exam_result.grid(padx=10, pady=5, sticky=NSEW) 
  79.  79            sc = Scrollbar(self.exam_result) 
  80.  80            sc.grid(row=0, column=1, sticky=NS) 
  81.  81            result_info = Text(self.exam_result, font=("黑体", '11'), width=85, yscrollcommand=sc.set) 
  82.  82            result_info.grid(row=0, column=0, sticky=NSEW) 
  83.  83            sc.config(command=result_info.yview) 
  84.  84            all_num = [] 
  85.  85            for style in Style_Dict: 
  86.  86                calc_num = list( 
  87.  87                    point for point in self.user_result if point in style and self.user_result[point] == style[point]) 
  88.  88                if calc_num == None: 
  89.  89                    all_num.append(0) 
  90.  90                else: 
  91.  91                    all_num.append(len(calc_num)) 
  92.  92            user_type = all_num.index(max(all_num)) 
  93.  93            for line in Enneagram_Result.Result[user_type]: 
  94.  94                result_info.insert(END, line) 
  95.  95 
  96.  96    # 启动测试所需控制按钮 
  97.  97    def start_exam(self): 
  98.  98        self.title = LabelFrame(self.root, text="自测说明", padx=10, pady=10, fg="red", font=("黑体", '11')) 
  99.  99        self.title.grid(padx=10, pady=5) 
  100. 100        note = Label(self.title, text=Standard, justify=LEFT, font=("黑体", '11')) 
  101. 101        note.grid() 
  102. 102        self.show = LabelFrame(self.root, text="选项", padx=10, pady=10, fg="red", font=("黑体", '11')) 
  103. 103        self.show.grid(padx=10, pady=5, sticky=EW) 
  104. 104        go_back = Button(self.show, text="上一题", width=8, command=lambda: self.before()) 
  105. 105        go_back.grid(row=4, column=0, padx=5, pady=10) 
  106. 106        to_forword = Button(self.show, text="下一题", width=8, command=lambda: self.after()) 
  107. 107        to_forword.grid(row=4, column=1, padx=5, pady=10, sticky=E) 
  108. 108        hand_in = Button(self.show, text="交卷", width=8, command=lambda: self.hand_paper()) 
  109. 109        hand_in.grid(row=4, column=2, padx=5, pady=10, sticky=E) 
  110. 110        exit_sys = Button(self.show, text="退出", width=8, command=lambda: sys.exit()) 
  111. 111        exit_sys.grid(row=4, column=3, padx=5, pady=10, sticky=E) 
  112. 112        self.main_exam() 
  113. 113 
  114. 114    # 测试题主界面 
  115. 115    def main_exam(self): 
  116. 116        self.normal_choice.set(0) 
  117. 117        self.body = LabelFrame(self.root, 
  118. 118                               text="测试题  第%s题,剩余%s题" % (self.status, (len(Enneagram_Exam.Exam) - self.status)), 
  119. 119                               padx=10, pady=10, fg="red", font=("黑体", '11')) 
  120. 120        self.body.grid(padx=10, pady=5, sticky=EW) 
  121. 121        for option, choice in self.exam_files(self.status): 
  122. 122            authority_choice = Radiobutton(self.body, text=choice, variable=self.normal_choice, value=option) 
  123. 123            authority_choice.grid(row=option, sticky=W) 
  124. 124        Label(self.body, text="  第%s道题,用户选择的结果是:" % self.status, fg="red", font=("黑体", '11')).grid(row=3, column=0, 
  125. 125                                                                                                   sticky=W) 
  126. 126        Label(self.body, textvariable=self.normal_choice).grid(row=3, column=0, sticky=E) 

剩余的练习题与答案代码,就不在这里赘述了。

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读