Skip to content

Instantly share code, notes, and snippets.

internetimagery

Block or report user

Report or block internetimagery

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View lockfile.py
from __future__ import print_function
import os
import time
import errno
import socket
class FileLock(object):
CONTENTS = "Locked by\n * host: {}\n * process: {}".format(
@internetimagery
internetimagery / manager.py
Last active May 2, 2020
Context manager for your context managers
View manager.py
import sys
import contextlib
@contextlib.contextmanager
def context_manager_manager(*managers):
for manager in managers:
manager.__enter__()
err = (None, None, None)
try:
@internetimagery
internetimagery / transmute.py
Last active Apr 26, 2020
Transmutation Framework (provide tiny functions to convert a to b, and let the framework chain them together to convert anything to anything)
View transmute.py
# Copyright 2020 Jason Dixon
# 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, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
@internetimagery
internetimagery / execution_path.py
Created Jan 30, 2020
Follow execution path around. Carry scope along with objects.
View execution_path.py
from contextlib import contextmanager
from threading import current_thread
from collections import defaultdict
class Scope(object):
_scope = defaultdict(list)
_tagged = {}
@internetimagery
internetimagery / decoration.py
Last active Jan 30, 2020
Decorators broken down. Useful if needing to access the class or instance of a method from within a decorator, while it still needs to be a classmethod or staticmethod.
View decoration.py
# Decoration broken down. Useful if needing to access the class or instance of a method from within a decorator, while it still needs to be a classmethod or staticmethod.
from functools import partial
class StaticMethod(object):
def __init__(self, function):
self._function = function
def __get__(self, instance, class_):
@internetimagery
internetimagery / lib.rs
Created Jan 13, 2020
Map a named struct to enum entries.
View lib.rs
macro_rules! collection {
(enum $enum_name:ident {$($variant_name:ident {$($field_name:ident: $field_type:ty$(,)*)*},)+}) => {
$(struct $variant_name {
$($field_name: $field_type,)*
})+
enum $enum_name {
$($variant_name($variant_name),)+
}
}
@internetimagery
internetimagery / main.rs
Last active Dec 31, 2019
Enum to and from concrete value
View main.rs
macro_rules! convertable_enum {
($enum_name: ident, $enum_type: ty, {$($name: ident = $value: expr,)+}) => {
enum $enum_name {
$($name = $value,)+
}
impl std::convert::TryFrom<$enum_type> for $enum_name {
type Error = String;
fn try_from(value: $enum_type) -> Result<Self, Self::Error> {
match value {
$($value => Ok($enum_name::$name), )+
@internetimagery
internetimagery / blackfmt.rs
Last active Jun 29, 2019
Simple lightweight blackd client. Black, the uncompromising python formatter.
View blackfmt.rs
// For use with an active blackd service. eg:
// >>> rustc -Oo blackfmt blackfmt.rs
// >>> blackd --bind-port 6789
// >>> blackfmt --port 6789 /path/to/file.py
use std::fs;
use std::io::{Read, Write};
use std::net;
use std::path;
use std::time;
@internetimagery
internetimagery / write.py
Created Jun 26, 2019
Simple file writing.
View write.py
import os
import shutil
data = b"some data"
path = "file.txt"
try:
lockfile = path + ".lock"
fd = os.open(lockfile, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
os.write(fd, data)
@internetimagery
internetimagery / Cargo.toml
Last active Jun 22, 2019
Some examples of rust to python using cpython (for my own reference)
View Cargo.toml
[package]
name = "my_module"
version = "0.1.0"
authors = ["Jason Dixon <--->"]
edition = "2018"
[lib]
name = "my_module"
crate-type = ["cdylib"]
You can’t perform that action at this time.