科学の箱

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

graphvizのコマンドラインから実行

dot -Kdot -Tpng test.dot -o test.png Related posts:kaggle Titanic Tutorial – 9すべての要素が同じ値を持つ配列を生成空の配列 …

no image

Python + Slack Bot – 3

さてリアルタイムでとりあえずうまくいったので、もう少し違うサンプルコードを試してみる。 参考にしたのはこちら。 https://www.fullstackpython.com/blog/build-f …

no image

Python + Slack Bot – 2

Slackのチャネルに投稿するにはChanel IDが必要。 Chanel IDは下記から取得できる。 https://api.slack.com/methods/channels.list/test …

no image

flickrapiを使うための手順

https://stuvel.eu/flickrapiで提供されるflickrapiを利用するにあたり、エラーが出てインストールできない。 condaでは提供されていない pipはSSLError(S …

no image

Python + Slack Bot – 1

こちらのページを参考にしてslack botを作成する。 https://www.fullstackpython.com/blog/build-first-slack-bot-python.html …

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

side bar top



アーカイブ

カテゴリー