By Al Sweigart. Over 500,000 copies sold. Free to read under a CC license. "The best part of programming is the triumph of seeing the machine do something useful. Automate the Boring Stuff with Python frames all of programming as these small triumphs; it makes the boring fun." - Hilary Mason, Data Scientist and Founder of Fast Forward Labs "I'm having a lot of fun breaking things and then putting
Pythonを使ってこの方さまざまな点につまずいたが、ここではそんなトラップを回避して快適なPython Lifeを送っていただくべく、書き始める前に知っておけばよかったというTipsをまとめておく。 Python2系と3系について Pythonには2系と3系があり、3系では後方互換性に影響のある変更が入れられている。つまり、Python3のコードはPython2では動かないことがある(逆もしかり)。 Python3ではPython2における様々な点が改善されており、今から使うなら最新版のPython3で行うのが基本だ(下記でも、Python3で改善されるものは明記するようにした)。何より、Python2は2020年1月1日をもってサポートが終了した。よって今からPython2を使う理由はない。未だにPython2を使う者は、小学生にもディスられる。 しかし、世の中にはまだPython3に
Pythonを始めたばかりのユーザーの多くが、どちらのバージョンを使えばいいのか迷っています。私の答えは、「気に入ったチュートリアルに書かれているバージョンにしましょう。そして、あとで違いを調べてください」という言葉につきます。 それでは、新しいプロジェクトを始めるときにはどちらを選べばいいのでしょうか? 使おうとしているライブラリを全てサポートしているなら、2.7.x系と3.x系のどちらを使ってもよいでしょう。そうはいっても、この2つのメジャーバージョンについて大きな違いを見ておくのは良いでしょう。どちらかのみでコードを書いたり、プロジェクトに使おうとしている時によくある落とし穴を避けられるからです。 __future__ モジュール Python 3.x で導入されていて Python 2 で使えないキーワードについては、 __furute__ モジュールをインポートすることで Pyt
概要 この記事は自然言語処理という分野の最新手法word2vec を利用して誰でも遊べるようにするための手順を説明するものです。 word2vecを利用すると意味の計算が実現できます。 例えば"king"から"man"を引いて"woman"を足すと"queen"が出てきたり、 "東京"から"日本"を引いて"フランス"を足すと"パリ"が出てくるという面白い手法です。 自然言語処理とは人間が日常的に用いる自然言語をコンピュータに処理させ、 翻訳や要約、文字入力支援や質問応答システムを作るなどに活用されている分野です。 自然言語処理と言うと耳慣れない言葉かもしれませんが、 実は検索や推薦などで私たちが日常的に利用しているなじみ深い技術でもあります。 自然言語処理の適用範囲や要素技術は幅広いのですが、 その中でもword2vecの特色は、 冒頭でも挙げたように「意味の計算」が出来ることです。 これ
INDEX コーディングスタイル ファイル操作 日付・時刻 その他 その他 .pycを作りたくない 環境変数PYTHONDONTWRITEBYTECODEを設定すると、.pycおよび.pyoファイルが作成されなくなる。値は何でもいいので、 export PYTHONDONTWRITEBYTECODE=1 などと書いておけばよい。 辞書(dict)のソート キー(key)の順に取り出す >>> score = {'Jack':300, 'Mike':200, 'Jane':100} >>> for (k, v) in sorted(score.items()): ... k, v ... ('Jack', 300) ('Jane', 100) ('Mike', 200) 値(value)の順に取り出す >>> for (k, v) in sorted(score.items(), key=l
OpenSSLを使うと、次のようにして2048bitのRSA鍵が作成できる。 $ openssl genrsa 2048 Generating RSA private key, 2048 bit long modulus ......................+++ .................+++ e is 65537 (0x10001) -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAui/OeOYeMrLv+U2w13hQkL204OQVlB05nksKa5LaNE6mT3WY (snip) -----END RSA PRIVATE KEY----- ここで出力される内容は、ASN.1という構文規則で表現された情報をDERと呼ばれるバイナリ形式にし、それをさらにBase64エンコードしたものになっている。 このフォーマット
Note: This post is based on a presentation I just did at RuPy in Budapest. Instead of just posting my slides I thought it would be more useful if I wrote it down as a blog post while it’s still fresh in my mind. You can also watch the video recording of the presentation. FYI I’m planning to do a similar presentation at RubyConf, except I’ll remove the Python info, and instead compare how GC works
StackOverFlow:Understanding Python decoratorsに対するe-satis氏によるデコレータの丁寧な解説。 一つエクスキューズしておくと、翻訳の作法とかよく分かってません。でも頑張ったつもり。つもり。 Imran氏による質問の内容 @makebold @makeitalic def say(): return "Hello" というデコレータに対して <b><i>Hello</i></b> という文字列を返すような関数ってどうやって書いたらいいの? e-satis氏による解説 Pythonの関数はオブジェクトである デコレータを理解するためには、まずPythonにおける関数がオブジェクトであることを理解しなければならない。このことは重大な影響をもたらす。なぜなのか簡単な例とともに見てみよう。 def shout(word="yes"): return
Python3 に特化した専門書という位置付けですが、(Python3 に関した) 言語仕様やその変更、ライブラリの詳細の違いなどを除けば、Python2 でも活用できる知識が大半です。まだ Python2 しか使っていないという方でも十分に役立つ内容だと思います。 ただ、本書を読み進める上で1つだけ忘れてはいけないことがあります。Python 全般について丁寧に解説されていますが 著者名が Python サポーターズとなっていますが、ついったーなんかでは Python モヒカンズなんて言われています。 パーフェクト Python の執筆に参加しました — プログラマのネタ帳 二冊目 と何だかこわい人たちが書いた本だということです。まずはそのことを念頭におくことで本書を楽しんで読み進められる心の準備が整った、言い換えると、覚悟はできたと言えます。そうすれば、途中でこわくなってきても勇気をも
#!/usr/bin/env python # -*- coding: utf-8 -*- import sqlite3 import csv writer = csv.writer(file('result.csv','w'),lineterminator="\n",quoting = csv.QUOTE_ALL) con = sqlite3.connect(':memory:') cur = con.cursor() sql2 = "SELECT * FROM (SELECT * FROM tweet" for i in xrange(1,10): #dbファイルの数 sql = "ATTACH DATABASE 'tweet%d.db' as tweet%d" % (i,i) con.execute(sql) con.commit() sql2 = sql2 + """ UNION
EmscriptenはLLVMをJavaScriptに変換するソフトウェア。PythonやLuaをWebブラウザ上で実行できる。 EmscriptenはPython/JavaScript製のオープンソース・ソフトウェア。LLVM(Low Level Virtual Machine)という技術がある。ソースコードをアーキテクチャに依存しない中間コードに変換し、最適化した上で各マシン向けにネイティブなコードを出力することでより効率的なバイナリを作成できるというものだ。 Python実行例 つまりLLVMが生成する中間コードを使えば、元々の言語は気にせずに動くという訳だ(おそらく)。それを実証してくれるプロジェクトがEmscriptenだ。 EmscriptenはLLVMの中間コードをJavaScriptに変換するソフトウェアだ。つまり中間コードにさえ変換できれば、それをJavaScriptに変
今日はさっくり小ネタ。 最近私はPythonを勉強中なのですが、「このての軽量言語はさっと書いてさっと実行できなきゃだめ」という信念のもと、編集にはvimを使っています。 とはいえ、Pythonは実はかなり「エディタを選ぶ」言語です。インデントによってブロックを表現するので、インデントがきちんと表示されないと困りますし、タブとスペースが入り交じってしまわないようなエディタの補助も欲しいところです。 というわけで、今日は、そんなPythonをすっきり快適に編集できるような vim の設定をご紹介します。 いきなり結論 私は以下のような記述を ~/.vimrc に追記しました。 autocmd FileType python setl autoindent autocmd FileType python setl smartindent cinwords=if,elif,else,for,wh
Find, install and publish Python packages with the Python Package Index Search PyPI Search Or browse projects The Python Package Index (PyPI) is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community. Learn about installing packages. Package authors use PyPI to distribute their software. Learn how to packa
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く