-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.py
128 lines (114 loc) · 5.16 KB
/
writer.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
from enum import Enum
from database import NearEarthObject,OrbitPath
import csv
class OutputFormat(Enum):
"""
Enum representing supported output formatting options for search results.
"""
display = 'display'
csv_file = 'csv_file'
@staticmethod
def list():
"""
:return: list of string representations of OutputFormat enums
"""
return list(map(lambda output: output.value, OutputFormat))
class NEOWriter(object):
"""
Python object use to write the results from supported output formatting options.
"""
def __init__(self):
# TODO: How can we use the OutputFormat in the NEOWriter?
pass
def write(self, format, data, **kwargs):
"""
Generic write interface that, depending on the OutputFormat selected calls the
appropriate instance write function
:param format: str representing the OutputFormat
:param data: collection of NearEarthObject or OrbitPath results
:param kwargs: Additional attributes used for formatting output e.g. filename
:return: bool representing if write successful or not
"""
# TODO: Using the OutputFormat, how can we organize our 'write' logic for output to stdout vs to csvfile
# TODO: into instance methods for NEOWriter? Write instance methods that write() can call to do the necessary
# TODO: output format.
if(not isinstance(data, list)):
return False
writer=False
if(format=="display"):
writer=self.display_write(data)
elif(format=="csv_file"):
writer=self.csv_write(data)
else:
return False
if(writer):
return True
else:
return False
return True
def display_write(self,data):
try:
if(len(data)==0):
print("\n")
print("**************************************************************")
print("Sorry no near earth objects could be found for the given parameters.")
else:
if(type(data[0])==NearEarthObject):
count=0
for neo in data:
count=count+1
print("\n\n")
print("***************************** Near Earth Object {} *********************************".format(count))
print(neo)
print()
print("*********** Orbit Paths *************")
for orbit in neo.orbits:
print(orbit)
elif(type(data[0])==OrbitPath):
count=0
for orbit in data:
count=count+1
print("\n\n")
print("***************************** Orbit Path {} *********************************".format(count))
print(orbit)
else:
return False
print()
return True
except:
return False
def csv_write(self,data):
try:
with open('neo_database_result.csv', 'w') as file:
writer = csv.writer(file)
if(len(data)==0):
writer.writerow(["No data found","No data found","No data found","No data found","No data found","No data found"])
return True
if(type(data[0])==NearEarthObject):
writer.writerow(["NEO Id", "Name", "Near Earth Object No.", "Orbit Path No.", "Total Orbits", "Close Approach Date"])
if(len(data)==0):
writer.writerow(["No data found","No data found","No data found","No data found","No data found","No data found"])
else:
count=0
for neo in data:
count=count+1
count2=0
for orbit in neo.orbits:
count2=count2+1
row_list=[orbit.neo_reference_id,orbit.neo_name,count,count2,len(neo.orbits),orbit.close_approach_date]
writer.writerow(row_list)
elif(type(data[0])==OrbitPath):
writer.writerow(["Orbit-NEO Id", "NEO-Name", "Orbit No.", "Miss-Distance (in km)", "Close Approach Date"])
if(len(data)==0):
writer.writerow(["No data found","No data found","No data found","No data found","No data found"])
else:
count=0
for orbit in data:
count=count+1
row_list=[orbit.neo_reference_id,orbit.neo_name,count,orbit.miss_distance_kilometers,orbit.close_approach_date]
writer.writerow(row_list)
else:
return False
return True
except:
return False