Skip to content

Instantly share code, notes, and snippets.

View deven96's full-sized avatar
:shipit:
Too much to learn... so little time

Diretnan Domnan deven96

:shipit:
Too much to learn... so little time
View GitHub Profile
@lordsarcastic
lordsarcastic / Implement OTP for Django application for multiple uses
Last active October 8, 2023 13:38
This is a generic implementation of OTP for Django applications. It can be extended to any framework or platform that uses OTP.
This is a robust implementation that is extensible for anything that requires the use of OTP. Want to use OTP to verify
a user? Check! Want to use OTP to validate an order? CHeck! Want to use OTP to reset a password? CHEck! Want to use
OTP to verify a device? CHECk! Want to use OTP to fight people? CHECK!
I used: Django (framework), Django Rest Framework (a plugin for REST API), PostgreSQL (database)
and email (for sending the otp). You can substitute any of these for whatever you want, like using Redis instead of Postgres.
While this solution is built to be used for Django, I have added comments to explain the process for developers using
other frameworks.
Legend:
@toksdotdev
toksdotdev / why-size-of-option-t-is-doubled.md
Last active May 9, 2022 16:21
An explanation of why the size of Option<T> is always often double the size of T

Someone asked a question some weeks back about why size_of::<Option<T>> is always double. Answer is because of alignment.

Explanation

How are Rust enum represented in C?

C doesn't have the ability to directly represent complex Rust enum, hence, the need for a workaround. To understand that, let's take a look at how Option<i32> is represnted in C.

E.g. Given:

@victor-iyi
victor-iyi / .vimrc
Last active May 19, 2021 11:09
My Vim configuration file
" File: ~/.vimrc
"
" Author: Victor I. Afolabi
syntax enable
colorscheme desert
" highlight Normal guibg=none
" =============================================================================
@sadhasivam
sadhasivam / go-interview-questions.md
Created November 23, 2020 00:04
Golang Interview Questions
  • Installation: 1- Explain how Go path works? 2- What are the benefits of Go Module (reference its commands)?

  • Concurrency: 1- Explain Concurrency & when to use it? 2- How would you allow communication between goroutines in Go? 3- How would you manage their access to resources?

  1. why do you use Go (my answer was as simple as "why i shouldn't", and some extra points Grimacing face)
@mblondel
mblondel / check_convex.py
Last active March 21, 2022 22:25
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@jimmychu0807
jimmychu0807 / string-conversion.rs
Created November 21, 2019 10:20
Conversion between String, str, Vec<u8>, Vec<char> in Rust
use std::str;
fn main() {
// -- FROM: vec of chars --
let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// to String
let string1: String = src1.iter().collect::<String>();
// to str
let str1: &str = &src1.iter().collect::<String>();
// to vec of byte
# Set up the environment and collect the observation space and action space sizes
env = gym.make("CartPole-v1")
observation_space = env.observation_space.shape[0]
action_space = env.action_space.n
# The function for creating the initial population
organism_creator = lambda : Organism([observation_space, 16, 16, 16, action_space], output='softmax')
def simulate_and_evaluate(organism, trials=1):
"""
# Load the data
df = pd.read_csv('iris.csv')
# Enumerate the classes
unique_classes = sorted(list(set(df['variety'])))
class_number = {y : x for x,y in enumerate(unique_classes)}
df['variety'] = [class_number[x] for x in df['variety']]
# Convert to numpy array and standardize the features
data_X = df[['sepal.length', 'sepal.width', 'petal.length', 'petal.width']].values
data_Y = df[['variety']].values
data_X = data_X - np.min(data_X, axis=0)
# The function to create the initial population
organism_creator = lambda : Organism([1, 16, 16, 16, 1], output='linear')
# The function we are trying to learn. numpy doesn't have tau...
true_function = lambda x : np.sin(2 * np.pi * x) #
# The loss function, mean squared error, will serve as the negative fitness
loss_function = lambda y_true, y_estimate : np.mean((y_true - y_estimate)**2)
def simulate_and_evaluate(organism, replicates=1):
"""
Randomly generate `replicates` samples in [0,1],
@RileyLazarou
RileyLazarou / connga_full.py
Last active November 17, 2019 21:20
Neural Network Evolutionary Algorithm
import copy
import numpy as np
class Organism():
def __init__(self, dimensions, use_bias=True, output='softmax'):
self.layers = []
self.biases = []
self.use_bias = use_bias
self.output = self._activation(output)