py-basic-commands


Namepy-basic-commands JSON
Version 0.2.24 PyPI version JSON
download
home_pagehttps://github.com/RasseTheBoy/Py_Basic_Commands
SummaryBasic tools for Python
upload_time2023-05-31 00:39:06
maintainer
docs_urlNone
authorRasmus Ohert
requires_python>=3.7
license
keywords python tool help
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
Py Basic Commands
==================

<p align="center">
    <img src="https://raw.githubusercontent.com/RasseTheBoy/Py_Basic_Commands/main/Logo/py_basic_commands.png" width=300>
</p>

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

[![GitHub release (latest by date)](https://img.shields.io/github/v/release/RasseTheBoy/Py_Basic_Commands?style=flat-square)](https://github.com/RasseTheBoy/Py_Basic_Commands/releases/latest)
![Working status](https://img.shields.io/badge/status-working-success?style=flat-square)
[![Working Python version](https://img.shields.io/badge/Python-%2B3.7-informational?style=flat-square&logo=python)](https://www.python.org/)
![PyPI - License](https://img.shields.io/pypi/l/Py_Basic_Commands?style=flat-square)

# Author

- [@RasseTheBoy](https://github.com/RasseTheBoy)

# Installation

Install using pip

```bash
  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

## Links to functions

- [fprint()](#fprint)
- [finput()](#finput)
- [enter_to_continue()](#enter_to_continue)
- [choose_from_list()](#choose_from_list)
- [read_file()](#read_file)
- [create_file_dir()](#create_file_dir)
- [remove_file_dir()](#remove_file_dir)
- [get_dir_path_for_file()](#get_dir_path_for_file)
- [join_path()](#join_path)
- [try_traceback()](#try_traceback)
- [func_timer()](#func_timer)


## fprint()

- Customizable input function

```python
>>> 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

```python
>>> 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

```py
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

```py
>>> 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

```py
>>> 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

```py
>>> 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

```py
>>> 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

```py
>>> 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()`

```py
>>> join_path('hello', 'world')
'hello\\world'
>>> join_path('hello', 'world', join_with='/')
'hello/world'
```
## try_traceback()

Code:
```python
@try_traceback()
def foo():
    raise Exception('foo')

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

foo()
bar()
```

Output:
```python
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:
```py
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:
```py
Function timer started

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

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/RasseTheBoy/Py_Basic_Commands",
    "name": "py-basic-commands",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "python,tool,help",
    "author": "Rasmus Ohert",
    "author_email": "Rasmus Ohert <rassemichael@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/92/80/6d4e2f50c261cc4cb5fa4353ccd45f1d7b7602b143ddded759879ad0a6e7/py_basic_commands-0.2.24.tar.gz",
    "platform": null,
    "description": "\nPy Basic Commands\n==================\n\n<p align=\"center\">\n    <img src=\"https://raw.githubusercontent.com/RasseTheBoy/Py_Basic_Commands/main/Logo/py_basic_commands.png\" width=300>\n</p>\n\nA package with some basic tools and commands for Python (+3.7)\n\n[![GitHub release (latest by date)](https://img.shields.io/github/v/release/RasseTheBoy/Py_Basic_Commands?style=flat-square)](https://github.com/RasseTheBoy/Py_Basic_Commands/releases/latest)\n![Working status](https://img.shields.io/badge/status-working-success?style=flat-square)\n[![Working Python version](https://img.shields.io/badge/Python-%2B3.7-informational?style=flat-square&logo=python)](https://www.python.org/)\n![PyPI - License](https://img.shields.io/pypi/l/Py_Basic_Commands?style=flat-square)\n\n# Author\n\n- [@RasseTheBoy](https://github.com/RasseTheBoy)\n\n# Installation\n\nInstall using pip\n\n```bash\n  pip install py_basic_tools\n\n  pip3 install py_baisc_tools\n```\n\n# Table of input variables\n\n| Variable | Usage | Type |\n| :---: | :---: | :---: |\n| text | The text to be printed | str |\n| nl | Whether to print a newline or not | bool |\n| flush | Whether to flush the output print or not | bool |\n| do_print | Whether to print the text or not | bool |\n| use_end_addon | Whether or not to add a colon to the end of the text | bool |\n| file_path | The path of the file to be read | str |\n| create | If True, the file/directory will be created if it does not exist | bool |\n| force | If True, the file/directory will be created even if it exists | bool |\n| remove_empty | If True, empty lines will be removed from list | bool |\n| do | Action for directory or file | `'dir'`/`'file'` |\n| ret_type | Variable type to return | type |\n| ret_var | Variable to return | str |\n\n# Examples\n\n## Links to functions\n\n- [fprint()](#fprint)\n- [finput()](#finput)\n- [enter_to_continue()](#enter_to_continue)\n- [choose_from_list()](#choose_from_list)\n- [read_file()](#read_file)\n- [create_file_dir()](#create_file_dir)\n- [remove_file_dir()](#remove_file_dir)\n- [get_dir_path_for_file()](#get_dir_path_for_file)\n- [join_path()](#join_path)\n- [try_traceback()](#try_traceback)\n- [func_timer()](#func_timer)\n\n\n## fprint()\n\n- Customizable input function\n\n```python\n>>> fprint('Hello World')\nHello World\n\n>>> fprint('Hello World', nl=False)\nHello World\n>>> fprint()\n\n>>> fprint('Hello World', do_print=False)\n\n```\n\nThe above fprint() examples are very basic. But you could implement and customise it to your liking.\n## finput()\n\n- Customizable input function\n\n```python\n>>> finput('Enter a number', ret_type=int)\nEnter a number: 5\n\n5\n>>> finput('Enter a number', ret_type=int)\nEnter a number: 5.5\n\nCouldn't return input 5.5 as <class 'int'>\nInput type: <class 'str'>\nReturning value as string\n\n'5.5'\n>>> finput('Enter a number', ret_type=int)\nEnter a number: five\n\nCouldn't return input five as <class 'int'>\nInput type: <class 'str'>\nReturning value as string\n\n'five'\n```\n## enter_to_continue()\n\n- Wait until user presses enter to continue\n- Returns:\n    - `True`: No input was given, only enter was pressed\n    - `False`: Something was written before pressing enter\n\n```py\nfrom py_basic_tools import enter_to_continue\n\n>>> enter_to_continue()\nPress enter to continue...\n\n>>> enter_to_continue('Waiting')\nWaiting (enter to continue)\n\n>>> enter_to_continue('Waiting', nl=False)\nWaiting (enter to continue)\n>>> enter_to_continue('Waiting', use_help_text=False)\nWaiting\n\n>>> enter_to_continue(use_help_text=False)\n\n```\n## choose_from_list()\n\n- Choose one or more variables from user given list\n- When choosing more than one, add a space between chosen indexes\n- Returns:\n    - `list of variables`: If variables were chosen correctly\n    - `empty list`: If an `exception` occured\n\n```py\n>>> lst = ['foo', 'bar']\n>>> choose_from_list(lst)\n---Choose 1 value---\n(0) foo\n(1) bar\n\nInput index: 0\n'foo'\n>>> choose_from_list(lst, choose_total=2, start_num=1)\n---Choose 2 values---\n(1) foo\n(2) bar\n\nInput index: 1 2\n['foo', 'bar']\n>>> choose_from_list(lst, start_num=3, choose_until_correct=True)\n---Choose 1 value---\n(3) foo\n(4) bar\n\nInput index: 0\n\nGiven index is out of range\nList length: 2    Input index: 0\n\nInput index: 3\n\nfoo\n>>> choose_from_list(lst, choose_total=2, start_num=1, choose_until_correct=False)\n---Choose 2 values---\n(1) foo\n(2) bar\n\nInput index: 0 1\nGiven index is out of range\nValue has to be between: 1-2\n[]\n>>> choose_from_list(lst, header_text='Choose what you will', header_nl=True, input_text='Input what you will')\nChoose what you will\n\n(0) foo\n(1) bar\n\nInput what you will: 0\n\n['foo']\n```\n## read_file()\n\n- Function tries to read the file, and if it fails, it will create the file if the `create` argument is True.\n- If the file is read successfully, the function will return a list of lines from the file\n\n```py\n>>> read_file('path/to/file.txt')\nFile not found: path/to/file.txt\n\nNone\n>>> read_file('path/to/file.txt', do_print=False)\nNone\n>>> read_file('path/to/file.txt', create=True)\nFile created: path/to/file.txt\n\n>>> read_file('path/to/file.txt')\n['hello', 'world', 'foo bar']\n\n>>> read_file('path/to/file.txt', remove_empty_lines=False)\n['hello', 'world', '', 'foo bar']\n```\n## create_file_dir()\n\n- Creates a file or directory to the given path\n\n```py\n>>> create_file_dir('file', 'path/to/file.txt')\nFile created: path/to/file.txt\n\n>>> create_file_dir('file', 'path/to/file.txt')\nFile already exists: path/to/file.txt\n\n>>> create_file_dir('file', 'path/to/file.txt', do_print=False)\n\n>>> create_file_dir('file', 'path/to/file.txt', force=True)\nFile created: path/to/file.txt\n\n>>> create_file_dir('dir', 'path/to/new directory')\nDirectory created: path/to/new directory\n\n```\n## remove_file_dir()\n\n- Removes a file or directory to the given path\n\n```py\n>>> remove_file_dir('file', 'path/to/file.txt')\nFile removed: path/to/file.txt\n\n>>> remove_file_dir('file', 'path/to/another_file.txt')\nFile is not empty, not removing: file.txt\n\n>>> remove_file_dir('file', 'path/to/another_file.txt', force=True)\nFile removed: path/to/another file.txt\n\n>>> remove_file_dir('file', 'path/to/another_file.txt')\nFile not found: path/to/another file.txt\n\n>>> remove_file_dir('dir', 'path/to/directory')\nDirectory path not found: path/to/directory\n```\n## get_dir_path_for_file()\n\n- Gets the path to the direcotry where given file is\n- Returns: \n    - `dir_path`: Paht to the directory\n    - `filename`: Name of the file\n\n```py\n>>> get_dir_path_for_file('path/to/file.txt')\n('path/to', 'file.txt')\n>>> get_dir_path_for_file('path/to/file.txt', ret_var='filename')\n'file.txt'\n>>> get_dir_path_for_file('path/to/file.txt', ret_var='dir')\n'path/to'\n```\n## join_path()\n\n- Joins given paths together\n    - Like `os.path.join()`\n\n```py\n>>> join_path('hello', 'world')\n'hello\\\\world'\n>>> join_path('hello', 'world', join_with='/')\n'hello/world'\n```\n## try_traceback()\n\nCode:\n```python\n@try_traceback()\ndef foo():\n    raise Exception('foo')\n\n@try_traceback(skip_traceback=True)\ndef bar():\n    raise Exception('bar')\n\nfoo()\nbar()\n```\n\nOutput:\n```python\nTraceback (most recent call last):\n    File \"test.py\", line 13, in wrapper\n        return func(*args, **kwargs)\n    File \"test.py\", line 8, in foo\n        raise Exception('foo')\n\n```\n\n`bar()` doesn't raise an exception, because traceback was skipped.\n## func_timer()\n\n- The decorator can be used to time any function, and it will print the time it took to run the function\n- Returns:\n    - `ret_val`: Values to return from the function\n    - `time_delta`: The time it took for the function to run\n\nCode:\n```py\nfrom time import sleep\n\n@func_timer()\ndef foo(sec_to_sleep):\n    sleep(sec_to_sleep)\n    return 'Foo is done'\n\n@func_timer(ret_time=True, do_print=False)\ndef bar(sec_to_sleep):\n    sleep(sec_to_sleep)\n    return 'bar is done'\n\nfoo(3)\nbar(3)\n```\n\nOutput:\n```py\nFunction timer started\n\nFunction foo(3,) {} Took 3.0094 seconds to run\n\nFoo is done\n('bar is done', 3.0135743618011475)\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Basic tools for Python",
    "version": "0.2.24",
    "project_urls": {
        "GitHub": "https://github.com/RasseTheBoy/Py_Basic_Commands",
        "Homepage": "https://github.com/RasseTheBoy/Py_Basic_Commands"
    },
    "split_keywords": [
        "python",
        "tool",
        "help"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "67301e118bd26b314094e702f30d3bdd70ae02f14d556c5332def8d5854166a4",
                "md5": "410ad8e1ed49e31353be6a227f14e088",
                "sha256": "57c86147f50662f2495c1fe465ebf310721a8422e425cfa42652d842db841286"
            },
            "downloads": -1,
            "filename": "py_basic_commands-0.2.24-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "410ad8e1ed49e31353be6a227f14e088",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 25507,
            "upload_time": "2023-05-31T00:39:04",
            "upload_time_iso_8601": "2023-05-31T00:39:04.434658Z",
            "url": "https://files.pythonhosted.org/packages/67/30/1e118bd26b314094e702f30d3bdd70ae02f14d556c5332def8d5854166a4/py_basic_commands-0.2.24-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92806d4e2f50c261cc4cb5fa4353ccd45f1d7b7602b143ddded759879ad0a6e7",
                "md5": "8f15d8dd5f47777711a65595bb4b8c6b",
                "sha256": "2cd2806924bee93dbb5b2aafee0b466e1bf0f971c0e6689bd340a31272e61f9c"
            },
            "downloads": -1,
            "filename": "py_basic_commands-0.2.24.tar.gz",
            "has_sig": false,
            "md5_digest": "8f15d8dd5f47777711a65595bb4b8c6b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 18066,
            "upload_time": "2023-05-31T00:39:06",
            "upload_time_iso_8601": "2023-05-31T00:39:06.339628Z",
            "url": "https://files.pythonhosted.org/packages/92/80/6d4e2f50c261cc4cb5fa4353ccd45f1d7b7602b143ddded759879ad0a6e7/py_basic_commands-0.2.24.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-31 00:39:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "RasseTheBoy",
    "github_project": "Py_Basic_Commands",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-basic-commands"
}
        
Elapsed time: 0.44240s