程序員的自我救贖,使用Python開發性格分析工具


程序員的自我救贖,使用Python開發性格分析工具

帕累托法則

上世紀初,意大利經濟學家維爾弗雷多▪帕累託發現了一個有趣的現象:
在意大利, 大約80%的財富掌握在大約20%的人手中,這在後來被概括為帕累托法則(80/20法則),即二八法則。
而全球財富報告稱,中國最富有的那10%的人,擁有中國64%的財富。

如此不均衡的貧富差距,各行業的領導者如何能管理好公司,讓員工們即努力產出,又能安於現狀呢?每個領導者必學的一門課程就是職場心理學。只有你充分了解員工心理與對應的行為表現,才能從容的掌控各類型的人員,從而達到“物盡其用”。

程序員的自我救贖,使用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

<code> 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()/<code>

Enneagram_GUI.py

<code>  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  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)/<code>

剩餘的練習題與答案代碼,就不在這裡贅述了。

對於一位程序員的審美,大家要求別太高,重點來關注下功能實現吧!當然在此之前我們需要先將代碼打包為exe工具,大小8MB。

程序員的自我救贖,使用Python開發性格分析工具

功能OK了,現在不要打擾我,我要做題了!

程序員的自我救贖,使用Python開發性格分析工具

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


分享到:


相關文章: