Code Monkey home page Code Monkey logo

python-tutorial-scripts's Introduction

ここに置かれたファイル群は、Python チュートリアル(2.7系)に記載された対話形式でのプログラム実行例から、実行結果を削ってスクリプト部分だけを抜き出し、対話モードではなくファイルとして実行できるようにしたものです。

対話モードでは入力した内容が失われ、再入力が面倒だったりするので、一度ファイルに記載してから実行したいと思いました。そこで、対話形式のプログラム例からスクリプトを抜き出してファイルにしました。なお、対話モードでは、入力した式を評価した結果が表示されますが、コマンドプロンプトからファイルを実行した場合は式の評価結果が表示されないので、一部「print」文を追加しています。

また、ファイルを実行した際に、プログラム例の内容と実行結果が一緒に出力されるようにしています。この目的で、print_and_exec.pyというモジュールを作りました(数行ですが)。例として、次のように記載すると、スクリプトの内容と実行結果が出力されます。また、例外が発生する部分は、try_exec("実行スクリプト")という形で書いておくと、そこで実行が止まらず、例外発生結果が出力されます。

# print_and_exec_example.py
# coding: UTF-8

from print_and_exec import *
print_and_exec(ur'''
"■コード例の説明"
"以下、Pythonのスクリプトを記載"
print "hello world"
try_exec('raise TypeError("Wrong type")')
''')

上記の実行結果は下記です。


===========================================================
【スクリプト】
"■コード例の説明"
"以下、Pythonのスクリプトを記載"
print "hello world"
try_exec('raise TypeError("Wrong type")')

【実行結果】
hello world
Error:TypeError('Wrong type',)

自分の学習用に取り纏めたものなので、一部のプログラム例は抜けていたりします。また、自分の理解を深めるために独自のプログラム例を加えたりしています(ch9のComplex classなど)。また、プログラム例で参照するファイルも追加しています(ファイル入出力、パッケージの部分など)。

ご参考まで。

Itsukara

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.