可視化は大事 結果が関数で求まってもどういうものかは直感的にわからないことが多い。 そこでグラフを描くことが分析・思考のための第一歩になる。 matplotlibの日本語メモはここが詳しい。 sympyのplotはここ。 公式とかも見ながら四苦八苦。 import import sympy as sym sym.init_printing() Pi = sym.S.Pi # 円周率 E = sym.S.Exp1 # 自然対数の底 I = sym.S.ImaginaryUnit # 虚数単位 # 使用する変数の定義(小文字1文字は全てシンボルとする) (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) = sym.symbols('a b c d e f g h i j k l m n o p q r s t u v w x y z')
Logging HOWTO¶ Author: Vinay Sajip <vinay_sajip at red-dove dot com> This page contains tutorial information. For links to reference information and a logging cookbook, please see Other resources. Basic Logging Tutorial¶ Logging is a means of tracking events that happen when some software runs. The software’s developer adds logging calls to their code to indicate that certain events have occurred.
自分で小さいツールを作る時に心に留めているtipsです. 書き始めたときは「どうせ書捨てだし」と思って書き始めると意外と長い間,もしくはいろんなところで使うことになったりするので,気をつけておくと後から楽になるというような小技です.大規模なソフトウェアの開発ではまた違った流儀があると思います. メインルーチンを関数にする 関数名はなんでもいいのですが,自分は趣味で main() という名前の関数を用意し,メインルーチンは全てそこに書くようにしています. #!/usr/bin/env python def main(): print('hello, hello, hello!') if __name__ == '__main__': main() pythonの小さなサンプルコードを見たりすると関数外の部分にベタで実行コードが書かれていたりします.もちろんそれでも動くのですが,以下の2点で後
unittest.TestCaseを継承した以下のようなファイルtest_hoge.pyを作成します。 ここでは値が期待する値と同じかどうかを調べるeq_と, 値が真であるかどうかを調べるok_をnose.toolsからインポートして使っています。 テストフレームワークによくある仕組みとして、テストの前後に初期化/リソース解放などの処理を行うためのsetUp, tearDownが提供されていますので、試しにprint文を入れてみます。 from unittest import TestCase from nose.tools import ok_, eq_ from hoge import sum, is_even class HogeTestCase(TestCase): def setUp(self): print 'before test' def tearDown(self): p
こんにちは、@yoheiMuneです。 皆様はUnitテスト書かれていますかー?僕は自分のアプリケーションでunitテストを書くようにしています。今日はPythonでのUnitテストの書き方をブログに書きたいと思います。 目次 単体テストを書く理由 僕が単体テストを書く理由は、別に誰からか強制されているわけではなく、自分のために書きます。ユニットテストを書くことで以下のメリットがあるなぁと感じています。 コーディングしながら同時にテストもできて品質を確保しやすい テストしやすいメソッド設計になって、今後のメンテナンスをしやすい(例えば将来仕様変更があった場合にも、メソッドを修正してテストも修正すれば、最低限品質を担保できる 細かくテストができてテストバリエーションを確保でき、品質に繋がる。例えばHTTP〜アプリ〜DBを通してテストする場合には、どうしても細かい部分はテストしづらい) 新しい
マルチバイト文字の含まれたテキストファイルを扱おうとすると、文字コードがまず問題になる。 そのファイルがいったい何でエンコードされているか分からないと、それを適切に扱うことは到底できない。 そんなとき使うと便利なのが、今回紹介する chardet というサードパーティ製のパッケージ。 今回の検証環境には Mac OS X を使った。 $ sw_vers ProductName: Mac OS X ProductVersion: 10.11.1 BuildVersion: 15B42 インストール まずは pip を使って chardet をインストールする。 $ pip install chardet 下準備 次に、文字コードを検出するのに使うテキストファイルを用意しよう。 $ cat << EOF > helloworld.txt こんにちは、世界 EOF 上記のコマンドで作ったテキスト
計算機幾何 少しずつ、なぞる。調べ物をする。 from sympy.geometry import * P1 = Point(0, 0) P2 = Point(3, 4) P3 = Point(2, -1) P4 = Point(-1, 5) S1 = Segment(P1, P2) S2 = Segment(P3, P4) "sympy"自体を調べる help(sympy) Help on package sympy: NAME sympy FILE c:\python27\lib\site-packages\sympy\__init__.py DESCRIPTION SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (
Manipulation and analysis of geometric objects in the Cartesian plane. Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. It is using the widely deployed open-source geometry library GEOS (the engine of PostGIS, and a port of JTS). Shapely wraps GEOS geometries and operations to provide both a feature rich Geometry interface for singular (scalar) ge
Pythonにはとても便利な設定ファイル読み書き機能があります。決められた書式に従って記述することで、容易に設定値を得ることができます。 設定ファイルの読み込み Windowsのiniファイルのような構造で保存します。まずは設定ファイルそのものを作ってみましょう(文字コードはUTF-8です)。 # コメントアウトも出来ます [settings] host = localhost port = 12345 # 「 = 」前後にスペースがあってもなくても問題なし [system] os=Windows version=8 path= # 区切りは「 = 」でも「 : 」可 [user] name : python-izm password : 0000 mail : mail@example.com ハイライトされている部分がセクションで、セクション名を「 [] 」(大カッコ)で囲みます。セク
概要 Python のクラスメンバ設定の方法について調べてみた。 引数が増えたときは、dictionary 使った方が良さそう。 引数チェックするのにコード増える⇒効率良く書くには―― setattr うまく使うと楽に書ける!! サンプル概要 Pet クラスを作ってみる。メンバは2つ。 name : ペットの名前 (string) category : ペットの種類 (string) category ごとの鳴き声を出させる。 (その1) 普通に作る。 コンストラクタで普通に設定。引数に連ねて書く。 test.py class Pet1 : def __init__(self, name, category) : self.name = name self.category = category def bark(self) : if self.category == 'dog' : re
PIL/Pillowはコンパクトで高速なPython用の画像ライブラリです。 よく使う処理をまとめました(随時更新) PILとPillowの違い 基本的にPILを使う理由はありません、Pillowの方がリサイズフィルタのバグフィックスがされており高品質です。 Pillowの速度について Pillowは非常に高速にチューニングされており、同様なライブラリであるImageMagickよりも常に高速に動作します。 ただし、getpixel/putpixelは非常に低速です、画像生成以外には使わないようにしましょう。 更に高速なpillow-simdもあります。概ねオリジナルのPillowの4〜5倍の速度が出るようです。 pillow-simd https://github.com/uploadcare/pillow-simd 参考 Imageモード一覧 モード 説明
GitHubの Trending を眺めていたりすると時たま気になるpython製のツールやライブラリがあって試したくなったりします。 でもよく知らないし試したらすぐ消すようなものを普段の環境に入れるのも抵抗があって、その都度仮想環境を作ったりしていました。 try はそんな時に手軽に仮想環境を作って使い終わったらさくっと廃棄してくれる便利ツールです。 インストール tryで作る環境は2.7などでもいいようですが、try自体はPython3.4以上の対応のようです。 python3 -m pip install trypackage これ(またはpip3など)でインストールでき、コマンドラインからtryコマンドが使えるようになります。 使い方 try [試したいライブラリ名] [[ライブラリ2] [ライブラリ3] ...] これで新しい仮想環境が作られてライブラリがインストール&impor
のように引数をキーワードで指定でしたいという基本的なことについて。オプションは毎回指定するものではなくて、指定しなければデフォルト値が使われるようにしたい。
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く