跳转到主要内容

通过命令行即时获取编码答案

项目描述

这是一个Benjamin Gleitzman的出色工具Howdoi的分支。

它已被扩展以支持使用Elasticsearch作为后端搜索服务器的本地索引缓存。这允许更快地搜索,并能够通过本地YAML文件添加自定义答案注释和文档。

当我意识到howdoi非常适合在需要查找更常见的一行代码时,我发现了这个修改。在这种情况下,我发现howdoi提供的解决方案仍然需要我做更多的研究并写笔记,这些笔记我需要存储起来以便日后参考。

我的解决方案是将我的笔记组织在YAML文件中,使用Elasticsearch索引此文件,并修改howdoi首先引用我的Elasticsearch索引。

通过命令行即时获取编码答案

你是黑客程序员吗?你是否总是不断在谷歌上搜索如何执行基本的编程任务?

假设你想知道如何在bash中格式化日期。为什么打开浏览器并阅读博客(冒着重大分心的风险),而你可以在控制台中简单地询问howdou

$ howdou format date bash
> DATE=`date +%Y-%m-%d`

howdou将回答各种查询

$ howdou print stack trace python
> import traceback
>
> try:
>     1/0
> except:
>     print '>>> traceback <<<'
>     traceback.print_exc()
>     print '>>> end of traceback <<<'
> traceback.print_exc()

$ howdou convert mp4 to animated gif
> video=/path/to/video.avi
> outdir=/path/to/output.gif
> mplayer "$video" \
>         -ao null \
>         -ss "00:01:00" \  # starting point
>         -endpos 10 \ # duration in second
>         -vo gif89a:fps=13:output=$outdir \
>         -vf scale=240:180

$ howdou create tar archive
> tar -cf backup.tar --exclude "www/subf3" www

安装

首先,安装Elasticsearch。在Ubuntu上,这很简单

sudo apt-get install default-jre elasticsearch

或者

cd /tmp
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.0.0/elasticsearch-2.0.0.deb
sudo dpkg -i elasticsearch-2.0.0.deb
sudo service elasticsearch start

启用内联脚本搜索

sudo sh -c "echo 'script.engine.groovy.inline.search: on' >> /etc/elasticsearch/elasticsearch.yml"
sudo service elasticsearch restart

您可能还需要启用服务在启动时自动运行

sudo update-rc.d elasticsearch defaults
sudo update-rc.d elasticsearch enable

请确保ElasticSearch的版本与您虚拟环境中安装的elasticsearch Python包的版本相匹配。

然后使用以下命令通过pip安装howdou:

pip install howdou

或者

pip install git+https://github.com/chrisspen/howdou.git#egg=howdou

或者

python setup.py install

https://elasticsearch-py.readthedocs.org/en/master/

用法

命令行非常直接

usage: howdou.py [-h] [-p POS] [-a] [-l] [-c] [-n NUM_ANSWERS] QUERY [QUERY ...]

instant coding answers via the command line

positional arguments:
  QUERY                 the question to answer

optional arguments:
  -h, --help            show this help message and exit
  -p POS, --pos POS     select answer in specified position (default: 1)
  -a, --all             display the full text of the answer
  -l, --link            display only the answer link
  -c, --color           enable colorized output
  -n NUM_ANSWERS, --num-answers NUM_ANSWERS
                        number of answers to return
  -C, --clear-cache     clear the cache

为了充分利用howdou,您需要维护一个本地的howdou.yml文件,这是一个简单的QA集序列化列表,如下所示

-   questions:
    -   format date bash
    answers:
    -   weight: 1
        date: 2014-5-14
        source: http://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script
        formatter: bash
        text: |-
            DATE=`date +%Y-%m-%d`

注意每个条目都是许多问题与许多答案的关联。这是因为有许多方式可以提出相同的问题,我们希望索引尽可能有可能正确匹配您的问题到答案。

还有一个显式的权重值,这将纳入Elasticsearch自身的搜索权重,以控制结果的显示顺序。

要自动每5分钟重新索引您的更改,运行以下命令:

crontab -e

并添加以下行

# Do a quick frequent update.
*/5 * * * * . /home/yourusername/.bash_aliases; howdou --action=reindex

# Do a slower but more thorough update less frequently.
0 6 * * * . /home/yourusername/.bash_aliases; howdou --action=reindex --force

Elasticsearch

Elasticsearch后端的注意事项是默认配置可能是一个非常耗内存的配置。

您可能需要编辑/etc/elasticsearch/jvm.options,并大幅减少XmsXmx的值。

开发

测试需要安装Python开发头文件,您可以在Ubuntu上使用以下命令安装:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python-dev python3-dev python3.4-minimal python3.4-dev python3.5-minimal python3.5-dev pandoc

要运行所有测试

export TESTNAME=; tox

要为特定环境(例如Python 2.7)运行测试

export TESTNAME=; tox -e py27

要运行特定的测试

export TESTNAME=:HowdouTestCase.test_unicode_answer; tox -e py27

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

howdou-3.0.6.tar.gz (15.0 kB 查看哈希值)

由以下机构支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF 赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面