2016年9月8日木曜日

開発環境

Introducing Python: Modern Computing in Simple Packages (Bill Lubanovic (著)、 O'Reilly Media)のChapter 7(Mangle Data Like a Pro)、Things to Do 7.1-14.を取り組んでみる。

Things to Do 7.1-14.

コード(Emacs)

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

import unicodedata

print('7.1.')
mystery = '\U0001f4a9'
print(mystery)
print(unicodedata.name(mystery))

print('7.2.')
pop_bytes = mystery.encode('utf-8')
print(pop_bytes)

print('7.3.')
pop_string = pop_bytes.decode('utf-8')
print(pop_string)
print(pop_string == mystery)

print('7.4.')
print('''My kitty cat likes %s,
My kitty likes %s,
My kitty cat fell on his %s
And now thinks he's a %s.''' % ('roast beef', 'ham', 'head', 'clam'))

print('7.5.')
letter = '''Deat {salutation} {name},

Thank you or 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} or shipping and handling. We
will send
you another {product} that, in our tests, is {percent}% less likely to
have {verbed}.

Thank you or your support.

Sincerely.
{spokesman}
{job_title}'''

print('7.6.')
response = dict(
    salutation='salutation0',
    name='name0',
    product='product0',
    verbed='verbed0',
    room='room0',
    animals='animals0',
    amount='amount0',
    percent='percent0',
    spokesman='spokesman0',
    job_title='job_title0'
)
print(letter.format(**response))

print('7.7.')
filename = 'Poems of James McIntyre by James McIntyre.txt'
with open(filename) as f:
    mammoth = f.read()

print(len(mammoth))

print('7.8.')

import re

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

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

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

print('7.11.')
words = re.findall(r'\b\w*[aeiou]{3}[^aeiou]\w*\b', mammoth)
print(words[:10])

print('7.12.')

import binascii

hex_str = (
    '47494638396101000100800000000000ffffff21f9'
    '0401000000002c000000000100010000020144003b'
)

gif = binascii.unhexlify(hex_str)

print('7.13.')
print(gif[:6] == b'GIF89a')

print('7.14')

import struct

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

入出力結果(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 likes ham,
My kitty cat fell on his head
And now thinks he's a clam.
7.5.
7.6.
Deat salutation0 name0,

Thank you or your letter. We are sorry that our product0 verbed0 in your
room0. Please note that it should never be used in a room0,
especially
near any animals0.

Send us your receipt and amount0 or shipping and handling. We
will send
you another product0 that, in our tests, is percent0% less likely to
have verbed0.

Thank you or your support.

Sincerely.
spokesman0
job_title0
7.7.
231875
7.8.
['cost', 'copy', 'cheese', 'cream', 'comprehensive', 'containing', 'composed', 'citizens', 'chasm', 'call']
7.9.
['cost', 'copy', 'call', 'come', 'cows', 'cars', 'come', 'come', 'care', 'came']
7.10.
['for', 'whatsoever', 'or', 'under', 'or', 'Author', 'Fair', 'our', 'year', 'Minister']
7.11.
['glorious', 'queen', 'glorious', 'Queenston', 'Queenston', 'Queenston', 'beauteous', 'Queenston', 'glorious', 'Queen']
7.12.
7.13.
True
7.14
width: 1, height: 1
$

0 コメント:

コメントを投稿

Comments on Google+: