PyCon mini Osaka
(@ats)
❖
❖
( )
=
15
❖
❖
❖
❖
❖
❖ Python
❖ ( )
❖
❖
❖
❖
/
Pros.Cons. Pros. Cons.
(2)
❖
❖
❖ ( ) (
)
❖
❖
etc.
etc.
etc.
etc.
❖
❖ :
❖ =
❖ /
(2)
❖
❖
❖ 

❖ 

(3)
❖
❖
❖
❖


( )
❖
❖
❖
❖ ( )
❖
❖ (= )
0
❖
❖
❖
❖
❖ CPU
❖
(Z80)
3E002100F03C160FCD2800200436FF18
151603CD2800200436FE180A1605CD28
00200236FD7710DD5F92280230FB7BC9
FizzBuzz ( )
LD A,$00
LD HL,$f000
_LOOP:
INC A
LD D,$0F
CALL CANDIV
JR nz,_DIV3
LD (HL), $ff
JR _LOOPEND
_DIV3:
LD D,$03
CALL CANDIV
JR nz,_DIV5
LD (HL),$fe
JR _LOOPEND
_DIV5:
LD D,$05
CALL CANDIV
JR nz, _PUTNUM
LD (HL),$fd
77
10DD
5F
92
2802
30FB
7B
C9
_PUTNUM:
LD (HL), A
_LOOPEND:
DJNZ _LOOP
CANDIV:
LD E, A
_SUB:
SUB D
JR Z,_EXIT
JR NC,_SUB
_EXIT:
LD A, E
RET
3E00
2100F0
3C
160F
CD2800
2004
36FF
1815
1603
CD2800
2004
36FE
180A
1605
CD2800
2002
36FD
LD A,$00
LD HL,$f000
3E00
2100F0
( )
LD A $00
CANDIV:
LD E, A
_SUB:
SUB D
JR Z,_EXIT
JR NC,_SUB
_EXIT:
LD A, E
RET
A D
LD D,$0F
CALL CANDIV
:
5F
92
2802
30FB
7B
C9
❖
❖
❖
❖
❖
Python
: http://akaptur.com/blog/2013/08/14/python-bytecode-fun-with-dis/
>>> def foo():
... a = 2
... b = 3
... return a + b
...
>>> foo.func_code
<code object foo at 0x106353530, file "<stdin>",
line 1>
>>> print [ord(x) for x in foo.func_code.co_code]
[100, 1, 0, 125, 0, 0, 100, 2, 0, 125, 1, 0, 124,
0, 0, 124, 1, 0, 23, 83]
1
Python
>>> def foo():
... a = 2
... b = 3
... return a + b
...
>>> import dis
>>> dis.dis(foo)
2 0 LOAD_CONST 1 (2)
3 STORE_FAST 0 (a)
3 6 LOAD_CONST 2 (3)
9 STORE_FAST 1 (b)
4 12 LOAD_FAST 0 (a)
15 LOAD_FAST 1 (b)
18 BINARY_ADD
19 RETURN_VALUE
( )
100 1 0
125 0 0
100 2 0
125 1 0
124 0 0
124 1 0
23
83
❖ (ID )
❖ etc.
❖
❖
❖
❖
❖
❖ ( )
l = [158, 157, 163, 157, 145]
monk_fish_team = [158, 157, 163, 157, 145]
Better
for c in range(100):
print(c)
(2)
api = twitter.Api('consumerkey',
'consumersecret',
'accesstoken',
'accesstokensecret')
CONSUMER_KEY = ‘consumerkey’
CONSUMER_SECRET = ‘consumersecret’
ACCESS_TOKEN = ‘accesstoken’
ACCESS_TOKEN_SECRET = ‘accesstokensecret’
api = twitter.Api(CONSUMER_KEY, CONSUMER_SECRET,
ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
Better
❖
❖
❖
❖
❖
# person
if (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 ):
member_list.append(person)
def can_marry(person):
return (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 )
if can_marry(person):
member_list.append(person)
Better
Doc String
def can_marry(person):
“”” “””
return (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 )
Better
def can_marry(person):
return (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 )
Doc String ( )
❖ Python
❖
❖
❖
❖
if signal.color = ‘red’:
stop()
logging.info(‘Invalid: This is no longer
short string.’)
msg = (‘Invalid: This is no longer ‘
‘short string.’)
logging.info(msg)
Better
(2)
complex_dict = {
"data": [{"type": "locale", "lat":
-34.43778387240597, "lon": 150.04799169921876},
{"type": "poi", "lat": -34.96615974838191, "lon":
149.89967626953126},
{"type": "locale", "lat": -34.72271328279892,
"lon": 150.46547216796876},
{"type": "poi", "lat": -34.67303411621243, "lon":
149.96559423828126}]}
(3)
def msg_generator(irc):
“”” Bot Message “””
while irc.alive:
for msg in irc.recv():
if len(msg) > 3:
try:
yield Message(msg)
except Exception as e:
logging.info(‘Error %sn' % str(e)))
def msg_generator(irc):
“”” Bot Message “””
while irc:
for msg in irc.recv():
if not len(msg) > 3:
continue
yield handle_message(msg)
def handle_message(msg):
“”” “””
try:
return Message(msg)
except Exception as e:
logging.info(‘Error %sn' % str(e)))
Better
(4 )
❖
❖
❖
❖
❖
me = Person()
me.age = 49
myson = Person()
myson.age = 6
me.add_child(myson)
(2)
❖
❖
❖
❖ 

❖ 

❖
me = Person()
me.age = 49
myson = Person()
myson.age = 6
me.add_child(myson)
mydaughter = Person()
mudaughter.age = 3
me.add_child(mydaughter)
import audio
core = audio.AudioCore()
controller = audio.AudioController()
import audio
core = audio.Core()
controller = audio.Controller()
Better
/
( )
setter/getter VS property
person.age = 42
age = person.age
Better
setter/getter
@property
person.set_age(42)
age = person.get_age()
❖ Python
❖
❖
❖
class Vector2D:
def __add__(self, other):
return Vector2D(self.x+other.x,
self.y+other.y)
>>> vector_a = Vector2D(0, 1)
>>> vector_b = Vector2D(1, 0)
>>> vector_a+vector_b
Vector2D(1, 1)
>>> [1, 2, 3, 4]+[5]
>>> [1, 2, 3, 4].remove(4)
>>> {1, 2, 3}-{3}
>>> {1, 2, 3}.add(4)
(2)
class PowerOfTwo:
def __init__(self, max = 0):
self.max = max
def __iter__(self):
self.num = 0
return self
def __next__(self):
if self.num <= self.max:
result = 2 ** self.num
self.num += 1
return result
else:
raise StopIteration
>>> for i in PowerOfTwo(5):
... print(i)
...
1
2
4
8
16
32
(3)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return “Person(‘{}’, {})”.format(self.name, self.age)
__repr__()
OR
doctor_strange = Film("Doctor Strange”, "Scott Derrickson", "2016")
db_session.add(doctor_strange)
result_set = session.query(Film).filter(Film.title == ‘Doctor Strange’)
for r in result_set:
print(r)
INSERT INTO films (title, director, year)
VALUES ('Doctor Strange’,
'Scott Derrickson', ‘2016');
SELECT * FROM films WHERE
title = ‘Doctor Strange’;
RDBMS
2
vs
❖
❖ ( etc.)
❖
vs (2)
❖
❖
❖ ( )
❖
❖ PEP 8 - Style Guide for Python Code
❖ https://www.python.org/dev/peps/pep-0008/
❖ pycodestyle - PEP 8
❖ https://github.com/PyCQA/pycodestyle
❖
DocString
def train(train_data):
"""
Usage::
>>> data = [("orange", “fruits"),
… ("cabbage", "vesitables")]
>>> classifier = train(data)
:param train_data: ``(color, label)`` .
:rtype: A :class:`Classifier <Classifier>`
"""
PEP 257 Doc String
pydocstyle
❖ Sphinx
❖ PEP 257
❖ (HTML PDF ePub LaTeX)
❖ Python
❖
❖
# person
if (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 ):
member_list.append(person)
def can_marry(person):
return (person.gender == 'male' and
person.age >= 18) or
(person.gender == 'female' and
person.age >= 16 )
if can_marry(person):
member_list.append(person)
Better
❖
❖
❖
❖ github
❖
❖
❖ 100%
❖
❖ NG
❖
/
❖
3
❖ OS
❖ Web
❖ Python
❖
❖
❖
❖ (Python )
❖
❖
Python
: https://devguide.python.org/coredev/
When you have consistently contributed patches which meet quality standards without
requiring extensive rewrites prior to being committed, you may qualify for commit
privileges and become a core developer of Python. You must also work well with other
core developers (and people in general) as you become an ambassador for the Python
project.
Typically a core developer will offer you the chance to gain commit privilege. The
person making the offer will become your mentor and watch your commits for a while to
make sure you understand the development process. If other core developers agree that
you should gain commit privileges you are then extended an official offer.
Python
❖
❖
❖
❖
❖
❖
❖
❖
❖
コミュニケーションとしてのコード

コミュニケーションとしてのコード