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 / user_object_max_length_validation.py
Created June 24, 2011 14:59
Validate Django user max length object against database
from django.contrib.auth.models import User
from django.db.models.fields import FieldDoesNotExist
# Used for user import feature.
# There's no form validation; so we validate max_length this way.
# loop through user properties; truncate at max_length
for key, value in user.__dict__.items():
max_length = None
try:
@eloyz
eloyz / is_email_valid.py
Created June 24, 2011 15:10
Check if Valid Email via Django Regex
from django.core.validators import email_re
def is_email_valid(email):
""" Check if valid email via Django regex """
return bool(email_re.match(email))
@eloyz
eloyz / entity2unicode.py
Created June 30, 2011 22:39
Convert HTML Entity to Unicode in Django Pages Module
import re
import HTMLParser
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""
Converts title and content html entities to unicode
"""
@eloyz
eloyz / content_type_and_permissions.py
Created July 24, 2011 13:05
Content Type and Permissions
from django.core.management import setup_environ
try:
import settings
except ImportError:
import sys
sys.stderr.write("Couldn't find the settings.py module.")
sys.exit(1)
setup_environ(settings)
@eloyz
eloyz / email_to_username.py
Created July 26, 2011 17:59
Clean Username if Email Address
def clean_username(un):
import re
# clean username
un = re.sub(r'[^a-zA-Z0-9._]+', '', un)
# soft truncate
if len(un) > 30:
un = un.split('@')[0] # pray for email address
@eloyz
eloyz / bash_profile
Last active September 27, 2015 02:38
Change Aliases and Reload Aliases
# subl is a command line interface (cli) for a program called sublime text 2
# http://www.sublimetext.com/docs/2/osx_command_line.html
alias subl="'/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl'"
alias nano="subl"
export EDITOR="subl"
alias edit_bash="subl ~/.bash_profile"
alias rebash=". ~/.bash_profile"
@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():
@eloyz
eloyz / pw_reset_link.py
Created September 15, 2011 17:39
Create Password Reset Link in Django
@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 / 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)])