How can I create a custom help menu in Fabric? - python

When I run fab --list I get output as
error Call ``func`` with given error ``message``.
get_all_tags
get_head_position
get_latest_patch
get_latest_tag
get_pending_patches
glob Return a list of paths matching a pathname pattern.
handle_prompt_abort
help
indent Return ``text`` indented by the given number of spaces.
this contains several user defined functions like get_all_tags get_head_position etc but without any description.I wish to include the description for these functions as well so that my list looks something like this
error Call ``func`` with given error ``message``.
get_all_tags Returns a list of all available tags
get_head_position Returns the current Head position
get_latest_patch Returns most recently created patch file name
get_latest_tag Returns the most recent tags among all the tags
get_pending_patches Returns list of all patches which are yet to be applied
glob Return a list of paths matching a pathname pattern.
handle_prompt_abort
help
indent Return ``text`` indented by the given number of spaces.
How can I do this ?

Add docstring to the functions.
#task
def get_all_tags():
"This is a docstring ..."
The docstring is used to display help message.
According to fab -l option documentation:
Imports a fabfile as normal, but then prints a list of all discovered
tasks and exits. Will also print the first line of each task’s
docstring, if it has one, next to it (truncating if necessary.)

Related

How we can categorize the testcase to be executed in the user defined order in robotframework?

For example consider the below is the folder structure for my project.
Now I want my testcase to be executed in below order.
TC_101.ABC.robot
TC_201.ABC.robot
TC_301.ABC.robot
TC_102.ABC.robot
TC_103.ABC.robot
TC_203.ABC.robot
TC_202.ABC.robot
How this can be achieved in robotframework? Is there something like testng feature in robotframework using which we can manage the order in which our suites to be executed?
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#execution-order Please refer this link, it executes in alphabetical order or you can prefix a number with two underscore (01__TC_Suite1.robot)
But if you don't want to change anything in the file, please use command line to execute as per your order. Below is the sample screenshot as per your file and i executed the below command in cmd prompt
robot TestSuite1\TC_101_Catenate.robot TestSuite2\TC_102_MergeLsit.robot TestSuite1\TC_103_DateTime.robot TestSuite2\TC_104_SplitLine.robot
The above command can be split into multiple line as well, if required
Please find the output and see the first line, order of execution as per given in command line

Trying to transfer a file name to a different function

I am writing code for class and I'm stuck on just one problem. I'm supposed to use the main function to call to a different function called 'file2list' which will take the file (that I brought from main) and convert it to a list. I have everything needed to create the list, I just can't get the file to run. (also I'm new to programming, so there can also be some silly errors)
This is in my main function
#Call file2list
vt=file2list('vt_municipalities.txt')
nh=file2list('nh_municipalities.txt')
And then this is what my file2list function looks like
def file2list(muni_file):
#Create lists
muni_list=[]
#Open files
file=open('muni_file','r')
Basically, how can I bring the .txt file down to file2list? I get the error that muni_file doesn't exist. Thanks!
Your error is that you are trying to open the file with the name muni_file, as you surrounded it with ', making it a string. To reference the variable that was passed into the function, remove the quotation marks (') surrounding muni_file.
The file=open line should look something like this:
file=open(muni_file,'r')
you are passing to open() a string that contains 'muni_file' as value, to use the parameter that you recieve, you should pass without ' '

Editing every 3rd line of file in vim

