-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
263 lines (230 loc) · 8.25 KB
/
main.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import os
import readline
import pandas as pd
import mysql.connector as sql
import matplotlib.pyplot as plt
from simple_term_menu import TerminalMenu
db = sql.connect(host="127.0.0.1",port="3306",
user="root",password="JoiAIAxom")
cursor = db.cursor()
cursor.execute("create database gunv;")
cursor.execute("use gunv;")
try: cursor.execute("drop table shootings;")
except : pass
cursor.execute('''create table if not exists shootings
(slno INTEGER PRIMARY KEY AUTO_INCREMENT,
casename varchar(50),
location varchar(50),
fatalities int,
injured int,
total_victims int,
place varchar(50) ,
age int,
race varchar(10),
gender varchar(1),
type varchar(10),
year int(4));''')
df = pd.read_csv(r"shootings.csv")
def convertToSQL_Table():
for x,y in df.iterrows():
data = tuple(y)
cursor.execute(f'''insert into shootings
(casename, location, fatalities, injured,
total_victims, place, age, race, gender, type,
year) values("{data[0]}","{data[1]}",{data[2]},{data[3]},
{data[4]},"{data[5]}",{data[6]},"{data[7]}",
"{data[8]}","{data[9]}",{data[10]});''')
db.commit()
convertToSQL_Table()
def convertToCSV():
cursor.execute('''select casename, location,
fatalities, injured, total_victims, place,
age, race, gender, type, year from shootings;''')
newdf = pd.DataFrame(cursor.fetchall(),
columns=['casename', 'location', 'fatalities',
'injured', 'total_victims', 'place', 'age',
'race', 'gender', 'type', 'year'])
newdf.to_csv("edited-data.csv", index=False)
def editMenu():
print("Choose Action :")
options = ["Add Data", "Delete Row", "Edit Data"]
terminal_menu = TerminalMenu(options,
menu_highlight_style=("fg_yellow",))
index = terminal_menu.show()
if index == None : return
if index == 0: AddData()
if index == 1: DeleteRow()
if index == 2: EditData()
def AddData():
casename = input("Enter Casename : ")
print('\033[1A' + '\033[K', end='')
location = input("Enter Location : ")
print('\033[1A' + '\033[K', end='')
fatalities = input("Enter no of fatalities : ")
print('\033[1A' + '\033[K', end='')
injured = input("Enter no of injured : ")
print('\033[1A' + '\033[K', end='')
total_victims = input("Enter total no of victims : ")
print('\033[1A' + '\033[K', end='')
place = input("Enter place : ")
print('\033[1A' + '\033[K', end='')
age = input("Enter age of shooter : ")
print('\033[1A' + '\033[K', end='')
race = input("Enter race of shooter : ")
print('\033[1A' + '\033[K', end='')
gender = input("Enter gender : ")
print('\033[1A' + '\033[K', end='')
types = input("Enter type of shooting: ")
print('\033[1A' + '\033[K', end='')
year = input("Enter year : ")
print('\033[1A' + '\033[K', end='')
cursor.execute(f'''insert into shootings (casename, location,
fatalities, injured, total_victims, place, age, race, gender,
type, year) values("{casename}","{location}",{fatalities},{injured},
{total_victims},"{place}",{age},"{race}","{gender}","{types}",{year});''')
db.commit()
def EditData():
id = input("Enter the slno : ")
print("Select the field you want to update : ")
options = ["casename", "location", "fatalities", "injured",
"total_victims", "place", "age", "race",
"gender", "type", "year"]
terminal_menu = TerminalMenu(options,menu_highlight_style=("fg_yellow",))
index = terminal_menu.show()
value = input(f"Enter new Value for {options[index]} : ")
if index == 2 or index == 3 or index == 4 or index == 6 or index == 10 :
cursor.execute(f'''update table shootings set {options[index]} = {value} where slno = {id};''')
db.commit()
return
cursor.execute(f'''update shootings set {options[index]} = "{value}" where slno = {id};''')
db.commit()
def DeleteRow():
id = int(input("Enter Row number : "))
cursor.execute(f"delete from shootings where slno = {id};")
db.commit()
def viewMenu():
print("Choose Action :")
options = ["No of people killed by criminals of each race",
"No of Shootings by Place", "No of Shootings by Year",
"Mass vs Spree", "Find cases by location", "List all Data"]
terminal_menu = TerminalMenu(options,menu_highlight_style=("fg_yellow",))
index = terminal_menu.show()
if index == None : return
if index == 0: CriminalsEthnicity()
if index == 1: ShootingsByPlace()
if index == 2: ShootingsByYear()
if index == 3: MassVsSpree()
if index == 4: FindCasesByLocation()
if index == 5:
cursor.execute("select * from shootings;")
data = cursor.fetchall()
print(pd.DataFrame(data).to_string(index=False))
def FindCasesByLocation():
location = input("Enter the Location : ")
cursor.execute(f'''select casename, location, fatalities, injured,
total_victims, place, age, race, gender, type, year from shootings
where location like '%{location}%';''')
data = cursor.fetchall()
print(pd.DataFrame(data,columns=['casename', 'location', 'fatalities',
'injured', 'total_victims', 'place', 'age', 'race', 'gender',
'type', 'year']).to_string(index=False))
def MassVsSpree():
x1 = []
y1 = []
x2 = []
y2 = []
cursor.execute("select year, count(*), type ts from shootings group by year having ts ='Mass';")
data1 = cursor.fetchall()
cursor.execute("select year, count(*), type ts from shootings group by year having ts ='Spree';")
data2 = cursor.fetchall()
for i in data1:
x1.append(i[0])
y1.append(i[1])
for i in data2:
x2.append(i[0])
y2.append(i[1])
plt.xlabel = "Year"
plt.ylabel = "Frequency"
plt.plot(x1,y1,label="Mass",linestyle="-")
plt.plot(x2,y2,label="Spree",linestyle="--")
plt.legend()
plt.show()
def CriminalsEthnicity():
x = []
y = []
cursor.execute("select race, sum(total_victims) as total from shootings group by race;")
data = cursor.fetchall()
for i in data:
x.append(i[0])
y.append(i[1])
plt.barh(x,y)
plt.title("Number of people killed by people of each race")
plt.xlabel("No of victims killed by the race")
plt.ylabel("Race of the criminal")
plt.show()
def ShootingsByPlace():
x = []
y = []
cursor.execute("select place, count(*) as total from shootings group by place;")
data = cursor.fetchall()
for i in data:
x.append(i[0])
y.append(i[1])
plt.barh(x,y)
plt.title("Shootings by place")
plt.xlabel("No of shootings")
plt.ylabel("Place")
plt.show()
def ShootingsByYear():
x = []
y = []
cursor.execute("select Year, count(*) as total from shootings group by year;")
data = cursor.fetchall()
for i in data:
x.append(i[0])
y.append(i[1])
plt.bar(x,y)
plt.title("Number of Shootings per year")
plt.xlabel("Year")
plt.ylabel("No of Shootings")
plt.show()
def clear():
try : os.system("clear")
except : os.system("cls")
return
def help():
print('''
\033[33mFollowing are the commands you can type in this CLI :
> view // To open the view menu
> edit // To open the edit menu
> print // To get the edited data in csv format
> clear // To clear the terminal
> exit // To close this application
''')
if __name__ == "__main__":
commandsList = {
'edit' : editMenu,
'view' : viewMenu,
'help' : help,
'clear' : clear,
'print' : convertToCSV,
'default' : lambda : print('\033[31mType a Valid command')
}
print('''
\033[1;37mWelcome to the world of Crime !
\033[0;37mThis CLI based application will help you analyze data of some selected gun violence case in the USA.
\033[33mFollowing are the commands you can type in this CLI :
> view // To open the view menu
> edit // To open the edit menu
> print // To get the edited data in csv format
> clear // To clear the terminal
> exit // To close this application
> help // To open the help menu
''')
while True:
cm = input("\033[1;34mλ \033[0;37m")
if cm=="exit" : break
try: commandsList.get(cm)()
except Exception as e:
print(e)
commandsList.get('default')()