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 / 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 / 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 / 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 / clean_untracked
Created June 15, 2018 14:48
How to clean untracked git files
# trying to commit without needing to add all those extra files? just clean them
git clean -n -d # to see the files and folders to be deleted first before applying command below
git clean -f -d # -f force clean and -d cleaning directories. git just deletes.
@nara-l
nara-l / empty_postgres_table
Created June 15, 2018 14:57
Postgres empty table using TRUNCATE
# using truncate
TRUNCATE TABLE table_name RESTART IDENTITY; # to reset index counts
TRUNCATE TABlE table_name CASCADE; # to delete foreign key dependencies
@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,