Commits

Tim Savage committed 8bb5241 Draft

Tied up and cleaned up code a little. Added checks for paths

  • Participants
  • Parent commits 2d2bb92

Comments (0)

Files changed (2)

django_extras/core/types.py

 
 
 def to_dms(value, absolute=False):
-    """ Split a float value into DMS (degree, minute, second) parts
-    value - Float value to split
-    absolute - Obtain the absolute value
-    return tupple containing DMS values
+    """
+    Split a float value into DMS (degree, minute, second) parts
+
+    :param value - Float value to split
+    :param absolute - Obtain the absolute value
+    :return tuple containing DMS values
     """
     invert = not absolute and value < 0
     value = abs(value)
     value = (value - degree) * 60
     minute = int(math.floor(value))
     second = (value - minute) * 60
-    if invert: return (-degree, minute, second)
-    else: return (degree, minute, second)
+    return (
+        degree * -1 if invert else degree,
+        minute,
+        second
+    )
 
 
 def to_dm(value, absolute=False):
-    """ Split a float value into DM (degree, minute) parts
-    value - Float value to split
-    absolute - Obtain the absolute value
-    return tupple containing DM values
+    """
+    Split a float value into DM (degree, minute) parts
+
+    :param value - Float value to split
+    :param absolute - Obtain the absolute value
+    :return tuple containing DM values
     """
     invert = not absolute and value < 0
     value = abs(value)
     degree = int(math.floor(value))
     minute = (value - degree) * 60
-    if invert: return (-degree, minute)
-    else: return (degree, minute)
+    return (
+        degree * -1 if invert else degree,
+        minute
+    )
 
 
 class latitude(float):
     """ Latitude value """
-
-    __slots__ = []
-
     def __new__(cls, value=None):
         if value is None:
             return float.__new__(cls, 0.0)
         if isinstance(value, float):
-            if value <= 90.0 and value >= -90.0:
+            if 90.0 >= value >= -90.0:
                 return float.__new__(cls, value)
             raise ValueError("Value %d out of range(-90.0, 90.0)" % value)
         raise ValueError("Expected type float or latitude")
 
 class longitude(float):
     """ Longitude value """
-
-    __slots__ = []
-
     def __new__(cls, value=None):
         if value is None:
             return float.__new__(cls, 0.0)
         if isinstance(value, float):
-            if value <= 180.0 and value >= -180.0:
+            if 180.0 >= value >= -180.0:
                 return float.__new__(cls, value)
             raise ValueError("Value %d out of range(-180.0, 180.0)" % value)
         raise ValueError("Expected type float or longitude")

django_extras/utils.py

+import os.path
+
+
+def sub_path_in_path(sub_path, path):
+    """
+    Confirms that a sub_path is under a specified path.
+
+    i.e.
+    > sub_path_in_path("/usr/bin/python", "/usr/bin")
+    True
+    > sub_path_in_path("/etc/passwd", "/usr/share/python")
+    False
+    > sub_path_in_path("/usr/bin/../sbin/adduser", "/usr/bin")
+    False
+    """
+    sub_path = os.path.abspath(sub_path)
+    path = os.path.abspath(path)
+    return path.startswith(sub_path)
+
+
+def sub_path_in_paths(sub_path, paths):
+    """
+    Confirms that a sub_path is under one of the specified paths.
+
+    i.e.
+    > is_sub_path("/usr/bin", ["/usr/bin/python", )
+    True
+    > is_sub_path("/usr/share/python", "/etc/passwd")
+    False
+    """
+    return len(filter(lambda p: sub_path_in_path(sub_path, p), paths)) > 0