From 91a9305644131a4f5d5c5d58c1c5d61ab8e76778 Mon Sep 17 00:00:00 2001 From: alessandro90 Date: Sun, 11 Nov 2018 16:16:00 +0100 Subject: [PATCH] Constants in uppercase --- audio_player.py | 2 +- main.py | 44 ++++++++++++++++++++++---------------------- threads.py | 8 ++++---- utilities.py | 26 +++++++++++++------------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/audio_player.py b/audio_player.py index d2a7b6a..d8b6b36 100644 --- a/audio_player.py +++ b/audio_player.py @@ -79,7 +79,7 @@ class AudioPlayer(QObject): def set_audio_player(self, fname = ""): self.__first_call = True self.__reset_audio_widget() - full_name = os.path.join(Constants.data_folder, Constants.audio_folder, fname + '.ogg') + full_name = os.path.join(Constants.DATA_FOLDER, Constants.AUDIO_FOLDER, fname + '.ogg') if os.path.exists(full_name): self.__play.setEnabled(True) self.__audio_file = full_name diff --git a/main.py b/main.py index c469ae4..0f534d8 100644 --- a/main.py +++ b/main.py @@ -308,7 +308,7 @@ class MyApp(QMainWindow, Ui_MainWindow): ] def set_mode_tree_widget(self): - for parent, children in Constants.modes.items(): + for parent, children in Constants.MODES.items(): iparent = QTreeWidgetItem([parent]) self.mode_tree_widget.addTopLevelItem(iparent) for child in children: @@ -318,11 +318,11 @@ class MyApp(QMainWindow, Ui_MainWindow): def manage_mode_selections(self): selected_items = self.mode_tree_widget.selectedItems() - parents = Constants.modes.keys() + parents = Constants.MODES.keys() for parent in parents: for item in selected_items: if parent == item.text(0): - for i in range(len(Constants.modes[parent])): + for i in range(len(Constants.MODES[parent])): item.child(i).setSelected(True) def set_initial_size(self): @@ -398,7 +398,7 @@ class MyApp(QMainWindow, Ui_MainWindow): "category_code", "acf",] try: - self.db = read_csv(os.path.join(Constants.data_folder, 'db.csv'), + self.db = read_csv(os.path.join(Constants.DATA_FOLDER, 'db.csv'), sep = '*', header = None, index_col = 0, @@ -442,8 +442,8 @@ class MyApp(QMainWindow, Ui_MainWindow): upper_units = upper_combo_box.currentText() lower_value = lower_spin_box.value() upper_value = upper_spin_box.value() - inf_limit = (lower_value * Constants.conversion_factors[lower_units]) \ - // Constants.conversion_factors[upper_units] + inf_limit = (lower_value * Constants.CONVERSION_FACTORS[lower_units]) \ + // Constants.CONVERSION_FACTORS[upper_units] counter = 0 while inf_limit > upper_spin_box.maximum(): counter += 1 @@ -475,13 +475,13 @@ class MyApp(QMainWindow, Ui_MainWindow): range_lbl): activate_low = False activate_high = False - color = Constants.inactive_color + color = Constants.INACTIVE_COLOR title = '' to_display = '' if activate_low_btn.isChecked(): to_display += str(lower_spinbox.value()) + ' ' + lower_unit.currentText() activate_low = True - color = Constants.active_color + color = Constants.ACTIVE_COLOR if lower_confidence.value() != 0: to_display += ' - ' + str(lower_confidence.value()) + ' %' else: @@ -490,7 +490,7 @@ class MyApp(QMainWindow, Ui_MainWindow): if activate_up_btn.isChecked(): to_display += str(upper_spinbox.value()) + ' ' + upper_unit.currentText() activate_high = True - color = Constants.active_color + color = Constants.ACTIVE_COLOR if upper_confidence.value() != 0: to_display += ' + ' + str(upper_confidence.value()) + ' %' else: @@ -531,7 +531,7 @@ class MyApp(QMainWindow, Ui_MainWindow): def update_status_tip(self, available_signals): if available_signals < self.total_signals: - self.statusbar.setStyleSheet(f'color: {Constants.active_color}') + self.statusbar.setStyleSheet(f'color: {Constants.ACTIVE_COLOR}') else: self.statusbar.setStyleSheet('color: #ffffff') self.statusbar.showMessage(f"{available_signals} out of {self.total_signals} signals displayed.") @@ -598,7 +598,7 @@ class MyApp(QMainWindow, Ui_MainWindow): band_filter_ok = False any_checked = False - for btn, band_limits in zip(self.frequency_filters_btns, Constants.bands): + for btn, band_limits in zip(self.frequency_filters_btns, Constants.BANDS): if btn.isChecked(): any_checked = True if signal_freqs[0] < band_limits.upper and signal_freqs[1] >= band_limits.lower: @@ -667,14 +667,14 @@ class MyApp(QMainWindow, Ui_MainWindow): if not self.apply_remove_mode_filter_btn.isChecked(): return True signal_mode = self.db.at[signal_name, "mode"] - if signal_mode == Constants.unknown: + if signal_mode == Constants.UNKNOWN: if self.include_unknown_modes_btn.isChecked(): return True else: return False selected_items = [item for item in self.mode_tree_widget.selectedItems()] selected_items_text = [i.text(0) for i in selected_items] - parents = [item for item in selected_items_text if item in Constants.modes.keys()] + parents = [item for item in selected_items_text if item in Constants.MODES.keys()] children = [item for item in selected_items_text if item not in parents] ok = [] for item in selected_items: @@ -686,7 +686,7 @@ class MyApp(QMainWindow, Ui_MainWindow): @staticmethod def filters_ok(spinbox, filter_unit, confidence, sign = 1): - band_filter = spinbox.value() * Constants.conversion_factors[filter_unit.currentText()] + band_filter = spinbox.value() * Constants.CONVERSION_FACTORS[filter_unit.currentText()] return band_filter + sign * (confidence.value() * band_filter) // 100 @pyqtSlot(QListWidgetItem, QListWidgetItem) @@ -727,9 +727,9 @@ class MyApp(QMainWindow, Ui_MainWindow): self.description_text.setText(current_signal.at["description"]) for cat, cat_lab in zip(category_code, self.category_labels): if cat == '0': - cat_lab.setStyleSheet(f"color: {Constants.inactive_color};") + cat_lab.setStyleSheet(f"color: {Constants.INACTIVE_COLOR};") elif cat == '1': - cat_lab.setStyleSheet(f"color: {Constants.active_color};") + cat_lab.setStyleSheet(f"color: {Constants.ACTIVE_COLOR};") self.set_band_range(current_signal) self.audio_widget.set_audio_player(self.current_signal_name) else: @@ -741,7 +741,7 @@ class MyApp(QMainWindow, Ui_MainWindow): for lab in self.property_labels: lab.setText("N/A") for lab in self.category_labels: - lab.setStyleSheet(f"color: {Constants.inactive_color};") + lab.setStyleSheet(f"color: {Constants.INACTIVE_COLOR};") self.set_band_range() self.audio_widget.set_audio_player() @@ -788,20 +788,20 @@ class MyApp(QMainWindow, Ui_MainWindow): return 10**9 def display_spectrogram(self): - default_pic = os.path.join(Constants.icons_folder, "nosignalselected.png") + default_pic = os.path.join(Constants.ICONS_FOLDER, "nosignalselected.png") item = self.result_list.currentItem() if item: spectrogram_name = item.text() - path_spectr = os.path.join(Constants.data_folder, Constants.spectra_folder, spectrogram_name + ".png") + path_spectr = os.path.join(Constants.DATA_FOLDER, Constants.SPECTRA_FOLDER, spectrogram_name + ".png") if not QFileInfo(path_spectr).exists(): - path_spectr = os.path.join(Constants.icons_folder, "spectrumnotavailable.png") + path_spectr = os.path.join(Constants.ICONS_FOLDER, "spectrumnotavailable.png") else: path_spectr = default_pic self.spectrogram.setPixmap(QPixmap(path_spectr)) @staticmethod def activate_band_category(band_label, activate = True): - color = Constants.active_color if activate else Constants.inactive_color + color = Constants.ACTIVE_COLOR if activate else Constants.INACTIVE_COLOR for label in band_label: label.setStyleSheet(f"color: {color};") @@ -809,7 +809,7 @@ class MyApp(QMainWindow, Ui_MainWindow): if current_signal is not None and not self.is_undef_freq(current_signal): lower_freq = int(current_signal.at["inf_freq"]) upper_freq = int(current_signal.at["sup_freq"]) - zipped = list(zip(Constants.bands, self.band_labels)) + zipped = list(zip(Constants.BANDS, self.band_labels)) for i, w in enumerate(zipped): band, band_label = w if lower_freq >= band.lower and lower_freq < band.upper: diff --git a/threads.py b/threads.py index 99db647..43ff35b 100644 --- a/threads.py +++ b/threads.py @@ -30,8 +30,8 @@ class DownloadThread(QThread): def run(self): try: - db = urllib3.PoolManager().request('GET', Constants.db_location) - # db = urllib.request.urlopen(Constants.db_location) + db = urllib3.PoolManager().request('GET', Constants.DB_LOCATION) + # db = urllib.request.urlopen(Constants.DB_LOCATION) # raise urllib.error.URLError('Test') except urllib3.exceptions.MaxRetryError: # No internet connection. self.__status = ThreadStatus.NO_CONNECTION_ERR @@ -43,8 +43,8 @@ class DownloadThread(QThread): if not checksum_ok(db.data, "folder"): self.__status = ThreadStatus.BAD_DOWNLOAD_ERR return - if os.path.exists(Constants.data_folder): - rmtree(Constants.data_folder) + if os.path.exists(Constants.DATA_FOLDER): + rmtree(Constants.DATA_FOLDER) try: # data_folder = db.read() with ZipFile(BytesIO(db.data)) as zipped: diff --git a/utilities.py b/utilities.py index 09f6c93..178831e 100644 --- a/utilities.py +++ b/utilities.py @@ -14,12 +14,12 @@ class _ReadOnlyProperty(object): class Constants(object): - db_location = _ReadOnlyProperty('https://aresvalley.com/Storage/Artemis/Database/data.zip') - ref_loc = _ReadOnlyProperty('https://aresvalley.com/Storage/Artemis/Database/data.zip.log') - data_folder = _ReadOnlyProperty('Data') - spectra_folder = _ReadOnlyProperty('Spectra') - audio_folder = _ReadOnlyProperty('Audio') - icons_folder = _ReadOnlyProperty('icons_imgs') + DB_LOCATION = _ReadOnlyProperty('https://aresvalley.com/Storage/Artemis/Database/data.zip') + REF_LOC = _ReadOnlyProperty('https://aresvalley.com/Storage/Artemis/Database/data.zip.log') + DATA_FOLDER = _ReadOnlyProperty('Data') + SPECTRA_FOLDER = _ReadOnlyProperty('Spectra') + AUDIO_FOLDER = _ReadOnlyProperty('Audio') + ICONS_FOLDER = _ReadOnlyProperty('icons_imgs') __Band = namedtuple("Band", ["lower", "upper"]) __ELF = __Band(0, 30) # Formally it is (3, 30) Hz. __SLF = __Band(30, 300) @@ -32,11 +32,11 @@ class Constants(object): __UHF = __Band(300 * 10**6, 3000 * 10**6) __SHF = __Band(3 * 10**9, 30 * 10**9) __EHF = __Band(30 * 10**9, 300 * 10**9) - bands = _ReadOnlyProperty((__ELF, __SLF, __ULF, __VLF, __LF, __MF, __HF, __VHF, __UHF, __SHF, __EHF)) - active_color = _ReadOnlyProperty("#39eaff") - inactive_color = _ReadOnlyProperty("#9f9f9f") - conversion_factors = _ReadOnlyProperty({"Hz":1, "kHz":1000, "MHz":1000000, "GHz":1000000000}) - modes = _ReadOnlyProperty({"FM": ["NFM", "WFM"], + BANDS = _ReadOnlyProperty((__ELF, __SLF, __ULF, __VLF, __LF, __MF, __HF, __VHF, __UHF, __SHF, __EHF)) + ACTIVE_COLOR = _ReadOnlyProperty("#39eaff") + INACTIVE_COLOR = _ReadOnlyProperty("#9f9f9f") + CONVERSION_FACTORS = _ReadOnlyProperty({"Hz":1, "kHz":1000, "MHz":1000000, "GHz":1000000000}) + MODES = _ReadOnlyProperty({"FM": ["NFM", "WFM"], "AM": [], "CW": [], "SK": ["FSK", "PSK", "MSK"], @@ -46,7 +46,7 @@ class Constants(object): "RAW": [], "SC-FDMA": [],} ) - unknown = "Unknown" + UNKNOWN = "Unknown" def reset_apply_remove_btn(button): @@ -65,7 +65,7 @@ def checksum_ok(data, what): else: raise ValueError("Wrong entry name.") try: - reference = read_csv(Constants.ref_loc, delimiter = '*').iat[-1, n] + reference = read_csv(Constants.REF_LOC, delimiter = '*').iat[-1, n] except HTTPError: return False return code.hexdigest() == reference \ No newline at end of file