Skip to content

Instantly share code, notes, and snippets.

View Xion's full-sized avatar

Karol Kuczmarski Xion

View GitHub Profile
@Xion
Xion / fluent.py
Created June 30, 2013 12:18
Fluent interface decorators
#!/usr/bin/env python
"""
Decorator for fluent interface classes.
"""
import functools
import inspect
def chained(method):
"""Method decorator to allow chaining."""
@Xion
Xion / lambdacode.py
Last active November 23, 2023 03:59
Retrieve source code of a (short) lambda -- http://xion.io/post/code/python-get-lambda-code.html
import ast
import inspect
import os
def get_short_lambda_source(lambda_func):
"""Return the source of a (short) lambda function.
If it's impossible to obtain, returns None.
"""
try:
@Xion
Xion / requestcontexttask.py
Created November 4, 2015 06:39
Base class for Celery tasks running inside Flask request context
from celery import Task
from flask import has_request_context, make_response, request
from myapp import app
__all__ = ['RequestContextTask']
class RequestContextTask(Task):
EffectAsset {
name: "particle_swirl",
capacity: 8192,
spawner: Spawner {
num_particles: Single(
64.0,
),
spawn_time: Single(
1.0,
),
@Xion
Xion / git-today
Last active February 14, 2023 21:39
Git command displaying work summary for today
#!/bin/sh
# git-today
#
# Usage:
# * Name it `git-today` and put somewhere inside a `$PATH` directory
# * Invoke as $ git today
LAST_BEFORE_TODAY=$(git log --oneline --until='yesterday 23:59:59' | head -1 | cut -d' ' -f 1)
git --no-pager diff ${LAST_BEFORE_TODAY}..HEAD --stat
@Xion
Xion / count_bevy_plugins.rs
Last active October 18, 2022 18:00
Count installed Bevy plugins
// License: BSD-3
use std::io::{self, Write};
use std::string::FromUtf8Error;
use std::sync::Arc;
use antidote::RwLock;
use bevy::prelude::*;
use bevy::reflect::FromReflect;
use bevy::utils::tracing::{self, Dispatch};
@Xion
Xion / auth.py
Last active October 2, 2022 02:16
Query string authenticators for Requests
"""
Requests' authenticator that attaches given parameters
to the URL query string.
License: Public Domain.
"""
from requests.auth import AuthBase
class QueryStringAuth(AuthBase):
@Xion
Xion / keyword_stats.py
Created May 28, 2012 16:39
Analysis of keywords in few popular languages
"""
Analyzing keywords from different programming languages.
"""
def word_stats(words):
count = len(words)
len_sum = sum(map(len, words))
return {
'count': count,
'total_chars': len_sum,
@Xion
Xion / compact.py
Created November 13, 2011 18:12
PHP compact() function in Python
def compact(*args):
''' compact() function, analoguous to the PHP version.
Converts a series of (possible nested) iterables containing variable names
into a dictionary mapping those names to variable values.
Example:
>>> x, y, z, a, b, c = 1, 2, 3, 4, 5, 6
>>> compact('x', ['y','z'], ['a', ['b'], [['c']]])
{'a': 4, 'b': 5, 'c': 6, 'x': 1, 'y': 2, 'z': 3}
'''
def _compact_arg(res, arg):
@Xion
Xion / missing_dict.py
Created April 5, 2012 21:49
dict with "missing" values
import collections
missing = object()
class MissingDict(dict):
"""Dictionary that supports a special 'missing' value.
Assigning a missing value to key will remove the key from dictionary.
Initializing a key with missing value will result in key not being
added to dictionary at all.