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 / chat.py
Created January 15, 2024 14:04
Python Chat app
import socket
import threading
# define the host and port for the chat server
HOST = 'localhost'
PORT = 12345
# create a new socket for the chat server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@victory-sokolov
victory-sokolov / to_typed_dict.py
Created January 15, 2024 13:59
Python dictionary to TypedDict
from typing import TypedDict
# Define a regular dictionary
my_dict = {
"foo": 1,
"bar": "hello",
"baz": True,
}
# Create a TypedDict class by passing the dictionary keys and their corresponding types to the TypedDict constructor
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WorkerPlugin = require('worker-plugin');
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const smp = new SpeedMeasurePlugin();
const Dotenv = require('dotenv-webpack');
const TerserPlugin = require("terser-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/public/index.html',
@victory-sokolov
victory-sokolov / email-client.ts
Created December 31, 2023 23:35
Node email send
import * as nodemailer from 'nodemailer';
import * as fs from 'fs';
import handlebars from 'handlebars';
import Mail from 'nodemailer/lib/mailer';
type EmailClientArgs<TemplateData> = {
to: string;
subject: string;
templatePath: string;
templateData: TemplateData;
@victory-sokolov
victory-sokolov / lock_contextmanager.py
Created December 18, 2023 10:11
Python lock context manager
from multiprocessing import Manager
class ContextLock:
def __init__(self) -> None:
self.manager = Manager()
self.lock = self.manager.Lock()
def __enter__(self) -> bool:
@victory-sokolov
victory-sokolov / ts-defenitions.d.ts
Created June 11, 2023 19:38
TypeScript TypeDefefnitions
declare module 'express-session' {
export interface SessionData {
userName: string;
}
}
@victory-sokolov
victory-sokolov / server-client.ts
Created June 10, 2023 15:28
Supabase snippets
import { cookies, headers } from 'next/headers';
import { createServerComponentSupabaseClient } from '@supabase/auth-helpers-nextjs';
const supabase = createServerComponentSupabaseClient({
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
headers,
cookies
});
@victory-sokolov
victory-sokolov / admin.py
Created April 1, 2023 16:48
Django admin
# Add Logging to specific data
from django.contrib.admin.models import CHANGE, LogEntry
from django.contrib.contenttypes.models import ContentType
LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(self.model).pk,
object_id=object_id,
object_repr=object_id,
@victory-sokolov
victory-sokolov / memoized_method.py
Created January 18, 2023 06:56
Python method decorator
# ref: https://stackoverflow.com/questions/33672412/python-functools-lru-cache-with-instance-methods-release-object
import functools
import weakref
def memoized_method(*lru_args, **lru_kwargs):
def decorator(func):
@functools.wraps(func)
def wrapped_func(self, *args, **kwargs):
# We're storing the wrapped method inside the instance. If we had
@victory-sokolov
victory-sokolov / randomDate.php
Last active July 14, 2022 10:48
Reusable PHP functions
<?php
$start = '2022-07-10';
$end = '2022-07-12';
function randomDate($start_date, $end_date)
{
// Convert to timetamps
$min = strtotime($start_date);
$max = strtotime($end_date);