Skip to content

Instantly share code, notes, and snippets.

@agagniere
agagniere / Makefile
Last active December 2, 2023 17:50
Reading an infinite stream line per line
bins = single stdio buffered single.exe until.exe stream.exe buffered.exe
all: $(bins)
run: $(bins)
for bin in $^; do echo -n " - $$bin: "; echo "Hello World !\nNooo\nKO" | strace -e read ./$$bin 2>&1 >/dev/null | grep read | wc -l ; done
clean:
$(RM) $(bins)
@agagniere
agagniere / TCP_client.py
Created October 31, 2023 08:59
SImple TCP Client in Python
import sys
import json
import socket
import time
def is_json_complete(message: str) -> bool:
level = 0
for c in message:
level += {'{': 1, '}': -1}.get(c, 0)
if level < 0:
@agagniere
agagniere / demo_ptp.c
Last active January 17, 2023 14:47
Mock ptp
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <linux/ptp_clock.h>
struct external_timestamp
{
@agagniere
agagniere / demo_array2d.c
Last active January 16, 2023 14:41
Demonstrate
#include <stdio.h>
#define C_ARRAY_LENGTH(A) (sizeof(A) / sizeof(*(A)))
void print_memory(const void* addr, size_t size);
const char config[][8] = {
"Hello",
"Salut",
"Hola",
@agagniere
agagniere / demo_bitfields.c
Last active January 11, 2023 14:28
Demonstrate bitfields
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#define EPOLLIN 1
#define EPOLLOUT 4
struct simple
{
bool read;
@agagniere
agagniere / Isola.py
Last active December 30, 2021 18:19
from random import choice
import sys
EOC = "\033[0m"
BOLD = "\033[1m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
@agagniere
agagniere / Point.py
Last active December 9, 2022 14:28
Simple Point Class
class Point:
def __init__(self, x, y): self.x, self.y = x, y
def taxi_distance(self, other): return abs(self.x - other[0]) + abs(self.y - other[1])
def distance(self, other): return math.sqrt((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2)
def module(self): return self.distance((0, 0))
def __add__(self, other): return Point(self.x + other[0], self.y + other[1])
def __radd__(self, other): return self + other
def __sub__(self, other): return Point(self.x - other[0], self.y - other[1])
def __neg__(self): return Point(-self.x, -self.y)
@agagniere
agagniere / AtomicShells.py
Last active May 29, 2020 15:59
Generate the electronic configuration of neutral atoms
# Possible names of this ordering rule :
# * Madelung rule (after Erwin Madelung)
# * Janet rule (after Charles Janet)
# * Klechkowsky rule (after Vsevolod Klechkovsky)
# * aufbau approximation
# * Uncle Wiggly path or
# * diagonal rule
def aufbau():
i = 0
while True: