Created
February 18, 2015 09:06
-
-
Save mundry/54380741d267a3de9b0d to your computer and use it in GitHub Desktop.
Class to store the longest version of a path in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- coding: utf-8 -*- | |
import logging | |
logger = logging.getLogger(__name__) | |
class PathList: | |
""" | |
Class to store the longest version of a path. | |
A list of | |
a | |
a/b/c | |
a/b | |
a/c | |
a/e/b | |
will result in | |
a/b/c | |
a/c | |
a/e/b | |
The paths need to be absolute to the same origin (although not | |
absolute to the overall file system). No resolution of symbolic | |
links or parent directories is performed. | |
""" | |
def __init__(self): | |
self._pathList = [] | |
def add(self, path): | |
""" | |
Adds the given path to the list if it is not contained in a path | |
already in the list. | |
path - The path to add to the list. | |
""" | |
logger.debug("Adding '%s' to path list.", path) | |
pathLen = len(path) | |
for i, p in enumerate(self._pathList): | |
# If the current and the given path are the same no actions | |
# are required. Quit the method. | |
if len(p) == pathLen: | |
if path == p: | |
logger.debug("Path list already has '%s'.", path) | |
return | |
# If the current path is shorter than the given one, check | |
# if the current path is part of the given one. If so remove | |
# the current path from the list. | |
elif len(p) < pathLen: | |
if path.startswith(p): | |
self._pathList.pop(i) | |
logger.debug("Removed '%s' from the list as it is part of the given path '%s'.", p, path) | |
# If the current path is longer than the given one, check if | |
# the given path is already contained in the current one. | |
elif len(p) > pathLen: | |
if self._pathList[i].startswith(path): | |
logger.debug("The given path '%s' is part of '%s' already in the list.", path, p) | |
return | |
self._pathList.append(path) | |
def __iter__(self): | |
return self._pathList.__iter__() | |
def __len__(self): | |
return len(self._pathList) | |
def __contains__(self, path): | |
for p in self._pathList: | |
if p.startswith(path): | |
return True | |
return False | |
def __str__(self): | |
return self._pathList.__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- coding: utf-8 -*- | |
import unittest | |
from pathlist import PathList | |
class TestPathList(unittest.TestCase): | |
def test_equal(self): | |
pathList = PathList() | |
pathList.add("path/to/directory") | |
self.assertTrue("path/to/directory" in pathList) | |
pathList.add("path/to/directory") | |
self.assertTrue(1 == len(pathList)) | |
def test_shorter(self): | |
pathList = PathList() | |
pathList.add("path/to/directory") | |
pathList.add("path/to") | |
self.assertTrue("path/to/directory" in pathList) | |
self.assertTrue("path/to" in pathList) | |
self.assertTrue(1 == len(pathList)) | |
def test_longer(self): | |
pathList = PathList() | |
pathList.add("path/to") | |
pathList.add("path/to/directory") | |
self.assertTrue("path/to/directory" in pathList) | |
self.assertTrue("path/to" in pathList) | |
self.assertTrue(1 == len(pathList)) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment