Skip to content

Instantly share code, notes, and snippets.

@phalt
phalt / async.py
Created October 19, 2023 20:35
Run async functions together and return them
import asyncio
async def gather_functions(funcs):
tasks = [asyncio.create_task(f) for f in funcs]
return await asyncio.gather(*tasks)
def run(funcs):
"""
Run a list of async functions and return the results.
@phalt
phalt / dict_to_xml.py
Created August 28, 2020 01:50
Python dictionary to xml string
from xml.etree.ElementTree import Element, tostring
def dict_to_xml(tag: str, d: dict) -> str:
"""
Converts a Python dictionary to an XML tree, and then returns
it as a string.
Works with recursively nested dictionaries!
"tag" is the name of the top-level XML tag.
"""
elem = Element(tag)
@phalt
phalt / ordered_set.py
Created December 7, 2017 13:55
Python 3.6 ordered set using a Dict
'''
In python 3.6 dictionaries are ordered:
"The order-preserving aspect of this new implementation
is considered an implementation detail and should
not be relied upon."
But let's face it, we're going to use it and it is
going to become a consistent feature.

Functions that print their own source code.

Python

x = ['print("x =", x)', 'for s in x: print(s)']
print("x =", x)
for s in x: print(s)
@phalt
phalt / main.go
Created December 3, 2014 14:51
Simple HTTP API in Go
package main
import (
"github.com/gin-gonic/gin"
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"log"
"time"
"strconv"
@phalt
phalt / example.py
Created November 6, 2018 17:16
Context Manager and a decorator too!
from contextlib import ContextDecorator
class me_decorate(ContextDecorator):
def __init__(self, *args, **kwargs):
self.input_value = kwargs.get('keyword')
super(me_decorate, self).__init__()
def __enter__(self, *args, **kwargs):
print(self.input_value)
@phalt
phalt / abstract.py
Created September 7, 2017 09:00
Abstract Base models
from django.db import models
class DateTimeModel(models.Model):
class Meta:
abstract = True
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
@phalt
phalt / .bash_profile
Created September 4, 2017 15:52
bash_profile
# Virtualenv wrapper stuff
export WORKON_HOME=~/Envs
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
# Make sure virtualenv is active before using pip
export PIP_REQUIRE_VIRTUALENV=true
# Various git shorthands
alias gs="git status"
@phalt
phalt / com.googlecode.iterm2.plist
Last active September 1, 2017 14:30
iterm2 config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AboutToPasteTabsWithCancel</key>
<true/>
<key>AboutToPasteTabsWithCancel_selection</key>
<integer>2</integer>
<key>AppleAntiAliasingThreshold</key>
<integer>1</integer>
@phalt
phalt / merge_sort.py
Last active March 8, 2017 10:50
Binary tree / merge sort example in Python
# A merge sort algorithm that is probably bigger than it needs to be.
# Uses binary trees to sort integers.
# Runtime is probably O(NlogN)
class Node(object):
def __init__(self, value):
self.value = value
self.left = None