Decorator for stylized interactive constructor using DSL and parser.
Project description
The argteller package provides the class and method decorators for a self-documenting, highly user-friendly interactive class object constructor. It will ask you for parameters that you need to provide, one by one. It will tell you the options that are available, and ask you for different parameters depending on what you have already provided. You can encode all of these in the custom DSL (domain specific language) script.
Install
pip install argteller
Getting started: topics and parameters
To start using argteller, you must first create a DSL script, either in the form of a text file or a string value. This script contains the names of the parameters as well as other auxiliary information, which we will go over in detail.
The first two, and most basic, pieces of information are: topic and parameter. The name of a topic is just a line of string. The name of a parameter is a line of string preceded by -. Let’s look at an example:
person <--- topic -height <--- parameter -weight -name
We will now apply this DSL script to a Python class. First we need to import the class decorator. Also, we need to put the script either as a string variable or a text file, in which case, we need the path to the file. Let’s assume this variable is string.
from argteller import ArgtellerClassDecorator
@ArgtellerClassDecorator(map_str=string)
class MyClass():
def __init__(self):
print('Finally!')
my_object = MyClass()
Running the above code will parse the script into tree data structure and prompt the user for the required arguments by recursively traversing down this tree, giving appropriate prompts for the given node.
✔ Checking person requirements... Failed! Required argument(s): ► height weight name
Providing an argument for one of the parameter will prompt different set of required arguments:
my_object = MyClass(height=189, name='Tim')
✔ Checking person requirements... Failed! Required argument(s): ► weight
You could have multiple topics:
person <--- topic -height -weight -name other_details <--- 2nd topic -hobby -major
It will check them one by one, providing the user an opportunity to partition and organize the parameters by topics:
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► major
Once you provide all the required parameters, the original __init__ method will be called.
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball', major='math')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Passed! Finally!
At this point, all of the parameters are accessible as object fields, anywhere as if they were always there, including from within the __init__ method as well as any other instance methods.
print(my_object.major)
print(my_object.name)
Available options
You can specify available options by preceding the name with a tab and = character:
person -height -weight -name other_details -hobby =basketball <--- available options =soccer -major
my_object = MyClass(height=189, weight=80, name='Tim')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► hobby major Available [ hobby ] options: ► basketball soccer
Conditional parameters
What if there is a parameter that’s needed only if the hobby is basketball, such as style, that could be indoor or outdoor?
person -height -weight -name other_details -hobby =basketball -style <--- conditional parameter =indoor =outdoor =soccer -major
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► major Required argument(s) for [ basketball ] hobby: ► style Available [ style ] options: ► indoor outdoor
Optional parameters
You could also have optional parameters, which are parameters that you can leave empty and still pass the topic requirement. For these you use + character.
person -height -weight -name +gender <--- you can provide this parameter or not other_details -hobby =basketball -style =indoor =outdoor =soccer -major
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► major Required argument(s) for [ basketball ] hobby: ► style Available [ style ] options: ► indoor outdoor Optional argument(s) for person: ► gender
Boolean parameter conditioned parameter
You could have a parameter whose value is either True or False, and perhaps a conditional parameter depends on this boolean parameter. For these you use ? character. For example:
person -height -weight -name +gender other_details -hobby -major ?has_car <--- only ask for car_brand if this person has_car -car_brand
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball', has_car=True)
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► major Required argument(s) for [ has_car ] option: ► car_brand Optional argument(s) for person: ► gender
String examples of the parameter values
You could provide example for the parameter value with explicit string values. For these you use == characters.
person -height -weight -name +gender other_details -hobby -major ?has_car -car_brand =='Toyota', 'BMW', 'Tesla' <--- string examples of the value
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='basketball', has_car=True)
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Failed! Required argument(s): ► major Required argument(s) for [ has_car ] option: ► car_brand Examples for [ car_brand ]: 'Toyota', 'BMW', 'Tesla' Optional argument(s) for person: ► gender
Conditional parameter value assignement
Lastly, you can assign a value to a parameter depending on which available option argument was chosen:
person -height -weight -name +gender other_details -hobby =basketball -style =indoor =outdoor =soccer =coding -major:'comp-sci' -major
my_object = MyClass(height=189, weight=80, name='Tim',
hobby='coding')
✔ Checking person requirements... Passed! ✔ Checking other_details requirements... Passed! Optional argument(s) for person: ► gender Finally!
This object now has comp-sci string value assigned to major field because we chose coding option for the hobby parameter:
print(my_object.major) # comp-sci
Subtopics
In case there are too many parameters to cover under one topic, but you feel reluctant to branch it out into a separate topic, you have an option of using subtopic. The subtopic only differs in that it adds a tab.
main_topic sub_topic1 -param1 -param2 sub_topic2 -param3 -param4
my_object = MyClass(param1=1, param2=2)
✔ Checking main_topic requirements... ⮑ sub_topic1 requirements... Passed! ⮑ sub_topic2 requirements... Failed! Required argument(s): ► param3 param4
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 argteller_orig-0.0b21.tar.gz
.
File metadata
- Download URL: argteller_orig-0.0b21.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74bbfb49e7f23143df8a8e0da207d87d71aeb3c3e050721b4522e9dd2b2dec40 |
|
MD5 | cd70eb6b6e7a3107ed2eb577fcd5d1ff |
|
BLAKE2b-256 | b6cea934e1658a8a62a841fe2ce2aa0f0bfced389ab337eef9d0c37417ddedd5 |
File details
Details for the file argteller_orig-0.0b21-py3-none-any.whl
.
File metadata
- Download URL: argteller_orig-0.0b21-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c42b3ba4ff804d183e9e3b39c7155e0c0841037577fa3345781c9c58eec41690 |
|
MD5 | 22c04b31ed6254b82f732874914eeed7 |
|
BLAKE2b-256 | 27dea4c15859410086bae6d1e27e8a7173644642096110b1892da6e0fc2a2740 |