Python + bash
Если вам часто требуется запускать shell команды из Python-кода, какой способ вы используете?
Самый низкоуровневый это функция os.system(), либо os.popen(). Рекомендованный способ это subprocess.call(). Но это всё еще достаточно неудобно.
Советую обратить своё внимание на очень крутую библиотеку sh.
Что она умеет?
🔸 удобный синтаксис вызова команд как функций
# os
import os
os.system("tar cvf demo.tar ~/")
# subprocess
import subprocess
subprocess.call(['tar', 'cvf', 'demo.tar', '~/'])
# sh
import sh
sh.tar('cvf', 'demo.tar', "~/")
🔸 простое создание функции-алиаса для длинной команды
fn = sh.lsof.bake('-i', '-P', '-n')
output = sh.grep(fn(), 'LISTEN')
в этом примере также задействован пайпинг
🔸 удобный вызов команд от sudo
with sh.contrib.sudo:
print(ls("/root"))
Такой запрос спросит пароль. Чтобы это работало нужно соответствующим способом настроить юзера.
А вот вариант с вводом пароля через код.
password = "secret"
sudo = sh.sudo.bake("-S", _in=password+"\n")
print(sudo.ls("/root"))
Это не все фишки. Больше интересных примеров смотрите в документации.
Специально для Windows💀 юзеров
#libs#linux
#Desert
Iced Fudge Brownies
22p 342kcal/p
Brownie 240g butter 360g semi-sweet chocolate 200g sugar 1tbsp vanilla extract 1tbsp instant coffee granules 4 L eggs room temperature 62.5g all-purpose flour 43g cocoa powder 1tsp baking powder
CHOCOLATE ICING 110g butter, at room temperature 360g powdered sugar 43g cocoa powder 2tsp vanilla extract 60g warmed milk sprinkles, for decorating (opt)
Instructions
1. 9×13 inch pan w/paper 2. melt the butter & 3/4 chocolate chips until smooth. Stir in the sugar, vanilla, and instant coffee. 3. whisk 4 eggs, add into the chocolate mix. Add the flour, cocoa powder, and baking powder. Stir until just combined. Stir in the remaining 1/4 chocolate chips. Spread into baking pan.
175°C 28-30min. Cool. 4. make the icing. Mix the butter, powdered sugar, cocoa powder, vanilla, and 1/4 cup warm milk until smooth, adding additional warm milk until the frosting becomes thicker. Add more sugar if needed. Spread the icing over the bars.
Decorate with sprinkles.
Kinder Rice Krispie bar
60g salted or unsalted butter
300g mini marshmallows
125g Rice Krispies
24 mini Kinder chocolate bars, 8 (100g) for the base, 16 (200g) for the topping Optional: Kinder halves to decorate
Tin size: 8x8"
#Desert
🌎 In Australia’s dry grasslands, the spinifex hopping mouse leaps up to 3 meters in a single bound—over 10 times its body length! Powerful back legs and long tails help it escape predators and thrive in deserts where water is scarce. ✨
#animals⚡#adaptation⚡#desert
👉subscribe Interesting Planet