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 / 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)
const uuid = () => Date.now().toString(36) + Math.random().toString(36).substr(2);
@victory-sokolov
victory-sokolov / npm-reference
Last active September 10, 2021 12:35
NPM reference
NPM commands reference
// shows what will be published to npm
npm publish --dry-run
// Unpublish specific version
npm unpublish package-name@version
@victory-sokolov
victory-sokolov / git-reference.md
Last active March 25, 2023 17:50
Git commands reference

Git command reference

// remove all files marked as deleted from git git ls-files --deleted -z | xargs -0 git rm

// change remote url from HTTP to SSH git remote set-url origin [email protected]:USERNAME/REPOSITORY.git

// add files to the previous commit without modifying commit message git commit --amend --no-edit

@victory-sokolov
victory-sokolov / object-snippet.js
Last active November 29, 2021 20:38
Different object snippets
const obj = {
name: "Fenix",
age: 1000,
sallary: "9000$"
}
// Omit specific keys from object
const omit = (obj, ...props) => {
const filteredArray = Object.entries(obj).filter(([key, value]) => !props.includes(key));
return Object.fromEntries(filteredArray);
@victory-sokolov
victory-sokolov / https-python-server.py
Created August 26, 2021 09:18
Python https server
import http.server
import ssl
import os
def start_server(host: str = '127.0.0.1', port: int = 4443):
httpd = http.server.HTTPServer((host, port), http.server.SimpleHTTPRequestHandler)
server = http.server.HTTPServer.get_request(httpd)[0].getsockname()
print(f'Server is running at https://{server[0]}:{server[1]}')
@victory-sokolov
victory-sokolov / isMobile.ts
Created May 4, 2021 15:10
Check if user is on a mobile device
const isMobileDevice = (): boolean => {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
.test(navigator.userAgent)) {
return true;
}
return false;
};