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 / 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 / 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 / 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 / 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 / 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 / test_redirects.py
Created April 7, 2021 10:47
Test Python import redirects
import contextlib
import runpy
import site
import sys
import pytest
# ----------------------------------------------------------------------
import sys
import importlib.abc
@pfmoore
pfmoore / PEPs.py
Created March 12, 2021 15:13
A KeyPirinha plugin to search Python PEPs
# Keypirinha launcher (keypirinha.com)
import keypirinha as kp
import keypirinha_util as kpu
from collections import namedtuple
from html.parser import HTMLParser
import datetime
import os
from ctypes import windll
@pfmoore
pfmoore / Squares Problem.ipynb
Created February 26, 2021 16:23
Squares problem
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.