This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from typing import Awaitable, Final, List, TYPE_CHECKING, TypedDict | |
from channels.auth import AuthMiddlewareStack | |
from channels.db import database_sync_to_async | |
from django.contrib.auth.models import AnonymousUser | |
from rest_framework.exceptions import AuthenticationFailed | |
from rest_framework_jwt.authentication import JSONWebTokenAuthentication | |
from rest_framework_jwt.blacklist.exceptions import MissingToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maybe_get(obj: Any, items: List[Hashable], default: Optional[Any] = None) -> Any: | |
""" | |
Retrieves a value from nested dicts and objects. | |
Given an object which can be a dict-like object or regular objects (aka, objects | |
who have values accessible via `getattr` or `operator.getitem`), and a list of | |
accessors for those objects, this function will retrieve the specified value. | |
This is perhaps easier to understand with an example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get store number from https://www.ikea.com/ext/us/local-stores/ | |
# | |
# Requires python 3.8+ | |
# | |
# Usage | |
# ================== | |
# | |
# $ python mekea.py 410 00470251 | |
# Availability info for 00470251 | |
# ┏━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A decorator to lock celery tasks using django's cache framework. | |
Based upon example in Celery's task cookbook. | |
https://docs.celeryproject.org/en/stable/tutorials/task-cookbook.html | |
""" | |
import logging | |
import time | |
from celery import Task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PathPath(click.Path): | |
"""A Click path argument that returns a pathlib Path, not a string""" | |
def __init__(self, *args, require_dir: bool = False, make_absolute: | |
bool=True, **kwargs) -> None: | |
super().__init__(*args, **kwargs) | |
self.require_dir = require_dir | |
self.make_absolute = make_absolute | |
def convert(self, value, param, ctx): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
SMWinservice | |
by Davide Mastromatteo | |
Base class to create winservice in Python | |
----------------------------------------- | |
Instructions: | |
1. Just create a new class that inherits from this base class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodedata | |
def remove_control_characters(s): | |
return "".join(ch for ch in s if unicodedata.category(ch)[0] != "C") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from functools import lru_cache | |
from unicodedata import category | |
@lru_cache() | |
def get_punctuation_chars(): | |
""" | |
https://stackoverflow.com/a/60983873 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""https://stackoverflow.com/a/12480543/23972""" | |
import os | |
from os.path import join, getsize | |
dirs_dict = {} | |
#We need to walk the tree from the bottom up so that a directory can have easy | |
# access to the size of its subdirectories. | |
for root, dirs, files in os.walk('python/Lib/email',topdown = False): | |
# Loop through every non directory file in this directory and sum their sizes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const highlightWord = (text, word, className) => { | |
const parts = text.split(new RegExp(`(${word})`, "gi")); | |
for (let i = 1; i < parts.length; i += 2) { | |
parts[i] = <span className={className} key={i}>{parts[i]}</span>; | |
} | |
return parts | |
}; |