Skip to content

Instantly share code, notes, and snippets.

View Moligaloo's full-sized avatar

Moligaloo Moligaloo

  • ByteDance
  • Shanghai, China
View GitHub Profile
@Moligaloo
Moligaloo / libtomcrypt-demo.cpp
Last active March 19, 2024 12:37
Using libtomcrypt to encrypt and decrypt file
#include <tomcrypt.h>
#include <QFile>
#include <QDir>
// interface
class Crypto{
public:
static int getKeySize();
static void setKey(const char *key);
@Moligaloo
Moligaloo / gist:3850710
Created October 8, 2012 04:22
Send mail with attachment and signature via AppleScript in OS X
tell application "Mail"
set theSubject to "Subject" -- the subject
set theContent to "Content" -- the content
set theAddress to "xxx@163.com" -- the receiver
set theSignatureName to "signature_name"-- the signature name
set theAttachmentFile to "Macintosh HD:Users:moligaloo:Downloads:attachment.pdf" -- the attachment path
set msg to make new outgoing message with properties {subject: theSubject, content: theContent, visible:true}
@Moligaloo
Moligaloo / post-json.rb
Created April 10, 2022 19:34
Post HTTP JSON data using Ruby builtin HTTP library
# copied from https://stackoverflow.com/questions/2024805/ruby-send-json-request
require 'net/http' #net/https does not have to be required anymore
require 'json'
require 'uri'
uri = URI('https://your.secure-url.com')
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
request.body = {parameter: 'value'}.to_json
@Moligaloo
Moligaloo / saveImage.m
Created November 11, 2013 06:59
Save NSImage to png file
+ (void)saveImage:(NSImage *)image atPath:(NSString *)path {
CGImageRef cgRef = [image CGImageForProposedRect:NULL
context:nil
hints:nil];
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[newRep setSize:[image size]]; // if you want the same resolution
NSData *pngData = [newRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToFile:path atomically:YES];
[newRep autorelease];
}
@Moligaloo
Moligaloo / viterbi.io
Created December 10, 2021 08:06
Viterbi algorithm written in IoLanguage
obs := list("normal", "cold", "dizzy")
states := list("Healthy", "Fever")
start_p := Map with(
"Healthy", 0.6,
"Fever", 0.4
)
trans_p := Map with(
"Healthy", Map with(
"Healthy", 0.7,
"Fever", 0.3
@Moligaloo
Moligaloo / viterbi.js
Last active December 10, 2021 06:58
Viterbi algorithm example
const obs = ["normal", "cold", "dizzy"]
const states = ["Healthy", "Fever"]
const start_p = {
Healthy: 0.6,
Fever: 0.4,
}
const trans_p = {
Healthy: {
Healthy: 0.7,
@Moligaloo
Moligaloo / viterbi.moon
Created December 9, 2021 10:05
Viterbi algorithm written in MoonScript
obs = {'normal', 'cold', 'dizzy'}
states = {'Healthy', 'Fever'}
start_p = {
Healthy: 0.6,
Fever: 0.4
}
trans_p = {
Healthy: {
Healthy: 0.7,
@Moligaloo
Moligaloo / Main.hx
Created January 25, 2021 12:50
Find square root by dichotomy and newton iteration
class Main {
public static function main() {
trace(dichotomySqrt(2));
trace(newtonSqrt(2));
}
static function dichotomySqrt(x:Float):Float {
if (x < 0) {
return Math.NaN;
}
@Moligaloo
Moligaloo / AlwaysAllowSimulatorAccessNetwork.sh
Created April 27, 2020 09:21
always allow simulator process to access network, avoid annoying network permission request
#!/bin/bash
# Script to disable the iOS Simulator app from showing the "Do you want the application xxxx to accept incoming network connections?" pop-up every time the app is run
echo "> Enter password to temporarily shut firewall off"
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off
echo "> Add Xcode as a firewall exception"
/usr/libexec/ApplicationFirewall/socketfilterfw --add /Applications/Xcode.app/Contents/MacOS/Xcode
@Moligaloo
Moligaloo / plutil.pl
Created January 30, 2013 08:32
Convert Apple's property list file between binary format and XML format
# plutil.pl 1.6 - implementation of binary/UTF-8 (text) XML conversion of plist files
# does not (yet) support OS X plutil command line syntax
# 1.0 - first public release
# 1.1 - added support for date Type 3; fixed real Type 2
# 1.2 - fixed handling of empty arrays
# 1.3 - fixed handling of large strings and empty keys
# 1.4 - write utf8 in XML, convert as necessary on read
# 1.5 - read/write 8 byte integers and negative integers; handle special XML chars in dictionary keys
# - now requires Math::BigInt