Skip to content

Instantly share code, notes, and snippets.

View alfasin's full-sized avatar
🖖
He bag production, he got walrus gumboot

Nir Alfasi alfasin

🖖
He bag production, he got walrus gumboot
View GitHub Profile
@alfasin
alfasin / maxSubArray.ts
Created March 6, 2024 23:33
Kadane's Algorithm
function maxSubArray(nums: number[]): number {
let max = nums[0];
let curSum = nums[0];
for (let i = 1; i < nums.length; i++) {
if (curSum < 0) curSum = nums[i];
else curSum += nums[i];
if (max < curSum) max = curSum;
}
return max;
};
@alfasin
alfasin / fastapi_server.py
Last active March 19, 2022 22:53
A small example of a fastAPI server with JWT token
import jwt
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing import Optional
JWT_SECRET = "secret" # IRL we should NEVER hardcode the secret: it should be an evironment variable!!!
JWT_ALGORITHM = "HS256"
app = FastAPI()
@alfasin
alfasin / colorful_winston_logger.js
Last active December 19, 2023 01:27
A Nodejs implementation of a console transport logger based on Winston 3.x which also prints the filename and line-number
const winston = require('winston');
const { format } = winston;
const { combine, colorize, timestamp, printf } = format;
/**
* /**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @param numberOfLinesToFetch - optional, when we want more than one line back from the stacktrace
* @returns {string|null} filename and line number separated by a colon, if numberOfLinesToFetch > 1 we'll return a string
@alfasin
alfasin / InstallingPython3OnOSX.sh
Last active February 19, 2020 14:24
Installing Python3 On OSX
# shamelessly copied from Jesse Myers (https://github.com/jessemyers)
# Install pyenv from homebrew:
brew install pyenv
pyenv install 3.7.4
pyenv global 3.7.4
# Install virtualenvwrapper globally:
$(pyenv which pip) install virtualenv virtualenvwrapper
@alfasin
alfasin / getFileNameAndLineNumber.js
Last active January 12, 2020 16:47
Use prepareStackTrace to get the filename & line number
/**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @returns {string} filename and line number separated by a colon
*/
const getFileNameAndLineNumber = () => {
const oldStackTrace = Error.prepareStackTrace;
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
@alfasin
alfasin / better-nodejs-require-paths.md
Last active January 7, 2020 21:30 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

@alfasin
alfasin / stringify.js
Created January 3, 2020 15:31 — forked from zmmbreeze/stringify.js
json stringify can deal with circular reference
// http://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json/11616993#11616993
var o = {};
o.o = o;
var cache = [];
JSON.stringify(o, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
@alfasin
alfasin / xor.py
Created October 24, 2018 02:56 — forked from revolunet/xor.py
Simple python XOR encrypt/decrypt
#
# NB : this is not secure
# from http://code.activestate.com/recipes/266586-simple-xor-keyword-encryption/
# added base64 encoding for simple querystring :)
#
def xor_crypt_string(data, key='awesomepassword', encode=False, decode=False):
from itertools import izip, cycle
import base64
if decode:
@alfasin
alfasin / uniq.js
Created June 1, 2018 22:38 — forked from telekosmos/uniq.js
Remove duplicates from js array (ES5/ES6)
var uniqueArray = function(arrArg) {
return arrArg.filter(function(elem, pos,arr) {
return arr.indexOf(elem) == pos;
});
};
var uniqEs6 = (arrArg) => {
return arrArg.filter((elem, pos, arr) => {
return arr.indexOf(elem) == pos;
});