Skip to content

Instantly share code, notes, and snippets.

View e-dreyer's full-sized avatar

Erik Dreyer e-dreyer

View GitHub Profile
# Use the official Mastodon image as the base image
FROM tootsuite/mastodon
# Set the environment variables for Mastodon
ENV RAILS_ENV=production
ENV NODE_ENV=production
# Optionally, you may want to modify the database configuration here
# Expose the ports used by Mastodon
version: "3"
services:
mastodon:
build:
context: . # The path to the directory containing the Dockerfile
dockerfile: Dockerfile
ports:
- "3000:3000"
- "4000:4000"
@e-dreyer
e-dreyer / asyncio_example.py
Created July 25, 2023 07:53
Python asyncio with blocking functions
import asyncio
import time
async def func1():
print('func1 starting')
await asyncio.sleep(3)
print('func1 ending')
return "func1"
async def func2():
@e-dreyer
e-dreyer / main.py
Created July 29, 2023 22:25
Async main loop
if __name__ == "__main__":
config = ConfigAccessor("config.yml")
credentials = ConfigAccessor("credentials.yml")
bot = MyBot(credentials=credentials, config=config)
async def main_loop():
while True:
loop = asyncio.get_event_loop()
await asyncio.gather(
@e-dreyer
e-dreyer / bs4_async_find_test.py
Created August 2, 2023 12:01
Beautifulsoup Async find and find_all
import asyncio
import time
import aiohttp
import html5lib
from bs4 import BeautifulSoup, ResultSet, Tag, PageElement
from typing import List, Dict, AnyStr, Any
SELECTED_URL = "https://discuss.python.org/latest"
@e-dreyer
e-dreyer / zip_with_dicts.py
Created August 2, 2023 14:32
Python zip usage with dictionaries
# Sample dictionaries
names = {'Alice': 25, 'Bob': 30, 'Charlie': 22}
cities = {'Alice': 'New York', 'Bob': 'San Francisco', 'Charlie': 'London'}
# Using zip to combine keys and values from both dictionaries
combined_data = zip(names.keys(), names.values(), cities.values())
# Converting the zip object to a dictionary
result_dict = {name: (age, city) for name, age, city in combined_data}
@e-dreyer
e-dreyer / Dict_value_split_zip.py
Created August 2, 2023 14:39
Splitting dictionaries into separate values with zip
# Three dictionaries with the same structure
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 4, 'b': 5, 'c': 6}
dict3 = {'a': 7, 'b': 8, 'c': 9}
# Using zip and dictionary comprehension to extract the values
values_list = [[dict1[key], dict2[key], dict3[key]] for key in dict1.keys()]
print(values_list)
@e-dreyer
e-dreyer / deep_dict_compare.py
Created August 2, 2023 14:42
Deep comparison of dictionaries using zip
def deep_compare_dicts(dict1, dict2):
if len(dict1) != len(dict2):
return False
for (key1, value1), (key2, value2) in zip(dict1.items(), dict2.items()):
if key1 != key2:
return False
if isinstance(value1, dict) and isinstance(value2, dict):
if not deep_compare_dicts(value1, value2):
@e-dreyer
e-dreyer / zip_copy_missing_keys.py
Created August 2, 2023 15:19
Zip copy missing keys
def copy_missing_keys(source_dict, destination_dict):
for key, value in source_dict.items():
if key not in destination_dict:
destination_dict[key] = value
# Sample dictionaries
dict1 = {'key1': 42, 'key2': 10, 'key3': 5}
dict2 = {'key2': 15, 'key4': 23}
# Copy missing keys and values from dict1 to dict2
@e-dreyer
e-dreyer / configAccessor.py
Created August 3, 2023 10:12
ConfigAccessor ABC Python class
from typing import Any, Dict
import yaml
from collections import UserDict
class ConfigAccessor(UserDict):
def __init__(self, file_name: str) -> None:
super().__init__()
self.file_name = file_name
try: