-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrones.py
94 lines (76 loc) · 3.05 KB
/
drones.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import json
import sys
from utils import background_foreground_color, background_color
# Json valider
# https://jsonlint.com/
class Camera:
"Camera class"
def __init__(self, json_drone, json_camera):
"""Camera data """
self.drone_brand = json_drone['brand']
self.drone_name = json_drone['name']
self.camera_id = json_camera['id']
self.camera_name = json_camera['camera']
self.camera_resolution_x = json_camera['resolution_X']
self.camera_resolution_y = json_camera['resolution_Y']
self.camera_focal = json_camera['focal']
self.camera_x_sensor_size = json_camera['x_sensor_size']
self.camera_y_sensor_size = json_camera['y_sensor_size']
def __str__(self):
camera_string = F"{self.camera_id :^12s}|{self.drone_brand+' '+self.drone_name :^22s}|{self.camera_name :^20s}|"
camera_string += F"{self.camera_resolution_x:>9} x {self.camera_resolution_y:<8}|"
camera_string += F"{self.camera_focal:^13}|"
camera_string += F"{self.camera_x_sensor_size:>5} x {self.camera_y_sensor_size:<6}|"
return camera_string
def header(self):
# Header
header = f"{'Id':^12s}|{'Drone':^22s}|{'Camera':^20s}|"
header += f"{'Sensor resolution px':^17s}|"
header += f"{'Focal mm':^13s}|"
header += f"{'Sensor size mm':^13s}|"
return header
class Drones:
"Drones class"
def __init__(self, json_file='drones.json'):
"""Drones get data from json file """
self.camera_dict = {}
# Read JSON file
with open(json_file) as data_file:
self.data_loaded = json.load(data_file)
for drone in self.data_loaded['Drones']:
for cam in drone['Cameras']:
camdrone = Camera(drone, cam)
self.camera_dict[camdrone.camera_id] = Camera(drone, cam)
def print(self):
"""Custom print() function."""
# Header
header = f"{'Id':^12s}|{'Drone':^22s}|{'Camera':^20s}|"
header += f"{'Sensor resolution px':^17s}|"
header += f"{'Focal mm':^13s}|"
header += f"{'Sensor size mm':^13s}|"
print(background_foreground_color(header, 93, 11))
# Cameras with alternate colors
# for i,camdrone in enumerate(self.camera_dict.items()):
for i, (key, camdrone) in enumerate(self.camera_dict.items()):
if i % 2 == 0:
print(background_color(camdrone.__str__(), 28))
else:
print(background_color(camdrone.__str__(), 19))
def get_camera_by_id(self, camera_id):
"""return camera with camera_number index
"""
cam = self.camera_dict.get(camera_id)
if cam:
return cam
else:
print(F"camera_id {background_color(camera_id,9)} is not in the database. Available camera are")
self.print()
sys.exit(-1)
def main():
"""la fonction main"""
drones_db = Drones()
drones_db.print()
if __name__ == '__main__':
main()