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

上世纪初,意大利经济学家维尔弗雷多▪帕累托发现了一个有趣的现象:在意大利, 大约80%的财富掌握在大
首页 新闻资讯 行业资讯 程序员的自我救赎,使用Python开发性格分析工具

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

 

 


 

[[282200]]

 

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

九型人格

 

 


 

 

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

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

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

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

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

代码改变世界

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

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

 

 

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

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

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

 

 


 

 

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

基础准备

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

 

 

界面开发

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

30 minutes later…完成!

Main.py

 

复制

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

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

Enneagram_GUI.py

 

复制

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

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

  • 103.

  • 104.

  • 105.

  • 106.

  • 107.

  • 108.

  • 109.

  • 110.

  • 111.

  • 112.

  • 113.

  • 114.

  • 115.

  • 116.

  • 117.

  • 118.

  • 119.

  • 120.

  • 121.

  • 122.

  • 123.

  • 124.

  • 125.

  • 126.

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

对于一位程序员的审美,大家要求别太高,重点来关注下功能实现吧!当然在此之前我们需要先将代码打包为exe工具,大小8MB。

 

 

功能OK了,现在不要打扰我,我要做题了!

 

 


 

 

我的答案是完美型,处女座总是在追求完美的路上跟自己死磕,哎…活得好累。

32    2019-11-13 14:16:09    Python 开发 工具