Ранее мы уже говорили о том, как выполнить какой-либо код перед открытием интерактивной консоли.
Расскажу еще один способ! На самом деле, даже запустив интерпретатор в обычном режиме с выполнением скрипта из файла вы можете в любом месте активировать интерактивный режим. Или даже несколько по очереди. За это отвечает модуль code.
Как это может пригодиться?
🔸 Вам не хватает pdb и хочется больше "власти"
🔸 Нужно запросить у юзера данные в достаточно сложном виде. В этом случае можно попросить его создать что ему надо и сохранить в определённую переменную, с которой потом и работать.
🔸 Нужна изолированная среда для выполнения каких-либо действий.
🔸 Просто забавы ради😁
Запускается консоль очень просто
import code
ic = code.InteractiveConsole()
try:
ic.interact()
except SystemExit:
pass
Выход обратно на предыдущий уровень происходит как обычно, вызов функции exit() или клавиши Ctrl+D(Ctrl+Z для Windows).
Я набросал простой пример с некоторым функционалом
🔹 Меняются символы приглашения
🔹 В неймспейсы добавляются дополнительные объекты
🔹 Считается время, проведённое в интерактивном режиме
🔹 Скрипт просит юзера заполнить переменную и по выходу распечатывает её значение
Код здесь ↗️
Для запуска сохраните код в файл и запускайте как обычный скрипт.
python my_console.py
Для быстрого запуска можно сделать отдельный алиас
alias py="python3 /home/username/my_console.py"
#tricks#source
#Mannheim#stabbing
• Male cop tackles this guy for some reason
• Cop Attempts to restrain him
• Male cops gets stabbed by the actual #stabber
• Blue coat guy still gets arrested by useless female cop
W.T.F 🤷♂
These have to be the stupidest police officers ever!
LIVE: UK police give an update after stabbing of lawmaker David Amess
British police give a briefing after British lawmaker David Amess was stabbed to death by a man who attacked him at a meeting with constituents.
#Reuters#UKMP#stabbing # #Live#UKpolitics
➖@reutersworldchannel➖
LIVE: View outside the church where UK lawmaker David Amess was stabbed to death
Live view outside the Belfairs Methodist Church where British Conservative lawmaker David Amess was stabbed to death by a man who walked into a meeting with voters.
#Reuters#UKMP#stabbing # #Live#UKpolitics
Subscribe: http://smarturl.it/reuterssubscribe
Reuters brings you the latest business, finance and breaking news video from around the globe. Our reputation for accuracy and impartiality is unparalleled.
Get the latest news on: http://reuters.com/
Follow Reuters on Facebook: https://www.facebook.com/Reuters
Follow Reuters on Twitter: https://twitter.com/Reuters
Follow Reuters on Instagram: https://www.instagram.com/reuters/?hl=en
➖@reutersworldchannel➖
Several people injured in a mass-#stabbing at a rally in #Germany organised by #PaxEuropa, which is an organisation opposed to the "Islamisation of Europe".
Activist and blogger Michael #Stürzenberger was among the injured.
A German cop also got stabbed in the neck after he attacked one of the rally members, (one of those who was trying to subdue mad stabber), and then the stupid cop knelt on his chest like he was the problem - a microcosm of #EU decision-making.
Looked like the cop just saw white German guy grappling with a muslim guy and immediately presumed that the white guy was the problem.
Sometimes you deserve what you get 🤷♂
The last video looks the astoundingly stupid German police officers placing the white German guy under arrest 🤷♂
Compare and contrast #UK#PrimeMinister#Starmer's reaction to:
• "firey but mostly peaceful" #BLM riots in the US after the death of petty criminal drug-addict George Floyd
Vs
•#White Working class "Anti-#Stabbing" riots in the #UK after the #murder of 3 #children