科学の箱

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

pipインストールでエラーが出る場合

pipインストールでエラーが出る場合 Collecting tensorflow WARNING: Retrying (Retry(total=4, connect=None, read=None, …

no image

蟻本 P42 硬貨の問題

貪欲法の基本 その時点で最善の手を尽くす 尽くした結果を目的とする値に反映させる。 次善の手になるようにする。 1に戻る 硬貨の問題 A=int(input()) *C,=map(int,input( …

no image

python data scientist bootcamp

pythonでdata分析をしたいと思ったが学校に通う時間もお金もない。udemyで検索したところ英語版であるが1400円!でコースが見つかったのでこちらを受けることにした。 https://www. …

no image

condaでjupyter notebookが使えないとき

condaで環境を作るとjupyter notebookはインストールされていない。 この場合には個別にjupyterをインストールすればよい。   conda install jupyte …

no image

matplotlibでfigureを利用する

matplotlibでfigureオブジェクトを利用するとグラフを表示する位置を細かく指定できる。 まずはfigureを使ってグラフを一つ表示してみる。 >>> import mat …

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

side bar top



アーカイブ

カテゴリー