LoginSignup
107
84

More than 5 years have passed since last update.

Githubにある 「for Humans」 プロジェクト

Posted at

Githubにある 「for Humans」 プロジェクト

githubにあるリポジトリには、そのdescriptionに 「for Humans」 が入っているものがあります。
Pythonの requests というライブラリのdescriptionは 「python HTTP Requests for Humans™」 と書かれており、urllibなどのHTTP Requestを人間が使いやすいように設計されたものです。

つまり 「for Humans」 と書かれたライブラリなどのプロジェクトは、標準ライブラリなどを 人間が使いやすいように設計されたもの であるため、有用なものが多くあります。

この記事ではPythonを使った「for Humans」プロジェクトを紹介したいと思います。

1. keras-team/keras

Deep Learning for humans

Kerasは、TensorFlowなどで実行可能なディープラーニングライブラリ。
人間に使いやすい設計で、kerasで深層学習を始める初心者も多いようです。
日本語ドキュメントが充実してるので参照してください。

2. pypa/pipenv

Python Development Workflow for Humans

nodeは package.json 、rubyは Gemfile でパッケージを管理しています。
Pythonは piprequirements.txt を使ってパッケージ管理をしていますが、Gemfileなどに比べて機能が十分ではありません。

そこでpythonのパッケージ管理の機能を向上させるのがpipenvです。
日本語ドキュメントもありますし、qiitaにも様々な記事があります。

3. kennethreitz/records

SQL for Humans

recordsはSQLを書くためだけに作られたライブラリで、RedShift, Postgres , MySQL, SQLiteなどのDBをサポートしています。

import records

db = records.Database('postgres://...')
rows = db.query('select * from active_users')    # or db.query_file('sqls/active-users.sql')

for r in rows:
    print(r.name, r.user_email)

4. kennethreitz/delegator.py

Subprocesses for Humans 2.0

delegatorはpythonの subprocess を人間が使いやすいように設計されたライブラリで、envoypexpectというライブラリからインスパイアされたものです。

>>> c = delegator.run('ls')
>>> print c.out
README.rst   delegator.py

>>> c = delegator.run('long-running-process', block=False)
>>> c.pid
35199
>>> c.block()
>>> c.return_code
0

個人的にすごく便利だなと思うのは、 パイプを使ったコマンド を簡単に使えるとこです。

>>> c = delegator.chain('fortune | cowsay')
>>> print c.out
  _______________________________________
 / Our swords shall play the orators for \
 | us.                                   |
 |                                       |
 \ -- Christopher Marlowe                /
  ---------------------------------------
         \   ^__^
          \  (oo)\\_______
             (__)\       )\\/\
                 ||----w |
                 ||     ||

これをsubprocessでやると、

>>> from subprocess import Popen
>>> proc1 = Popen("fortune", stdin=-1, stdout=-1, stderr=-1)
>>> proc2 = Popen("cowsay", stdin=proc1.stdout, stdout=-1, stderr=-1)
>>> print proc2.communicate()[0]
   _______________________________________
 / Our swords shall play the orators for \
 | us.                                   |
 |                                       |
 \ -- Christopher Marlowe                /
  ---------------------------------------
         \   ^__^
          \  (oo)\\_______
             (__)\       )\\/\
                 ||----w |
                 ||     || 

このようにプロセスを2つ作らないとできません。

5. kennethreitz/maya

Datetimes for Humans

mayaはpythonの datetime を使いやすくしたライブラリです。
通常のdatetimeでは、タイムゾーンを設定するには pytz という専用のライブラリを使って以下のように設定します。

>>> from pytz import timezone
>>> from datetime import datetime
>>> jst_now = datetime.now().astimezone(timezone('Asia/Tokyo'))
>>> print(jst_now)
2016-03-20 10:07:29.056724+09:00

しかし、mayaを使うと…

>>> import maya
>>> now = maya.now()
>>> now.datetime(to_timezone="Asia/Tokyo")
datetime.datetime(2018, 3, 24, 17, 55, 10, 578125, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)

6. kvesteri/validators

Python Data Validation for Humans

validatorsは様々な種類のデータをvaliateしてくれるライブラリです。

>>> import validators
>>> validators.email("sample@gmail.com")
True
>>> validators.between(5, min=2)
True
>>> validators.between(500, max=400)
ValidationFailure(func=between, args=...)
>>> validators.domain('example.com')
True
>>> validators.ipv4("192.168.33.10")
True
>>> validators.url('http://foobar.dk')
True

7. cypreess/python-rex

Python Regular Expressions for Humans

rexは、pythonの正規表現ライブラリ re を使いやすくしたもので、 非常に使いやすくなっています。

reを使うと:

import re
regex = re.compile("\[a-z\]{3}-(\\d{4})", flags=re.IGNORECASE)
m = regex.search("Your ticket number: XyZ-1047. Have fun!")

if m is not None:
   print m.group(1)
else:
   print None

rexを使うと:

from rex import rex
print ("Your ticket number: XyZ-1047. Have fun!" == rex("/\[a-z\]{3}-(\\d{4})/i"))\[1\]

8. dbader/schedule

Python job scheduling for humans

scheduleは名前の通り、定義したジョブを定期的に実行するためのライブラリです。
scheduleはその定期実行の書き方が面白くて、 10分ごとにジョブを実行 するには、

import schedule

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)

このように書きます。他にも

schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)

これらの書き方があります。スケジューリングしたジョブを実行するためには、

import time
while True:
    schedule.run_pending()
    time.sleep(1)

このように書きます。

まとめ

以上、Pythonを使った「for Humans」プロジェクトの一部を紹介しました。
Githubで "for Humans" と検索すると、python以外にもいろんな言語で書かれたプロジェクトが出てきますので、試しにやってみて下さい。

107
84
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
107
84