Skip to main content

Py Basic Commands

A package with some basic tools and commands for Python (+3.7)

GitHub release (latest by date) Working status Working Python version PyPI - License

Author

Installation

Install using pip

  pip install py_basic_tools

  pip3 install py_baisc_tools

Table of input variables

Variable Usage Type
text The text to be printed str
nl Whether to print a newline or not bool
flush Whether to flush the output print or not bool
do_print Whether to print the text or not bool
use_end_addon Whether or not to add a colon to the end of the text bool
file_path The path of the file to be read str
create If True, the file/directory will be created if it does not exist bool
force If True, the file/directory will be created even if it exists bool
remove_empty If True, empty lines will be removed from list bool
do Action for directory or file 'dir'/'file'
ret_type Variable type to return type
ret_var Variable to return str

Examples

fprint()

  • Customizable input function
>>> fprint('Hello World')
Hello World

>>> fprint('Hello World', nl=False)
Hello World
>>> fprint()

>>> fprint('Hello World', do_print=False)

The above fprint() examples are very basic. But you could implement and customise it to your liking.

finput()

  • Customizable input function
>>> finput('Enter a number', ret_type=int)
Enter a number: 5

5
>>> finput('Enter a number', ret_type=int)
Enter a number: 5.5

Couldn't return input 5.5 as <class 'int'>
Input type: <class 'str'>
Returning value as string

'5.5'
>>> finput('Enter a number', ret_type=int)
Enter a number: five

Couldn't return input five as <class 'int'>
Input type: <class 'str'>
Returning value as string

'five'

enter_to_continue()

  • Wait until user presses enter to continue
  • Returns:
    • True: No input was given, only enter was pressed
    • False: Something was written before pressing enter
from py_basic_tools import enter_to_continue

>>> enter_to_continue()
Press enter to continue...

>>> enter_to_continue('Waiting')
Waiting (enter to continue)

>>> enter_to_continue('Waiting', nl=False)
Waiting (enter to continue)
>>> enter_to_continue('Waiting', use_help_text=False)
Waiting

>>> enter_to_continue(use_help_text=False)

choose_from_list()

  • Choose one or more variables from user given list
  • When choosing more than one, add a space between chosen indexes
  • Returns:
    • list of variables: If variables were chosen correctly
    • empty list: If an exception occured
>>> lst = ['foo', 'bar']
>>> choose_from_list(lst)
---Choose 1 value---
(0) foo
(1) bar

Input index: 0
'foo'
>>> choose_from_list(lst, choose_total=2, start_num=1)
---Choose 2 values---
(1) foo
(2) bar

Input index: 1 2
['foo', 'bar']
>>> choose_from_list(lst, start_num=3, choose_until_correct=True)
---Choose 1 value---
(3) foo
(4) bar

Input index: 0

Given index is out of range
List length: 2    Input index: 0

Input index: 3

foo
>>> choose_from_list(lst, choose_total=2, start_num=1, choose_until_correct=False)
---Choose 2 values---
(1) foo
(2) bar

Input index: 0 1
Given index is out of range
Value has to be between: 1-2
[]
>>> choose_from_list(lst, header_text='Choose what you will', header_nl=True, input_text='Input what you will')
Choose what you will

(0) foo
(1) bar

Input what you will: 0

['foo']

read_file()

  • Function tries to read the file, and if it fails, it will create the file if the create argument is True.
  • If the file is read successfully, the function will return a list of lines from the file
>>> read_file('path/to/file.txt')
File not found: path/to/file.txt

None
>>> read_file('path/to/file.txt', do_print=False)
None
>>> read_file('path/to/file.txt', create=True)
File created: path/to/file.txt

>>> read_file('path/to/file.txt')
['hello', 'world', 'foo bar']

>>> read_file('path/to/file.txt', remove_empty_lines=False)
['hello', 'world', '', 'foo bar']

create_file_dir()

  • Creates a file or directory to the given path
>>> create_file_dir('file', 'path/to/file.txt')
File created: path/to/file.txt

>>> create_file_dir('file', 'path/to/file.txt')
File already exists: path/to/file.txt

