Commits

Tyler Butler committed 32816cb

Separate functions that rely on Django from those that don't.

This way functions that are generally useful outside a Django
context can be imported and used.

  • Participants
  • Parent commits 84d9bad

Comments (0)

Files changed (3)

File potpourri/functions.py

-import base64
-import hashlib
-import sys
-from urlparse import urlunparse
-from django.conf import settings
-from django.contrib.contenttypes.models import ContentType
-import re
-
-def make_absolute_url(url_path):
-    host = unicode.format(u'{0}:{1}', settings.HOSTNAME, settings.PORT)
-    return urlunparse(('http', host, url_path, '', '', ''))
-
-
-def smart_content_type_for_model(model):
-    """
-    Returns the Django ContentType for a given model. If model is a proxy model, the proxy model's ContentType will
-    be returned. This differs from Django's standard behavior - the default behavior is to  return the parent
-    ContentType for proxy models.
-    """
-    if model._meta.proxy:
-        return ContentType.objects.get(app_label=model._meta.app_label,
-                                       model=model._meta.object_name.lower())
-    else:
-        return ContentType.objects.get_for_model(model)
-
-
-def dict_to_querystring(dictionary):
-    """Converts a python dict to a querystring suitable to be appended to a URL."""
-    s = u""
-    for d in dictionary.keys():
-        s = unicode.format(u"{0}{1}={2}&", s, d, dictionary[d])
-    return s[:-1]
-
-
-def get_class( class_string ):
-    """Given a string representing a path to a class, instantiates that class."""
-    parts = class_string.split('.')
-    module = ".".join(parts[:-1])
-    m = __import__(module)
-    for comp in parts[1:]:
-        m = getattr(m, comp)
-    return m
-
-
-def calc_sha(file):
-    """Calculates the base64-encoded SHA hash of a file."""
-    sha = hashlib.sha256()
-    sha.update(file)
-    r = sha.digest()
-    r = base64.b64encode(r)
-    return r
-
-
-def space_out_camel_case(stringAsCamelCase):
-    """
-    Adds spaces to a camel case string.  Failure to space out string returns the original string.
-
-    >>> space_out_camel_case('DMLSServicesOtherBSTextLLC')
-    'DMLS Services Other BS Text LLC'
-    """
-    pattern = re.compile('([A-Z][A-Z][a-z])|([a-z][A-Z])')
-
-    if stringAsCamelCase is None:
-        return None
-
-    return pattern.sub(lambda m: m.group()[:1] + " " + m.group()[1:], stringAsCamelCase)
-
-
-def add_to_path_if_needed(path):
-    """
-    Adds *path* to the system path if it is not already on it.
-
-    Prevents unnecessary 'polution' of the path with a bunch of redundant entries.
-    """
-    if path not in sys.path:
-        sys.path.append(path)

File potpourri/functions/__init__.py

+import base64
+import hashlib
+import re
+import sys
+
+__author__ = 'tyler@tylerbutler.com'
+
+def get_class( class_string ):
+    """Given a string representing a path to a class, instantiates that class."""
+    parts = class_string.split('.')
+    module = ".".join(parts[:-1])
+    m = __import__(module)
+    for comp in parts[1:]:
+        m = getattr(m, comp)
+    return m
+
+
+def dict_to_querystring(dictionary):
+    """Converts a python dict to a querystring suitable to be appended to a URL."""
+    s = u""
+    for d in dictionary.keys():
+        s = unicode.format(u"{0}{1}={2}&", s, d, dictionary[d])
+    return s[:-1]
+
+
+def calc_sha(file):
+    """Calculates the base64-encoded SHA hash of a file."""
+    sha = hashlib.sha256()
+    sha.update(file)
+    r = sha.digest()
+    r = base64.b64encode(r)
+    return r
+
+
+def space_out_camel_case(stringAsCamelCase):
+    """
+    Adds spaces to a camel case string.  Failure to space out string returns the original string.
+
+    >>> space_out_camel_case('DMLSServicesOtherBSTextLLC')
+    'DMLS Services Other BS Text LLC'
+    """
+    pattern = re.compile('([A-Z][A-Z][a-z])|([a-z][A-Z])')
+
+    if stringAsCamelCase is None:
+        return None
+
+    return pattern.sub(lambda m: m.group()[:1] + " " + m.group()[1:], stringAsCamelCase)
+
+
+def add_to_path_if_needed(path):
+    """
+    Adds *path* to the system path if it is not already on it.
+
+    Prevents unnecessary 'pollution' of the path with a bunch of redundant entries.
+    """
+    if path not in sys.path:
+        sys.path.append(path)

File potpourri/functions/django.py

+from urlparse import urlunparse
+from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
+
+def make_absolute_url(url_path):
+    host = unicode.format(u'{0}:{1}', settings.HOSTNAME, settings.PORT)
+    return urlunparse(('http', host, url_path, '', '', ''))
+
+
+def smart_content_type_for_model(model):
+    """
+    Returns the Django ContentType for a given model. If model is a proxy model, the proxy model's ContentType will
+    be returned. This differs from Django's standard behavior - the default behavior is to  return the parent
+    ContentType for proxy models.
+    """
+    if model._meta.proxy:
+        return ContentType.objects.get(app_label=model._meta.app_label,
+                                       model=model._meta.object_name.lower())
+    else:
+        return ContentType.objects.get_for_model(model)