Skip to content

Instantly share code, notes, and snippets.

Tomas Fagerbekk tomfa

View GitHub Profile
@tomfa
tomfa / fields.py
Last active Jan 22, 2019
Django compressed json field / compressed binary field
View fields.py
from django.utils.text import compress_string
from django.core.serializers.json import DjangoJSONEncoder
class CompressedBinaryField(models.BinaryField):
compress = compress_string
@staticmethod
def uncompress(s):
zbuf = io.BytesIO(s)
@tomfa
tomfa / fields.py
Created Jan 22, 2019
Django CompressedJSONField
View fields.py
from django.utils.text import compress_string
from django.core.serializers.json import DjangoJSONEncoder
class CompressedBinaryField(models.BinaryField):
compress = compress_string
@staticmethod
def uncompress(s):
zbuf = io.BytesIO(s)
View benchmark.sh
# Simple curl benchmarking
#
# Example: 10 (sequential) calls towards google.com
# sh benchmark.sh 10 http://google.com
#
# Output:
# Running 10 iterations for curl https://google.com
# .....
# Averaged 63.8024 ms in 10 iterations
@tomfa
tomfa / script.pseudo
Last active Sep 9, 2018
Pseudocode for check file thingy
View script.pseudo
function does_file_exist(filename):
...
function get_modified_at(filename):
...
function print_file_is_deleted(filename):
...
function print_file_is_created(filename):
@tomfa
tomfa / diff.py
Last active Sep 5, 2018
Python nested dict diff
View diff.py
"""
Hacky shell tool for finding dict deep diffs
a = {
'b': {
'c': 2,
'd': 3
},
'e': 2
}
@tomfa
tomfa / eval.py
Last active Jul 16, 2018
Evaluation method of classroom seating
View eval.py
def get_pupil_from_direction(classroom, x, y, dx, dy):
x += dx
y += dy
if min(x, y) < 0:
return None
if y >= len(classroom):
return None
if x >= len(classroom[y]):
return None
return classroom[y][x]
View ContainerExample.js
import { connect } from 'react-redux';
const mapStateToProps = ({ refreshThing }) => {
return {
refreshInterval: 60000,
refreshEnabled: true,
};
};
const mapDispatchToProps = dispatch => {
View script.py
def main(argv):
print('Hi')
the_name = input('Your name, please?\n> ')
print('Hi {} <3'.format(name))
@tomfa
tomfa / handler.py
Last active Jun 3, 2018
Python: AWS lambda receiving form file (with serverless.yml)
View handler.py
# This file is your Lambda function
import base64
import json
import boto3
def save_to_bucket(event, context):
AWS_BUCKET_NAME = 'my-bucket-name'
s3 = boto3.resource('s3')
@tomfa
tomfa / handler.py
Last active Jan 21, 2019
AWS Lambda: Python store to S3
View handler.py
# This file is your Lambda function
import json
import boto3
def save_to_bucket(event, context):
AWS_BUCKET_NAME = 'my-bucket-name'
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_BUCKET_NAME)
path = 'my-path-name.txt'
You can’t perform that action at this time.