A set of made for CLI interfaces in Python
Project description
yodaTools
A group of tools I programmed that I use regularly. Public because why not, exists because I was tired of scrolling through repos to copy/paste the same code again and again.
To see examples, check out examples.py
Install
Since I didn't want to copy git repos into other projects, I made a pip package
$ pip install yodas
Functions
camelCaseSplit
>>> from yodas import camelCaseSplit
>>> camelCaseSplit(string)
This function takes a string in camel case form and returns it in "normal" form
For example helloWorld
results in Hello World
string:
str
returns:
str
snakeCaseSplit
>>> from yodas import snakeCaseSplit
>>> snakeCaseSplit(string)
This function takes a string in snake case form and returns it in "normal" form
For example hello_world
results in Hello World
string:
str
returns:
str
caseSplit
>>> from yodas import caseSplit
>>> caseSplit(string)
This function takes a string in camel case or snake case form and returns it in "normal" form
For example helloWorld
and hello_world
both results in Hello World
string:
str
returns:
str
Classes
Menu
This creates a CLI menu based on the inputs.
Inputs
from yodas import Menu
Menu(items, title=None, subtitle=None, execute=True)
items:
list
(required)title:
str
subtitle:
str
execute:
bool
returns:
Menu
items
When inputting a [dictionary]
, it will assume that the key is the title of the object as a string, the value can be any data type, even functions
title
If not none, will display everytime menu.select()
is called.
subtitle
If not none, will display everytime menu.select()
is called.
execute
By default it is True
, if a function is inputted in items, it will automatically execute it when selected
Usage
>>> from yodas import Menu
>>> menu = Menu(["hello"])
To run the CLI
>>> menu.select()
====================
0 : hello
Choose an option:
Inputting 0
, the function menu.select()
will return 'hello'
>>> menu.add(exit)
adding pythons built-in exit
to the menu. Running menu.select()
again.
>>> menu.select()
====================
0 : hello
1 : Quit
Choose an option:
Selecting 1
results in executing exit()
automatically. To stop automatically running functions
>>> menu.setExecute(False)
This can also be setup upon menu creation
>>> menu = Menu([exit, "hello"], execute=False)
To add a custom label, just use a dictionary
>>> menu.add({"Exit Python": exit})
select
>>> Menu.select()
Will open a CLI interface where the user can select an option
It will return any data type given to upon creating the Menu
instance
returns:
any
add
>>> Menu.add(item)
See items
variable in creating a Menu instance. Adds a new item to the menu. Returns bool on whether operation was successful
item:
any
returns:
bool
setExecute
>>> Menu.setExecute(execute)
Sets the auto execute on the menu which dictates whether functions should be automatically executed when it is selected. Returns bool on whether operation was successful
execute:
bool
returns:
bool
Yoda
This manages JSON files in a safe manner. When creating an object, the path does not have to exist. If the json file does not exist, Yoda will automatically create one using the keys
as reference.
For each key
given upon creation, Yoda will ask the user in a user-friendly manner, what the data is.
Inputs
from yodas import Yoda
Yoda(path, keys=[])
path:
str
(required)keys:
list
*returns:
Yoda
path This is the path to the JSON file
keys This is a list of keys that will be in the JSON file. If the JSON file does not exist, Yoda will ask the user to fill out the JSON file based on the provided keys. This is used by me personally to write scripts that require authentication. For example in telegram, I don't want to have my first commits to have the telegram token.
Usage
To implement a Yoda object
>>> from yodas import Yoda
>>> yoda = Yoda("file.json", ["ping!"])
Executing this will result in
For each key, give its required value
ping!:
Entering pong!
will save it to the JSON file. To check this, run
>>> yoda.contents()
{'ping!': 'pong!'}
To doublecheck this
$ cat file.json
{
"ping!": "pong!"
}%
contents
>>> Yoda.contents()
Will return the contents of the JSON file, reading from the disk everytime.
returns:
dict
write
>>> Yoda.write(contents)
This will overwrite the existing JSON file with the variable contents
. Returns bool
if the operation was successful.
contents:
dict
returns:bool
delete
>>> Yoda.delete()
Will delete the JSON file associated with the Yoda instance. Returns True if it was successful, False if nothing was deleted.
returns:
bool
getPath
>>> Yoda.getPath()
Is used to get the path where the JSON file is stored.
returns:
str
setPath
>>> Yoda.setPath(path)
This used to set the path
path:
str
TODO
- In Yoda class, add option to have other variables like a
key: list
, orkey: dict
rather than juststr: str
- In Yoda class, add option to have custom questions, similar to what is implemented in Menu()
- In Menu class, add argument management
- Fix camelCaseSplit() where
thisIsATest
results inThis Is ATest
- Add multiple language support
Changelog
1.3.1
Adding return variables to all functions.
- Majorly reformatted README.md
Yoda.delete()
now returnsbool
depending on whether the operation was successful or not.Yoda.__setPath()
now returnsbool
if successfulYoda.write()
returnsbool
Menu.setExecute()
returnsbool
Menu.add()
returnsbool
1.3.0
- Major update to README.md
- In Yoda() changed name of function
open()
to__open()
- In Yoda() changed name of function
setPath()
to__setPath()
- In Menu() added new function
setExecute()
1.2.0
- Added full support to store items other than functions
- In Menu() added support to add new items after menu creation using Menu.add()
- In Menu() moved code to add items to Menu.add()
1.1.1
- Added support to create empty Yoda
Yoda("empty.json")
will create a JSON with{}
- Fixed issue where even in empty Yoda files, it will ask for the details
1.1.0
- Added support for string only menus
- Changed Menu.show() to Menu.select()
- Fixed issue where camelCaseSplit() wouldn't detect words with a length of 2
- Fixed issue whereupon creating Yoda, it won't ask for details until contents() is called
- Fixed issue in Yoda where if the path doesn't exist, Yoda will crash
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file yodas-1.3.1.tar.gz
.
File metadata
- Download URL: yodas-1.3.1.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bad61f540a8bd10ef1af77f55e2bf217550d53e1486acabf1c279f3b13119eda |
|
MD5 | 20f55b4e1991685ea7519dba14b7b2e3 |
|
BLAKE2b-256 | 1087f4625b2a9486b1226402ff8865f689e7080b97d430d85845404f481e182a |
File details
Details for the file yodas-1.3.1-py3-none-any.whl
.
File metadata
- Download URL: yodas-1.3.1-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01198907b13d229bb8480b2acf4dbe2c74db1731e007cbca186f962a2acf23d4 |
|
MD5 | 88859236fdefb4959bffe2fcf68908f8 |
|
BLAKE2b-256 | 34cee3265efa8021db7d4e3b2da1d169df209dd02b83eca58519233dc8e7092e |