Skip to content

Instantly share code, notes, and snippets.

View eloyz's full-sized avatar
🎯
Focusing

Eloy Zuniga Jr. eloyz

🎯
Focusing
View GitHub Profile
@eloyz
eloyz / pop_quiz.py
Created December 10, 2012 19:01
Pop Quiz
x = 'marks the spot'
def f():
if False:
x = ''
print x
@eloyz
eloyz / column_count.sql
Created October 11, 2012 14:51
Get number of columns in database table
SELECT COUNT(*) from information_schema.columns
WHERE table_name='forms_form';
@eloyz
eloyz / django_file_response.py
Created September 17, 2012 20:41
Python String to Django File Object
"""
Using Amazon S3 with filename to get data in string and convert to file object
then returning HTTP response from file object with mime type.
"""
import os
import mimetypes
from django.http import Http404, HttpResponse
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
@eloyz
eloyz / commafy.py
Created March 22, 2012 09:54
Commafiy: For those comma separated scenarios
# Used for lastname, firstname situations or
# when using incomplete addresses
# ', '.join() method doesn't account for blank items
# assumes string; else will break on strip method call
def commafy(*args, **kwargs):
delimiter = kwargs.get('delimiter', ', ')
return delimiter.join([i for i in args if i.strip()])
# ideal world: whole address
@eloyz
eloyz / clone.py
Created December 16, 2011 14:27
Return a list of cloned items
listify = lambda s='chicken egg',n=10: ((s+' ')*n).split()
listify()
listify('eloy')
listify('eloy',20)
@eloyz
eloyz / slugify.py
Created October 11, 2011 21:02
Slugify a string of text with a max of 200 characters
import re
print re.sub("[^a-zA-Z0-9]", "-", name.lower()[:200]).strip('-')
@eloyz
eloyz / password_generator.py
Last active September 27, 2015 07:27
Password Generator (with human friendly characters)
def password_generator(length=8):
from random import choice
from string import letters, digits
doppelgangers = set('io10szSZOIl25uvUV')
alphanums = set(letters+digits)
chars = list(doppelgangers - alphanums)
return ''.join([choice(chars) for i in range(length)])
@eloyz
eloyz / rand_chars.py
Created September 20, 2011 17:10
Random Characters
### one way ###
from string import letters
import random
rand_chars = ''.join(random.sample(letters, len(letters)))[:8]
### another way ###
from string import letters
from random import choice
rand_chars = ''.join([choice(letters) for i in xrange(8)])
@eloyz
eloyz / pw_reset_link.py
Created September 15, 2011 17:39
Create Password Reset Link in Django
@eloyz
eloyz / delete_digit_dirs.py
Created September 13, 2011 21:44
Loop through files then delete directories with digit names
"""
Loop through files in current directory
Delete all directories that are named with digits
"""
import os
import shutil
for root, dirs, files in os.walk('.'):
basename = os.path.basename(root)
if os.path.isdir(basename) and basename.isdigit():