Skip to content

Instantly share code, notes, and snippets.

@jhihn
jhihn / implementation.cpp
Created June 14, 2021 20:28
Lambdas with Qt's QNetworkAccessManager
class RemoteCloud {
QNetworkAccessManger m_nam;
QString URL(QString url); // left as an exercise to the reader, this just maps a short name to a server/versioned endpoint
QNetworkReplay *get(const QString& url);
QNetworkReply *post(const QString& url, const QVariantMap& object);
public:
QNetworkReply *get(const QString& endpoint, std::function<void(QNetworkReply*)> lambda, bool sync=false);
QNetworkReply *post(const QString& endpoint,
@jhihn
jhihn / serialize_keras_model.py
Last active February 7, 2018 21:17
Load Keras model and weights from JSON
import json
def load_keras_model(filename):
with open(filename) as file:
data = file.read()
json_object = json.loads(data)
model = model_from_json(data)
layer_arrays = []
for layer in json_object["weights"]:
layer_arrays.append(np.array(layer))
@jhihn
jhihn / tokenizer_serialization.py
Last active April 20, 2020 00:21
Keras Tokenizer Gist
# Keras tokenizer lacks serialization. Therefore I created the below to address this without changing the API.
# (Since I don't know how long it'll take for keras to support it)
# The Tokenizer __init__ should be modified to take the word_stats dictionary as a kwarg,
# and a method added to the class to return the stats
# Expiermentally this works, but I am not sure of any nuances in the Tokenizer class.
def test_tokenizer():
texts = ["It was the best of times, it was the worst of times, it was the age of wisdom",
"it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, ",
"it was the season of Light, it was the season of Darkness, it was the spring of hope, ",
@jhihn
jhihn / merge objects
Created November 30, 2015 17:29
merge any number of objects
function mergeObjects(){
var out = {};
var attrname;
for (var i =0; i< arguments.length; i++)
for (attrname in arguments[i]) { out[attrname] = arguments[i][attrname]; }
return out;
}
console.log(mergeObjects({a:1}, {b:2}))
@jhihn
jhihn / gist:dbcf0a1f83908c4ea638
Created November 30, 2015 17:00
validate an object by the presence of it's properties
function validateObjStructure(obj, structure) {
if (structure.length > 0)
while (structure[0] == '.')
structure = structure.substr(1)
console.log('Call:', JSON.stringify(obj), structure)
var dot = structure.indexOf('.')
var bracket = structure.indexOf('[')
var prop;
if (dot === -1 && bracket === -1) { // a