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
|
```sh
mkdir weather
cd weather
true > weather
chmod +x weather
ls
nvim weather
#---------------
#!/usr/bin/env python3.11
print("Hello World !")
#---------------
./weather
-> Hello World !
# создать ссылку чтобы запускалось везде:
sudo ln -s $(pwd)/weather /usr/local/bin/
# удалить:
sudo rm /usr/local/bin/weather
#################
true > gps_coordinates.py
true > weather_api_service.py
true > printer.py
ls -l
gps_coordinates.py
printer.py
weather
weather_api_service.py
mv printer.py weather_formatter.py
nvim weather
```
```python
#!/usr/bin/env python3.11
from gps_coordinates import get_gps_coordinates
from weather_api_service import get_weather
from weather_formatter import format_weather
def main():
coordinates = get_gps_coordinates()
weather = get_weather(coordinates)
print(format_weather(weather))
if __name__ == "__main__":
main()
```
`nvim gps_coordinates.py`
```python
def get_gps_coordinates():
""" Returns current coordinates using MackBook GPS """
pass
##########################
# Структура программы со слоями:
from abc import get_abc
def main():
abc = get_abc()
print("Что то там")
if __name__ == "__main__":
main()
##########################
# Просто кортеж:
def abc() -> tuple[float, int]:
"""Return"""
return (0.0, 890)
# Просто список:
def abc() -> dict[str, int]:
"""Return"""
return {'latitude': 10.0, 'longitide': 890}
####################################################
# Именованный кортеж:
from typing import NamedTuple
class Coordinates(NamedTuple):
latitude: float
longitude: float
def get_coordinates() -> tuple[float, float]:
"""Return"""
return Coordinates(latitude=10, longitude=20)
coordinates = get_coordinates()
print(coordinates.latitude)
print(coordinates.longitude)
lat, long = get_coordinates()
print(lat, long)
##########################
# Именованный словарь:
from typing import TupedDict
class Coordinates(TupedDict):
latitude: float
longitude: float
def get_coordinates() -> Coordinates:
"""Return"""
return Coordinates(**{latitude=10, longitude=20})
##########################
# Список:
from typing import Literal
def get_coordinates() \
-> dict[Literal['latitude'] | Literal['longitude], float]:
"""Return"""
return {'latitude': 10.0, 'longitude': 20.0}
##########################
# DataClass лучше именованного кортежа и можно менять в программе:
from dataclasses import dataclass
@dataclass
class Coordinates:
latitude: float
longitude: float
def get_coordinates() -> Coordinates:
"""Return"""
return Coordinates(**{latitude=10, longitude=20})
##########################
```
```python
# Выход/завершение программы:
import sys
sys.exit([status])
# Необязательный аргумент `status` представляет собой статус выхода. Это целочисленное значение, которое указывает на причину завершения программы. Принято считать, что статус 0 означает успешное выполнение, а любой ненулевой статус указывает на ошибку или ненормальное завершение.
# Если аргумент `status` не указан, используется значение по умолчанию 0.
#################
quit()
#################
exit()
```
|