科学の箱

科学・IT・登山の話題

Python

automated the boring – day5

投稿日:2018年2月26日 更新日:

さて、ここまででフロー、文字列、型、ファイル等を扱い、基礎プログラミングとしては一段落できた。

今日からは後半戦にはいり、開発をするうえで実践で必要になる技術を学んでいく。

まずはデバッグからである。デバッグ方法の一つ目としては本来発生するべきでない現象が起きた時にはexceptionをあげる。


def boxPrint(symbol, width, height):
  if len(symbol) != 1:
    raise Exception('Symbol must be a single character string.')
  if width <= 2:
    raise Exception('Width must be greater than 2.')
  if height <= 2:
    raise Exception('Height must be greater than 2.')
  print(symbol * width)
  for i in range(height - 2):
    print(symbol + (' ' * (width - 2)) + symbol)
  print(symbol * width)

for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
  try:
    boxPrint(sym, w, h)
  except Exception as err:
    print('An exception happened: ' + str(err))

次にtracebackをファイルに書き込む処理

try:
  raise Exception('This is error')
except:
  errorfile = open('.\\errorInfo.txt', 'w')
  errorfile.write(traceback.format_exc())
  errorfile.close()

ログを取得する際にはloggingパッケージを利用する。

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('start')
2018-02-26 10:56:23,764 - DEBUG - start

ステップbyステップのデバッグはIDLEから利用できる。シェルからは利用できないので注意する。

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

カテゴリデータのビジュアル

カテゴリ別データのビジュアル 参考)https://www.kaggle.com/omarelgabry/a-journey-through-titanic?scriptVersionId=44779 …

no image

seabornをEDAに応用する

seabornを利用すれば基本となるビジュアルと分布、regressionをすぐに取得できる。 import seaborn as sns tips = sns.load_dataset(‘tips’ …

no image

automated the boring – day6

https://automatetheboringstuff.com/chapter11/ さてこの辺からようやく面白くなってくる。webscrapingをするにあたり、まずはブラウザをpythonか …

no image

配列のインデックス

インデックスとは配列に対して[]で要素を抜き出す方法である。 マニュアルは以下になる。 Indexing Indexing Routines インデックスの方法としては以下がある。 整数値を使ったイン …

no image

scikit-learnで適切なアルゴリズムを選択するためのチートシート

  http://scikit-learn.org/stable/tutorial/machine_learning_map/index.html Related posts:cp932 e …

2018年2月
« 1月   3月 »
 1234
567891011
12131415161718
19202122232425
262728  

side bar top



アーカイブ

カテゴリー