Skip to content

Instantly share code, notes, and snippets.

@internetimagery
internetimagery / emoji.py
Last active October 27, 2024 08:35
Apply functions happily
from functools import partial
import builtins
class Emoji:
""" Execute functions with style """
__empty = object()
def __init__(self, left=__empty, right=__empty):
self.__left = left
self.__right = right
@internetimagery
internetimagery / range_expression.py
Last active April 24, 2023 11:20
Simple range expression parsing.
import re
def parse_compact_range(expression):
numbers = set()
for match_ in re.finditer(r"(?:(\d+)\s*\-\s*(\d+)|(\d+))\s*(?:,|$)", expression):
start, stop, exact = match_.group(1,2,3)
if exact is not None:
numbers.add(int(exact))
else:
begin, end = sorted(map(int, (start, stop)))
@internetimagery
internetimagery / git_annex_notes.md
Last active April 17, 2023 08:46
Git Annex Notes
@internetimagery
internetimagery / img_sync.py
Last active April 21, 2023 21:43
Extract thumbnail images from gpx files for gps usage.
import re
import os
import html
import shutil
import logging
import tempfile
from functools import partial
from threading import Lock
from urllib.request import urlopen, Request
@internetimagery
internetimagery / gevent_multiprocess.py
Last active September 22, 2022 11:05
Simple multiprocess with gevent
from multiprocessing import Pool as MPool, current_process
from gevent.threadpool import ThreadPool
from gevent.pool import Pool as GPool
from contextlib import closing
class ProcessPool(ThreadPool):
def __init__(self, procs, **kwargs):
super(ProcessPool, self).__init__(procs)
self._m_pool = MPool(procs, **kwargs)
from PyQt5 import QtWidgets, QtCore
import gevent
import random
import weakref
def getData(key):
data = "{}_{}_{}".format(key[0], key[1], random.choice("abcdefg"))
gevent.sleep(random.random() * 5 + 2)
return data
@internetimagery
internetimagery / gevent_poll_loop.py
Last active July 20, 2022 10:29
Super simple gevent qt loop polling
from PyQt5 import QtWidgets, QtCore
import gevent
def test():
print("PRESSED")
for i in range(5):
gevent.sleep(1)
print("Waited", i)
print("Done")
@internetimagery
internetimagery / lazy_import.py
Last active July 20, 2022 09:20
Lazy importing
from __future__ import print_function
import re
import imp
import sys
import time
import types
import inspect
import logging
import threading
@internetimagery
internetimagery / local_import.py
Last active July 8, 2022 08:35
Localize imports in batch. So modules are imported as they are needed. Useful when trying to improve import times.
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without
# fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Example:
@internetimagery
internetimagery / wrap_iter.py
Last active July 5, 2022 10:33
Wrap Iterators so they can be sent to multiprocessing managers. For example SyncManager.Pool
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without
# fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Pickle iterator for multiprocessing.Manager