[Python]#0 쉽게 잊는 것들 링크로 모아보기

Clay Ryu's sound lab·2022년 3월 23일
0

Framework

목록 보기
1/48

Super().init()

super().init()이라는 코드가 다른 클래스의 속성 및 메소드를
자동으로 불러와 해당 클래스에서도 사용이 가능하도록 해준다.

https://jimmy-ai.tistory.com/79
https://dojang.io/mod/page/view.php?id=2386

args, kwargs

파라미터를 몇개를 받을지 모르는 경우 사용한다. args는 튜플 형태로 전달된다.
파라미터 명을 같이 보낼 수 있다. kwargs는 딕셔너리 형태로 전달된다.

optimizer_param={'lr':0.01}
print({**optimizer_param})

https://jhproject.tistory.com/109

차원 줄이기

# sobel filter
# [이 4개이므로 4차원이다.
w_y = np.array([[[[1,2,1],[0,0,0],[-1,-2,-1]]]])
w_y[0][0] => [[1,2,1],[0,0,0],[-1,-2,-1]]

defaultdict()

dictionary 알아서 추가

https://dongdongfather.tistory.com/69

np.array와 tuple, list와의 차이

np.array([2,2,2,2]) + (4,)
# array([6, 6, 6, 6])

(2,2,2,2) + (4,)
# (2, 2, 2, 2, 4)

[2,2,2,2] + [4,]
# [2, 2, 2, 2, 4]

np.add.at(A, idx, B)

A에다가 idx의 순서대로 B의 array의 각 행을 더해준다.

https://rfriend.tistory.com/545

collections.counter

counts = collections.Counter()
counts['apple'] += 1

https://excelsior-cjh.tistory.com/94

np.array에서 벡터와 행렬의 표현

요거 은근히 헷갈린다. 가끔 오랜만에 벡터의 shape을 보게되면 아 왜 콤마 뒤에 아무것도 없지라는 생각을 하게 된다. 하지만 실제로 아무것도 없으니까 안적는 것이었다.

a = np.array([[1,2]])
a.shape # (1, 2)

a = np.array([1,2])
a.shape # (2,)

Path

os를 대신해서 사용한다. 익숙해지면 이게 더 편하다고 한다.
크게 봐서 2가지를 익혀야 할듯 하다. 그외에도 엄청나게 많은 메소드들이 있는데 이는 써보면서 익혀나가면 될것 같다.

만들기 p_file =  path(" ")
경로추가 p_file / 'temp'

https://engineer-mole.tistory.com/191

Generator

특별한 경우에 리턴이 array형태가 아니라 generator의 형태로 뽑아 쓸수 있게 만들어진다.

cnt = 0
for i in Path('nsynth-test').rglob("*.wav"):
  if cnt < 3:
    print(i)
    cnt += 1
    
nsynth-test/audio/guitar_acoustic_014-100-127.wav
nsynth-test/audio/organ_electronic_007-031-025.wav
nsynth-test/audio/flute_synthetic_000-062-025.wav

https://wikidocs.net/16069

List Comprehension

이중 for문을 사용하면 왼쪽 것이 더 큰 카테고리로 만들어진다.

[ (x, y) for x in ['쌈밥', '치킨', '피자'] for y in ['사과', '아이스크림', '커피']]

[('쌈밥', '사과'),
 ('쌈밥', '아이스크림'),
 ('쌈밥', '커피'),
 ('치킨', '사과'),
 ...

https://wikidocs.net/22805

Class call

간단히 말하면 call은 객체를 호출하는 함수이다.
보통 어떤 클래스 안에서 다른 클래스를 호출하는 경우 (vocab이 너무 크고 방대해서 다른 클래스로 표현할 수 밖에 없는 경우) 그 클래스를 object로 만들어 주었다면 get_item이 아니라 call을 이용해야 한다.

https://wjunsea.tistory.com/61

datetime.datetime.now().strftime()

from datetime import datetime
now = datetime.now()

date = now.strftime('%Y-%m-%d')
print(date)      # 2021-04-08
 
time = now.strftime('%H:%M:%S')
print(time)      # 21:28:20
 
datetime = now.strftime('%Y-%m-%d %H:%M:%S')
print(datetime)  # 2021-04-08 21:28:20
%y : 두 자리 수의 연도 ex) 19, 20, 21

%Y : 네 자리 수의 연도 ex) 2019, 2020, 2021

%m : 0을 채운 두 자리 수의 월 ex) 01, 02 ...  11 ,12

%d : 0을 채운 두 자리 수의 일 ex) 01, 02 ...  30, 31

%I : 0을 채운 12시간제의 시간 ex) 01, 0212

%H : 0을 채운 24시간제의 시간 ex) 00, 0123

%M : 0을 채운 두 자리 수의 분 ex) 00, 01 ... 58, 59

%S : 0을 채운 두 자리 수의 초 ex) 00, 01 ... 58, 59

https://dev-jy.tistory.com/5

profile
chords & code // harmony with structure

0개의 댓글