summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2015-07-12 19:43:17 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2015-07-12 19:43:17 -0700
commit6b95eba54a9e271939ead41260b10da74513cc9b (patch)
treeed0b6d6d248a9c96e6b525f358abcbbc7143e41a
parentdaaabb9aa8366c98c6fdb96b4b178c43e5be8fe9 (diff)
downloadisort-6b95eba54a9e271939ead41260b10da74513cc9b.tar.gz
Remove natsorted as dependency replace with built in solution.
-rw-r--r--isort/isort.py8
-rw-r--r--isort/natural.py46
-rwxr-xr-xsetup.py3
3 files changed, 52 insertions, 5 deletions
diff --git a/isort/isort.py b/isort/isort.py
index b838b3e4..e4845dee 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -36,7 +36,7 @@ from difflib import unified_diff
from sys import path as PYTHONPATH
from sys import stderr, stdout
-from natsort import natsorted
+from .natural import nsorted
from pies.overrides import *
from . import settings
@@ -322,7 +322,7 @@ class SortImports(object):
import_start = "from {0} import ".format(module)
from_imports = list(self.imports[section]['from'][module])
- from_imports = natsorted(from_imports, key=lambda key: self._module_key(key, self.config, True))
+ from_imports = nsorted(from_imports, key=lambda key: self._module_key(key, self.config, True))
if self.remove_imports:
from_imports = [line for line in from_imports if not "{0}.{1}".format(module, line) in
self.remove_imports]
@@ -427,9 +427,9 @@ class SortImports(object):
output = []
for section in itertools.chain(SECTIONS, self.config['forced_separate']):
straight_modules = list(self.imports[section]['straight'])
- straight_modules = natsorted(straight_modules, key=lambda key: self._module_key(key, self.config))
+ straight_modules = nsorted(straight_modules, key=lambda key: self._module_key(key, self.config))
from_modules = sorted(list(self.imports[section]['from'].keys()))
- from_modules = natsorted(from_modules, key=lambda key: self._module_key(key, self.config, ))
+ from_modules = nsorted(from_modules, key=lambda key: self._module_key(key, self.config, ))
section_output = []
if self.config.get('from_first', False):
diff --git a/isort/natural.py b/isort/natural.py
new file mode 100644
index 00000000..a20ba1d4
--- /dev/null
+++ b/isort/natural.py
@@ -0,0 +1,46 @@
+"""isort/natural.py.
+
+Enables sorting strings that contain numbers naturally
+
+usage:
+ natural.nsorted(list)
+
+Copyright (C) 2013 Timothy Edmund Crosley
+
+Implementation originally from @HappyLeapSecond stack overflow user in response to:
+ http://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+"""
+import re
+
+
+def _atoi(text):
+ return int(text) if text.isdigit() else text
+
+
+def _natural_keys(text):
+ return [_atoi(c) for c in re.split('(\d+)', text)]
+
+
+def nsorted(to_sort, key=None):
+ """Returns a naturally sorted list"""
+ if not key:
+ key_callback = _natural_keys
+ else:
+ key_callback = lambda item: _natural_keys(key(item))
+
+ return sorted(to_sort, key=key_callback)
diff --git a/setup.py b/setup.py
index f51220f7..ce0f99aa 100755
--- a/setup.py
+++ b/setup.py
@@ -57,7 +57,7 @@ setup(name='isort',
'distutils.commands': ['isort = isort.main:ISortCommand'],
},
packages=['isort'],
- requires=['pies', 'natsort'],
+ requires=['pies'],
install_requires=['pies>=2.6.2', 'natsort>=3.0.0'],
cmdclass={'test': PyTest},
keywords='Refactor, Python, Python2, Python3, Refactoring, Imports, Sort, Clean',
@@ -76,6 +76,7 @@ setup(name='isort',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
+ 'Programming Language :: Python :: 3.5',
'Topic :: Software Development :: Libraries',
'Topic :: Utilities'],
**PyTest.extra_kwargs)