Skip to content

Instantly share code, notes, and snippets.

View nsa-yoda's full-sized avatar
🎯
Focusing

Juan Sanchez nsa-yoda

🎯
Focusing
View GitHub Profile
@nsa-yoda
nsa-yoda / config.py
Last active August 29, 2015 14:24
Using Munch to load a JSON based configuration as an object
import json
import munch
import os
class Config(object):
def __init__(self, config_file=None):
self.config_file = config_file
def load(self):
try:
def sort(x):
'''Sort a numeric list'''
for i in range(0, len(x)):
while i != 0 and x[i] < x[i-1]:
x[i], x[i-1] = x[i-1], x[i]
i -= 1
return x
def revstrcp(x):
'''Reverse a str without using .reversed()'''
@nsa-yoda
nsa-yoda / insertion_sort.py
Created June 17, 2015 20:50
Insertion sort
x = [2, 6, 4, 8, 6, 1, 2, 11]
def f(x):
for j in range(1, len(x)):
while j > 0 and x[j] < x[j-1]:
x[j], x[j-1] = x[j-1], x[j]
j -= 1
return x
print f(x)
@nsa-yoda
nsa-yoda / free_all_ram.sh
Last active May 11, 2017 15:13
Free all RAM from cache for Linux
#!/bin/bash
free && sync && echo 3 > /proc/sys/vm/drop_caches && free
@nsa-yoda
nsa-yoda / CelsiusOrFahrenheit_reviewed.java
Last active May 11, 2017 15:14
CelsiusOrFahrenheit_reviewed.java
import java.util.Scanner;
public class CelsiusOrFahrenheit {
public static void main(String[] args) {
System.out.println("Hello there, lets make some calculations.");
System.out.println("What is the temperature today? Type only the degrees.");
Scanner keyboard = new Scanner(System.in);
double randomNum = keyboard.nextDouble();
double fahrenCalc = (randomNum * 9 / 5) + 32;
double celsiusCalc = (randomNum - 32) * 5 / 9;
@nsa-yoda
nsa-yoda / .bashrc
Last active January 14, 2020 17:45
.bashrc (with helpful aliases)
# .bashrc
user=$(whoami)
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi;
# Parse the current git branch for a name
@nsa-yoda
nsa-yoda / sys_cli_args.py
Last active May 11, 2017 15:15
Get command line arguments via sys.argv
####################################
# Dirty arg parse, utilizes just system
####################################
import sys
args = [{i.split('=')[0].replace('--',''):i.split('=')[1]} for i in sys.argv[1:]][0]
#####################################
# ArpParse.py, utilizes argparse:
#####################################
@nsa-yoda
nsa-yoda / stack.py
Last active August 29, 2015 14:09
Simple Python stack
class Stack:
stack = None
_size = None
def __init__(self):
self.stack = []
self.size()
def push(self, item):
self.stack.append(item)
@nsa-yoda
nsa-yoda / 2015taxbracket.py
Last active May 11, 2017 15:44
2015 Tax Bracket Calculations using Python
def tax(income, tax_percent, min_tax, tax_break):
tax = (min_tax + ((income - tax_break) * tax_percent))
post_tax = income - tax
return {
"net": {
"yearly": "{0:.2f}".format(float(post_tax)),
"monthly": "{0:.2f}".format(float(post_tax / 12)),
"biweekly": "{0:.2f}".format(float(post_tax / 26))
},
"taxes": "{0:.2f}".format(float(tax))
@nsa-yoda
nsa-yoda / enable_swap.sh
Last active May 11, 2017 15:21
Enable swap
#!/bin/bash
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1