Pythonを使ってみよう

Python 3 インストール

$ wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
$ sudo rpm -Uvh epel-release-6-8.noarch.rpm
$ sudo yum -y install ansible
$ ansible --version
ansible 2.3.0.0
$ sudo yum -y install git
$ sudo yum install patch
$ vi playbook.yml
---
- hosts: localhost
  connection: local
  sudo: yes
  vars:
    - python_version: 3.5.2
  tasks:
    - name: disable iptables
      service: name=iptables state=stopped enabled=no
    - name: install libselinux-python
      yum: name=libselinux-python state=latest
    - name: remove localtime
      file: path=/etc/localtime state=absent
    - name: change timezone
      file: src=/usr/share/zoneinfo/Asia/Tokyo dest=/etc/localtime state=link force=yes mode=0644
    - name: change locale
      lineinfile: >-
        dest=/etc/sysconfig/i18n
        state=present
        regexp=^LANG=
        line='LANG="ja_JP.UTF-8"'

    - name: install dependencies
      yum: name={{item}} state=present
      with_items:
        - gcc
        - openssl
        - openssl-devel
        - rpm-build
        - gcc-c++
        - bzip2
        - bzip2-devel
        - libtool
        - zlib
        - zlib-devel
        - httpd-devel
        - openssl-devel
        - curl-devel
        - ncurses-devel
        - gdbm-devel
        - readline
        - readline-devel
        - sqlite
        - sqlite-devel
        - libyaml-devel
        - libffi-devel
        - bison

    - name: check pyenv installed
      command: test -x /home/vagrant/.pyenv
      register: pyenv_present
      ignore_errors: yes
      become: no
    - name: git clone pyenv
      git: repo=https://github.com/yyuu/pyenv.git dest=/home/vagrant/.pyenv
      when: pyenv_present.rc != 0
      become: no

    - name: check pyvirtual installed
      command: test -x /home/vagrant/.pyenv/plugins/pyenv-virtualenv
      register: pyvirtual_present
      ignore_errors: yes
      become: no
    - name: git clone pyenv-virtual
      git: repo=https://github.com/yyuu/pyenv-virtualenv.git dest=/home/vagrant/.pyenv/plugins/pyenv-virtualenv
      when: pyvirtual_present != 0
      become: no

    - name: update pyenv
      command: git pull --rebase chdir=/home/vagrant/.pyenv
      become: no
    - name: update pyenv-virtualenv
      command: git pull --rebase chdir=/home/vagrant/.pyenv/plugins/pyenv-virtualenv
      become: no

    - name: check python installed
      shell: /bin/bash -lc "pyenv versions | grep {{python_version}}"
      register: python_installed
      ignore_errors: yes
      become: no
    - name: install python
      shell: /bin/bash -lc "pyenv install {{python_version}} && pyenv rehash && pyenv global {{python_version}}"
      when: python_installed.rc != 0
      become: no
$ ansible-playbook playbook.yml
$ vi .bashrc
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
fi
$ source ~/.bashrc
$ ansible-playbook playbook.yml
$ python -V
Python 3.5.2
$ pwd
/home/vagrant/python3_lessons

はじめてのPythonプログラム

$ python
Python 3.5.2 (default, May 20 2017, 02:46:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 + 2
5
>>> exit()
# coding: utf-8
print("Hello World")
$ python myapp.py
Hello World
# coding: utf-8
"""
Comment
Comment
Comment
"""
# Comment
print("Hello World")

定数について

ADMIN_EMAIL = "yuji@example.com"

データの演算について

y = 4
# y = y + 12
y += 12
print(y) # 16
print(True and False) # False
print(True or False) # True
print(not True) # False

文字列に値を埋め込んでみよう

name = "yuji"
score = 52.8

print("name: %s, score: %f" % (name, score))
$ python myapp.py
name: yuji, score: 52.800000
name = "yuji"
score = 52.8
 
print("name: %-10s, score: %10.2f" % (name, score))
$ python myapp.py
name: yuji      , score:      52.80
name = "yuji"
score = 52.8

print("name: {0}, score: {1}".format(name, score))
$ python myapp.py
name: yuji, score: 52.8
name = "yuji"
score = 52.8

print("name: {0:>10s}, score: {1:<10.2f}".format(name, score))
$ python myapp.py
name:       yuji, score: 52.80

ifで条件分岐をしてみよう

score = int(input("score ? "))

if score > 80:
    print("Great!")
elif score > 60:
    print("Good!")
else:
    print("so so ...")
$ python myapp.py
score ? 90
Great!
score = int(input("score ? "))

print("Great!" if score > 80 else "so so ...")

関数の返り値を使ってみよう

def say_hi():
     return "hi"
    print("hello") # return 後の記述のため処理されない

msg = say_hi()
print(msg)
$ python myapp.py
hi
def say_hi():
    pass

msg = say_hi()
print(msg)
$ python myapp.py
None

変数のスコープを理解しよう

msg = "hello" # グローバル変数

def say_hi():
    global msg
    msg = "hello global"
    print(msg)

say_hi()
print(msg)

クラス変数を使ってみよう

class User:
    # クラス変数
    count = 0
    def __init__(self, name):
        User.count += 1
        self.name = name

print(User.count) # 0
tom = User("tom")
bob = User("bob")
print(User.count) # 2

print(tom.count) # 2
$ python myapp.py
0
2
2

メソッドを使ってみよう

class User:
    count = 0
    def __init__(self, name):
        User.count += 1
        self.name = name
    # インスタンスメソッド
    def say_hi(self):
        print("hi {0}".format(self.name))
    # クラスメソッド
    @classmethod
    def show_info(cls):
        print("{0} instances".format(cls.count))

tom = User("tom")
bob = User("bob")

tom.say_hi()
bob.say_hi()

User.show_info()
$ python myapp.py
hi tom
hi bob
2 instances

アクセス制限をしてみよう

class User:
  def __init__(self, name):
    self._name = name
  def say_hi(self):
    print("hi {0}".format(self._name))

tom = User("tom")
print(tom._name)
tom.say_hi()
$ python myapp.py
tom
hi tom
class User:
  def __init__(self, name):
    self.__name = name
  def say_hi(self):
    print("hi {0}".format(self.__name))

tom = User("tom")
print(tom._name)
# print(tom._User__name) # 左記だとアクセス可能という抜け道あり
tom.say_hi()
$ python myapp.py
Traceback (most recent call last):
  File "myapp.py", line 18, in <module>
    print(tom._name)
AttributeError: 'User' object has no attribute '_name'

クラスを継承してみよう

class User:
    def __init__(self, name):
        self.name = name
    def say_hi(self):
        print("hi {0}".format(self.name))

class AdminUser(User):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age
    def say_hello(self):
        print("hello {0} ({1})".format(self.name, self.age))

bob = AdminUser("bob", 23)
print(bob.name)
bob.say_hi()
bob.say_hello()
$ python myapp.py
bob
hi bob
hello bob (23)
class User:
    def __init__(self, name):
        self.name = name
    def say_hi(self):
        print("hi {0}".format(self.name))

class AdminUser(User):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age
    def say_hello(self):
        print("hello {0} ({1})".format(self.name, self.age))
    # override
    def say_hi(self):
        print("[admin] hi {0}".format(self.name))

bob = AdminUser("bob", 23)
print(bob.name)
bob.say_hi()
bob.say_hello()
$ python myapp.py
bob
[admin] hi bob
hello bob (23)

クラスの多重継承をしてみよう

class A:
    def say_a(self):
        print("A!")
class B:
    def say_b(self):
        print("B!")

class C(A, B):
     pass

c = C()
c.say_a()
c.say_b()
$ python myapp.py
A!
B!
class A:
    def say_a(self):
        print("A!")
    def say_hi(self):
        print("hi! from A!")
class B:
    def say_b(self):
        print("B!")
    def say_hi(self):
        print("hi! from B!")

class C(B, A):
    pass

c = C()
c.say_hi()
$ python myapp.py
hi! from B!

モジュールでファイル分割してみよう

class User:
    def __init__(self, name):
        self.name = name
    def say_hi(self):
        print("hi {0}".format(self.name))

class AdminUser(User):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age
    def say_hello(self):
        print("hello {0} ({1})".format(self.name, self.age))
    def say_hi(self):
        print("[admin] hi {0}".format(self.name))

print("hello")
import user
  
bob = user.AdminUser("bob", 23)
   
print(bob.name)
bob.say_hi()
bob.say_hello()
$ python myapp.py
hello
bob
[admin] hi bob
hello bob (23)
from user import AdminUser, User

bob = AdminUser("bob", 23)

tom = User("tom")

print(bob.name)
bob.say_hi()
bob.say_hello()
class User:
    def __init__(self, name):
        self.name = name
    def say_hi(self):
        print("hi {0}".format(self.name))

class AdminUser(User):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age
    def say_hello(self):
        print("hello {0} ({1})".format(self.name, self.age))
    def say_hi(self):
        print("[admin] hi {0}".format(self.name))

# print("hello")
$ python myapp.py
bob
[admin] hi bob
hello bob (23)

パッケージでモジュールを管理しよう

/home/vagrant/python3_lessons
|--myapp.py
|--mypackage
|  |--__init__.py
|  |--user.py
import mypackage.user
  
bob = mypackage.user.AdminUser("bob", 23)
  
print(bob.name)
bob.say_hi()
bob.say_hello()
$ python myapp.py
bob
[admin] hi bob
hello bob (23)
import mypackage.user as mymodule

bob = mymodule.AdminUser("bob", 23)

print(bob.name)
bob.say_hi()
bob.say_hello()
from mypackage.user import AdminUser

bob = AdminUser("bob", 23)

print(bob.name)
bob.say_hi()
bob.say_hello()

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS