ABOUT ME

-

  • Python fastcore: 파이썬 업그레이드 모듈
    컴퓨터/파이썬 2020. 10. 17. 00:24
    728x90
    반응형

    fastcore

     

    fast.ai

    fastcore Github 링크

    github.com

    fast.ai에서 제작한 파이썬 언어 확장 모듈

     

    1. 소개

    fastcore은 Julia 언어의 multiple dispatch, Ruby 언어의 mixins, Haskell 언어의 currying, binding 등

    다른 언어의 기능들이나, 파이썬 내장 기능을 업그레이드한 모듈이다.

     

    설치법은 conda나 pip으로 설치

    bash
    conda install fastcore
    bash
    pip install fastcore

     

    2. 기능

    글 모든 예제는 아래를 import 하는 걸 전제로 한다.

    python
    from fastcore.all import *

     

    1. Test assertion

    test_eq(a, b)

    : 위 함수는 assert a == b와 같다.

    python
    test_eq([0, 1, 2, 3], range(4)) # True

    test_fail(f, msg='', contains='')

    : 위 함수는 Exception을 test 할 수 있다.

    python
    def customException(): return 1 / 0 test_fail(customException) # ZeroDivisionError: division by zero

     

    나머지 test 함수들은 문서 참고: 링크 열기

     

    2. **kwargs 위임

    delegates 문서 링크

     

    delegates (위임) 함수로 **kwargs를 명확히 표시할 수 있다.

    예를 들어 inspect.signature(함수나 클래스)를 이용하면 그 함수/클래스의 매개변수 목록이 나오는데,

    (API 사용할 때 유용)

     

    기본 파이썬에선 base로 하는 함수/클래스 키워드들을 정확히 표시 안 해주지만

    (e, a, **kwargs)

    python
    import inspect def adder(a, b=1, c=2, d=3): return a + b + c def more_adder(e, a, **kwargs): return e + adder(a, **kwargs) print(inspect.signature(more_adder) # 결과 = <Signature (e, a, **kwargs)>

     

    @delegates() 함수를 사용하면 아래와 같이 나온다.

    (e, a, b=1, c=2, d=3)

    python
    import inspect def adder(a, b=1, c=2, d=3): return a + b + c @delegates(adder) def more_adder(e, a, **kwargs): return e + adder(a, **kwargs) print(inspect.signature(more_adder) # 결과 = <Signature (e, a, b=1, c=2, d=3)>

     

    3. class init 함수 간결화

    store_attr() 문서 링크

    맘에 드는 기능 중 하나인데, store_attr() 함수를 이용한 예제를 보자.

    1. 원본: Node의 f, g, h 값 설정, self.? 를 반복적으로 사용하기 귀찮다.

    python
    class Node: def __init__(self, f, g, h=0): self.f = f self.g = g self.h = h

    2. fastcore의 store_attr() 사용

    python
    class Node: def __init__(self, f, g, h=0): store_attr()

    둘 다 같은 결과가 나온다.

    python
    node = Node(1, 2, 3) print(node.f, node.g, node.h) # 1, 2, 3

     

    store_attr 에다가 but=["param"]이나 cast=True를 이용할 수 있다.

    cast 예제

    python
    class T: def __init__(self, a:L, b, c:str): store_attr('a,b,c', cast=True) t = T(1,c=2,b=3) # 1 알아서 [1] , b = 3, 2 알아서 '2' 변한다. assert t.a==[1] and t.b==3 and t.c=='2'

     

    4. Type dispatch

    Julia란 언어의 멀티 디스패치 기능인데, Generic이랑 비슷? 하다.

    예제

    python
    @typedispatch def print_type(x: float): print("I'm float") @typedispatch def print_type(x: int): print("I'm int") @typedispatch def print_type(x: str): print("I'm str") @typedispatch def print_type(x: list): print("I'm list") @typedispatch def print_type(x: Callable): print("I'm function") if __name__ == "__main__": print_type(11.0) # I'm float print_type(11) # I'm int print_type("eleven") # I'm str print_type([11]) # I'm list print_type(print_type) # I'm function

     

    5. __repr__

    1) 파이썬 원본

    python
    class Node: def __init__(self, f, g, h=0): store_attr() print(Node(1, 2, 3)) # 결과 : <__main__.Node object at 0x000001A3E3DD4A48>

    2) fastcore 이용

    python
    class Node: def __init__(self, f, g, h=0): store_attr() __repr__ = basic_repr("f,g,h") print(Node(1, 2, 3)) # 결과 : Node(f=1, g=2, h=3)

     

    6. @patch

    @patch 데코레이터는 외부 라이브러리에 자신의 함수를 추가할 때 유용하다.

    예제

    bigO 라이브러리에 add란 함수를 추가한다.

    add 함수는 random 배열에다가 원하는 요소 하나를 추가한다.

    python
    from bigO import bigO @patch def add(self: bigO, num): return self.genRandomArray(10) + [num] if __name__ == "__main__": lib = bigO() print(lib.add("ADD THIS")) # genRandomArray(10) 크기 10 랜덤 배열을 생성한다. # 결과 # [9, 9, -9, -10, -1, -2, -2, 6, -10, 10, 'ADD THIS']

     

    7. mixin

    fastcore가 Path 라이브러리에 ls() 기능을 patch 시켰다.

    원본 파이썬에선 ls()가 없다. 하지만, fastcore랑 같이 import를 하면 아래와 같다.

    python
    from pathlib import Path p = Path("JS") # JS 폴더 불러오기 print(p.ls()) # dos 스타일 ls 명령어 # 결과 : (#6) [Path('JS/coffee html.js'),Path('JS/discordBot.js'),Path('JS/HTML5 audio.js'),Path('JS/imageminwebp.js'),Path('JS/test.json'),Path('JS/Untitled-3.js')]

     

    아까 배운 patch를 이용해서 ls를 한 배열의 길이를 구해보자.

    python
    @patch def length(self:Path): return len(self.ls()) p.length() # 6

     

    8. Self (람다)

    대문자 S, Self.sum()을 하면 lambda o: o.sum()한 것과 같다.

    python
    from fastcore.utils import Self, L if __name__ == "__main__": f = Self.sum() x = L(1, 2, 3) print(f(x)) f = Self.map(lambda a: a + 1) x = L(1, 2, 3) print(f(x)) f = Self.map(float) x = L(1, 2, 3) print(f(x)) f = Self.map(lambda a: a ** 2) x = L(1, 2, 3) print(f(x)) f = Self.filter(lambda a: a % 2 == 1) x = L(1, 2, 3) print(f(x)) f = Self.reduce(lambda a, b: a if a > b else b) x = L(1, 2, 3) print(f(x))

     

    9. L (List 업그레이드 버전)

    마지막으로 소개해드릴 기능은, 파이썬의 리스트를 확장시킨 fastcore의 L이다.

    print 하면 앞에 길이도 나온다.

    python
    a = L(1, 2, 3) print(a) # (#3) [1,2,3] print(a[1]) # 2

     

    예제

    python
    print(L.range(20).shuffle()) # 결과: (#20) [6,15,10,1,14,3,17,19,9,12...] 10 이상부턴 truncate 해준다. '...' print(1 + L(2,3,4)) # List + Int 가능 # 결과: (#4) [1,2,3,4] print(p[2,4,6]) # 인덱스 모음으로 배열을 만들어준다. # 결과: (#3) [10,14,17]

     

    쓸만한 기능들이 더 많은데, 공식 링크에서 한 번 구경해보면 좋다.

     

    참고 링크

    fast.ai fastcore 공식 문서 링크

    fastcore Github 링크

     

    fastai/fastcore

    Python supercharged for the fastai library. Contribute to fastai/fastcore development by creating an account on GitHub.

    github.com

     

    728x90

    '컴퓨터 > 파이썬' 카테고리의 다른 글

    댓글