Skip to content

Instantly share code, notes, and snippets.

@jingzhehu
jingzhehu / mac_sim.ahk
Created October 29, 2018 19:31
Simulate Mac keyboard shortcut for Windows
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory
; Make Ctrl + S work with cmd (windows) key
#s::
Send,^s
Return
@jingzhehu
jingzhehu / DelayedClosure.swift
Created October 26, 2018 22:49
Delayed execution of closure
func delay(_ delay: Double, closure: @escaping () -> ()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
@jingzhehu
jingzhehu / SplitSwiftArray.swift
Created October 26, 2018 22:48
Split swift array into multiple chunks
extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map { Array(self[$0..<Swift.min($0 + size, count)]) }
}
}
@jingzhehu
jingzhehu / exif_python.py
Last active September 17, 2018 14:49
Extract EXIF data from photo with Python
'''
usage:
path_name = "some_image_file.jpg"
meta_data = ImageMetaData(path_name)
latlng =meta_data.get_lat_lng() // result is converted from DMS to DD
source: https://www.codingforentrepreneurs.com/blog/extract-gps-exif-images-python/
'''
from PIL import Image
@jingzhehu
jingzhehu / scrollview_keyboard.swift
Created August 23, 2018 20:36
ScrollView unhide content with keyboard
@IBOutlet weak var scrollView: UIScrollView!
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}
@IBAction func keyboardWasShown(_ notification: NSNotification) {
guard let userInfo = notification.userInfo,
@jingzhehu
jingzhehu / delete_template_specialization.cpp
Created July 14, 2018 19:58
Mark template specialization as delete
// item 11: mark function as delete
class Widget {
public:
Widget(const Widget&) = delete; // delete copy ctor
Widget& operator=(const Widget& rhs)= delete; // delete copy=
template<typename T>
void process_pointer(T* ptr) {}
@jingzhehu
jingzhehu / enum_class.cpp
Last active July 14, 2018 18:14
Scoped Enum (enum class)
// item 10 - enum class
#include <iostream>
#include <string>
#include <thread>
#include <array>
template<typename E>
constexpr auto toUType(E enumerator) noexcept {
return static_cast<std::underlying_type_t<E>>(enumerator);
};
@jingzhehu
jingzhehu / generate_id.cpp
Created July 14, 2018 16:51
Generate random id
#include <random>
decltype(auto) generate_id(){
std::string id("ID"); // Holds the ID, starting with the characters "ID" followed
std::uniform_int_distribution<int> dist(0, 10000); // by a random integer in the range [0-10000].
std::random_device rd;
std::mt19937 engine(rd());
id += std::to_string(dist(engine));
@jingzhehu
jingzhehu / print_type_macro.h
Last active July 16, 2018 00:21
Print type with boost:type_index #c++
#include <iostream>
#include <boost/type_index.hpp>
#define PRINT_TYPE(param) std::cout << boost::typeindex::type_id_with_cvr<decltype(param)>().pretty_name() << std::endl;
#define PRINT_TYPE_T(T) std::cout << "param type is: " << boost::typeindex::type_id_with_cvr<T>().pretty_name() << std::endl;
@jingzhehu
jingzhehu / bash_cmd_python.py
Created July 1, 2018 23:06
Run bash command in Python
import shlex
import subprocess
def bash_cmd(command):
return subprocess.check_output(shlex.split(command))