Skip to content

Instantly share code, notes, and snippets.

View Rotzke's full-sized avatar
🇩🇪

Nils Bergmüller Rotzke

🇩🇪
  • Berlin, Germany
  • 10:55 (UTC +01:00)
View GitHub Profile
@Rotzke
Rotzke / logging.py
Last active September 26, 2021 20:40
Logging module config
# stdout
logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
level=logging.INFO, datefmt='%Y/%m/%dT%H:%M:%S')
# file
logging.basicConfig(filename="log",
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
@Rotzke
Rotzke / requests_pil.py
Last active February 11, 2018 22:25
Requests images downloader
import requests
from PIL import Image
from io import BytesIO
filename = 'image.jpg'
r = requests.get('https://example.com/image.jpg')
i = Image.open(BytesIO(r.content))
i.save(filename)
@Rotzke
Rotzke / multiprocessing.py
Created February 11, 2018 22:23
Multiprocessing example
import urllib2
import csv
from bs4 import BeautifulSoup
from multiprocessing.dummy import Pool # This is a thread-based Pool
from multiprocessing import cpu_count
def crawlToCSV(URLrecord):
OpenSomeSiteURL = urllib2.urlopen(URLrecord)
Soup_SomeSite = BeautifulSoup(OpenSomeSiteURL, "lxml")
OpenSomeSiteURL.close()
@Rotzke
Rotzke / asyncio.py
Last active October 1, 2023 02:25
Asyncio with semaphores
import random
import asyncio
from aiohttp import ClientSession
async def fetch(url, session):
async with session.get(url) as response:
delay = response.headers.get("DELAY")
date = response.headers.get("DATE")
print("{}:{} with delay {}".format(date, response.url, delay))
return await response.read()
PUT /_cluster/settings
{
"transient": {
"logger.org.elasticsearch.transport": "trace"
}
}
GET {index}/{type}/_search
{
"size": 0,
"aggs": {
"{aggregation_name}": {
"terms": {
"field": "{filed_value}",
"size": 10
}
}
PUT /twitter/_settings
{
"index" : {
"number_of_replicas" : 2
}
}
@Rotzke
Rotzke / dataclasses.py
Created June 30, 2020 16:34
Dataclasses in Python 3.7+
from dataclasses import dataclass
@dataclass
class Card:
rank: str
suit: str
card = Card("Q", "hearts")
print(card == card)
import signal
import sys
import asyncio
import aiohttp
import json
loop = asyncio.get_event_loop()
client = aiohttp.ClientSession(loop=loop)
async def get_json(client, url):
@Rotzke
Rotzke / cc.txt
Last active September 17, 2021 09:24
Universal Credit Card RegEx Pattern
(?:4[0-9]{12}(?:[0-9]{3})?|(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})