Skip to content

Instantly share code, notes, and snippets.

View tomjere's full-sized avatar

Thomas Jereczek tomjere

View GitHub Profile
@tomjere
tomjere / convert_encoding.py
Created March 22, 2023 09:38
Convert a file from one encoding to another
"""
Convert a file from one encoder to another
usage: python convert_encoding.py 'target encoding' 'source file' 'target file'
example: python convert_encoding.py 'utf-8' 'file.html' 'file-utf8.html'
"""
import sys
from chardet import detect
@tomjere
tomjere / find_duplicates_in_list.py
Created July 20, 2021 12:22
Function finds duplicates in a list. - Funktion findet Duplikate in einer Liste.
import collections
def find_duplicates_in_list(list_to_check: list) -> list:
"""Finds duplicate / multiple appearances in a given list,
returns a list with the duplicate elements
:param list_to_check: list to check for duplicate elements
:return: a list with the elements which appeared in the list more than one time
@tomjere
tomjere / flatten_nested_lists.py
Created July 20, 2021 11:52
Flatten a list which contains nested lists. - Vereinfacht eine Liste, die verschachtelte Listen enthält.
def flatten_nested_lists(list_of_lists: list) -> list:
"""Flatten a given list which might contains nested lists
and returns one flat list with all elements.
:param list_of_lists: list which might have nested lists as elements
:return: a list with all elements as flatten content
"""
for item in list_of_lists:
@tomjere
tomjere / confluence_util.py
Last active June 2, 2022 20:58
With this script you can access Confluence via REST-API to create, modify or delete pages. You can create the needed "auth" file for authentication with the script create_authorization.py. - Mit diesem Skript kann man per REST-API auf Confluence zugreifen, um Seiten anzulegen, zu ändern oder zu löschen. Die benötigte "auth"-Datei zur Authentifiz…
"""With this script you can access Confluence via REST-API to create, modify or delete pages.
An auth file for the Confluence authorization is needed.
You can create this file with the script create_authorization.py."""
import datetime
from os.path import exists
import json
import requests
URL_BASE = 'https://<your-confluence-url>/rest/api/content'
@tomjere
tomjere / date_for_weekday.py
Last active July 20, 2021 08:20
The function determines the date for a given combination ​of year, month and weekday. - Die Funktion ermittelt das Datum für eine gegebene Kombination aus Jahr, Monat und Wochentag.
import calendar
from datetime import date
from enum import IntEnum
class Weekday(IntEnum):
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
@tomjere
tomjere / leap_year.py
Created July 19, 2021 10:30
Function checks if year is a leap year or not. - Funktion prüft, ob ein Jahr ein Schaltjahr ist.
def is_leap_year(year: int) -> bool:
"""Function determined if the given year is a leap year or not.
:param year: the year to be checked.
:return: True, if the year is a leap year.
False, if the year is no leap year.
"""
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
@tomjere
tomjere / parse_ranges.py
Created July 19, 2021 09:07
The script contains a generator function which parses a string of ranges of numbers, and returns every single number of the range(s). - Das Skript enthält eine Generator-Funktion, die eine Zeichenkette mit Zahlenbereichen parst und jede einzelne Zahl des Bereichs/der Bereiche zurückgibt.
from typing import Iterable
def parse_ranges(range_string: str) -> Iterable[int]:
"""This generator function parses a given string for ranges of numbers,
which are separated by a comma,
and return the range as single numbers.
:param range_string - string which contains one or more range(s) of numbers, e.g. '0, 4-8, 20->exit, 43-45'
@tomjere
tomjere / tail_of_sequence.py
Created July 19, 2021 08:35
The script contains a function which returns a number of elements of a given sequence, starting at the end of the sequence. - Das Skript enthält eine Funktion, die eine Anzahl von Elementen einer gegebenen Sequenz zurückgibt, beginnend am Ende der Sequenz.
import collections
import types
from typing import Sequence
def tail_of_sequence(sequence: Sequence, number: int) -> Sequence:
"""Returns n elements from a given sequence, counted from the end of the sequence.
:param sequence - sequence which elements are been filtered
:param number - number of elements which will be returned
@tomjere
tomjere / alphanumeric_sort.py
Last active July 19, 2021 08:29
The script contains an example of a "key" function that allows the "sorted" function to return a correct alphanumeric sort. - Das Skript enthält ein Beispiel für eine key-Funktion, die es der sorted-Funktion ermöglicht, eine korrekte alphanumerische Sortierung zurückzugeben.
import re
def alphanumeric_sort(alphanumeric_list):
"""Returns a given list in correct alphanumeric sort,
e.g. 1, 2, 3, 10, 21 instead of 1, 10, 2, 21, 3
:return sorted alphanumeric list
"""
@tomjere
tomjere / examples_for_beautifulsoup.py
Last active July 19, 2021 08:28
The script contains some examples of basic commands for web scraping with BeautifulSoup that can be used as a starting point. - Das Skript enthält einige Beispiele für Basisbefehle für Web-Scraping mit BeautifulSoup, die als Ausgangspunkt verwendet werden können.
import requests
from bs4 import BeautifulSoup
'''Beispiele für Zugriffsmöglichkeiten auf HTML-Code,
der per request geholt und mit BeautifulSoup aufbereitet wurde.
Original-Doku von BeautifulSoup unter https://www.crummy.com/software/BeautifulSoup/bs4/doc/#
Dieses Skript enthält nur ein paar Basis-Befehle, um die Syntax im praktischen Beispiel zu zeigen.
Die Befehle sollten in eigene Skripte übernommen, und dort entsprechend angepasst werden.
Wenn man dieses Skript startet, sollte man die meisten print-Befehle kommentieren,
ansonsten wird die Ausgabe zu umfangreich und unübersichtlich.