This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
язык_программирования_python [2025/06/16 14:18] val |
язык_программирования_python [2025/07/08 09:53] (current) val |
||
---|---|---|---|
Line 26: | Line 26: | ||
===== Интерактивная оболочка REPL (Read-Eval-Print Loop) ===== | ===== Интерактивная оболочка REPL (Read-Eval-Print Loop) ===== | ||
+ | |||
+ | <code> | ||
+ | (venv1) # python | ||
+ | </code> | ||
+ | |||
+ | * [[https://ru.wikipedia.org/wiki/IPython]] | ||
+ | |||
<code> | <code> | ||
(venv1) # pip install ipython | (venv1) # pip install ipython | ||
</code> | </code> | ||
+ | |||
+ | * [[https://jupyter.org/try-jupyter/notebooks/?path=notebooks/Intro.ipynb|try-jupyter/notebooks]] | ||
+ | * [[https://matplotlib.org/stable/tutorials/pyplot.html|Pyplot tutorial]] | ||
===== Web приложение ===== | ===== Web приложение ===== | ||
Line 157: | Line 167: | ||
* [[https://tproger.ru/articles/testiruem-na-python-unittest-i-pytest-instrukcija-dlja-nachinajushhih|Тестируем на Python: unittest и pytest. Инструкция для начинающих]] | * [[https://tproger.ru/articles/testiruem-na-python-unittest-i-pytest-instrukcija-dlja-nachinajushhih|Тестируем на Python: unittest и pytest. Инструкция для начинающих]] | ||
+ | |||
+ | * [[https://habr.com/ru/articles/349860/|Регулярные выражения в Python от простого к сложному. Подробности, примеры, картинки, упражнения]] | ||
+ | * [[https://regex101.com/|Online Regular expression tester with syntax highlighting]] | ||
<code> | <code> | ||
Line 187: | Line 200: | ||
* [[https://www.geeksforgeeks.org/python-find-dictionary-matching-value-in-list/|Find dictionary matching value in list - Python]] | * [[https://www.geeksforgeeks.org/python-find-dictionary-matching-value-in-list/|Find dictionary matching value in list - Python]] | ||
* [[https://docs-python.ru/tutorial/vstroennye-funktsii-interpretatora-python/funktsija-next/|Функция next() в Python, следующий элемент итератора]] | * [[https://docs-python.ru/tutorial/vstroennye-funktsii-interpretatora-python/funktsija-next/|Функция next() в Python, следующий элемент итератора]] | ||
+ | |||
+ | ==== Пример 1 ==== | ||
<code> | <code> | ||
gate# apt install python3-xmltodict/stable | gate# apt install python3-xmltodict/stable | ||
Line 222: | Line 237: | ||
</code> | </code> | ||
+ | ==== Пример 2 ==== | ||
+ | <code> | ||
+ | # cat /usr/local/bin/asterisk.chansip.discovery.py | ||
+ | </code><code> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
+ | import subprocess, io, json | ||
+ | |||
+ | result=[] | ||
+ | |||
+ | proc = subprocess.Popen('/usr/sbin/asterisk -x "sip show users" | tail -n +2 | cut -d" " -f1', stdout=subprocess.PIPE, shell=True) | ||
+ | for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): | ||
+ | #print(line.rstrip()) | ||
+ | s={} | ||
+ | s["{#CHANNAME}"]="SIP/"+line.rstrip() | ||
+ | result.insert(1,s) | ||
+ | print(json.dumps(result)) | ||
+ | </code> | ||
+ | |||
+ | Вариант от DeepSeek | ||
+ | |||
+ | <code> | ||
+ | # cat /usr/local/bin/asterisk.chansip.discovery.ds.py | ||
+ | </code><code> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
+ | import subprocess | ||
+ | import json | ||
+ | |||
+ | # Запускаем команду и сразу получаем вывод | ||
+ | output = subprocess.check_output( | ||
+ | '/usr/sbin/asterisk -x "sip show users" | tail -n +2 | cut -d" " -f1', | ||
+ | shell=True, | ||
+ | text=True | ||
+ | ) | ||
+ | |||
+ | # Формируем список словарей в одну строку | ||
+ | result = [{"{#CHANNAME}": f"SIP/{line.strip()}"} for line in output.splitlines() if line.strip()] | ||
+ | |||
+ | # Выводим результат в формате JSON | ||
+ | print(json.dumps(result)) | ||
+ | </code> | ||
===== Zabbix API приложение ===== | ===== Zabbix API приложение ===== | ||