要素のスタイル

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(Omi Chiba)

要素のスタイル

PEP836は、Pythonでプログラミングをする時の良いスタイルのプラクティスが含まれています。web2pyはこれらのプラクティスにすべて従ってるわけではないことに気づくでしょう。これは省略や手抜きのためではありません。web2pの利用者はこれらのルールに従うべきであると信じていますし、それを奨励しています。我々は、これらのルールの内いくつかに従わないことを選択しました。web2pyのヘルパーオブジェクトがユーザー定義オブジェクトに対して名前競合を起こす確率を最小限にしようとして定義したときにです。

PEP8 36 contains good style practices when programming with Python. You will find that web2py does not always follow these rules. This is not because of omissions or negligence; it is our belief that the users of web2py should follow these rules and we encourage it. We chose not to follow some of those rules when defining web2py helper objects in order to minimize the probability of name conflict with objects defined by the user.

たとえば、<div>を表現するクラスはDIVですが、Pythonのスタイルレファレンスによれば、Divとするべきです。我々は、この特定の例については、すべて大文字の"DIV"を使用するほうがより自然な選択だと考えています。さらに、このアプローチでは、必要とあればプログラマが"Div"クラスを自由に作ることができます。これらの構文はほとんどのブラウザのDOM表記法に自然にマッピングされます(たとえばFirefoxなどを含みます)。

For example, the class that represents a <div> is called DIV, while according to the Python style reference it should have been called Div. We believe that, for this specific example that using an all-upper-case "DIV" is a more natural choice. Moreover, this approach leaves programmers free to create a class called "Div" if they choose to do so. Our syntax also maps naturally into the DOM notation of most browsers (including, for example, Firefox).

Pythonのスタイルガイドによると、すべて大文字の文字列は、変数ではなく定数として使用するべきとあります。先の例を続けると、DIVがクラスであることを考慮しても、それは二度と修正されるべきない特別なクラスになります。そうでないと、他のweb2pyアプリケーションが動かなくなるからです。このことからDIVクラスは定数のように扱われるべきものとして適格であり、我々はこの表記の選択が正当化されると信じています。

According to the Python style guide, all-upper-case strings should be used for constants and not variables. Continuing with our example, even considering that DIV is a class, it is a special class that should never be modified by the user because doing so would break other web2py applications. Hence, we believe this qualifies the DIV class as something that should be treated as a constant, further justifying our choice of notation.

要約すると、次のような慣例に従います:

In summary, the following conventions are followed:

  • HTMLヘルパーとバリデータは、上記の議論の通りすべて大文字です(たとえば、DIV, A, FORM, URL)

    • HTML helpers and validators are all upper case for the reasons discussed above (for example DIV, A, FORM, URL).

  • 翻訳オブジェクトのTは、クラス自身ではなくクラスのインスタンスであるという事実にもかかわらず、大文字で表されます。論理的に、翻訳オブジェクトは、HTMLヘルパーと似た動作を行い、プレゼンテーションの一部の表示に影響します。また、 Tはコード内で検索しやすい必要がある同時に、短かい名前を持つ必要があります。

    • The translator object T is upper case despite the fact that it is an instance of a class and not a class itself. Logically the translator object performs an action similar to the HTML helpers, it affects rendering part of the presentation. Also, T needs to be easy to locate in the code and must have a short name.

  • DALのクラスは、Pythonのスタイルガイド(先頭文字を大文字にする)に従います。たとえば、Table、Field、Query 、Row、Rowsなどがあります。

    • DAL classes follow the Python style guide (first letter capitalized), for example Table,Field, Query, Row, Rows, etc.

他のすべてのケースでは、我々は、可能な限りPythonのスタイルガイド(PEP8)に従ってきたと信じています。たとえば、すべてのインスタンスオブジェクトは小文字(request、response、session、cache)であり、すべての内部クラスは大文字で始まっています。

In all other cases we believe we have followed, as much as possible, the Python Style Guide (PEP8). For example all instance objects are lower-case (request, response, session, cache), and all internal classes are capitalized.

この本のすべての例において、web2pyのキーワードは太字で、文字列やコメントはイタリック体で記載されています。

In all the examples of this book, web2py keywords are shown in bold, while strings and comments are shown in italic.