Skip to content

Instantly share code, notes, and snippets.

View johannesl's full-sized avatar

Johannes Ridderstedt johannesl

View GitHub Profile
@johannesl
johannesl / 20240625-hacka-jens.md
Created June 25, 2024 11:33
Länkar till Hacka Jens
@johannesl
johannesl / rotating_logger.py
Last active January 9, 2023 08:56
Example of how to log one file per day to /var/log with Python 3
#!/usr/bin/env python3
from logging import getLogger, Formatter
from logging.handlers import TimedRotatingFileHandler
from os.path import basename
def daylogger( name ):
logger = getLogger( name )
logger.setLevel( 'INFO' )
@johannesl
johannesl / canvastest.html
Created August 25, 2022 12:13
An example of how to use the CANVAS tag in HTML
<canvas width="640" height="480" id="testcanvas">
</canvas>
<script>
function clear() {
var ctx = document.getElementById('testcanvas').getContext('2d');
ctx.fillStyle = "rgb(0,0,0)"
ctx.fillRect( 0, 0, 640, 480 );
}
function paint( i ) {
var ctx = document.getElementById('testcanvas').getContext('2d');
@johannesl
johannesl / sandbox.lua
Created September 22, 2020 21:53
My first attempt live coding VR inside VR with indeck and LÖVR! :-)
local m = {}
function m.load()
end
function m.update(dt)
end
function m.draw()
-- draw ground and 12 rotating blocks around world origin
@johannesl
johannesl / simplejwt.php
Created May 29, 2018 20:45
JSON Web Token without third-party dependencies
<?php
function jwt_hs256_encode( $payload, $secret ) {
$headers = array( 'alg' => 'HS256', 'typ' => 'JWT' );
$parts[] = base64url_encode( json_encode( $headers, JSON_FORCE_OBJECT ) );
$parts[] = base64url_encode( json_encode( $payload, JSON_FORCE_OBJECT ) );
$signature = hash_hmac( 'SHA256', $parts[0] .'.'. $parts[1], $secret, true );
$parts[] = base64url_encode( $signature );
@johannesl
johannesl / iteratortest.php
Last active January 17, 2018 19:16
Sample PHP iterator. Took a while to gather all the pieces so might be useful for someone else. I made this test in order to extend my easiermysql.php with a version of mSelectRows() that supports retrieving big row sets from MySQL (more than what's feasible storing in an array).
<?
class mIterator implements Iterator {
public function __construct( $data ) {
$this->data = $data;
}
public function valid () {
if ($this->row) return true;
return false;
}
@johannesl
johannesl / gist:23ca3c82a0be8e557d270b30f4952533
Last active April 13, 2016 07:41
How to get documents from MongoDB within a specific date or time range using default ObjectId indexes
import time
from datetime import datetime
def mongoDatetimeToObjectId(dt):
return '%08x0000000000000000' % time.mktime(dt.timetuple())
# Allows doing queries based on creation time without a specific index
day1 = datetime(2016,1,1)
day2 = datetime(2016,2,1)
conn.collection.query({'_id':{'$gt':mongoDatetimeToObjectId(day1),'$lt':mongoDatetimeToObjectId(day2)}})
@johannesl
johannesl / gist:035c7263b19d55699eab
Created March 9, 2016 10:41
Code editor survery, Junction NOV 2015
We asked all the participants of Junction 2015 (hackjunction.com) what code editor they were using?
15 Android Studio
6 Arduino Studio
1 Atmel Studio
10 Atom
1 Bracket
1 Brackets
1 Dreamweaver
3 Emacs
@johannesl
johannesl / Send SMS with 46elks from Ruby
Created March 25, 2014 13:56
My first ever lines of Ruby code. It's fun to be able to send SMS from a new programming language after only 10 mins.. :-)
require 'net/http'
uri = URI('https://api.46elks.com/a1/SMS')
req = Net::HTTP::Post.new(uri)
req.basic_auth 'u12341234123412341234123412341234', 'ABCDABCDABCDABCDABCDABCDABCDABCD'
req.set_form_data(
:from => 'SenderName',
:to => '+46704508449',
:message => 'Login code 123456',
:flashsms => 'yes'
@johannesl
johannesl / gist:4682698
Created January 31, 2013 13:03
How to force your window to front in Cocoa / OSX.
NSApplication *myApp = [NSApplication sharedApplication];
[myApp activateIgnoringOtherApps:YES];
[self.window orderFrontRegardless];