Skip to content

Instantly share code, notes, and snippets.

@Gribouillis
Gribouillis / self-socket.py
Created February 2, 2025 22:40 — forked from bregma/self-socket.py
Using the self-pipe trick in Python3
#!/usr/bin/python3
#
# Investigate the use of the self-pipe trick from Python
#
import logging
import os
import selectors
import signal
import struct
@Gribouillis
Gribouillis / vengy.py
Created January 6, 2025 14:37
Testing a formula puporting to give the k-th prime number
# testing a formula for the k-th prime number
# https://math.stackexchange.com/questions/5019919/an-exact-formula-for-the-k-th-prime-number
from mpmath import ceil, ln, mp, mpf, zeta
mp.dps = 500
mp.pretty = True
one = mpf("1")
two = mpf("2")
@Gribouillis
Gribouillis / matrices.py
Last active December 13, 2024 08:25
An algorithm to partition a bipartite graph
from collections import defaultdict
from itertools import count
import numpy as np
__version__ = "2024.12.13"
def create_graph(a):
"""Returns a graph from a n x n array of bools
@Gribouillis
Gribouillis / check_syntax.py
Last active October 20, 2024 16:19
CLI program to validate the syntax of Python source files by compiling them
#!/usr/bin/env python
# check_syntax.py - script to check the syntax of Python files
# inspired by the standard library's 'py_compile' module.
import importlib.machinery
import sys
import traceback
__all__ = ["has_valid_syntax", "main", "CheckSyntaxError"]
__version__ = "2024.10.20"
@Gribouillis
Gribouillis / pycdump.py
Created November 16, 2021 19:56 — forked from anonymous/pycdump.py
Dump .pyc file (Python 3.5 version)
#
# read a .pyc file and pretty-print it
#
# copied from http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html
# and updated to Python 3.5 (Nov 10th 2015)
@Gribouillis
Gribouillis / lace.py
Created December 28, 2020 12:57
Draw random squares in a rectangle
from collections import namedtuple
from random import randrange
Point = namedtuple('Point', 'x y')
Part = namedtuple('Part', 'inf sup subpart area')
def mincoord(point, other):
return Point(min(point.x, other.x), min(point.y, other.y))
def maxcoord(point, other):
@Gribouillis
Gribouillis / oocollage.py
Created July 3, 2020 20:43
Two functions to glue python objects together
#!/usr/bin/env python
# -*-coding: utf8-*-
# Terms of the MIT license, which apply to this software
# ========================================================
# Copyright (C) 2014 Eric Ringeisen
#
# Permission is hereby granted, free of charge, to
# any person obtaining a copy of this software and
# associated documentation files (the "Software"),
# to deal in the Software without restriction,
@Gribouillis
Gribouillis / anyfloat.py
Last active October 6, 2024 03:35
Play with the IEEE 754 format in python
#!/usr/bin/env python
# -*-coding: utf8-*-
# Title: anyfloat.py
# Author: Gribouillis for the python forum at www.daniweb.com
# Created: 2012-05-02 06:46:42.708131 (isoformat date)
# License: Public Domain
# Use this code freely.
"""Conversion module between floating point representations.
@Gribouillis
Gribouillis / spreadsheet_to_dict.py
Last active December 29, 2021 22:11
Spreadsheet to dictionary
from collections import namedtuple
import pathlib
import pickle
import subprocess as sp
import tempfile
__version__ = '2019.01.16.2'
__all__ = ['spreadsheet_to_dict',]
WorksheetKey = namedtuple('WorksheetKey', "index name")
@Gribouillis
Gribouillis / slicer.py
Created December 16, 2018 21:43
Attempt of a DSL to extract parts from several files
from collections import deque
import io
import re
import sys
__version__ = '0.0.1'
def noop(arg):
pass