Published Date : 2021年1月3日18:22

Pythonでシーザー暗号 Part 2
Try the Caesar cipher in Python Part 2


This blog has an English translation


ニコニコ動画にアップした動画のまとめ記事です。

This is a summary blog post about a video I uploaded to NicoNico.

細かい部分は動画を参考にしてください。

Please refer to the video for details.


目次

Table of Contents




① 動画の説明
① Video Description



お次はこのスクリプトを汎用的に使えるように、クラスファイルにまとめてみましょう。

Now let's put this script together in a class file for general use.

前回のjupyter-notebookにて作成したスクリプトをコピペしていきます。

We will copy and paste the script We created in the previous jupyter-notebook.

クラスファイルの作り方は以前の動画を参考にしてください。

See the previous video for how to create a class file.

ではスクリプトの挙動を確かめてみましょう。

Let's see how the script works.

先ほどのクラスファイル内で作成したアンダースコアが二個ついたインスタンス変数がありました。

There was an instance variable created earlier in the class file with two underscores.

class CaesarCipher:
    def __init__(self):
        self.__pv_key = 3
        ------------------

このインスタンス変数はPythonでは特別な変数になり、外部からこの変数にアクセスできなくなります。

This instance variable becomes a special variable in Python, and is no longer externally accessible.

これを確かめてみるために、クラスファイルを呼び出して変数にアクセスできるか試してみましょう。

To see this, let's call the class file and see if we can access the variables.

from caesar_cipher import CaesarCipher
cc = CaesarCipher()
cc.__pv_key
------------------------------------------------------------
AttributeError             Traceback (most recent call last)
-----> 1 cc.__pv_key

AttributeError: 'CaesarCipher' object has no attribute '__pv_key'

これだけでは面白くないので、またFlaskを使った簡単なWEBアプリを作成してみましょう。

It's not interesting enough, so let's create a simple web app using Flask again.

いつものように必要なフォルダとファイルを作成します。

Create the necessary folders and files as usual.

今回はお試し用の簡易的なアプリですので、Bootstrap等は使用しません。

This is a simple application for trial, so I don't use Bootstrap.

VSCode等のエディタではHTMLファイル内でビックリマークを入力してエンターを押すと、HTMLのひな型が自動で作成されます。

In an editor such as VSCode, type an exclamation point in the HTML file and press Enter to automatically create an HTML template.

inputタグを用意して、ユーザーが平文を入力できるようにします。

Prepare the input tag to allow the user to enter plaintext.

メソッドにPOSTを指定したフォームタグでサブミットボタンを押すと、POST通信され、name属性値で指定されたinputタグの入力された値を取り出すことができます。

If you press the submit button on a form tag that specifies POST as the method, you can POST to retrieve the input value of the input tag specified by the name attribute value.

そしてFlaskサーバーのほうで暗号の処理をして、message変数としてHTML内に表示させます。

The Flask server then handles the encryption and displays it as a message variable in the HTML.

Pythonファイルに必要なFlaskモジュールや、正規表現のためのreモジュール等をインポートします。

Import the Flask module needed for Python files, the re module for regular expressions, and so on.

そして先ほど作成したクラスファイルをインポートして使えるようにします。

Then import the class file you just created to use it.

GETリクエストとPOSTリクエストとで、処理を分けます。

Separate processing for GET and POST requests.

POSTリクエストの処理を書いていきましょう。

Let's write a script to handle POST requests.

先ほど作成したクラスファイル内で使用したmainのスクリプトをコピペしていくだけです。

Just copy and paste the main script of the class file you just created.

では、アプリの挙動を確かめてみましょう。

Let's see how the app works.



以上です。お疲れ様です。

That's all. Thank you for your hard work.