Skip to content

Instantly share code, notes, and snippets.

View calexandrepcjr's full-sized avatar
🎯
Focusing

Carlos Alexandre calexandrepcjr

🎯
Focusing
View GitHub Profile
import pymongo
def mongo_make_client(_mongo_uri):
if not _mongo_uri:
raise ValueError("MongoURI must have a value")
return pymongo.MongoClient(_mongo_uri)
def mongo_make_database(_db_name, _mongo_uri=None, _client=None):
@calexandrepcjr
calexandrepcjr / add_columns_to_google_sheets.py
Created October 13, 2020 17:29
Add/Replace columns of your Google Sheet using Google API
"""
Turn on the credentials in your account:
https://developers.google.com/sheets/api/quickstart/python
Put the redirect uri as http://localhost:9999/
"""
from __future__ import print_function
import argparse
@calexandrepcjr
calexandrepcjr / tuple-map.ts
Created August 18, 2020 00:49
Simple Tuple map experiment in TS
type Tuple = [unknown, unknown];
export class TupleMap<T> implements Map<Tuple, T> {
public [Symbol.iterator](): IterableIterator<[Tuple, T]> {
throw new Error("Method not implemented.");
}
public keys(): IterableIterator<Tuple> {
throw new Error("Method not implemented.");
}
@calexandrepcjr
calexandrepcjr / ts-map.ts
Created July 2, 2020 15:17
A Map with a getByValue routine
export class TSMap<T, K> extends Map<T, K> {
public getByValue(value: K): T | undefined {
return [...this.entries()]
.filter((tuple) => tuple[1] === value)
.map(([key]) => key)
.pop();
}
}
@calexandrepcjr
calexandrepcjr / date-freeze.ts
Created June 2, 2020 19:11
Helps testing some routines regarding time, freezing dates
export class DateFreeze {
private readonly originalNow: () => number = Date.now.bind(Date);
public constructor(private timestamp: number) {}
public freeze(timestamp?: number): this {
if (timestamp !== undefined) {
this.timestamp = timestamp;
}
@calexandrepcjr
calexandrepcjr / validator.ts
Created May 27, 2020 14:58
An experiment with validations
export class Validator<A, B> {
private validationBlock?: () => Promise<void>;
public constructor(
private readonly value: A,
private result: B,
private isValid: boolean = true,
) {}
public map<C>(mapper: (value: A) => C): Validator<C, B> {
@calexandrepcjr
calexandrepcjr / unconditional-code-experiment.ts
Last active May 22, 2020 17:56
Inspired by Michael Feathers
/**
* UNCONDITIONAL CODE EXAMPLE - INSPIRED BY Michael Feathers
*
* https://www.youtube.com/watch?v=AnZ0uTOerUI
*/
// OLD CODE
enum MessageType {
EXTERNAL = "external",
@calexandrepcjr
calexandrepcjr / string-enum-value.ts
Created February 24, 2020 10:31
A way to load string enum values from request to your domains without doing nasty unsafe casts or turning off type assertion
/*
A way to load string enum values from request to your domain
without doing nasty unsafe casts or turning off type assertion
Usage:
public deserialize(input: string): Entity {
const serializedEntity = JSON.parse(input) as SerializedEntity;
return new |Entity(
new StringEnumValue(serializedEntity.type, EntityType).toString(),
serializedEntity.entityId,
@calexandrepcjr
calexandrepcjr / wait-for-pg-database.sh
Created November 12, 2019 22:56
Script to help pipelines detect postgres database containers ready
#!/bin/bash
triesLimit=20 # Tries for 10s
isDatabaseReady() {
local lastContainerName=$(docker ps -lq)
local databaseReadyLogMessage="database system is ready to accept connections"
echo $(docker logs $lastContainerName --tail 1 | grep -c "$databaseReadyLogMessage")
}
waitForDatabase() {
local tries=0
@calexandrepcjr
calexandrepcjr / abbreviations.js
Created October 24, 2019 17:58
All wikipedia current abbreviations as javascript object
const abbreviations = {
"A": "ampère(s)",
"a": "are(s)",
"A": "argônio",
"a": "assinado(a)",
"Å": "decimilimícron(s), ångström",
"A.": "aceite",
"A.": "alto (música)",
"a.": "arroba(s)",
"a.": "assinado(a)",