パイソンでプログラッミング

パイソン言語に基づき、プログラミングを紹介する。

パイソンをゲットしよう

(以下のリンクはバージョン3.4ですが、現在3.5が最新。)

オンラインチュトリアルを見よう

本をゲットしよう

残念だが、前面的にお勧めできる教科書は見付からない。パイソンチュトリアルを勉強する方がいいだろう。

開発環境

開発環境とは、プログラマーがよく使うサービス(エディター、デバッガーなど)を提供するプログラム。パイソン言語とバナダルされたIDLEは悪くないがIPythonがお勧めです。

アルゴリズム

パイソンデータ構造

Back to contents

簡単なプログラム

"""
複数のプログラムが利用するため、別のプログラムとして映画のデータベース
を構成。データベースに表が2つある:映画とその公開日付、そして映画とス
ター俳優・女優。
"""

## import は他のプログラムの定義を利用できるにする。
## こうして利用したプログラムはモジュール(module)という。

## モジュールとして import
## モジュールの名前を利用して含まれた名前を利用

import pytz

## モジュールの内容を直接に import
## モジュールの名前は import されない

## datetime と timedelta は datetime + timedelta = datetime,
## datetime - datetime = timedelta として使える。

from datetime import datetime, timedelta

# アメリカのニューヨーク市の朝9時が公開時間と定義するため、そのタイム
# ゾーンを利用します。
## pytz. を使わなければなりません。timezone という名前だけで使えない。
nyzone = pytz.timezone('US/Eastern')

# データは
#
# Released on Feb. 27
# The DUFF
# Hot Tub Time Machine 2
# McFarland, USA
# Badlapur
# Digging Up the Marrow
# Queen and Country
# Wild Tales
#
# というかたちで送ってもらうので、以下のようにデータ化する。

