YAML
install
read data from a .yml file
Usage
1
2
3
4
5
6
7
| import yaml
with open('config.yml', 'r') as file:
yaml_file = yaml.safe_load(file)
print(yaml_file['prime_numbers'][0])
print(yaml_file['rest']['url'])
|
1
2
3
4
| rest:
url: "https://example.org/primenumbers/v1"
port: 8443
prime_numbers: [2, 3, 5, 7, 11, 13, 17, 19]
|
Icecream
icecream is a Python library that makes it easier to debug your code by printing variables and expressions to stdout. It’s a drop-in replacement for print() that adds a lot of useful features.
install
Usage
1
2
3
4
5
| import icecream as ice
from icecream import ic
test = "hello world"
ic(test)
|
output:
1
| ic| test: 'hello world'
|
Documentation
You don’t need to import icecream in every file. Instead you can add this to your main.py:
You can disable or enable icecream with:
icecream
Pick
install
Usage
Select multiple items
1
2
3
4
5
6
| from pick import pick
title = 'Please choose your favorite programming language (press SPACE to mark, ENTER to continue): '
options = ['Java', 'JavaScript', 'Python', 'PHP', 'C++', 'Erlang', 'Haskell']
selected = pick(options, title, multiselect=True, min_selection_count=1)
print(selected)
|
[('Java', 0), ('C++', 4)]
Returns an array with Tuples containing the selected String and the coressponding index
You can access items like a 2d array
Select one item
1
2
3
4
5
6
7
| from pick import pick
title = 'Please choose your favorite programming language: '
options = ['Java', 'JavaScript', 'Python', 'PHP', 'C++', 'Erlang', 'Haskell']
option, index = pick(options, title)
print(option)
print(index)
|
Options
options
: a list of options to choose fromtitle
: (optional) a title above options listindicator
: (optional) custom the selection indicator, defaults to *
default_index
: (optional) set this if the default selected option is not the first onemultiselect
: (optional), if set to True its possible to select multiple items by hitting SPACEmin_selection_count
: (optional) for multi select feature to dictate a minimum of selected items before continuingscreen
: (optional), if you are using pick
within an existing curses application set this to your existing screen
object. It is assumed this has initialised in the standard way (e.g. via curses.wrapper()
, or curses.noecho(); curses.cbreak(); screen.kepad(True)
)
Beautiful Soup 4
install
1
2
| pip3 install requests
pip3 install beautifulsoup4
|
Usage
Get HTML
1
2
3
4
5
6
7
| import requests
from bs4 import BeautifulSoup
URL = "https://example.org"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html.parser')
|
Search for elements
Search for elements by tag and class name
1
| tag = soup.find('h2', class_='sc-1b6bgon-2 edmOvr font-primary')
|
Search for elements by tag and id
1
| tag = soup.find('h2', id='title')
|
Search for elements by tag and attribute
1
| tag = soup.find('h2', attrs={'data-id': 'title'})
|
Get text and attributes
Get text inside a tag
Get attribute value
Selenium
install
1
2
| brew install geckodriver
pip3 install selenium
|
automate web browser
Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| from selenium import webdriver
from selenium.webdriver.common.by import By
from os import system as cmd
driver = webdriver.Firefox() #open browser
driver.maximize_window() #maximize window
driver.get("https://www.google.com") #open url
tag_name = driver.find_elements(by=By.TAG_NAME, value="a") #find all elements with tag name a
class_names = driver.find_elements(by=By.CLASS_NAME, value="myclass") #find all elements with class name myclass
id_name = driver.find_elements(by=By.ID, value="myid") #find element with id myid
element = tag_name[0] #select first element
element.click() #click element
inner_html_text = element.text #get text inside a tag
href = element.get_attribute("href") #get attribute value
url = driver.current_url #get current url
driver.close() #close browser
cmd("pkill firefox") #close all firefox instances
|
headless mode
Run selenium without opening a browser window.
1
2
3
| op = webdriver.FirefoxOptions()
op.add_argument('--headless')
driver = webdriver.Firefox(options=op)
|
Streamlit
install
Usage
1
2
3
| import streamlit as st
st.title("Hello World!")
|
Components
title
The title of a streamlit page
1
| st.title("Hello World!")
|
Display text
1
2
3
4
5
6
7
8
9
10
| st.text('Fixed width text')
st.markdown('_Markdown_') # see *
st.latex(r''' e^{i\pi} + 1 = 0 ''')
st.write('Most objects') # df, err, func, keras!
st.write(['st', 'is <', 3]) # see *
st.title('My title')
st.header('My header')
st.subheader('My sub')
st.code('for i in range(8): foo()')
* optional kwarg unsafe_allow_html = True
|
Display data
1
2
3
4
| st.dataframe(my_dataframe)
st.table(data.iloc[0:10])
st.json({'foo':'bar','fu':'ba'})
st.metric('My metric', 42, 2)
|
1
2
3
| st.image('./header.png')
st.audio(data)
st.video(data)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| st.button('Click me')
st.data_editor('Edit data', data)
st.checkbox('I agree')
st.toggle('Enable')
st.radio('Pick one', ['cats', 'dogs'])
st.selectbox('Pick one', ['cats', 'dogs'])
st.multiselect('Buy', ['milk', 'apples', 'potatoes'])
st.slider('Pick a number', 0, 100)
st.select_slider('Pick a size', ['S', 'M', 'L'])
st.text_input('First name')
st.number_input('Pick a number', 0, 10)
st.text_area('Text to translate')
st.date_input('Your birthday')
st.time_input('Meeting time')
st.file_uploader('Upload a CSV')
st.download_button('Download file', data)
st.camera_input("Take a picture")
st.color_picker('Pick a color')
|
Layout
Streamlt uses a column based layout. You can add columns with st.columns()
and then add elements to the columns.
1
2
3
4
5
| col1, col2 = st.columns(2) # split into 2 columns
with col1:
st.write('This will appear in column 1')
with col2:
st.write('This will appear in column 2')
|
Multiple Pages
Stremlit can automatically create a sidebar for you. If you create every page in a seperate python file just put every page file except the main file in a folder called pages
. Then a sidebar will be created automatically.
Pyautogui
install
Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| import pyautogui as bot
#only needed if you want to use the mouse
size = bot.size()
width = size[0]
height = size[1]
# left click
bot.click("left")
# right click
bot.click("right")
# drag from (0, 0) to (100, 100) relatively with a duration of 0.1s
bot.drag(0, 0, 100, 100, absolute=False, duration=0.1)
bot.press("enter")
bot.press("space")
bot.keyUp("shift")
bot.keyDown("shift")
bot.write("Hello world!", interval=0.25) #write text with 0.25 seconds delay between each key
|
Keep in mind that pyautogui may use a different keyboard layout than your system. For example, if you use a german keyboard layout, the key “z” is mapped to the key “y” in pyautogui.
documentation
Eel
Run a webserver with python in the backend and javascript in the frontend
Usage
All website files are in the site
folder.
This starts a webserver on localhost:8000
and opens the index.html
file in firefox
1
2
3
4
5
6
7
8
| import eel
eel.init('site')
@eel.expose #expose function to javascript
def hello_world():
print("Hello World!")
eel.start('index.html', mode='firefox', host='localhost', port=8000)
|
1
2
3
4
5
| eel.hello_world() //call python function
function hello_world() {
eel.hello_world() //wrap python function in javascript function
}
|
passing arguments
1
2
3
| @eel.expose
def get_ip():
return 'loclahost'
|
Here the python function returns a string which then is used in javascript
1
2
3
4
5
| function getIP() {
eel.get_ip()((ip) => {
document.querySelector('#ip').innerHTML = ip
})
}
|
pyfiglet
pyfiglet is a python library to create ascii art from text
Usage
1
2
3
4
5
6
7
| import pyfiglet
def text_to_ascii(text: str):
ascii_art = pyfiglet.figlet_format(text)
print(ascii_art)
text_to_ascii("Hello World!")
|
You can also pass options to the figlet_format() function to change the font and layout of the ASCII art.
1
| text_to_ascii("Hello World!", font="big")
|
This will change the font to “big”
You can see the list of available fonts by running
1
| print(pyfiglet.getFonts())
|
Discord Bot
1
| pip3 install discord.py
|
Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| import discord
client = discord.Client()
@client.event
async def on_ready():
print("Bot is ready.")
@client.event
async def on_message(message):
if message.content.startswith("!hello"):
await message.channel.send("Hello!")
client.run("YOUR_TOKEN_HERE")
|
To check for admin permissions:
1
2
| if message.author == client.user and str(message.author) != ADMIN:
break
|
Telegram Bot
1
| pip3 install python-telegram-bot
|
To create a new bot serarch for @BotFather in telegram and type /newbot
.
Usage
starter template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| import logging
from telegram import Update
from telegram.ext import *
TOKEN = 'your bot token'
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
application = ApplicationBuilder().token(TOKEN).build()
# /start function
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text="Ok I'm working on it")
# main bot function
async def bot_response(update: Update, context: ContextTypes.DEFAULT_TYPE):
text = update.message.text.lower()
if text.__contains__('hi'):
response = 'Hello'
await context.bot.send_message(chat_id=update.effective_chat.id, text=response)
# handle slash commands
application.add_handler(CommandHandler('start', start))
# handle normal messages
application.add_handler(MessageHandler(filters.TEXT, bot_response))
application.run_polling()
|
documentation