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 / 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 / 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 / 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 / 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 / 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 / pop_quiz.py
Created December 10, 2012 19:01
Pop Quiz
x = 'marks the spot'
def f():
if False:
x = ''
print x
@eloyz
eloyz / csv_to_list.py
Created January 17, 2013 08:17
Using Python to read CSV file into list.
import csv
def csv_to_list(file_path):
"""
Reads csv file into list.
"""
with open(file_path, 'rb') as f:
# load all data into memory, scary
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0) # reset cursor
v = 'True'
def bool2():
v[0].lower() in ('t','1','y')
@eloyz
eloyz / read_env.py
Created March 23, 2013 03:42
Function that reads environment variables from .env file and adds them to the os.environment dictionary.
# manage.py in Django project
import re
def read_env():
"""
Pulled from Honcho code with minor updates, reads local default
environment variables from a .env file located in the project root
directory.
"""
try:
@eloyz
eloyz / codeintel.sublime-mousemap
Created March 26, 2013 10:46
This is a mouse binding that lets you keep the column select feature in Sublime Text 2 If you're using the CodeIntel plugin it gets overwritten, this is a way of bring it back. Not sure how to keep both bindings. Sublime > Preferences > Package Settings > SublimeCodeIntel > Mouse Bindings - User
[
{
"button": "button1", "modifiers": ["alt"],
"press_command": "drag_select",
"press_args": {"by": "columns"}
}
]