Skip to content

Instantly share code, notes, and snippets.

View victory-sokolov's full-sized avatar
🎯
Focusing

Viktor Sokolov victory-sokolov

🎯
Focusing
View GitHub Profile
@victory-sokolov
victory-sokolov / font.js
Created March 16, 2022 10:20
Dynamically load font
var junction_font = new FontFace('Junction Regular', 'url(fonts/junction-regular.woff)');
junction_font.load().then(function(loaded_face) {
document.fonts.add(loaded_face);
document.body.style.fontFamily = '"Junction Regular", Arial';
}).catch(function(error) {
// error occurred
});
@victory-sokolov
victory-sokolov / django-launch.json
Last active February 13, 2024 07:06
VSCode debugging configs
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debugpy attach",
"type": "python",
"request": "attach",
@victory-sokolov
victory-sokolov / img_utils.py
Last active January 24, 2022 08:34
Image utils for Python
import io
import cv2
import base64
import numpy as np
from PIL import Image
# Take in base64 string and return PIL image
def string_to_image(base64_string):
imgdata = base64.b64decode(base64_string)
return Image.open(io.BytesIO(imgdata))
@victory-sokolov
victory-sokolov / twiiterUncheckAdsInterests.js
Created December 26, 2021 19:36
Twitter uncheck all interests
function clearNextInterest(){
const selector = "div > input[type='checkbox']:checked";
const next = document.querySelector(selector);
if(next){
next.scrollIntoView();
next.click();
setTimeout(clearNextInterest,500)
};
};
clearNextInterest();
@victory-sokolov
victory-sokolov / fetch.js
Created December 20, 2021 08:55
Fetch wrapper
export class HttpMethod {
static GET = "GET";
static POST = "POST";
static PUT = "PUT";
static DELETE = "DELETE";
}
const request = async ({endpoint, params, method = HttpMethod.GET}) {
@victory-sokolov
victory-sokolov / circular-types.py
Created December 9, 2021 15:05
Circular Types import
# Reference: https://adamj.eu/tech/2021/05/13/python-type-hints-how-to-fix-circular-imports/
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from models import Book
@victory-sokolov
victory-sokolov / decorator_logger.py
Last active February 25, 2023 12:38
Python logger class
from functools import wraps, partial
import logging
def attach_wrapper(obj, func=None): # Helper function that attaches function as attribute of an object
if func is None:
return partial(attach_wrapper, obj)
setattr(obj, func.__name__, func)
return func
def log(level, message): # Actual decorator
@victory-sokolov
victory-sokolov / post.js
Last active November 18, 2021 10:31
Gatsby Table of contents
{typeof tableOfContents.items === "undefined" ? null : (
<Toc>
<h3 className="uppercase">Table of contents</h3>
<ContentsList items={tableOfContents.items} />
</Toc>
)}
@victory-sokolov
victory-sokolov / imutabledict.py
Created November 3, 2021 19:26
Python immutable dictionary
# reference: https://stackoverflow.com/questions/11014262/how-to-create-an-immutable-dictionary-in-python?utm_source=pocket_mylist
class imdict(dict):
def __hash__(self):
return id(self)
def _immutable(self, *args, **kws):
raise TypeError('object is immutable')
__setitem__ = _immutable
@victory-sokolov
victory-sokolov / django_soft_delete.py
Last active January 14, 2022 09:45
Django SoftDelete
from django.db import models
from django.utils import timezone
from django.db.models.query import QuerySet
class SoftDeletionQuerySet(QuerySet):
def soft_delete(self):
return super(SoftDeletionQuerySet, self).update(deleted=timezone.now())
def restore(self):
return super(SoftDeletionQuerySet, self).update(deleted=None)