Skip to content

Instantly share code, notes, and snippets.

View pedrovasconcellos's full-sized avatar

Pedro Vasconcellos pedrovasconcellos

View GitHub Profile
@pedrovasconcellos
pedrovasconcellos / Utilities.ts
Last active May 20, 2024 21:47
Utilities TypeScript
class Utilities {
public static isValidStringHeaders(headers: string): boolean {
const regex = /^[a-zA-Z0-9,_]+$/;
return regex.test(headers);
}
public static isValidStringHeaders(headers: string): boolean {
const allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_,';
for (let i = 0; i < headers.length; i++) {
@pedrovasconcellos
pedrovasconcellos / ResultT.ts
Created October 23, 2023 11:37
Result T in TypeScript
type StringMap = Map<string, string>;
export class ResultT<T> {
value: T | null;
errors: StringMap;
constructor() {
this.value = null;
this.errors = new Map<string, string>();
}
@pedrovasconcellos
pedrovasconcellos / getCurlFromHttpClient.cs
Created June 2, 2023 01:59
get Curl From HttpClient
public void Print(HttpClient httpClient){
var jsonContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"curl -X {httpClient.HttpRequestMessage.Method} \"{url}\" \\\n" +
$"{GetHeadersAsString(httpClient.HttpRequestMessage.Headers)} \\\n" +
$"-d \"{jsonContent}\"");
}
static string GetHeadersAsString(HttpHeaders headers)
{
@pedrovasconcellos
pedrovasconcellos / ChecksForThePresenceOfDirectory.bash
Last active May 5, 2023 02:01
Remove duplicates of environment variable PATH
#Checks for the presence of directory /usr/local/go/bin in $PATH.
echo "$PATH" | grep -q -E "(^|:)/usr/local/go/bin($|:)"
is_present=$?
if [ $is_present -eq 1 ]; then
export PATH=$PATH:/usr/local/go/bin
echo "add go bin"
fi
@pedrovasconcellos
pedrovasconcellos / ResultT.go
Created April 28, 2023 20:19
Result class in Go Language
type ResultT struct {
Response interface{}
Errors map[string]string
}
func NewResultT() ResultT {
return ResultT{
Response: nil,
Errors: make(map[string]string),
}
@pedrovasconcellos
pedrovasconcellos / ResultT.rs
Last active April 28, 2023 20:15
Result class in Rust Language
use std::collections::HashMap;
pub struct ResultT<T> {
response: Option<T>,
errors: HashMap<String, String>,
}
impl<T> ResultT<T> {
pub fn new() -> Self {
ResultT {
@pedrovasconcellos
pedrovasconcellos / EnumExtensions.cs
Last active June 7, 2023 17:38
InvalidToDefaultEnumConverter
public static class EnumExtensions
{
public static T? GetDefaultValue<T>() where T : Enum
{
var enumType = typeof(T);
var defaultValueAttribute = Attribute
.GetCustomAttribute(enumType, typeof(DefaultValueAttribute)) as DefaultValueAttribute;
if (defaultValueAttribute is not null &&
defaultValueAttribute.Value is T)
@pedrovasconcellos
pedrovasconcellos / spark-example.py
Last active April 15, 2023 05:15
Spark Example
from pyspark import SparkContext
import os
ip = os.popen("ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'").read().strip()
os.environ['SPARK_LOCAL_IP'] = ip
#bash> export SPARK_LOCAL_IP=$(ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}')
sc = SparkContext()
sc.setLogLevel("OFF")
@pedrovasconcellos
pedrovasconcellos / IRR.rs
Created April 14, 2023 06:44
Internal Rate of Return
fn main() {
let initial_investment = -1000.0;
let cash_receipts = vec![250.0, 350.0, 450.0, 550.0];
let cash_flows = vec![initial_investment,
cash_receipts[0], cash_receipts[1], cash_receipts[2], cash_receipts[3]];
let rate = irr(&cash_flows);
println!("IRR: {:.2}%", rate * 100.0);
}
@pedrovasconcellos
pedrovasconcellos / connect-in-vm-linux.sh
Last active April 12, 2023 19:29
Use SSH with VM Linux Ubuntu
# Transfer folder with files to vm
scp -r -i certificate.pem home/userlocal/diretory uservm:ip_vm_host home/uservm/directory
# Connect to vm
ssh -i certificate.pem uservm:ip_vm_host
# INSTALL .NET 7
# 1) Add package
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb