2016年8月17日水曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の7章(プロのようにデータを操る)、7.3(復習問題)を取り組んでみる。

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


print('7.1')
mystery = '\U0001f4a9'
print(mystery)

import unicodedata

print(unicodedata.name(mystery))

print('\n7.2')
pop_bytes = mystery.encode('utf-8')
print(pop_bytes)

print('\n7.3')
pop_string = pop_bytes.decode('utf-8')
print(pop_string)
print(pop_string == mystery)

print('\n7.4')
print(('My kitty cat likes %s,\n'
       'My kitty cat likes %s,\n'
       'My kitty cat fell on his %s\n'
       "And now thins he's a %s.") % ('roast beef', 'ham', 'head', 'clam'))

print('\n7.5')
letter = '''Dear {salutation} {name},

Tahnk you for your letter. We are sorry that our {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially
near any {animals}.

Send us your receipt and {amount} for shipping and handling. We will send
you another {product} that, in our tests, is {percent}% less likey to
have {verbed}.

Thank you for your support.

Sincerely,
{spokesman}
{job_title}'''

print('\n7.6')
response = dict(salutation='SALUTATION.',
                name='NAME',
                product='PRODUCT',
                verbed='VERBED',
                room='ROOM',
                animals='ANIMALS',
                amount='AMOUNT',
                percent='PERCENT',
                spokesman='SPOKESMAN',
                job_title='JOB_TITLE')
print(letter.format(**response))

print('\n7.7')
import requests

req = requests.get('http://www.gutenberg.org/cache/epub/36068/pg36068.txt')
mammoth = req.text

print('\n7.8')

import re

words = re.findall(r'\bc\w*\b', mammoth)
print(words[:5])

print('\n7.9')
words = re.findall(r'\bc\w{3}\b', mammoth)
print(words[:5])

print('\n7.10')
words = re.findall(r'\b\w*r\b', mammoth)
print(words[:5])

print('\n7.11')
words = re.findall(r'\b\w*[aeiou]{3}\w*\b', mammoth)
print(words[:5])

print('\n7.12')

import binascii

s = (
    '47494638396101000100800000000000ffffff21f90401000000002c00000000010001000'
    '0020144003b'
)

gif = binascii.unhexlify(s)

print('\n7.13')
gif_header = b'GIF89a'
print(gif_header == gif[:6])

print('\n7.14')
import struct

width, height = struct.unpack('>2H', gif[6:10])
print('width: {0}, height: {1}'.format(width, height))

入出力結果(Terminal, IPython)

$ ./sample.py
7.1
💩
PILE OF POO

7.2
b'\xf0\x9f\x92\xa9'

7.3
💩
True

7.4
My kitty cat likes roast beef,
My kitty cat likes ham,
My kitty cat fell on his head
And now thins he's a clam.

7.5

7.6
Dear SALUTATION. NAME,

Tahnk you for your letter. We are sorry that our PRODUCT VERBED in your
ROOM. Please note that it should never be used in a ROOM, especially
near any ANIMALS.

Send us your receipt and AMOUNT for shipping and handling. We will send
you another PRODUCT that, in our tests, is PERCENT% less likey to
have VERBED.

Thank you for your support.

Sincerely,
SPOKESMAN
JOB_TITLE

7.7

7.8
['cost', 'copy', 'cheese', 'cream', 'comprehensive']

7.9
['cost', 'copy', 'call', 'come', 'cows']

7.10
['for', 'whatsoever', 'or', 'under', 'or']

7.11
['glorious', 'queen', 'glorious', 'Queenston', 'Queenston']

7.12

7.13
True

7.14
width: 256, height: 256
$

0 コメント:

コメントを投稿