Skip to content

Instantly share code, notes, and snippets.

View monperrus's full-sized avatar

Martin Monperrus monperrus

View GitHub Profile
@monperrus
monperrus / document_file.py
Created March 16, 2025 10:30
write a python program that parses another python program, summarizes the full file, replace the module docstring and rewrite the program
import ast
import astor
class DocstringReplacer(ast.NodeTransformer):
"""AST Node Transformer to replace module docstring."""
def visit_Module(self, node):
"""Visit Module node and replace docstring."""
# Replace docstring
@monperrus
monperrus / pace.py
Created March 16, 2025 10:30
a python program to compute pace min/kilo from a tcx file
import xml.etree.ElementTree as ET
from datetime import datetime
import math
import sys
def parse_tcx_file(tcx_file):
"""Parse TCX file and return trackpoints with time and distance"""
# Create namespace dictionary for TCX format
ns = {'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'}
@monperrus
monperrus / merge-laps.py
Created March 16, 2025 10:30
a python script to merge all activities in a tcx file
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
import sys
def merge_tcx_activities(input_file, output_file):
# Parse the input TCX file
tree = ET.parse(input_file)
root = tree.getroot()
nspace = root.tag.split('}')[0].strip('{')
@monperrus
monperrus / in_canada.py
Created March 16, 2025 10:30
go over all tx files, if the tcx coordinates are in canada, print the file name
import os
import xml.etree.ElementTree as ET
import glob
def is_in_canada(lat, lon):
# Rough boundaries for Canada
# Latitude: Between 41.7° and 83.1° N
# Longitude: Between -141.0° and -52.6° W
return (41.7 <= float(lat) <= 83.1) and (-141.0 <= float(lon) <= -52.6)
@monperrus
monperrus / repos.py
Created March 16, 2025 10:30
iterate over all repos of a github organization in python
from github import Github
# First create a Github instance using an access token
g = Github("98b9ad24ffc7df18d14c9943eef3700e1b3958c5")
# Then get the organization by its name
org = g.get_organization("chains-project")
# Then iterate over all repos in the organization
for repo in org.get_repos():
@monperrus
monperrus / t-sne.py
Created March 16, 2025 10:30
t-sne visualization in python, with tooltips on points
import numpy as np
from sklearn.manifold import TSNE
import plotly.express as px
import pandas as pd
import glob
import json
# Sample data - replace with your own dataset
# data = np.random.rand(100, 20) # 100 samples, 20 features
@monperrus
monperrus / example-hash.py
Created March 16, 2025 10:30
python compute a hash of file and sign it with cryptography.hazmat
import os
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives.serialization import (
Encoding, PrivateFormat, PublicFormat, NoEncryption
)
def generate_key_pair():
"""Generate an RSA key pair."""
private_key = rsa.generate_private_key(
@monperrus
monperrus / example-fastapi.py
Created March 16, 2025 10:30
a running example of a fastapi server
# fully functional example of fastapi
# # Create an item
# curl -X POST "http://localhost:8000/items/" -H "Content-Type: application/json" -d '{"id": 1, "name": "Test Item", "price": 9.99}'
# # Get all items
# curl "http://localhost:8000/items/"
# # Get specific item
# curl "http://localhost:8000/items/1"
@monperrus
monperrus / example-dbus.py
Created March 16, 2025 10:30
a simple python application which expose an example service hello on dbus
#!/usr/bin/env python3
"""
dbus-send --session --dest=com.example.HelloService \
--type=method_call --print-reply \
/com/example/HelloService org.freedesktop.DBus.Introspectable.Introspect
dbus-send --session --dest=com.example.HelloService \
--type=method_call --print-reply \
@monperrus
monperrus / example-flask.py
Created March 16, 2025 10:30
a simple python application which expose an example service hello
from flask import Flask, request, jsonify
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def root():
return jsonify({
'message': 'Welcome to the Hello Service',
'endpoints': {