Ранее мы уже говорили о том, как выполнить какой-либо код перед открытием интерактивной консоли.
Расскажу еще один способ! На самом деле, даже запустив интерпретатор в обычном режиме с выполнением скрипта из файла вы можете в любом месте активировать интерактивный режим. Или даже несколько по очереди. За это отвечает модуль 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
Multipurpose Court Project
Bishoftu,Ethiopia
Construction on the new basketball court at Hibret (Tokuma) Elementary School is progressing smoothly and is nearly halfway complete. We're excited about the progress and are on track to achieve our goal.
Your support is crucial to ensure the project's successful completion.
Our goal is to create a vibrant playground for the children of Hibret School.
Play is not a privilege; it's a life skill. Every child deserves a place to play!
To contribute and support us please contact us at:
[email protected]
#Playground#AllDeserveToPlay
#Rotaract_Kuriftu
#Rotary
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia
Follow Us:
Facebook | Twitter | Instagram | Telegram | LinkedIn
On Sunday, February 23, 2025, we will be holding a blood donation, dog adoption, and fundraising program in collaboration with Ahadu Rotaract Club at the Red Cross compound around Addis Ababa Stadium starting 9AM
To those who want to donate:
- Food (Rice) for feeding purpose
- Pet beds
- Comfy blankets
- Medications
- Pet toys
- Dry pet foods
- pet accessaries
We will gladly be waiting, the program will be held until 5pm. Come, save lives by donating blood, create a family by adopting dogs!
#BloodDonation#DogAdoption#CommunityService#AddisAbaba#Ethiopia#SaveLives#GiveBack#AnimalRescue#Rotaract#AhaduRotaract#Fundraising#CharityEvent
https://t.me/starvetbooks
YES! 🙌 It's a wrap! 🥳
The HaraCoders Bootcamp training, brought to you by the awesome Rotaract Club of Kuriftu 🤝, BuildX Ethiopia , Sira Ale, and Addis Ababa University College of Veterinary Medicine and Agriculture , has been a HUGE success!
We're sending out waves of gratitude to everyone involved and a BIG congrats to our fantastic trainees! 🎉
So proud of you all! 🔥
Stay tuned... round 2 might be closer than you think! 😉🚀
#AddisCoders#BuildXEthiopia#TechForAll#EmpowerTheFuture#CodingBootCamp#EthiopianTech#LearnToCode#CodingForChange
#Rotary
#Rackuriftu
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia
🚀 Exciting News! 🚀
The AddisCoders BootCamp is brought to you by BuildX Ethiopia in partnership with Rotaract Kuriftu, SERRALE, and Addis Ababa University! 🎓💻
Together, we’re empowering Ethiopian youth with hands-on web development skills to build the future of tech! 🔥
Stay tuned for more updates!
#AddisCoders#BuildXEthiopia#TechForAll#EmpowerTheFuture#CodingBootCamp#EthiopianTech#LearnToCode#CodingForChange
#Rotary
#Rackuriftu
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia