-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
172 lines (122 loc) · 4.45 KB
/
test.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
import os
import json
import csv
# Step 1
def list_files_and_folders(path="."):
file_names = []
try:
# Level 1
with os.scandir(path) as entries:
for entry in entries:
if entry.is_dir():
# Level 2
new_path = path + "//" + entry.name
with os.scandir(new_path) as entries:
for entry in entries:
if entry.is_dir():
# Level 3
newest_path = new_path + "//" + entry.name
with os.scandir(newest_path) as entries:
for entry in entries:
if entry.is_dir():
# Level 4
final_path = newest_path + "//" + entry.name
with os.scandir(final_path) as entries:
for entry in entries:
if entry.is_file():
path_to_save = (
final_path
+ "//"
+ entry.name
)
file_names.append(path_to_save)
except Exception as e:
print(f"Error: {e}")
return file_names
# Step 2
def clean_file_names(file_names):
cleaned_file_names = []
for file_name in file_names:
cleaned_file_names.append(file_name.split("//"))
return cleaned_file_names
# Step 3
def get_only_recquired_path(cleaned_file_names):
recquired_path = []
for file_name in cleaned_file_names:
recquired_path.append(file_name[2:])
return recquired_path
def options(state_needed, district_needed, market_needed):
states = []
districts = []
markets = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
state = a["state"]
states.append(state)
if state == state_needed:
for b in a["districts"]:
district = b["district"]
districts.append(district)
if district == district_needed:
for c in b["markets"]:
markets.append(c["market"])
if c["market"] == market_needed:
return states, districts, markets, True
return states, districts, markets, False
def get_commodities():
commodities = []
with open("./commodities.json", "r") as file:
data = json.load(file)
for a in data:
c = a["commodity"]
commodities.append(c)
return commodities
def get_states():
states = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
return states
def get_districts(state):
states = []
districts = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
if s == state:
for b in a["districts"]:
districts.append(b["district"])
return districts
def get_markets(state, district):
states = []
markets = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
if s == state:
for b in a["districts"]:
d = b["district"]
if d == district:
for c in b["markets"]:
markets.append(c["market"])
return markets
if __name__ == "__main__":
# directory_path = "data//CommodityData"
# file_names = list_files_and_folders(directory_path)
# cleaned_file_names = clean_file_names(file_names)
# file_paths = get_only_recquired_path(cleaned_file_names)
# for fp in file_paths:
# print(fp)
# print(options("Gujarat", "Ahmedabad", "Ahmedabad"))
# print(get_states())
# print(get_districts("Gujarat"))
# print(get_markets("Gujarat", "Ahmedabad"))
print(get_commodities())
print("Hello World")