I was making a templated list of methods in vim for a python project. I added lines between each method and wanted to add a pass to each method for now-until I implement the method, this will still be interpretable python code. In vim I know how to edit spatially contiguous lines of a file using :10,17s/<search regex>/<substitute>/ but after doing my edits to add empty lines between methods, I needed to insert the a pass every 3rd line. The way I found to do this used pipes and & via:
:10s/<search regex>/<substitute>|13&|16& etc. I had maybe 15 of the ampersands chained together to get this to work. Is there a more succint way to get this behaviour in vim?
To address comment, here is a minimal example, in the file myfile.py I have:
def _fun1(self):
def _fun2(self):
def _fun3(self):
def _fun4(self):
...etc
On the 2nd line, the 5th line, the 8th line, etc. I want to insert pass (w/4 spaces before to keep consistent spacings), /i have this up to _fun15(self): so would like to get the behavior w/o 14 |lineNo&s chained together. Perhaps an incrementing feature w/a variable for the line numbers or some other code that creates the behavior.
Here is one possible way:
:g/def _fun/normal! opass
On each line matching def _fun…
open a new line below…
and insert pass.
If you want to have one single line between each stub:
:g/def _fun/normal! opass^OJ^Ox
On each line matching def _fun…
open a new line below…
insert pass…
leave insert mode for a single command…
join the line below with the current line…
leave insert mode for a single command…
and remove that pesky <Space>.
Record a macro
qajopass<Esc>jq
Now execute it by running #a (next time you can use ##).
As #midor said it can be then used with :g command in form of:
:g/def _fun\d\+/norm #a
To execute this macro on all matching lines.
To put 'pass' with indentation below each function definition I would use:
:g/^def/put =' pass'
^ ........... begining of each line
put ......... puts contents bellow
To squeeze blank lines:
:g/^$/,/./-1j
a global command the gets from each empty line ^$
until next non-empty line minus one, performs a join command

Exclude header comments when generating documentation with sphinx

I am trying to use sphinx to generate documentation for my python package. I have included well documented docstrings within all of my functions. I have called sphinx-quickstart to create the template, filled in the template, and called make html to generate the documentation. My problem is that I also have comments within my python module that are also showing up in the rendered documentation. The comments are not within the functions, but rather as a header at the top of the .py files. I am required to have these comment blocks and cannot remove them, but I don't want them to show up in my documentation.
I'm current using automodule to pull all of the functions out of the module. I know I can use autofunction to get the individual functions one by one, and this avoids the file headers, but I have a lot of functions and there must be a better way.
How can I adjust my conf.py file, or something else to use automodule, but to only pick up the functions and not the comments outside of the functions?
This is what my .py file looks like:
"""
This code is a part of a proj repo.
https://github.com/CurtLH/proj
author: greenbean4me
date: 2018-02-07
"""
def hit(nail):
"""
Hit the nail on the head
This function takes a string and returns the first character in the string
Parameter
----------
nail : str
Can be any word with any length
Return
-------
x : str
First character in the string
Example
-------
>>> x = hit("hello")
>>> print(x)
>>> "h"
"""
return nail[0]
This is my the auto-generated documentation looks like:
Auto-Generated Documentation
This code is a part of a proj repo.
https://github.com/CurtLH/proj
author: greenbean4me date: 2018-02-07
hammer.hit(nail)
Hit the nail on the head
This function takes a string and returns the first character in the string
nail : str
Can be any word with any length
x : str
First character in the string
>>> x = hit("hello")
>>> print(x)
>>> "h"
For a more comprehensive example, check out this example repo on GitHub: https://github.com/CurtLH/proj
As far as I know, there is no way to configure autodoc not to do this.
There is, however, a simple workaround: Adding an empty docstring at the top of your module.
"""""" # <- add this
"""
This code is a part of a proj repo.
https://github.com/CurtLH/proj
author: greenbean4me
date: 2018-02-07
"""
It's barely noticeable, and will trick autodoc into not displaying your module's real docstring.

Using argparse within a class for repeated iterations?

Maybe this isn't the best way to frame my problem. Right now, I have a program that already uses argparse to enter my class in 'manual' mode. So for example, if I type python parser.py --m, I go to Parse(args), which is my class. This all works fine.
Once this is done, the class parses the file for its table of contents list and prints it to the screen. The table of contents is an OrderedDict with the page number as the key and the page title as the value. The idea is that you could press a number and it would print out the text on that respective page, and that you could do this until you type any command that doesn't correspond to a number in the dict.
I was wondering if this would be possible to do with argparse or sys?
args = parser.parse_args() parses sys.argv[1:], the list like structure that the command line produced and gave to the Python interpreter. You can also call parse_args with any similar list of strings.
How to split a string like the shell in python?
ipython uses a modified argparse to handle its main input. It uses the config files to populate the parser, giving the user a last minute way of fiddling with the configuration. But its magic commands also parse their arguments with a form of argparse. For that it has its own REPL, rather than using input/raw_input.

Categories