Welcome to reuse’s documentation!

reuse is a collection of useful python functions in you work.

Here is a demo about how to use reuse: demo.

Installation

pip3 install --user py-reuse

Examples

>>> import reuse as rs
>>> rs.check_exists('/etc/apt/source.lists')
False
>>> rs.check_exists('/etc/apt')
True
>>> rs.concat_path(['/home', 'user', 'project'])
'/home/user/project'
>>> rs.create_dir_if_not_exist('./tmp-dir/test')
>>> ll = [[1,2], [3,4], [6,7]]
>>> rs.flat_list(ll)
[1, 2, 3, 4, 6, 7]
>>> rs.full_name('/etc/apt/sources.list')
'sources.list'
>>> rs.pure_name('/etc/apt/sources.list')
'sources'
>>> rs.parent_dir('/etc/apt/sources.list')
'/etc/apt'
>>> rs.run_cmd('ls -l')
总用量 1068
-rw-rw-r--  1 wang wang  15200 9月   4 20:21 00218.jpg
-rw-rw-r--  1 wang wang  15071 9月   4 20:21 00219.jpg
drwxrwxr-x  2 wang wang  36864 11月 22 16:49 960
-rw-rw-r--  1 wang wang   5496 11月 25 16:07 A.odf
-rw-rw-r--  1 wang wang   1380 11月 22 16:40 a.txt
drwxrwxr-x  2 wang wang   4096 7月  14 09:05 bin
-rw-rw-r--  1 wang wang   5358 11月 25 16:08 B.odf
drwxrwxr-x  3 wang wang   4096 11月 10 13:19 cpp-test
drwxrwxr-x  2 wang wang   4096 11月 19 11:54 diff_masks
drwxr-xr-x  2 wang wang  40960 11月 15 18:44 dp
drwxrwxr-x  6 wang wang  61440 11月 19 17:32 dy4
drwxrwxr-x  2 wang wang  69632 11月 18 22:48 dy4_1G
drwxr-xr-x  2 wang wang   4096 11月 20 13:04 epoch-500
drwxrwxr-x  7 wang wang   4096 11月 18 09:22 FlameGraph
drwxrwxr-x  4 wang wang   4096 11月 23 23:56 go
-rw-rw-r--  1 wang wang     14 11月 23 00:17 hello.txt
-rw-rw-r--  1 wang wang 309348 11月 20 10:34 img1_kp_50.png
drwxrwxr-x  3 wang wang   4096 7月  14 09:05 opt
-rw-rw-r--  1 wang wang 383100 11月 20 19:53 ORB.png
-rw-rw-r--  1 wang wang    337 11月 20 21:02 ORB.py
drwxrwxr-x  2 wang wang   4096 11月 19 10:48 outputs
-rw-rw-r--  1 wang wang     35 11月 20 10:56 project.py
drwxrwxr-x  3 wang wang   4096 11月  1 22:05 PycharmProjects
drwxrwxr-x 11 wang wang   4096 11月 18 09:24 pyflame
drwxrwxr-x  4 wang wang   4096 11月 14 12:00 results
drwxrwxr-x  2 wang wang   4096 11月 16 14:09 S001C001P008R002A060_rgb_out
drwxrwxr-x  2 wang wang   4096 11月 16 14:05 S017C003P020R002A060
drwxrwxr-x  2 wang wang   4096 11月 12 21:49 tmp
drwxrwxr-x  3 wang wang   4096 11月 25 18:01 tmp-dir
drwxrwxr-x  2 wang wang   4096 10月 26 13:49 videos
drwxrwxr-x 12 wang wang   4096 11月 20 21:40 workspace
drwxrwxr-x  2 wang wang  20480 11月  9 17:22 ws_1_step_8
drwxr-xr-x  5 wang wang   4096 11月  1 20:08 视频
drwxr-xr-x 11 wang wang   4096 11月 24 13:30 图片
drwxr-xr-x 16 wang wang   4096 11月 12 23:29 文档
drwxr-xr-x  7 wang wang  12288 11月 25 16:23 下载
drwxr-xr-x  2 wang wang   4096 10月  2 09:19 桌面
0

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')

Indices and tables