diff options
| author | vlapa <vlapa@ya.ru> | 2026-06-13 20:33:26 +0300 |
|---|---|---|
| committer | vlapa <vlapa@ya.ru> | 2026-06-13 20:33:26 +0300 |
| commit | e5abc1fe99340b979b18864d978179f71fe8f5c5 (patch) | |
| tree | 98fd0ad7c6d0744894d6e74e446daacd9e6dd1b5 /PYTHON/PROGRAMS.md | |
First
Diffstat (limited to 'PYTHON/PROGRAMS.md')
| -rw-r--r-- | PYTHON/PROGRAMS.md | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/PYTHON/PROGRAMS.md b/PYTHON/PROGRAMS.md new file mode 100644 index 0000000..b4d3d53 --- /dev/null +++ b/PYTHON/PROGRAMS.md @@ -0,0 +1,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() + +```
\ No newline at end of file |
