Skip to content

Instantly share code, notes, and snippets.

View pitermarx's full-sized avatar

ριтєя мαяχ pitermarx

View GitHub Profile
@pitermarx
pitermarx / DataPipeline.cs
Created November 3, 2023 15:28
Idea for an abstraction over TPL DataFlow
public class DataPipeline<T>
{
private static ExecutionDataflowBlockOptions Options => new() { BoundedCapacity = 1 };
private readonly IReceivableSourceBlock<T> sourceBlock;
public DataPipeline(IReceivableSourceBlock<T> sourceBlock) => this.sourceBlock = sourceBlock;
public Task Completion => sourceBlock.Completion;
public IAsyncEnumerable<T> ReceiveAllAsync(CancellationToken ct = default) => sourceBlock.ReceiveAllAsync(ct);
public DataPipeline<TOut> Activity<TOut>(Func<T, Task<TOut>> action) => Link(new TransformBlock<T, TOut>(action, Options));
@pitermarx
pitermarx / Channels.cs
Created November 3, 2023 09:51
An example on how to use channels
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
class Processor<T> where T : class
{
private readonly Channel<T> _channel = Channel.CreateUnbounded<T>();
private readonly Task processors;
public Processor(Func<T, Task> action, int numberOfConsumers = 4)
@pitermarx
pitermarx / otp.ps1
Created June 29, 2023 10:42
Powershell OTP function
function otp($SECRET="ABCDEFGHIJKLMNOP", $LENGTH=6, $WINDOW=30){
$span = (New-TimeSpan -Start (Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0) -End (Get-Date).ToUniversalTime()).TotalSeconds
$unixTime = [Convert]::ToInt64([Math]::Floor($span/$WINDOW))
$timeBytes = [BitConverter]::GetBytes($unixTime)
[array]::Reverse($timeBytes)
$enc = [System.Text.Encoding]::UTF8
$hmac = New-Object -TypeName System.Security.Cryptography.HMACSHA1
$hmac.key = Convert-Base32ToBytes($SECRET.ToUpper())
$randHash = $hmac.ComputeHash($timeBytes)
@pitermarx
pitermarx / DesignTimeFactory.cs
Created May 4, 2023 13:22
Script to generate EF migration scripts. One per migration
public class DesignTimeFactory : IDesignTimeDbContextFactory<MyDbContext>
{
public MarketDataServiceDbContext CreateDbContext(string[] args)
=> new MyDbContext(new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(args[0]).Options);
}
@pitermarx
pitermarx / Readme.md
Last active March 23, 2023 12:34
Enred Scraper with Playwright
$certPath = ".\seq\Certificates\443.pem"
$needNewCert = if (Test-Path $certPath) {
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath)
#should return true if cert is expired
$cert.NotAfter -lt (Get-Date)
} else {
$true
}
# download certificates if needed
if ($needNewCert) {
@pitermarx
pitermarx / SerializableTestCase.cs
Created July 29, 2021 15:42
A base class that implements IXunitSerializable
using System;
using System.IO;
using System.Xml.Serialization;
using Xunit.Abstractions;
public abstract class SerializableTestCase : IXunitSerializable
{
/// <inheritdoc/>
public void Deserialize(IXunitSerializationInfo info)
{
@pitermarx
pitermarx / NaiveModelBinder.cake
Created June 4, 2021 10:15
Cake Model Binding
using System.ComponentModel;
// Uses cake Configuration to bind a model
// Configuration values should come from
// 1 - CAKE_ prefixed env vars
// 2 - cake.config files
// 3 - cmdline arguments
class NaiveModelBinder
{
private readonly Func<string, string> getValue;
set clipboard=unnamed "Use System clipboard
set autoindent "New lines inherit the indentation of previous lines
set expandtab "Convert tabs to spaces.
set smarttab "Insert 'tabstop' number of spaces when the 'tab' key is pressed.
set tabstop=4 "Indent using four spaces.
set shiftwidth=4
set hlsearch "Enable search highlighting.
set incsearch "Incremental search that shows partial matches.
set smartcase "Automatically switch search to case-sensitive when search query contains an uppercase letter.
@pitermarx
pitermarx / covid.linq
Created August 4, 2020 09:02
LinqPad script for seeing Covid Data
var data = Parse(@"https://raw.githubusercontent.com/dssg-pt/covid19pt-data/master/data.csv",
v => new
{
data = DateTime.Parse(v["data"]),
novos_total = decimal.Parse(v["confirmados"]),
novos = decimal.Parse(v["confirmados_novos"])
});
var amostras = Parse(@"https://raw.githubusercontent.com/dssg-pt/covid19pt-data/master/amostras.csv",
v => new
{