DATES_MOVIES = [ # "movie",
    ## フルナームは datetime.datetime です。 datetime. はモジュール
    ## を表す、 .datetime はクラス。datetime.datetime() という形でオ
    ## ブジェクトを組む。
    [datetime(2015, 2, 20, 9, tzinfo=nyzone),
     # Released on Feb. 20
     "The DUFF",
     "Hot Tub Time Machine 2",
     "McFarland, USA",
     "Badlapur",
     "Digging Up the Marrow",
     "Queen and Country",
     "Wild Tales"],
    [datetime(2015, 2, 27, 9, tzinfo=nyzone),
     # Released on Feb. 27
     "Focus",                            #  (2015)
     "The Lazarus Effect",
     "'71",
     "Deli Man",
     "Eastern Boys",
     "Everly",
     "Farewell to Hollywood",
     "Futuro Beach",
     "The Hunting Ground",
     "A La Mala",
     "Maps to the Stars",
     "My Life Directed",
     "The Salvation",
     "Snow Girl and the Dark Crystal",
     "Wild Canaries"],
    [datetime(2015, 3, 6, 9, tzinfo=nyzone),
     # Released on Mar. 6
     "Chappie",
     "Unfinished Business",
     "Bad Asses on the Bayou",
     "Compared to What? The Improbable Journey of Barney Frank",
     "Hayride 2",
     "The Life and Mind of Mark DeFriest",
     "The Mafia Only Kills in Summer",
     "Merchants of Doubt",
     "The Second Best Exotic Marigold Hotel",
     "These Final Hours"],
    [datetime(2015, 3, 13, 9, tzinfo=nyzone),
     # Released on Mar. 13
     "Cinderella", # (2015)
     "Run All Night",
     "Eva",
     "Home Sweet Hell",
     "It Follows",
     "Seymour: An Introduction",
     "The Tales of Hoffmann", # (2015 re-issue)
     "The Wrecking Crew"],
    [datetime(2015, 3, 20, 9, tzinfo=nyzone),
     # Released on Mar. 20
     "The Divergent Series: Insurgent",
     "Do You Believe?",
     "The Gunman",
     "Can't Stand Losing You: Surviving the Police",
     "Danny Collins",
     "Kumiko, The Treasure Hunter",      # (on Wed.)
     "Love and Lost",                    # (Shi Gu)
     "Shi Gu",
     "Spring",                           # (2015)
     "Zombeavers"],
    [datetime(2015, 3, 27, 9, tzinfo=nyzone),
     # Released on Mar. 27
     "Get Hard",
     "Home",                             # (2015)
     "Cupcakes",
     "A Girl Like Her",
     "Man From Reno",
     "The Riot Club",
     "The Salt of the Earth",
     "Serena",
     "Welcome to New York",
     "While We're Young",
     "White God"],
    [datetime(2015, 4, 3, 9, tzinfo=nyzone),
     # Released on Apr. 3
     "Furious 7",
     "5 to 7",
     "Cheatin'",
     "Effie Gray",
     "Lambert & Stamp",
     "The Living",
     "SBK The-Movie",
     "Woman in Gold"],
    [datetime(2015, 4, 10, 9, tzinfo=nyzone),
     # Released on Apr. 10
     "The Longest Ride",
     "Clouds of Sils Maria",
     "Desert Dancer",
     "Ex Machina",
     "Freetown",
     "Kill Me Three Times",
     "Rebels of the Neon God",           # (2015 re-release)
     "The Sisterhood of Night"],
    [datetime(2015, 4, 17, 9, tzinfo=nyzone),
     # Released on April 17th (Friday)
     "Child 44",
     "Monkey Kingdom",
     "Paul Blart: Mall Cop 2",
     "Unfriended",
     "Alex of Venice",
     "Beyond the Reach",
     "The Dead Lands",
     "Felix and Meira",
     "Monsters: Dark Continent",
     "The Road Within",
     "True Story"],
    [datetime(2015, 4, 24, 9, tzinfo=nyzone),
     # Released on April 24th (Friday)
     "The Age of Adaline",
     "Little Boy",
     "Adult Beginners",
     "After The Ball",
     "Brotherly Love",
     "Kung Fu Killer",
     "Misery Loves Comedy",
     "WARx2",
     "The Water Diviner"],
    [datetime(2015, 5, 1, 9, tzinfo=nyzone),
     # Released on May 1st Friday
     "Avengers: Age of Ultron",
     # The 100-Year Old Man Who Climbed Out the Window and Disappeared
     "100-Year Old Man Who Climbed Out Window Disappeared",
     "Far from Men",
     "Far from the Madding Crowd",
     "Hyena",
     "Iris",                             # (2015)
     "Ride",                             # (2015)
     "Tangerines",
     "Welcome to Me"],
    [datetime(2015, 5, 8, 9, tzinfo=nyzone),
     # Released on May 8th (Friday)
     "Hot Pursuit",
     "5 Flights Up",
     "The D Train",
     "I Am Big Bird",                    # (on Wed.)
     "In the Name of my Daughter",
     "Maggie",
     "Noble",
     "Saint Laurent",
     "The Seven Five",                   # (on Thu.)
     "Skin Trade",
     # "WARx2",                          # Not released on 4/24?
     "Sister Code"],
    [datetime(2015, 5, 15, 9, tzinfo=nyzone),
     # Released on May 15th (Friday)
     "Mad Max: Fury Road",
     "Pitch Perfect 2",
     "Animals",                          # (2015)
     "The Connection",
     "Every Secret Thing",
     "The Film Critic",
     "Good Kill",
     "I'll See You In My Dreams",
     "Set Fire to the Stars",
     "Where Hope Grows",
     "Forbidden Films",                  # (on Wed.)
     "Know How",
     "One Cut, One Life",                # (on Wed.)
     "Our Man in Tehran"],
    [datetime(2015, 5, 22, 9, tzinfo=nyzone),
     # Released on May 22th (Friday)
     "Poltergeist",                      # (2015)
     "Tomorrowland",
     "Aloft",
     "Chocolate City",
     "The Farewell Party",
     "Love At First Sight",
     "Sunshine Superman",
     "When Marnie Was There"],
    [datetime(2015, 5, 29, 9, tzinfo=nyzone),
     # Released on May 29th (Friday)
     "Aloha",
     "San Andreas",
     "Club Life",
     "Gemma Bovary",
     "Heaven Knows What",
     "Results"],
    # *Released on June 5th, Friday: *
    [datetime(2015, 6, 5, 9, tzinfo=nyzone),
     "Entourage",
     "Insidious Chapter 3",
     "Spy",
     "Every Last Child",
     "How to Save Us",
     "Hungry Hearts",
     "Love & Mercy",
     "Pigeon",
     "Police Story: Lockdown",
     "Testament of Youth"],
    # *Released on June 12 Friday: *
    [datetime(2015, 6, 12, 9, tzinfo=nyzone),
     "Jurassic World",
     "11th Hour",
     "Me and Earl and the Dying Girl",
     #"Set Fire to the Stars",
     "Soaked in Bleach",
     #"WARx2",
     "The Wolfpack"],
     #*Released on June 19 Friday: *
     [datetime(2015, 6, 19, 9, tzinfo=nyzone),
      "Dope",
      "Inside Out",
      "Dark Awakening",
      "Eden",
      "Gabriel",
      "Infinitely Polar Bear",
      "Manglehorn",
      "The Overnight",
      "The Tribe"],
      # *Released on June 26 Friday: *
      [datetime(2015, 6, 26, 9, tzinfo=nyzone),
       "Max",                           # (2015)
       "Ted 2",
       "7 Minutes",
       "The Algerian",
       "Batkid Begins",
       "Big Game",
       "Felt",
       "The Little Death",
       "Murder in the Park",
       "The Pardon",
       "Runoff"],
    ]

