Skip to content

Instantly share code, notes, and snippets.

@ivanroosvelt
ivanroosvelt / getNestedValues.ts
Last active August 18, 2023 14:06
Get nested properties from object based on array of dotted hierarchical name of properties
function extractPropertiesValues(obj: any, props: string[]): any {
function getNestedPropertyValue(obj: any, path: string): any | undefined {
const pathArray = path.split('.');
if (pathArray.length === 1) return obj[pathArray[0]];
const prop = pathArray.splice(0, 1)[0];
if (!!!obj[prop]) {
console.log(
'[xyz] PropNotFound:',
{ prop, obj, pathArray }
);
@ivanroosvelt
ivanroosvelt / jsonToCsv.js
Created May 12, 2023 20:47
jsonToCsv NodeJs
const fs = require('fs');
const jsonToCsv = (data, filename) => {
const allProperties = new Set();
for (const record of data) {
extractProperties(record, '', allProperties);
}
const properties = Array.from(allProperties).sort();
// Generar los datos para el archivo
@ivanroosvelt
ivanroosvelt / main.dart
Created May 2, 2023 11:05
Dart websocket server
import 'dart:io';
void main() {
int port = 8080;
HttpServer.bind(InternetAddress.anyIPv4, port).then((HttpServer server) {
print("HttpServer listening... localhost:$port");
server.serverHeader = "Sample websocket server";
server.listen((HttpRequest request) {
if (WebSocketTransformer.isUpgradeRequest(request)) {
WebSocketTransformer.upgrade(request).then(handleWebSocket);
@ivanroosvelt
ivanroosvelt / tensorPolynomialRegression.js
Created April 3, 2023 12:47
Regresion polinomica Tensorflow
let xVals = [];
let yVals = [];
let a, b, c, d, e;
const learningRate = 0.2;
let optimizer;
function setup() {
createCanvas(400, 400);
@ivanroosvelt
ivanroosvelt / install-redis.sh
Created March 22, 2023 14:24 — forked from jpickwell/install-redis.sh
Installing Redis 5.0.0 on Amazon Linux
#!/bin/bash
###############################################
# To use:
# chmod +x install-redis.sh
# ./install-redis.sh
###############################################
version=5.0.0
@ivanroosvelt
ivanroosvelt / fixedQueue.py
Last active February 24, 2023 10:37
Abre y mantiene una cola de tamaño definido.
class FixedQueue:
"""
Abre y mantiene una cola de tamaño definido.
:param width: Tamaño de cola
:param init: Valores iniciales
"""
def __init__(self, width:int, init=[]):
self.q = deque(init[-width:])
self.w = width
def update(self, data):
@ivanroosvelt
ivanroosvelt / tcp_redirect.py
Created January 7, 2023 15:56
Python3 tcp connection redirector
#!/usr/bin/python3
import socket
import threading
import select
import sys
terminateAll = False
class ClientThread(threading.Thread):
@ivanroosvelt
ivanroosvelt / ema.py
Created January 7, 2023 15:55
Calculate EMA
import array
def ema(s, n):
"""
returns an n period exponential moving average for
the time series s
s is a list ordered from oldest (index 0) to most
recent (index -1)
n is an integer
returns a numeric array of the exponential
@ivanroosvelt
ivanroosvelt / set_interval.py
Created January 7, 2023 15:54
Python non blocking set Interval
import threading
class ThreadJob(threading.Thread):
def __init__(self,callback,event,interval):
'''runs the callback function after interval seconds
:param callback: callback function to invoke
:param event: external event for controlling the update operation
:param interval: time in seconds after which are required to fire the callback
:type callback: function
:type interval: int
@ivanroosvelt
ivanroosvelt / slope-angle.js
Created January 7, 2023 15:54
Slope angle in JavaScript
function getSlopeAngle(s1,s2) {
return Math.atan((s2[1] - s1[1]) / (s2[0] - s1[0])) * 180/Math.PI;
}
console.log(getSlopeAngle([0,0],[2,3]); // 56.309932474020215