DocoptLanguageError: "usage:" (case-insensitive) not found - docopt

I'm attempting to replicate the notebook located here. However, I keep receiving the read-error:
DocoptLanguageError: "usage:" (case-insensitive) not found.
Docopt documentation: http://docopt.org/
Do you have a suggestion on how I can solve this?
I've tried installing the latest version: docopt-0.6.2.tar.gz.
if __name__ == "__main__":
opt = docopt(__doc__)
print(opt)
if opt["download"] and opt["index"]:
download_and_extract_index(opt)
elif opt["download"] and opt["10k"]:
download_10k(opt)
elif opt["extract"] and opt["mda"]:
extract_mda(opt)

This error might occur because the 'usage' keyword is missing in the programs doc string.
On the the docopt.org webpage it says:
Text occuring between keyword usage: (case-insensitive) and a visibly empty line is interpreted as list of usage patterns. The first word after usage: is interpreted as the program's name.
You need at least one usage pattern. If you add one, the error goes away.
"""
usage:
myprog.py download
"""
from docopt import docopt
if __name__ == "__main__":
opt = docopt(__doc__)
print(opt)
if opt["download"] and opt["index"]:
download_and_extract_index(opt)
elif opt["download"] and opt["10k"]:
download_10k(opt)
elif opt["extract"] and opt["mda"]:
extract_mda(opt)
It can be a hard getting started with docopt, because you'll have to think the other way around compared to argparse for example. With docopt it is the doc string the decides how the arguments should be parsed, and not so much the code.

Related

What is the '...' in sklearn.feature_extraction.text TfidfVectorizer return output?

When I used TfidfVectorizer to encode a paragraph, I receive the output that include '...' inside, this look like the matrix is too large and python cut off something. This problem happens occasionally, but not always, and cause error in another code because '...' has no meaning.
I run in on Conda with Python 36
My source code is like this:
vectorizer = TfidfVectorizer(stop_words='english',use_idf=True)
# print(data)
X = vectorizer.fit_transform(data).todense()
with open("spare_matrix.txt","w") as file:
file.write(str(X))
file.close()
Please suggest me some ideas for that with thanks

Is there any equivalent command for !findstack to filter the managed code?

I found the !findstack just can be used to filter unmanaged code, however it failed to filter managed code, so is there any equivalent command for !findstack to filter the managed code ?
I don't know of a ready-to-use function like this, so I see two options here:
fiddle around with some WinDbg internal commands (which you can hardly understand half a year later)
use PyKd and write a nice script in Python
Approach a)
This may look similar to this:
~*e .foreach(word {!clrstack}) {.if ($spat("${word}", "?*RunMessageLoop?*") == 1) {.printf "Found!\n"}}
Approach b)
Put the following into a file called clrfindstack.py
from pykd import *
import sys
if len(sys.argv) == 1: # script name only
print "Please provide a search string as argument"
exit()
threads = getNumberThreads()
for thread in range(0, threads):
dbgCommand("~"+str(thread)+"s") # select the thread
stack = dbgCommand("!clrstack") # run !clrstack
if sys.argv[1] in stack: # [0] is the script name
print "Found", sys.argv[1], "in thread", thread, "(Use ~"+str(thread)+"s to select it)"
And then run it
0:000> !py c:\tmp\clrfindstack.py
Please provide a search string as argument
0:000> !py c:\tmp\clrfindstack.py RunMessageLoop
Found RunMessageLoop in thread 0 (Use ~0s to select it)
The implementation is probably not very pythonic, but does its job.

Python Main Function Command Line Argument Long List

I'm trying to make a simple main function for a Caesars Shift cryptography program, however I'm not quite sure how to configure all this command line opt/arg stuff. I want to configure the program to accept command line arguments like so:
./caesars.py -e 13 message to encrypt
with 13 being the amount of shifts and the following lines to be the message to encrypt. I have the functions defined, but I am just not sure how to configure the opt arg stuff to accept the first argument after argv[1] to be the key, then everything after that to get split into a list/long string. Here is what I have so far:
def main():
global decoder
global encoder
if not len(sys.argv[1:]):
usage()
# read the commandline options
try:
opts, args = getopt.getopt(sys.argv[1:],"hd:e:", ¬
["help","decode","encode"])
except getopt.GetoptError as err:
print str(err)
usage()
for o,a in opts:
if o in ("-h","--help"):
usage()
elif o in ("-d","--decode"):
decode = True
elif o in ("-e", "--encode"):
encode = True
else:
assert False,"Unhandled Option"
Thanks in advance.
If you haven't already, you should look into docopt. While I have never used it myself, it's very popular and should be perfect for you.

inline iPython call to system

I really like to use system shell commands in iPython. But I was wondering if it is possible to loop over the returned values from a call to e.g. !ls. This works:
files = !ls ./*_subcell_cooc.txt
for f in files:
print f
But this does not:
for f in ( !ls ./*_subcell_cooc.txt):
print f
Error is:
File "<ipython-input-1-df2bc72907d7>", line 5
for f in ( !ls $ROOT/*_subcell_cooc.txt):
^
SyntaxError: invalid syntax
No it is not possible, the syntax var = !something is special cased in IPython. It is not valid python syntax, and we will not extend for loops and so on to work with it.
You can do assignment as you show in your first example, but using glob,os and other real python module to do that will be more robust, not much harder, and also work outside of IPython...
For the anecdote Guido was really not happy with IPython half-shell syntax when he saw it last time at SciPy2013.
(Also it uppercase I in IPython please.)

Spotify Tech Puzzle - stdin in Python

I'm trying to solve the bilateral problem on Spotify's Tech Puzzles. http://www.spotify.com/us/jobs/tech/bilateral-projects/ I have something that is working on my computer that reads input from a file input.txt, and it outputs to ouput.txt. My problem is that I cannot figure out how to make my code work when I submit it where it must read from stdin. I have looked at several other posts and I don't see anything that makes sense to me. I see some people just use raw_input - but this produces a user prompt?? Not sure what to do. Here is the protion of my code that is suposed to read the input, and write the output. Any suggestions on how this might need changed? Also how would I test the code once it is changed to read from stdin? How can I put test data in stdin? The error i get back from spotify says Run Time Error - NameError.
import sys
# Read input
Input = []
for line in sys.stdin.readlines():
if len(line) <9:
teamCount = int(line)
if len(line) > 8:
subList = []
a = line[0:4]
b = line[5:9]
subList.append(a)
subList.append(b)
Input.append(subList)
##### algorithm here
#write output
print listLength
for empWin in win:
print empWin
You are actually doing ok.
for line in sys.stdin.readlines():
will read lines from stdin. It can however be shortened to:
for line in sys.stdin:
I don't use Windows, but to test your solution from a command line, you should run it like this:
python bilateral.py < input.txt > output.txt
If I run your code above like that, I see the error message
Traceback (most recent call last):
File "bilateral.py", line 20, in <module>
print listLength
NameError: name 'listLength' is not defined
which by accident (because I guess you didn't send in that) was the error the Spotify puzzle checker discovered. You have probably just misspelled a variable somewhere.