Skip to content

Instantly share code, notes, and snippets.

View kasimte's full-sized avatar

Kasim kasimte

View GitHub Profile
@kasimte
kasimte / gist:3ecbc5a374315f4ac4db2e13d0b111e2
Created January 29, 2017 19:38
Make an Objective-C model NSString property into an RxSwift Observable.
// This is an example of taking an Objective-C model and making a string property into an RxSwift style Observable.
// Note that it is "String", even though it may be "NSString" in the original model class.
objectiveCModelInstance.rx.observe(String.self, "stringPropertyName", options: .new)
.subscribe(onNext: { value in
NSLog(value!)
})
.addDisposableTo(disposeBag)
@kasimte
kasimte / gist:e98b6f2c3510333c1cdad76524591783
Last active February 1, 2017 04:34
Instantiate an initial view controller from a UIStoryboard in Objective-C.
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
NSAssert(storyboard != nil, @"Missing expected storyboard.");
UIViewController *controller = [storyboard instantiateInitialViewController];
@kasimte
kasimte / TypeCastingChaining.swift
Last active April 5, 2017 18:33
Example of Swift Type Casting Chaining
//: Playground - noun: a place where people can play
var dict = [ "user" : ["attribute" : "attribute_value"] ]
if let d = dict as? [String : AnyObject],
let user_d = d["user"] as? [String : String] {
print(user_d)
} else {
print("Didn't work.")
}
@kasimte
kasimte / ConflictingProtocolExtensions.swift
Created April 6, 2017 19:08
This is an example of conflicting protocol implementations in Swift
protocol X {
func myfunc() -> Int
}
protocol Y {
}
protocol Z {
}
@kasimte
kasimte / AppDelegateLocalNotificationExample.m
Created April 8, 2017 20:46
An example of receiving UILocalNotifications in the AppDelegate and presenting an UIAlert.
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
UIViewController *top = [[application keyWindow] rootViewController];
while (top.presentedViewController != nil) {
top = top.presentedViewController;
}
UIAlertController *alert =
@kasimte
kasimte / UniqueIdentifier.swift
Created April 22, 2018 02:38
Creating a unique string identifier in Swift based on the current process.
static func uniqueIdentifier() -> String {
let quasiUniqueString = ProcessInfo.processInfo.globallyUniqueString
let index = quasiUniqueString.index(quasiUniqueString.startIndex, offsetBy: 10)
return quasiUniqueString.substring(to: index)
}
@kasimte
kasimte / ConditionalStringOptionalUnwrapping.swift
Last active October 24, 2018 11:36
An example of conditional String optional unwrapping in Swift 3.
var optional: String? = "Hello World"
if let o = optional { // Same as: optional as String!
print(o)
}
// Output: Hello World.
if let o = optional as String! {
print(o)
}
// Output: Hello World.
@kasimte
kasimte / matlib_plot.py
Created April 29, 2019 01:02
Simple plot in Python using MatLib.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(NUM_EPOCH), train_loss1, label="MLP")
ax.plot(range(NUM_EPOCH), train_loss2, label="CNN")
ax.legend()
ax.set(xlabel='Epochs', ylabel='Loss',
title="Training Loss (MLP vs CNN)")
ax.grid()
@kasimte
kasimte / anaconda-shell-run-python.sh
Last active June 10, 2019 06:35
How do I run a command line script using Anaconda and Python?
source activate environment_name
python script.py
# Example:
#
# Kasim:pytorch-cifar Kasim$ python --version
# Python 2.7.14
# Kasim:pytorch-cifar Kasim$ source activate base
# (base) Kasim:pytorch-cifar Kasim$ python --version
# P ython 3.6.5 :: Anaconda, Inc.
@kasimte
kasimte / git-grep-replace.sh
Created June 26, 2019 06:35
How do I replace a string in a git repository on Mac?
# note, this is for mac
# more: https://blog.jasonmeridth.com/posts/use-git-grep-to-replace-strings-in-files-in-your-git-repository/
git grep -l 'original_text' | xargs sed -i '' -e 's/original_text/new_text/g'