Skip to content

Instantly share code, notes, and snippets.

View nara-l's full-sized avatar

Lawrence Nara nara-l

View GitHub Profile
@nara-l
nara-l / convert_to_snake_case
Created December 2, 2017 16:44
Convert to Snake case Python
# convert to snake case, added replace - could do better with re https://stackoverflow.com/a/1176023/851056
def convert(name):
new_name = name.replace(" ", "").replace("'","")
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', new_name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
@nara-l
nara-l / find_largest_number.py
Last active May 22, 2018 18:49
Finding the largest number in a list without usiing for loop python
def find_largest_num(Li):
if len(Li) ==1:
return Li[0]
x1 = Li[0]
x2 = find_largest_num(Li[1:])
if x1 > x2:
return x1
else:
return x2
@nara-l
nara-l / pass.py
Last active May 22, 2018 18:49
What does 'pass' return in python?
# if pass returns it returns <NoneType> !None <str>, Explained: https://stackoverflow.com/a/36622862/851056
def f():
pass
def g(x):
if x > 2:
return x
def h():
return None
@nara-l
nara-l / adding_dictionaries.py
Created May 24, 2018 11:45
Adding 2 dictionaries python 2 and 3
# Python 3.5 >
>>> x = {'a':3, 'b':4, 'c': 7}
>>> y = {'a': 6, 'b': 8, 'd': 9}
>>> z = {**x, **y}
>>> z
{'a': 6, 'b': 8, 'c': 7, 'd': 9}
# Python 2.x
z = dict(x, **y)
@nara-l
nara-l / sorting_dictionary_by_value.py
Created May 24, 2018 12:32
Sorting a dictionary by value
# Given a dictionary
>>> ds = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
>>> sorted(ds.items(), key=lambda y:y[1]) ## sorts values in ascending order
'''Result'''
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
# Or we can use itemgetter to sort by any item
from operator import itemgetter
@nara-l
nara-l / django111_template_settings.py
Last active June 1, 2018 00:46
Django 1.11 template settings config
# Build path
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'OPTIONS': {
'loaders': (
'django.template.loaders.filesystem.Loader',
@nara-l
nara-l / update_vue_prop_in_promise.vue
Created June 1, 2018 01:46
Understanding JS scope in promise call back: Update Vuejs property in promise call back (capturing 'this' in closure )
/*
* There are about 2 ways to do tis
*/
import firebase from 'firebase'
export default {
name: "SignUp",
data: () => ({
successAlert: false,
@nara-l
nara-l / django_models_standalone.py
Last active June 20, 2018 14:20
Using Django Model in a python standalone script
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE",'project_name.settings') # replace project_name with your project name
import django
django.setup()
from my_app.models import (
Customers,
Organizations,
@nara-l
nara-l / port_already_in_use
Created June 14, 2018 15:37
Open clogged port. Error: port already in use python on Mac
# change port number to clear the neccessary port
sudo lsof -t -i tcp:8000 | xargs kill -9
@nara-l
nara-l / cherry_picking_commits
Last active December 20, 2019 21:01
GIT: Cherry Picking commits from another branch to a new branch git
# Picking specific commits from one git branch to a new one
#. 1. Create a new branch off master or any clean branch and checkout into the new branch
# 2. Use git log --pretty=oneline # on the branch with the needed commits to see the <sha> for the commits needed
# command
git cherry-pick 108e055..3f7cf07 # does not include commits from 108e055. 108e055 being the oldest commit and 3f7cf07 the lastest
git cherry-pick 108e055^..3f7cf07 # to include commits from 108e055