cheat.sh
cheat.sh copied to clipboard
Get more, or next stackoverflow answer.
use case and feature request
-
I'm in my editor and I want instructions for root mean squared error in python, so I press my keystroke to package my selected string:
rmseto the cht.sh servers like this:curl cht.sh/python/rmse?T
-
It responds with the selected answer from here: https://stackoverflow.com/questions/17197492/is-there-a-library-function-for-root-mean-square-error-rmse-in-python
-
That's fine, now this selected answer is kind of trash (not your fault) and the next highest-upvoted but not-selected answer is great: https://stackoverflow.com/a/37861832/445131
-
Is there a way I can tell your server to: "Get N more stackoverflow answers on this page?" or "Get the next stackoverflow answer on this stackoverflow page"? I could even give you search term like the:
so/q/17197492
Attempting to solve my own problem my way, I saw: https://github.com/dbeniamine/cheat.sh-vim has a <leader>KAN Next Answer keystroke, but that's not the next stackoverflow answer, that's the last/next query.
What I have to do now is take my hand off the keyboard and visit stackoverflow by that questionid, and then use the mouse to scroll down, which is doubleplusungood.
How would you do this so I can keep my fingers on home row; my right hand not taking the thousand-year journey to the GUI-land.
Hi,
I tryied to reproduce the behavior you describe, : :Cheat! rmse in a python file I indeed got the first answer of the same topic, but after hitting <leader>KAN I have the answer you are looking for.
I think you miss understood the navigation in cheat.sh-vim (perhaps the doc is poorly written):
KA[N|P]Navigate through cht.sh answers for the same questionKQ[N|P]Navigate through cht.sh questions for the same queryKS[N|P]Navigate through cht.sh see also for the same queryKH[N|P]Navigate through your local history of cht.sh navigation
@dbeniamine David, thank you very much for your comment. I think that is exactly what Eric @sentientmachine is looking for.
What I could add here, is that when you are using cheat.sh without the cheat.sh vim plugin, but querying it directly, you can use the /question/answer notation to access other answers and other questions.
For example, to access the second answer (to the same question):
$ curl cht.sh/python/rmse/0/1?Q
import numpy as np
d = [0.000, 0.166, 0.333] #ideal target distances, these can be all zeros.
p = [0.000, 0.254, 0.998] #your performance goes here
print("d is: " + str(["%.8f" % elem for elem in d]))
print("p is: " + str(["%.8f" % elem for elem in p]))
def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())
rmse_val = rmse(np.array(d), np.array(p))
print("rms error is: " + str(rmse_val))
d is: ['0.00000000', '0.16600000', '0.33300000']
p is: ['0.00000000', '0.25400000', '0.99800000']
rms error between lists d and p is: 0.387284994115
def rmse(predictions, targets):
differences = predictions - targets #the DIFFERENCEs.
differences_squared = differences ** 2 #the SQUAREs of ^
mean_of_differences_squared = differences_squared.mean() #the MEAN of ^
rmse_val = np.sqrt(mean_of_differences_squared) #ROOT of ^
return rmse_val #get the ^
8 - 5 = 3 #absolute distance between 8 and 5 is +3
-20 - 10 = -30 #absolute distance between -20 and 10 is +30
3*3 = 9 = positive
-30*-30 = 900 = positive
# [Eric Leschinski] [so/q/17197492] [cc by-sa 3.0]
(I used ?Q in the query, because the answer contains a lot of textual comments, so I cut them here out.
What I think, David @dbeniamine , what should be really useful, if we could fetch multiple answers/queries simultaneously, and then just browse them using a special minibufer, or maybe open a list of suggestions in some fashion like here:
https://github.com/wincent/command-t
What do you think?
Perfect, that /0/1?Q syntax is exactly what I needed:
curl cht.sh/python/rmse/0/1?Q
Such that the first 0 means: In this stackoverflow page. And the next /1 means: index 1, aka next answer and the ?Q means only show code.
Best software ever thanks.
@sentientmachine Keep in mind Eric, that you do not necessary need ?Q; I've used it here just to hide the comments
What I think, David @dbeniamine , what should be really useful, if we could fetch multiple answers/queries simultaneously, and then just browse them using a special minibufer, or maybe open a list of suggestions in some fashion like here:
Yes I think this is the "Multiple answers" feature that is already implemented in sublime and IDEA. I actually openned an issue almost one year ago but did not do it by pure lazyness ...
I think the most readable way would be to prepare a buffer conainning:
# Question
#### First answer
contents
#### Second answer
contents
# Footer
@chubin What do you think ? Does the answer concatenation could / should be done on server side ?
@sentientmachine : Would that feature be useful to you ?
@dbeniamine Yes here's the workflow that would be ideal for me.
-
I selected the string
rmsein my editor code window, and hit my keystroke that runscurl cht.sh/python/rmseunder the hood and opens the results in a new tab. -
This answer isn't quite good enough so I close
:qthe tab bringing me back to the code window. -
I do another query in my buffer like
rmse?N2that my editor parses into something like curl cht.sh/python/rmse?N2 which would return:# Question #### First answer first answer contents #### Second answer second answer contents # Footer
Now I don't want to hammer your server unnecessarily from people saying: "yeah lol give me 200 answers". So you can encourage responsible behavior by putting in a required 2 second sleep cost for every N greater than say three. That way you're happy to provide 10 answers if you're willing to wait an extra N seconds. Ten answers is about the limit I would consume in one session. If 10 isn't enough, my query needs to be better.
Here's a great page talking about different strategies to stop such abusive behavior at the moat: https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/
In the vim plugin, I think that the smoother approach would be to add both an optional parameter and an optional configuration variable:
let g:CheatSheetNumAnswers=1
So if you type:
:Cheat! how to reverse a list
It will ask for g:CheatSheetNumAnswers (1 in this case) answers, but you can quickly ask for:
:Cheat! how to revers a list 3
And get exactly 3 answers
I'll implement that as soon as agree on what part should be done client side.
Does the other plugin that provides this functionality have rate limitation ?
This ticket is still open, so here's an update on my workflow and thoughts.
The already-provided curl cht.sh/python/rmse/0/1?Q style syntax gives me all the functionality I need to get n answers. So I have some code where I can tell it to "fetch me 3 answers" and then it will go out and do 3 separate curl requests and assemble them. I put a delay between each hit so I don't get rate limited, it discourages requesting 50 hits because of a time delay.
This isolation of unnecessary complexity in get n answers is rightfully shifted to the user, via their custom scripts. And so you don't have to worry about tradeoff in rate limiting a request for 50 answers, vs convenience of passing back 50 answers.
So I'm happy with the functionality. I just have to enumerate the 'get next answer' api syntax. You can close this, or keep it open to periodically revisit.
@sentientmachine Eric, I think your solution is though good, but it would be much better, if we could get 50 (or 20) answers at once (as a JSON structure for example), so it would be possible to search through them. Of course, for that we need some additional support from the client (cht.sh or editor plugins side), but currently we do not have this functionality even on the server side.
The best solution probably should be some combination of the "multiple answers" feature with the "client-side search" feature, so that we could just fetch a set of possible answers first (with some more or less general query) and then refine the search with additional keywords.