Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

186 views

Published on

PyCon mini Osaka 2018で使ったスライドです。

Published in: Engineering
  • Be the first to comment

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

  1. 1. PyCon mini Osaka (@ats)
  2. 2. ❖ ❖
  3. 3. ( ) =
  4. 4. 15
  5. 5. ❖ ❖ ❖ ❖ ❖
  6. 6. ❖ Python ❖ ( ) ❖
  7. 7. ❖ ❖ ❖
  8. 8. / Pros.Cons. Pros. Cons.
  9. 9. (2) ❖ ❖ ❖ ( ) ( ) ❖ ❖
  10. 10. etc. etc. etc. etc.
  11. 11. ❖ ❖ : ❖ = ❖ /
  12. 12. (2) ❖ ❖ ❖ 
 ❖ 

  13. 13. (3) ❖ ❖ ❖ ❖ 
 ( )
  14. 14. ❖ ❖ ❖ ❖ ( ) ❖ ❖ (= )
  15. 15. 0
  16. 16. ❖ ❖
  17. 17. ❖ ❖ ❖ CPU ❖
  18. 18. (Z80) 3E002100F03C160FCD2800200436FF18 151603CD2800200436FE180A1605CD28 00200236FD7710DD5F92280230FB7BC9 FizzBuzz ( )
  19. 19. 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
  20. 20. LD A,$00 LD HL,$f000 3E00 2100F0 ( ) LD A $00
  21. 21. 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
  22. 22. ❖ ❖ ❖ ❖ ❖
  23. 23. 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]
  24. 24. 1
  25. 25. 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
  26. 26. ❖ (ID ) ❖ etc. ❖ ❖ ❖
  27. 27. ❖ ❖ ❖ ( )
  28. 28. l = [158, 157, 163, 157, 145] monk_fish_team = [158, 157, 163, 157, 145] Better for c in range(100): print(c)
  29. 29. (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
  30. 30. ❖ ❖ ❖ ❖ ❖
  31. 31. # 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
  32. 32. 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 ( )
  33. 33. ❖ Python ❖ ❖ ❖ ❖
  34. 34. 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
  35. 35. (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}]}
  36. 36. (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 )
  37. 37. ❖ ❖ ❖ ❖ ❖
  38. 38. me = Person() me.age = 49 myson = Person() myson.age = 6 me.add_child(myson)
  39. 39. (2) ❖ ❖ ❖ ❖ 
 ❖ 
 ❖ me = Person() me.age = 49 myson = Person() myson.age = 6 me.add_child(myson) mydaughter = Person() mudaughter.age = 3 me.add_child(mydaughter)
  40. 40. import audio core = audio.AudioCore() controller = audio.AudioController() import audio core = audio.Core() controller = audio.Controller() Better / ( )
  41. 41. setter/getter VS property person.age = 42 age = person.age Better setter/getter @property person.set_age(42) age = person.get_age()
  42. 42. ❖ Python ❖ ❖ ❖
  43. 43. 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)
  44. 44. (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
  45. 45. (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__()
  46. 46. 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
  47. 47. 2
  48. 48. vs ❖ ❖ ( etc.) ❖
  49. 49. vs (2) ❖ ❖ ❖ ( )
  50. 50. ❖ ❖ PEP 8 - Style Guide for Python Code ❖ https://www.python.org/dev/peps/pep-0008/ ❖ pycodestyle - PEP 8 ❖ https://github.com/PyCQA/pycodestyle ❖
  51. 51. 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
  52. 52. ❖ Sphinx ❖ PEP 257 ❖ (HTML PDF ePub LaTeX) ❖ Python ❖ ❖
  53. 53. # 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
  54. 54. ❖ ❖ ❖ ❖ github ❖ ❖
  55. 55. ❖ 100% ❖ ❖ NG ❖ / ❖
  56. 56. 3
  57. 57. ❖ OS ❖ Web ❖ Python ❖
  58. 58. ❖ ❖ ❖ (Python ) ❖ ❖
  59. 59. 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
  60. 60. ❖ ❖ ❖ ❖
  61. 61. ❖ ❖ ❖ ❖ ❖

×
Save this presentationTap To Close