Skip to content

Instantly share code, notes, and snippets.

@pfmoore
pfmoore / create.py
Created September 2, 2023 16:05
PyPI downloader for py-code.org
import json
import subprocess
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from pathlib import Path
from urllib.request import urlopen
REPOSITORIES = "https://github.com/pypi-data/data/raw/main/stats/repositories.json"
with urlopen(REPOSITORIES) as f:
@pfmoore
pfmoore / sqlmagic.py
Created October 11, 2012 14:42
Simple ipython SQL magic
# This code can be put in any Python module, it does not require IPython
# itself to be running already. It only creates the magics subclass but
# doesn't instantiate it yet.
from IPython.core.magic import (Magics, magics_class, line_magic,
cell_magic, line_cell_magic)
import sqlite3
# The class MUST call this class decorator at creation time
@magics_class
class MyMagics(Magics):
@pfmoore
pfmoore / getmeta.py
Created July 5, 2023 14:32
Download metadata files from PyPI
import hashlib
import json
from multiprocessing.dummy import Pool
from pathlib import Path
import urllib3
DOWNLOAD_CACHE = Path("DownloadedMetadata")
@pfmoore
pfmoore / pypiget.py
Created July 5, 2023 11:02
Get the PyPI simple index in JSON format
from datetime import datetime
import email.message
import re
from multiprocessing.dummy import Pool
import json
import traceback
import urllib3
@pfmoore
pfmoore / LazyFile.py
Created August 30, 2021 15:37
Implementation of an on-demand readable file base class in Python
import io
class LazyFile(io.RawIOBase):
def __init__(self):
self.pos = 0
# Cache this as it may be costly to compute
self.end = self.get_size()
def get_size(self):
# Subclasses implement this
raise NotImplemented
@pfmoore
pfmoore / persist.py
Created September 21, 2021 15:40
SQLite-backed persistent dictionary
import collections.abc
import sqlite3
class PersistentDict(collections.abc.MutableMapping):
def __init__(self, db=":memory:", table="dict"):
# Set to autocommit mode (isolation_level=None)
self.conn = sqlite3.connect(db, isolation_level=None)
if not table.isidentifier():
raise ValueError(f"Table name {table!r} is not a valid identifier")
self.table = table
@pfmoore
pfmoore / dummyloader.py
Last active September 21, 2022 20:59
A dummy finder/loader that returns *any* module name under a given prefix.
"""A dummy loader implementation.
This is a deliberate attempt to implement an absolutely minimal finder/loader
combination.
The spec is:
1. If I add an entry dummy:XXX to sys.path, then that entry will be picked
up by my finder and used to satisfy import requests for any modules
starting with 'XXX'.
@pfmoore
pfmoore / extras.txt
Created March 10, 2022 11:13
All extras used in wheels from PyPI
twine
guideways
pygrib
openpyxl
amber
srl
pytrip
revisioning
geoSpatial
xresources
@pfmoore
pfmoore / words_in_file.rs
Created June 29, 2022 12:37
Count words in a file, in Rust
use clap::Parser;
use std::fs::File;
use std::io;
use std::path::Path;
use std::io::BufRead;
use std::collections::HashMap;
use std::cmp::Reverse;
use crossbeam::thread;
@pfmoore
pfmoore / factorio-recipe-parser.lua
Last active September 10, 2021 15:42
Parse the Factorio recipe files to create a CSV of recipes
data = {}
data["extend"] = function (data, t)
for n, recipe in ipairs(t) do
for i, component in ipairs(recipe["ingredients"]) do
cname = component[1] or component["name"]
camt = component[2] or component["amount"]
print('"' .. recipe["name"] .. '","' .. cname .. '",' .. camt)
end
end
end