Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unlocking Python's Core Magic

Lee Wei
September 28, 2024

Unlocking Python's Coreย Magic

Lee Wei

September 28, 2024

Transcript

135 slides
  1. wei-lee.me commitizen-tools Create committing rules for projects ๐Ÿš€ auto bump

    versions โฌ† and auto changelog generation ๐Ÿ“‚
  2. wei-lee.me Apache Airflow Apache Airflowยฎ is an open-source platform for

    developing, scheduling, and monitoring batch-oriented workflows.
  3. wei-lee.me Hey, I thought this was going to be a

    Python core-centric talk. Are we going to discuss libraries now?
  4. wei-lee.me What is this talk about? * Python core magics

    and standard libraries * Real-world examples of how these techniques are used in those libraries
  5. wei-lee.me What dunder methods? โ€ข Methods that start and end

    with __ (e.g., __init__) โ€ข Dunder โ†’ Double Underscore โ€ข Allow us to customize classes in a more Pythonic way โ€ข Implicitly called by Python โ€ข Officially named as special methods
  6. wei-lee.me List of dunder methods 1. x.__init__(a, b) 2. T.__new__(T,

    a, b) 3. x.__del__() 4. x.__eq__(y) 5. x.__ne__(y) 6. x.__lt__(y) 7. x.__rt__(y) 8. x.__le__(y) 9. x.__ge__(y) 10. x.__hash__() 11. x.__repr__() 12. x.__str__() 13. x.__bool__() 14. x.__int__() 15. x.__float__() 16. x.__bytes__() 17. x.__complex__() 18. x.__format__(s) 19. x.__enter__() 20. x.__exit__() 21. x.__len__() 22. x.__iter__() 23. x.__getitem__(a) 24. x.__setitem__(a, b) 25. x.__delitem__(a) 26. x.__contains__(a) 27. x.__reversed__() 28. x.__next__() 29. x.__missing__(a) 30. x.__length_hint__() 31. x.__add__(y) 32. y.__radd__(x) 33. x.__sub__(y) 34. y.__rsub__(x) 35. x.__mul__(y) 36. y.__rmul__(x) 37. x.__truediv__(y) 38. y.__rtruediv__(x) 39. x.__mod__(y) 40. y.__rmod__(x) 41. x.__floordiv__(y) 42. y.__rfloordiv__(x) 43. x.__pow__(y) 44. y.__rpow__(x) 45. x.__matmul__(y) 46. y.__rmatmul__(x) 47. x.__and__(y) 48. y.__rand__(x) 49. x.__or__(y) 50. y.__ror__(x) 51. x.__xor__(y) 52. y.__rxor__(x) 53. x.__rshift__(y) 54. y.__rrshift__(x) 55. x.__lshift__(y) 56. y.__rlshift__(x) 57. x.__neg__() 58. x.__pos__() 59. x.__invert__() 60. x.__divmod__(y) 61. x.__abs__() 62. x.__index__() 63. x.__round__() 64. x.__trunc__() 65. x.__floor__() 66. x.__ceil__() 67. x.__iadd__(y) 68. x.__isub__(y) 69. x.__imul__(y) 70. x.__itruediv__(y) 71. T.x.__set_name__(T, 'x') 72. T.x.__get__(t, T) 73. T.x.__set__(t, y) 74. T.x.__delete__(t) 75. T.__init_subclass__(U) 91. x.__ixor__(y) 92. x.__irshift__(y) 93. x.__ilshift__(y) 94. x.__getattribute__('y') 95. x.__getattr__('y') 96. x.__setattr__('y', z) 97. x.__delattr__('y') 98. x.__dir__() 99. x.__anext__() 100.x.__buffer__(flags) 101.x.__release_buffer__(m) 76. x.__mro_entries__([x]) 77. T.__class_getitem__(y) 78. type(base).__prepare__() 79. T.__instancecheck__(x) 80. T.__subclasscheck__(U) 81. x.__await__() 82. x.__aenter__() 83. x.__aexit__() 84. x.__aiter__() 85. x.__imod__(y) 86. x.__ifloordiv__(y) 87. x.__ipow__(y) 88. x.__imatmul__(y) 89. x.__iand__(y) 90. x.__ior__(y)
  7. wei-lee.me __new__ Example in Airflow - Why do we need

    it? https://github.com/apache/airflow/blob/35087d7d10714130cc3e9e9730e34b07fc56938d/ airflow/models/xcom_arg.py#L99-L102 XComArg should no longer be used. PlainXComArg should be used instead. But we want to keep XComArg backward compatibility.
  8. wei-lee.me __new__ ๐Ÿ•‘ When to use it? When you need

    to do operations before class creation
  9. wei-lee.me __new__ ๐Ÿ•‘ When to use it? - More specific

    Don't use it Use it only and only if you know what you're doing
  10. wei-lee.me __call__ Example in commitizen - Why we designd it

    this way https://github.com/commitizen-tools/commitizen/blob/ ee41af27f88c9e718c2a3e18ed7c8c10ef290d25/commitizen/cli.py#L119
  11. wei-lee.me __call__ Example in commitizen - Why we designd it

    this way https://github.com/commitizen-tools/commitizen/blob/ ee41af27f88c9e718c2a3e18ed7c8c10ef290d25/commitizen/cli.py#L119
  12. wei-lee.me __call__ Example in commitizen - Why we designd it

    this way https://github.com/commitizen-tools/commitizen/blob/ ee41af27f88c9e718c2a3e18ed7c8c10ef290d25/commitizen/cli.py#L119
  13. wei-lee.me __str__, __repr__ ๐Ÿค” What are they? __str__ __repr__ Called

    by str(obj), format(), print() repr() What it should be informal or nicely printable official look like a valid Python expression
  14. wei-lee.me __str__, __repr__ ๐Ÿ•‘ When to use them? When you

    want to print your objects with useful info
  15. wei-lee.me Operator Overloading * Dunder methods like __eq___, __and__, __or__,

    etc. * Called in operations like a == b, a and b, a or b, etc. ๐Ÿค” What are they?
  16. wei-lee.me Operator Overloading ๐Ÿ•‘ When to use it? When you

    want to leverage Python operators and make your object work more Pythonic
  17. wei-lee.me Module __getattr__ ๐Ÿค” What is it? Called when a

    module is unable to find a specified attribute
  18. wei-lee.me Module __getattr__ Example in Airflow - Deprecate global variable

    https://github.com/apache/airflow/pull/33189/files
  19. wei-lee.me Module __getattr__ ๐Ÿ•‘ When to use it? - More

    specific * Deprecate global variable * Lazy import
  20. wei-lee.me try...except...else... ๐Ÿ•‘ When to use it? When you want

    to execute something when exceptions do not occur
  21. wei-lee.me try...except...else... ๐Ÿ•‘ When to use it? - More specific

    Move the logic that is not supposed to raise an exception out of the try block
  22. wei-lee.me Decorator ๐Ÿค” What is it? A function that takes

    a function as an argument and wraps around that function
  23. wei-lee.me Decorator ๐Ÿ•‘ When to use it? When you have

    common logic that needs to be applied to multiple functions
  24. wei-lee.me functools.partial ๐Ÿ•‘ When to use it? When you have

    functions that looks almost the same except for a few arguments
  25. wei-lee.me Iterator ๐Ÿค” What is it? * Repeatedly calls to

    __next__() which returns successive items * Raise a StopIteration exception when there's no more data * Required __iter__() that returns the iterator object itself
  26. wei-lee.me Iterator Protocol ๐Ÿค” What is it? https://docs.python.org/3/library/stdtypes.html#iterator-types * __iter__()

    Return the iterator object itself * __next__() Return the next item from the iterator. Raise the StopIteration exception if there's no next item
  27. wei-lee.me Iterator Protocol Example in Airflow - Why do we

    need it? https://github.com/apache/airflow/pull/36202/files GCS blobs get new attributes when it's consumed
  28. wei-lee.me $ cat weilee.py __name__ = ๆŽๅ”ฏ / Wei Lee

    __what_i_am_doing__ = [ First Time Speaker @ PyCon Japan, Volunteer @ PyCon Taiwan, Member @ PyCon APAC, Maintainer of commitizen-tools, Software Engineer @ Astronomer, Committer @ Apache Airflow, ] __github__ = Github Lee-W __linkedin__ = linkedin clleew __site__ = pen https://wei-lee.me
  29. wei-lee.me File "weilee.py", line 1 __name__ = ๆŽๅ”ฏ / Wei

    Lee ^^^ SyntaxError: invalid syntax $ python weilee.py
  30. wei-lee.me PyCon JP 2024 Sprint! But I'll probably be a

    bit late. Hope I can arrive around 11:30. Sorry ๐Ÿ˜ž
  31. wei-lee.me I guess I also talk too much. Thanks James

    for the awesome keynote speech. and... this talk too much thing! ๐Ÿ™Œ

More Decks by Lee Wei

Other Decks in Programming