reuse

Collections of useful python functions.

reuse.check_exists(path)[source]

Check if the directory exists.

Parameters:path (str) – path to a directory or a file.
Returns:if path exist, return True, else return False
Return type:bool

Example

>>> dir_path = '/path/to/not/exist/dir'
>>> check_dir_exists(dir_path)
False
reuse.concat_path(parts)[source]

Concatenate multiple parts to get a complete path.

Parameters:parts (list) – list contains each part of the path.
Returns:concatenated path.
Return type:str

Examples

>>> parts = ['home', 'myname', 'project']
>>> path = concat_path(parts)
>>> print(path)
home/myname/project
>>> parts = ['/home', 'myname', 'project']
>>> path = concat_path(parts)
>>> print(path)
/home/myname/project
reuse.create_dir_if_not_exist(dir_path)[source]

Create the directory if it didn’t exist.

Parameters:dir_path (str) – path to directory that to create.

Example

>>> dir_path = '/path/to/dir1'
>>> create_dir_if_not_exist(dir_path)
reuse.flat_list(ll)[source]

Flatten a list of list to a list.

Parameters:ll (list) – a list has elments of type list.
Returns:one dimension list.
Return type:list

Examples

>>> lol = [[1, 2], [3, 4]]
>> l = flat_list(lol)
>> print(l)
[1, 2, 3, 4]
reuse.full_name(path)[source]

Get the full name (name + ‘.’ + extension) of a path.

Parameters:path (str) – a str contain a POSIX path.
Returns:name of the file or dir in path.
Return type:str

Example

>>> path = '/path/to/a.txt'
>>> fill_name(path)
'a.txt'
>>> path = '/path/to/mydir'
>>> fill_name(path)
'mydir'
reuse.list_dir(path, sort=True)[source]

List directories in given directory.

Parameters:
  • path (str) – path of target directory.
  • sort (bool) – if set to True, return sorted list of dirs.
Returns:

a list contains subdirectories in the directory.

Return type:

list

Example

>>> path = '/path/to/my/dir'
>>> list_dir(path)
['/path/to/my/dir/dir1', '/path/to/my/dir/dir2']
reuse.list_file(path, ptn=None, sort=True)[source]

List files in given directory.

Parameters:
  • path (str) – path of target directory.
  • ptn (str) – pattern to selected certain type of file. Defeault: None.
  • sort (bool) – if set to True, return sorted list of files.
Returns:

files in the directory that match the pattern.

Return type:

list

Example

>>> path = '/path/to/my/dir'
>>> ptn = "*.py" # only choose *.py file
>>> list_file(path, ptn=ptn, sort=True)
['/path/to/my/dir/1.py', '/path/to/my/dir/2.py']
reuse.parent_dir(path)[source]

Return the parent directory of a path.

Parameters:path (str) – a str contain a POSIX path.
Returns:the parent directory of target path.
Return type:str

Example

>>> path = '/path/to/my/a.txt'
>>> parent_dir(path)
'/path/to/my'
reuse.pure_name(path)[source]

Get the name with extension of a path.

Parameters:path (str) – a str contain a POSIX path.
Returns:name of the file or dir in path.
Return type:str

Example

>>> path = '/path/to/a.txt'
>>> pure_name(path)
a
reuse.read_from_file(file_name)[source]

Read content in a text file.

Parameters:file_name (str) – name of a text file.
Returns:all content in the text file.
Return type:str

Example

>>> content = read_from_file('/path/to/a.txt')
>>> print(content)
print('hello')
reuse.run_assert(cond, out)[source]

Run assert and output out if failed.

Parameters:
  • cond (func) – condtion to judge True or False.
  • out (str) – output when assert fails.

Example

>>> i = 20
>>> run_assert(isinstance(i, str), 'i must be a str')
AssertionError: i must be a str
reuse.run_cmd(cmd)[source]

Run a Bash command in python.

Parameters:cmd (str or list) – the Shell command to execute.
Returns:0 if runs successfully, other value if fails.
Return type:int

Example

>>> cmd = 'ls -l'
>>> run_cmd(cmd)
-rw-rw-r-- 1 user user  4651 Nov 10 20:19 reuse.py
>>> cmd = ['ls', '-l']
>>> run_cmd(cmd)
-rw-rw-r-- 1 user user  4651 Nov 10 20:19 reuse.py
reuse.write_to_file(content, file_name)[source]

Write content to a text file.

Parameters:
  • content (str) – content wanted to write to file.
  • file_name (str) – name of file to wirte.

Example

>>> content = 'line1    line2   line3'
>>> write_to_file(content, 'a.txt')