Skip to content

Instantly share code, notes, and snippets.

@johnludwigm
johnludwigm / AppDelegate.swift
Last active December 18, 2017 23:32
Regular AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
@johnludwigm
johnludwigm / NotificationsAppDelegate.swift
Last active December 19, 2017 17:31
Allows iOS app to present local notifications while app is in foreground.
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let notificationDelegate = CustomNotifications()
@johnludwigm
johnludwigm / CustomNotifications.swift
Created December 19, 2017 17:11
Custom notifications delegate class for iOS development.
import Foundation
import UserNotifications
class CustomNotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//Play sound, show alert (will occur if enabled by user AND scheduled in the notification request).
@johnludwigm
johnludwigm / IMDbtsv.py
Last active January 3, 2018 20:14
Explains and solves IMDb tsv file problem.
import csv
import sys
def getmaxsize():
"""Returns max field size."""
return csv.field_size_limit(sys.maxsize)
def setmaxsize(size = 500 * 1024 * 1024):
"""Sets max field size as high as possible."""
@johnludwigm
johnludwigm / noneandnull.py
Last active August 29, 2023 10:02
NULL vs None in Sqlite3, Python
dbpath = "/.../test.db"
>>> import sqlite3
>>> #Create the database.
>>> connection = sqlite3.connect(dbpath)
>>> cursor = connection.cursor()
>>> cursor.execute("CREATE TABLE Test (testcolumn TEXT);")
<sqlite3.Cursor object at 0x111952ce0>
>>> cursor.execute("INSERT INTO Test VALUES(NULL);")
<sqlite3.Cursor object at 0x111952ce0>
@johnludwigm
johnludwigm / builddb.py
Created January 16, 2018 15:54
Creates IMDb Database Using sqlite3 in Python.
import sqlite3
import StreamTSV
import sqliteops
import create_statements as cts
import os
table_urls = [("Title", "https://datasets.imdbws.com/title.basics.tsv.gz"),
("Name", "https://datasets.imdbws.com/name.basics.tsv.gz"),
@johnludwigm
johnludwigm / create_statements.py
Last active December 12, 2018 17:47
SQL CREATE statements for IMDb database.
create_Title = ("CREATE TABLE IF NOT EXISTS Title ("
"tconst TEXT PRIMARY KEY, "
"title_type TEXT, "
"primary_title TEXT, "
"original_title TEXT, "
"is_adult INTEGER, "
"start_year INTEGER, "
"end_year INTEGER, "
"runtime_minutes INTEGER, "
"genres TEXT);")
@johnludwigm
johnludwigm / enc.py
Created January 25, 2018 22:28
A possible way to encrypt a secret key only using base
aws_secret_access_key = "BmUejjI9Fp23FAOJoijeRAJse/fAEIfo4FAJFIwe"
#It is absolutely infeasible to find a string which hashes to something
#that even remotely resembles your aws_secret_access_key.
the_password_I_will_enter = "MySecretPassword"
#Note that the_password_I_will_enter is a string rather than a bytes object:
"""
>>> isinstance("MySecretPassword", str)
@johnludwigm
johnludwigm / securestore.py
Last active January 26, 2018 00:20
Securely storing JSON credentials for AWS.
>>> aws_access_key_id = "BLPEVU91RZCTCC4CE01J"
>>> aws_secret_access_key = "AOIjf2CEW2faefAErwrt43G/9OaJfijvansEJfib"
>>> region_name = "us-east-1"
>>> the_password_I_type = "MySecretPassword"
>>> ciphertext = encrypt(the_password_I_type.encode(), aws_secret_access_key.encode())
>>> #Let's take a look!
>>> ciphertext
'OJ+T8lHBRqxGyZjTsJjP634iBhjJImYyaep/nv03aCnQkv9+8+I/m+2m1dsAv9JGu6JjnUcVyQSd5WPUEAqSYg=='
@johnludwigm
johnludwigm / inline_example1.java
Last active February 4, 2018 17:11
Example of in-line assignment
import java.io.*;
class Main {
public static void main(String[] args) {
String fileName = "blog.txt";
FileReader fileReader = new FileReader(fileName);
BufferedReader br = new BufferedReader(fileReader);
String line;