科学の箱

科学・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

kaggle Titanic Tutorial – 1

KaggleでTitanic tutorialにチャレンジしてみる。 Titanic: Machine Learning from Disaster https://www.kaggle.com/c/ …

no image

グリッドサーチ

機械学習のアルゴリズムを利用する際に一つの問題がパラメータの最適化。例えばSVMではガンマパラメータを適切に設定しないと結果は使い物にならないことがある。このパラメータは自分で設定する方法もあるが、P …

no image

pythonでsha3をつかう

pip install sha3だと何かがダウンロードされるが下記のエラーが表示される。 c:\users\usera\appdata\local\temp\pip-install-1v_qtdxo\ …

no image

Scrapy – Install

インストールガイドはこちら https://doc.scrapy.org/en/latest/intro/install.html#intro-install anacondaにインストールする co …

no image

すべての要素が同じ値を持つ配列を生成

配列はリストから生成できるが、numpyでは様々な方法で目的とする配列を生成できる。 すべての要素が0である配列の生成 すべての要素が1である配列の生成 すべての要素が同じ値である配列の生成 すべての …

2018年2月
月 火 水 木 金 土 日
« 1月   3月 »
 1234
567891011
12131415161718
19202122232425
262728  

side bar top



アーカイブ

カテゴリー