MOVIES = []                    # empty list
now = datetime.now(tz=nyzone)
for date_movies in DATES_MOVIES:
    date = date_movies[0]
    if (date + timedelta(70) >= now
        and date - timedelta(7) <= now):
        MOVIES.extend(date_movies[1:])

STOPSET = {"a", "an", "the", "some", "to", "from", "for", "with"}

## str.translate 用データ。この dict は , ? : ; を削除するときに利用。
PUNCT = { ord(',') : None,
          ord('?') : None,
          ord(':') : None,
          ord(';') : None,
          }

## DATES_MOVIES と同じく、各レコードの最初のメンバーが共通のもの、つ
## まり映画、そしてその他のメンバーが映画のスター俳優です。
## よい構造といえないが簡単に入力できるためにここで利用。なぜなら、
## 映画の名前が「MOVIES_STARS[i][0]」となっています。
## movie.py ではよりわかりやすい構造を構築。そこで m.name のような記
## 号で名前を指す。
MOVIES_STARS = [
    ["The DUFF", "Mae Whitman", "Bella Thorne", "Robbie Amell"],
    ["Hot Tub Time Machine 2", "Rob Corddry", "Craig Robinson", "Clark Duke"],
    ["McFarland, USA", "Kevin Costner", "Maria Bello", "Ramiro Rodriguez"],
    ["Badlapur", "Nawazuddin Siddiqui", "Varun Dhawan", "Radhika Apte"],
    ["Queen and Country", "Callum Turner", "Caleb Jones", "David Thewlis,"],
    ## 英数字だけでのユニコードの書き方。
    ["Wild Tales", "Ricardo Dar\u00edn", "Leonardo Sbaraglia"],
    ["Focus", "Will Smith", "Margot Robbie", "Rodrigo Santoro"],
    ["The Lazarus Effect", "Olivia Wilde", "Mark Duplass", "Evan Peters"],
    ["'71", "Jack O'Connell", "Sam Reid", "Sean Harris"],
    ["Deli Man", "Ziggy Gruber", "Larry King", "Jerry Stiller"],
    ["Eastern Boys", "Olivier Rabourdin", "Kirill Emelyanov", "Daniil Vorobyov"],
    ["Farewell to Hollywood", "Henry Corra", "Regina Nicholson"],
    ["Futuro Beach", "Wagner Moura", "Clemens Schick", "Jesu\u00EDta Barbosa"],
    ["The Hunting Ground", "Kirby Dick", "Amy Ziering", "Amy Herdy"],
    ["A La Mala", "Aislinn Derbez", "Mauricio Ochmann", "Papile Aurora"],
    ["Maps to the Stars", "Julianne Moore", "Mia Wasikowska", "Robert Pattinson"],
    ["The Salvation", "Mads Mikkelsen", "Eva Green", "Eric Cantona"],
    ["Snow Girl and the Dark Crystal", "Bei-Er Bao", "Winston Chao", "Kun Chen"],
    ["Wild Canaries", "Sophia Takal", "Lawrence Michael Levine", "Alia Shawkat"],
    ["Chappie", "Sharlto Copley", "Dev Patel", "Hugh Jackman"],
    ["Unfinished Business", "Vince Vaughn", "Dave Franco", "Tom Wilkinson"],
    ["The Mafia Only Kills in Summer", "Cristiana Capotondi", "Pif", "Alex Bisconti"],
    ["Merchants of Doubt", "Frederick Singer", "Naomi Oreskes", "Jamy Ian Swiss"],
    ["The Second Best Exotic Marigold Hotel", "Judi Dench", "Maggie Smith", "Bill Nighy"],
    ["Cinderella", "Lily James", "Cate Blanchett", "Richard Madden"],
    ["Run All Night", "Liam Neeson", "Ed Harris", "Joel Kinnaman"],
    ["It Follows", "Maika Monroe", "Keir Gilchrist", "Olivia Luccardi"],
    ["Seymour: An Introduction", "Seymour Bernstein", "Jiyang Chen", "Ethan Hawke"],
    ["The Tales of Hoffmann", "Moira Shearer", "Robert Rounseville", "Ludmilla Tch\u00E9rina"],
    ["The Wrecking Crew", "Lou Adler", "Herb Alpert", "The Association"],
    ["The Divergent Series: Insurgent", "Patrick Cronen", "Mekhi Phifer", "Miles Teller"],
    ["Do You Believe?", "Mira Sorvino", "Sean Astin", "Alexa PenaVega"],
    ["The Gunman", "Sean Penn", "Idris Elba", "Jasmine Trinca"],
    ["Can't Stand Losing You: Surviving the Police", "Sting", "Andy Summers", "Stewart Copeland"],
    ["Danny Collins", "Al Pacino", "Annette Bening", "Jennifer Garner"],
    ["Kumiko, The Treasure Hunter", "Rinko Kikuchi", "Nobuyuki Katsube", "Shirley Venard"],
    ["Love and Lost", "Andy Lau", "Boran Jing", "Tony Leung Ka-Fai"],
    ["Shi Gu", "Andy Lau", "Boran Jing", "Tony Leung Ka-Fai"],
    ["Spring", "Lou Taylor Pucci", "Nadia Hilker", "Vanessa Bednar"],
    ["Get Hard", "Will Ferrell", "Kevin Hart", "Alison Brie"],
    ["Home", "Jim Parsons", "Rihanna", "Steve Martin"],
    ["Cupcakes", "Dana Ivgy", "Keren Berger", "Yael Bar-Zohar"],
    ["Man From Reno", "Ayako Fujitani", "Pepe Serna", "Kazuki Kitamura"],
    ["The Riot Club", "Sam Claflin", "Max Irons", "Douglas Booth"],
    ["The Salt of the Earth", "Sebasti\u00E3o Salgado", "Wim Wenders", "Juliano Ribeiro Salgado"],
    ["Serena", "Bradley Cooper", "Jennifer Lawrence", "Rhys Ifans"],
    ["While We're Young", "Ben Stiller", "Naomi Watts", "Adam Driver"],
    ["White God", "Zs\u00F3fia Psotta", "S\u00E1ndor Zs\u00F3t\u00E9r", "Lili Horv\u00E1th"],
    ["Furious 7", "Vin Diesel", "Paul Walker", "Dwayne Johnson"],
    ["5 to 7", "Anton Yelchin", "B\u00E9r\u00E9nice Marlohe", "Olivia Thirlby"],
    ["Cheatin'"],
    ["The Living", "Fran Kranz", "Jocelin Donahue", "Kenny Wormald"],
    ["Woman in Gold", "Helen Mirren", "Ryan Reynolds", "Daniel Br\u00FChl"],
    ["The Longest Ride", "Scott Eastwood", "Britt Robertson", "Alan Alda"],
    ["Clouds of Sils Maria", "Juliette Binoche", "Kristen Stewart", "Chlo\u00EB Grace Moretz"],
    ["Desert Dancer", "Nazanin Boniadi", "Freida Pinto", "Tom Cullen"],
    ["Ex Machina", "Alicia Vikander", "Domhnall Gleeson", "Oscar Isaac"],
    ["Freetown", "Henry Adofo", "Michael Attram", "Alphonse Menyo"],
    ["Kill Me Three Times", "Simon Pegg", "Teresa Palmer", "Alice Braga"],
    ["Child 44", "Tom Hardy", "Gary Oldman", "Noomi Rapace"],
    ["Monkey Kingdom", "Tina Fey"],
    ["The Dead Lands", "James Rolleston", "Lawrence Makoare", "Te Kohe Tuhaka"],
    ["Paul Blart: Mall Cop 2", "Kevin James", "Raini Rodriguez", "Eduardo Ver\u00E1stegui"],
    ["Adult Beginners", "Nick Kroll", "Rose Byrne", "Bobby Cannavale"],
    ["The Age of Adaline", "Blake Lively", "Michiel Huisman", "Harrison Ford"],
    ["Kung Fu Killer", "Donnie Yen", "Charlie Yeung", "Baoqiang Wang"],
    ["Misery Loves Comedy", "Amy Schumer", "James L. Brooks", "Judd Apatow"],
    ["Avengers: Age of Ultron", "Robert Downey Jr.", "Chris Evans", "Mark Ruffalo"],
    ["Far from the Madding Crowd", "Carey Mulligan", "Matthias Schoenaerts", "Michael Sheen"],
    ]

Back to contents