Skip to content

Instantly share code, notes, and snippets.

View johanastborg's full-sized avatar
🧭
Dreaming in Code

Johan Astborg johanastborg

🧭
Dreaming in Code
  • Sweden
View GitHub Profile
@johanastborg
johanastborg / decorator.py
Created May 12, 2020 14:10
Python decorator
from colorama import Fore, Back, Style
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
print(Fore.GREEN)
func()
print(Style.RESET_ALL)
print("Something is happening after the function is called.")
return wrapper
let print_list f lst =
let rec print_elements = function
| [] -> ()
| h::t -> f h; print_string "\n"; print_elements t
in
print_string "";
print_elements lst;
print_string "";;
import pandas as pd
df = pd.DataFrame({
"timestamp": pd.date_range("now", periods=6),
"value": [1.005, 1.015, 1.020, 1.010, 1.000, 1.015],
"symbol": ['EUR/USD']*6,
"exchange": ['OTC']*6,
"description": ['EURUSD']*6
})
@johanastborg
johanastborg / styles.less
Created May 30, 2020 09:41
Atom editor font size and more
@font-size: 12px;
html, body, .tree-view, .tab-bar .tab {
font-size: @font-size;
}
@johanastborg
johanastborg / upper.js
Created June 2, 2020 12:38
Using pipes and transform streams
var stream = require('stream');
var util = require('util');
const fs = require('fs');
var Transform = stream.Transform;
function Upper(options) {
if (!(this instanceof Upper)) {
return new Upper(options);
}
@johanastborg
johanastborg / copy.js
Created June 2, 2020 12:41
Copy file content using pipes
const fs = require('fs');
const localFilename1 = 'input.txt';
const localFilename2 = 'output.txt';
let rs = fs.createReadStream(localFilename1);
let ws = fs.createWriteStream(localFilename2);
rs.pipe(ws);
let translation = {'a': 'apa', 'b': 'beta'}
_.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
if(translation[key]) result[translation[key]] = value;
return result;
}, {});
// {apa: 1, beta: 2}
@johanastborg
johanastborg / process.js
Last active June 3, 2020 08:26
Translate and filter columns
//-------------------------------------------------------
/* data:csv
NAME,AGE,PHONE,CITY
Daffy Duck,24,08-123987,Stockholm
Bugs Bunny,22,08-987123,Copenhagen
Bunny Duck,31,08-719823,Paris
*/
const stream = require('stream');
const fs = require('fs');
@johanastborg
johanastborg / hash.js
Created June 3, 2020 09:13
Create hash from keys in map
const crypto = require('crypto')
const _ = require('lodash');
let translation = {'name': 'symbol', 'age': 'age', 'phone': 'mobile'};
let keys = _.keys(translation);
let string = keys.join(':');
let hash = crypto.createHash('md5').update('string').digest("hex");
console.log(string + " == > " + hash);
@johanastborg
johanastborg / parse.js
Created June 3, 2020 16:59
Parse string to a format
//
// Parse string to a format
//
const _ = require('lodash');
// Try parse to Date
function tryParseDate(obj) {
let d = Date.parse(obj);
if (_.isNaN(d)) {
return {type: 'string', value: obj};