Skip to content

Commit 83905f1

Browse files
committed
-
1 parent 1b12b90 commit 83905f1

File tree

12 files changed

+49
-46
lines changed

12 files changed

+49
-46
lines changed

source_py2/test_python_toolbox/test_temp_value_setting/test_temp_working_directory_setter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'''Testing `python_toolbox.temp_value_setting.TempWorkingDirectorySetter`.'''
55

66
import os
7-
import pathlib
7+
?import pathlib
88
import shutil
99
import tempfile
1010

source_py3/python_toolbox/cute_profile/profile_handling.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
import datetime as datetime_module
66
import marshal
7+
import pathlib
78
import abc
89
import os.path
910
import pstats
@@ -79,12 +80,11 @@ def thread_job(self):
7980
class FolderProfileHandler(AuxiliaryThreadProfileHandler):
8081
'''Profile handler that saves the profile to disk on separate thread.'''
8182

82-
def __init__(self, folder_path):
83-
self.folder_path = folder_path
83+
def __init__(self, folder):
84+
self.folder = pathlib.Path(folder)
8485

8586
def thread_job(self):
86-
with open(os.path.join(self.folder_path, self.make_file_name()), \
87-
'wb') as output_file:
87+
with (self.folder / self.make_file_name()).open('wb') as output_file:
8888
output_file.write(self.profile_data)
8989

9090

@@ -102,6 +102,9 @@ def handle(self):
102102

103103
def get_profile_handler(profile_handler_string):
104104
'''Parse `profile_handler_string` into a `ProfileHandler` class.'''
105+
if isinstance(profile_handler_string, pathlib.Path):
106+
assert profile_handler_string.is_dir()
107+
return FolderProfileHandler(profile_handler_string)
105108
if not profile_handler_string or profile_handler_string in \
106109
['0', '1', '2', '3', '4']:
107110
try:

source_py3/python_toolbox/import_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ def _find_module_in_some_zip_path(module_name, path=None):
266266
#else:
267267
#leading_path = ''
268268

269-
return os.path.join(zip_path,
270-
_module_address_to_partial_path(module_name))
269+
return pathlib.Path(str(zip_path)) / \
270+
_module_address_to_partial_path(module_name)
271271

272272
if original_path_argument is not None:
273273
raise ImportError('Module not found in the given zip path.')

source_py3/python_toolbox/os_tools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ def start_file(path):
2929

3030

3131
_is_windows = (os.name == 'nt')
32-
null_path = pathlib.Path('\\Device\\Null') if _is_windows \
33-
else pathlib.Path('/dev/null')
32+
null_path = pathlib.Path(os.path.devnull)
3433
path_type = pathlib.WindowsPath if _is_windows else pathlib.PosixPath

source_py3/python_toolbox/sequence_tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,16 @@ def to_tuple(single_or_sequence, item_type=None, item_test=None):
235235
which is the type of the items, or alternatively `item_test` which is a
236236
callable that takes an object and returns whether it's a valid item. These
237237
are necessary only when your items might be sequences themselves.
238+
239+
You may optionally put multiple types in `item_type`, and each object would
240+
be required to match to at least one of them.
238241
'''
239242
if (item_type is not None) and (item_test is not None):
240243
raise Exception('You may specify either `item_type` or '
241244
'`item_test` but not both.')
242245
if item_test is not None:
243246
actual_item_test = item_test
244247
elif item_type is not None:
245-
assert isinstance(item_type, type)
246248
actual_item_test = \
247249
lambda candidate: isinstance(candidate, item_type)
248250
else:

source_py3/python_toolbox/sys_tools.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66

77
import sys
8+
import pathlib
89
import io
910

1011
from python_toolbox.context_management import (ContextManager,
1112
BlankContextManager)
1213
from python_toolbox.temp_value_setting import TempValueSetter
1314
from python_toolbox.reasoned_bool import ReasonedBool
15+
from python_toolbox import sequence_tools
1416

1517

1618
class OutputCapturer(ContextManager):
@@ -71,11 +73,11 @@ def __init__(self, addition):
7173
7274
`addition` may be a path or a sequence of paths.
7375
'''
74-
if isinstance(addition, str):
75-
addition = [addition]
76-
for entry in addition:
77-
assert isinstance(entry, str)
78-
self.addition = addition
76+
self.addition = map(
77+
unicode,
78+
sequence_tools.to_tuple(addition,
79+
item_type=(str, pathlib.PurePath))
80+
)
7981

8082

8183
def __enter__(self):

source_py3/python_toolbox/temp_value_setting/temp_working_directory_setter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def __init__(self, working_directory):
2525
2626
`working_directory` is the temporary working directory to use.
2727
'''
28-
assert isinstance(working_directory, str)
2928
TempValueSetter.__init__(self,
3029
(os.getcwd, os.chdir),
31-
value=working_directory)
30+
value=str(working_directory))

source_py3/test_python_toolbox/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import sys
77
import os.path
8+
import pathlib
89

910
import nose
1011

@@ -43,12 +44,8 @@ def exists(module_name):
4344
return True
4445

4546
if not exists('python_toolbox'):
46-
python_toolbox_candidate_path = os.path.realpath(
47-
os.path.join(
48-
os.path.split(__file__)[0],
49-
'..',
50-
)
51-
)
47+
python_toolbox_candidate_path = \
48+
pathlib(__file__).parent.parent.absolute()
5249
sys.path.append(python_toolbox_candidate_path)
5350

5451

source_py3/test_python_toolbox/test_cute_profile/test_cute_profile.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,27 @@ def test_folder_handler():
233233
f = cute_profile.profile_ready(profile_handler=temp_folder)(func)
234234

235235
f(1, 2)
236-
assert len(os.listdir(temp_folder)) == 0
236+
assert len(list(temp_folder.iterdir())) == 0
237237

238238
f(1, 2)
239-
assert len(os.listdir(temp_folder)) == 0
239+
assert len(list(temp_folder.iterdir())) == 0
240240

241241
f.profiling_on = True
242242

243243
f(1, 2)
244-
assert len(os.listdir(temp_folder)) == 1
244+
assert len(list(temp_folder.iterdir())) == 1
245245

246246
f(1, 2)
247-
assert len(os.listdir(temp_folder)) == 1
247+
assert len(list(temp_folder.iterdir())) == 1
248248

249249
time.sleep(0.01) # To make for a different filename.
250250

251251
f.profiling_on = True
252252
f(1, 2)
253253

254-
assert len(os.listdir(temp_folder)) == 2
254+
assert len(list(temp_folder.iterdir())) == 2
255255

256256
f(1, 2)
257-
assert len(os.listdir(temp_folder)) == 2
257+
assert len(list(temp_folder.iterdir())) == 2
258258

259259

source_py3/test_python_toolbox/test_import_tools/test_exists/test_zip.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def test_zip():
3434
with temp_file_tools.TemporaryFolder(prefix='test_python_toolbox_') \
3535
as temp_folder:
3636

37-
temp_zip_path = os.path.join(temp_folder, 'archive_with_module.zip')
38-
39-
with open(temp_zip_path, 'wb') as temp_zip_file:
37+
temp_zip_path = temp_folder / 'archive_with_module.zip'
4038

39+
with temp_zip_path.open('wb') as temp_zip_file:
4140
temp_zip_file.write(zip_string)
4241

4342
assert not exists('zip_imported_module_bla_bla')

0 commit comments

Comments
 (0)