>>> create_file_dir('file', 'path/to/file.txt', do_print=False)

>>> create_file_dir('file', 'path/to/file.txt', force=True)
File created: path/to/file.txt

>>> create_file_dir('dir', 'path/to/new directory')
Directory created: path/to/new directory

remove_file_dir()

  • Removes a file or directory to the given path
>>> remove_file_dir('file', 'path/to/file.txt')
File removed: path/to/file.txt

>>> remove_file_dir('file', 'path/to/another_file.txt')
File is not empty, not removing: file.txt

>>> remove_file_dir('file', 'path/to/another_file.txt', force=True)
File removed: path/to/another file.txt

>>> remove_file_dir('file', 'path/to/another_file.txt')
File not found: path/to/another file.txt

>>> remove_file_dir('dir', 'path/to/directory')
Directory path not found: path/to/directory

get_dir_path_for_file()

  • Gets the path to the direcotry where given file is
  • Returns:
    • dir_path: Paht to the directory
    • filename: Name of the file
>>> get_dir_path_for_file('path/to/file.txt')
('path/to', 'file.txt')
>>> get_dir_path_for_file('path/to/file.txt', ret_var='filename')
'file.txt'
>>> get_dir_path_for_file('path/to/file.txt', ret_var='dir')
'path/to'

join_path()

  • Joins given paths together
    • Like os.path.join()
>>> join_path('hello', 'world')
'hello\\world'
>>> join_path('hello', 'world', join_with='/')
'hello/world'

try_traceback()

Code:

@try_traceback()
def foo():
    raise Exception('foo')

@try_traceback(skip_traceback=True)
def bar():
    raise Exception('bar')

foo()
bar()

Output:

Traceback (most recent call last):
    File "test.py", line 13, in wrapper
        return func(*args, **kwargs)
    File "test.py", line 8, in foo
        raise Exception('foo')

bar() doesn't raise an exception, because traceback was skipped.

func_timer()

  • The decorator can be used to time any function, and it will print the time it took to run the function
  • Returns:
    • ret_val: Values to return from the function
    • time_delta: The time it took for the function to run

Code:

from time import sleep

@func_timer()
def foo(sec_to_sleep):
    sleep(sec_to_sleep)
    return 'Foo is done'

@func_timer(ret_time=True, do_print=False)
def bar(sec_to_sleep):
    sleep(sec_to_sleep)
    return 'bar is done'

foo(3)
bar(3)

Output:

Function timer started

Function foo(3,) {} Took 3.0094 seconds to run

Foo is done
('bar is done', 3.0135743618011475)

Metadata

Release files for py-basic-commands 0.2.24

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-basic-commands 0.2.24
File Size Uploaded
py_basic_commands-0.2.24.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-basic-commands 0.2.24
File Interpreter ABI Platform
py_basic_commands-0.2.24-py3-none-any.whl Python 3 none any Details

Total release size: 43.6 kB

Release files / py_basic_commands-0.2.24.tar.gz

Download URL py_basic_commands-0.2.24.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2cd2806924bee93dbb5b2aafee0b466e1bf0f971c0e6689bd340a31272e61f9c
BLAKE2b-256 checksum
How to use checksums
92806d4e2f50c261cc4cb5fa4353ccd45f1d7b7602b143ddded759879ad0a6e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / py_basic_commands-0.2.24-py3-none-any.whl

Download URL py_basic_commands-0.2.24-py3-none-any.whl
Size 25.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57c86147f50662f2495c1fe465ebf310721a8422e425cfa42652d842db841286
BLAKE2b-256 checksum
How to use checksums
67301e118bd26b314094e702f30d3bdd70ae02f14d556c5332def8d5854166a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.2.24 This release

2 release files

0.2.23

2 release files

0.2.1

2 release files

0.1.61

2 release files

0.1.60

2 release files

0.1.55

2 release files

0.1.53

2 release files

0.1.52

2 release files

0.1.51

2 release files

0.1.41

2 release files

0.1.32

2 release files

0.1.31

2 release files

0.1.21

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page