Antoine Pitrou avatar Antoine Pitrou committed b5db77e

Add PurePath.name

Comments (0)

Files changed (5)

 History
 -------
 
+Version 0.8
+^^^^^^^^^^^
+
+- Add PurePath.name property.
+
 Version 0.7
 ^^^^^^^^^^^
 
-0.7
+0.8
       >>> PureNTPath('//some/share').root
       '\\'
 
+.. data:: PurePath.name
+
+   A string representing the final path component, excluding the drive and
+   root, if any::
+
+      >>> PurePosixPath('my/library/setup.py').name
+      'setup.py'
+
+   UNC drive names are not considered::
+
+      >>> PureNTPath('//some/share/setup.py').name
+      'setup.py'
+      >>> PureNTPath('//some/share').name
+      ''
 
 .. data:: PurePath.ext
 
       >>> PurePosixPath('my/library').ext
       ''
 
-   UNC drive names are not considered::
-
-      >>> PureNTPath('//some/share/setup.py').ext
-      '.py'
-      >>> PureNTPath('//some.txt/share.py').ext
-      ''
-
 
 .. method:: PurePath.as_bytes()
 
                     doc="""The root of the path, if any""")
 
     @property
-    def ext(self):
-        """The final component's extension, if any."""
+    def name(self):
+        """The final path component, if any."""
         parts = self._parts
         if len(parts) == (1 if (self._drv or self._root) else 0):
             return ''
-        basename = parts[-1]
-        if basename == '.':
+        return parts[-1]
+
+    @property
+    def ext(self):
+        """The final component's extension, if any."""
+        basename = self.name
+        if basename == '' or basename == '.':
             return ''
         i = basename.find('.')
         if i == -1:
         self.assertEqual(P('/').root, sep)
         self.assertEqual(P('/a/b').root, sep)
 
+    def test_name_common(self):
+        P = self.cls
+        self.assertEqual(P('').name, '')
+        self.assertEqual(P('.').name, '')
+        self.assertEqual(P('/').name, '')
+        self.assertEqual(P('a/b').name, 'b')
+        self.assertEqual(P('/a/b').name, 'b')
+        self.assertEqual(P('/a/b/.').name, 'b')
+        self.assertEqual(P('a/b.py').name, 'b.py')
+        self.assertEqual(P('/a/b.py').name, 'b.py')
+
     def test_ext_common(self):
         P = self.cls
         self.assertEqual(P('').ext, '')
         self.assertEqual(P('//a/b/').root, '\\')
         self.assertEqual(P('//a/b/c/d').root, '\\')
 
+    def test_name(self):
+        P = self.cls
+        self.assertEqual(P('c:').name, '')
+        self.assertEqual(P('c:/').name, '')
+        self.assertEqual(P('c:a/b').name, 'b')
+        self.assertEqual(P('c:/a/b').name, 'b')
+        self.assertEqual(P('c:a/b.py').name, 'b.py')
+        self.assertEqual(P('c:/a/b.py').name, 'b.py')
+        self.assertEqual(P('//My.py/Share.php').name, '')
+        self.assertEqual(P('//My.py/Share.php/a/b').name, 'b')
+
     def test_ext(self):
         P = self.cls
         self.assertEqual(P('c:').ext, '')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.