Skip to content

Commit d1dbc96

Browse files
committed
Adds type hints for improved code clarity
Adds type hints to function signatures and variable declarations across multiple modules. This improves code readability, maintainability, and helps prevent potential type-related errors.
1 parent 1b7c6ee commit d1dbc96

3 files changed

Lines changed: 31 additions & 27 deletions

File tree

pydeepskylog/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Configuration file for pydeepskylog package.
33
Contains constants and configuration values used across the package.
44
"""
5+
from typing import List
56

67
# Constants for contrast reserve calculations
78
class ContrastReserveConfig:
@@ -13,19 +14,19 @@ class ContrastReserveConfig:
1314
# ANGLE is a 1D array of log10(angular size in arcminutes) values.
1415
# Each entry defines a grid point for the angular size axis in the LTC table.
1516
# Used for interpolation of threshold contrast as a function of object angular size.
16-
ANGLE = [
17+
ANGLE: List[float] = [
1718
-0.2255, 0.5563, 0.9859, 1.260,
1819
1.742, 2.083, 2.556,
1920
]
2021

21-
ANGLE_SIZE = len(ANGLE)
22+
ANGLE_SIZE: int = len(ANGLE)
2223

2324
# LTC: Log Threshold Contrast Table
2425
# LTC is a 2D array where each row corresponds to a specific sky background brightness (integer values from 4 to 23).
2526
# Each column corresponds to a log10(angular size) value as defined in the ANGLE array.
2627
# LTC[sb][i] gives the log threshold contrast for sky brightness index sb and angle index i.
2728
# Used for interpolating the minimum contrast required for detection at given sky brightness and object size.
28-
LTC = [
29+
LTC: List[List[float]] = [
2930
[
3031
4, -0.3769, -1.8064, -2.3368, -2.4601,
3132
-2.5469, -2.5610, -2.5660,
@@ -124,4 +125,4 @@ class ContrastReserveConfig:
124125
],
125126
]
126127

127-
LTC_SIZE = len(LTC)
128+
LTC_SIZE: int = len(LTC)

pydeepskylog/contrast_reserve.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import math
3+
from typing import Optional, Tuple, List
34
from pydeepskylog.config import ContrastReserveConfig
45

56

@@ -31,8 +32,8 @@ def surface_brightness(magnitude: float, object_diameter1: float, object_diamete
3132

3233
def validate_contrast_reserve_inputs(
3334
sqm: float, telescope_diameter: float, magnification: float,
34-
surf_brightness: float, magnitude: float,
35-
object_diameter1: float, object_diameter2: float
35+
surf_brightness: Optional[float], magnitude: Optional[float],
36+
object_diameter1: Optional[float], object_diameter2: Optional[float]
3637
) -> None:
3738
"""
3839
Validates the inputs for the contrast_reserve function.
@@ -85,7 +86,7 @@ def validate_contrast_reserve_inputs(
8586
def calculate_initial_parameters(
8687
sqm: float, telescope_diameter: float,
8788
object_diameter1: float, object_diameter2: float
88-
) -> tuple:
89+
) -> Tuple[float, float, float, float]:
8990
"""
9091
Calculates initial parameters needed for contrast reserve calculation.
9192
@@ -119,8 +120,8 @@ def calculate_initial_parameters(
119120

120121

121122
def calculate_log_object_contrast(
122-
sqm: float, surf_brightness: float, magnitude: float,
123-
object_diameter1: float, object_diameter2: float
123+
sqm: float, surf_brightness: Optional[float], magnitude: Optional[float],
124+
object_diameter1: Optional[float], object_diameter2: Optional[float]
124125
) -> float:
125126
"""
126127
Calculates the log object contrast.
@@ -237,8 +238,8 @@ def calculate_threshold_contrast(sky_background_brightness: float, angular_size_
237238

238239

239240
def contrast_reserve(
240-
sqm: float, telescope_diameter: float, magnification: float, surf_brightness: float, magnitude: float,
241-
object_diameter1: float, object_diameter2: float
241+
sqm: float, telescope_diameter: float, magnification: float, surf_brightness: Optional[float],
242+
magnitude: Optional[float], object_diameter1: Optional[float], object_diameter2: Optional[float]
242243
) -> float:
243244
"""
244245
Calculate the contrast reserve
@@ -300,8 +301,9 @@ def contrast_reserve(
300301

301302

302303
def optimal_detection_magnification(
303-
sqm: float, telescope_diameter: float, surf_brightness: float, magnitude: float, object_diameter1: float, object_diameter2: float,
304-
magnifications: list) -> float:
304+
sqm: float, telescope_diameter: float, surf_brightness: Optional[float],
305+
magnitude: Optional[float], object_diameter1: Optional[float], object_diameter2: Optional[float],
306+
magnifications: List[float]) -> float:
305307
"""
306308
Calculate the best magnification to use for the object to detect it
307309

pydeepskylog/deepskylog_interface.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import requests
22
import time
3+
from typing import Dict, List, Any, Optional
34

4-
DSL_API_BASE_URL = "https://test.deepskylog.org/api/" # Change this as needed
5+
DSL_API_BASE_URL: str = "https://test.deepskylog.org/api/" # Change this as needed
56

67
# Simple in-memory cache: {url: (timestamp, data)}
7-
_DSL_API_CACHE = {}
8-
_DSL_API_CACHE_TTL = 300 # seconds (5 minutes)
8+
_DSL_API_CACHE: Dict[str, tuple[float, Any]] = {}
9+
_DSL_API_CACHE_TTL: int = 300 # seconds (5 minutes)
910

10-
def dsl_instruments(username: str) -> dict:
11+
def dsl_instruments(username: str) -> Dict[str, Any]:
1112
"""
1213
Get all defined instruments of a DeepskyLog user.
1314
@@ -21,7 +22,7 @@ def dsl_instruments(username: str) -> dict:
2122
"""
2223
return _dsl_api_call("instrument", username)
2324

24-
def dsl_eyepieces(username: str) -> dict:
25+
def dsl_eyepieces(username: str) -> Dict[str, Any]:
2526
"""
2627
Get all defined eyepieces of a DeepskyLog user.
2728
@@ -35,7 +36,7 @@ def dsl_eyepieces(username: str) -> dict:
3536
"""
3637
return _dsl_api_call("eyepieces", username)
3738

38-
def dsl_lenses(username: str) -> dict:
39+
def dsl_lenses(username: str) -> Dict[str, Any]:
3940
"""
4041
Get all defined lenses of a DeepskyLog user.
4142
@@ -49,7 +50,7 @@ def dsl_lenses(username: str) -> dict:
4950
"""
5051
return _dsl_api_call("lenses", username)
5152

52-
def dsl_filters(username: str) -> dict:
53+
def dsl_filters(username: str) -> Dict[str, Any]:
5354
"""
5455
Get all defined filters of a DeepskyLog user.
5556
@@ -64,7 +65,7 @@ def dsl_filters(username: str) -> dict:
6465
return _dsl_api_call("filters", username)
6566

6667

67-
def calculate_magnifications(instrument: dict, eyepieces: dict) -> list:
68+
def calculate_magnifications(instrument: Dict[str, Any], eyepieces: List[Dict[str, Any]]) -> List[float]:
6869
"""
6970
Calculate possible magnifications for a given telescope and eyepieces.
7071
@@ -87,7 +88,7 @@ def calculate_magnifications(instrument: dict, eyepieces: dict) -> list:
8788
Returns:
8889
list: A list of possible magnifications for the telescope.
8990
"""
90-
magnifications = []
91+
magnifications: List[float] = []
9192
# Check if the instrument has a fixed magnification
9293
if instrument["fixedMagnification"]:
9394
magnifications.append(instrument["fixedMagnification"])
@@ -105,7 +106,7 @@ def convert_instrument_type_to_int(instrument_type: str) -> int:
105106
:param instrument_type: The instrument type as a string.
106107
:return: The instrument type as an integer.
107108
"""
108-
instrument_types = {
109+
instrument_types: Dict[str, int] = {
109110
"Naked Eye": 0,
110111
"Binoculars": 1,
111112
"Refractor": 2,
@@ -126,7 +127,7 @@ def convert_instrument_type_to_string(instrument_type: int) -> str:
126127
:param instrument_type: The instrument type as an integer.
127128
:return: The instrument type as a string.
128129
"""
129-
instrument_types = {
130+
instrument_types: Dict[str, int] = {
130131
0: "Naked Eye",
131132
1: "Binoculars",
132133
2: "Refractor",
@@ -141,7 +142,7 @@ def convert_instrument_type_to_string(instrument_type: int) -> str:
141142

142143
return instrument_types[instrument_type]
143144

144-
def _dsl_api_call(api_call: str, username: str) -> dict:
145+
def _dsl_api_call(api_call: str, username: str) -> Dict[str, Any]:
145146
"""
146147
Make an API call to the DeepskyLog system.
147148
@@ -155,8 +156,8 @@ def _dsl_api_call(api_call: str, username: str) -> dict:
155156
Returns:
156157
dict: The response from the API call, parsed as a JSON dictionary.
157158
"""
158-
api_url = f"{DSL_API_BASE_URL}{api_call}/{username}"
159-
now = time.time()
159+
api_url: str = f"{DSL_API_BASE_URL}{api_call}/{username}"
160+
now: float = time.time()
160161

161162
# Check cache
162163
cache_entry = _DSL_API_CACHE.get(api_url)

0 commit comments

Comments
 (0)