Skip to content

Instantly share code, notes, and snippets.

View shner-elmo's full-sized avatar

Shneor E. shner-elmo

View GitHub Profile
@shner-elmo
shner-elmo / main.py
Created October 5, 2023 22:34
Regex split test cases (feel free to add more)
import re
lst = [
(
r"(x)",
"1x2",
["1", "x", "2"],
),
(
r"([^\d]+)",
#!/usr/bin/env python3
r"""
This script will install Poetry and its dependencies in an isolated fashion.
It will perform the following steps:
* Create a new virtual environment using the built-in venv module, or the virtualenv zipapp if venv is unavailable.
This will be created at a platform-specific path (or `$POETRY_HOME` if `$POETRY_HOME` is set:
- `~/Library/Application Support/pypoetry` on macOS
- `$XDG_DATA_HOME/pypoetry` on Linux/Unix (`$XDG_DATA_HOME` is `~/.local/share` if unset)
- `%APPDATA%\pypoetry` on Windows
@shner-elmo
shner-elmo / iter.py
Created April 16, 2023 16:17
Converting a function generator to a class-iterator
from __future__ import annotations
import string
from typing import Iterator
non_word_boundaries = set(string.digits + string.ascii_letters + '_')
class SplitSentence:
def __init__(self, sentence: str):
@shner-elmo
shner-elmo / driver.py
Created March 20, 2023 14:22
A wrapper around Selenium's WebDriver which makes it easier to open and close a given window
from __future__ import annotations
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
@shner-elmo
shner-elmo / concurrent_map.py
Created March 20, 2023 14:18
Concurrent map()
from __future__ import annotations
import concurrent.futures
from typing import Callable, Iterable
def concurrent_map(func: Callable[[T], T2], *it: Iterable[T], max_n_threads: int) -> list[T2]:
"""
Map a function to an iterable asynchronously
@shner-elmo
shner-elmo / dictionary_key_paths.py
Created March 20, 2023 13:51
Get the path of each key in a dictionary
from __future__ import annotations
from typing import Iterator, TypeVar
T = TypeVar('T')
def get_dict_keys_path(dct: dict[T, ...], keys: list[T] | None = None) -> Iterator[T]:
if keys is None:
keys = []
for k, v in dct.items():
@shner-elmo
shner-elmo / get_all_symbols.py
Last active March 11, 2023 18:16
A simple function that uses TradingView's API to get all the stock symbols in the US stock market, and you can filter them by exchange.
from __future__ import annotations
import requests
def get_all_symbols(exchanges: set[str]) -> list[str]:
"""
Get a list with all the symbols filtered by a given exchange.
Valid exchanges: {'AMEX', 'OTC', 'NYSE', 'NASDAQ'}
@shner-elmo
shner-elmo / mysql_weird_PK_error.sql
Created March 2, 2023 11:15
MySQL raises a `DUPLICATE KEY ERROR` from two different values
CREATE TABLE `test` (
`title` varchar(255) PRIMARY KEY
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO test (title) VALUES ('1. Līga'), ('1. liga'); -- raises: Error Code: 1062. Duplicate entry '1. liga' for key 'PRIMARY'
DROP TABLE `test`;
-- ############################################################
CREATE TABLE `test` (