[TIP] Class에 사전(dict)으로 property 추가하는 법
·
분석 Python/구현 및 자료
아래 코드는 특정 값들을 바로 propert에 추가하는 코드다 kwargs에 config라는 것이 들어오면, 각각의 값들을 다 property를 해준다. import numpy as np def assign_config(self, kwargs): for key, value in kwargs.items(): setattr(self, key, value) if hasattr(self, 'config'): # print(self.env_config) for key, value in self.config.items(): # Check types based on default settings if hasattr(self, key): if type(getattr(self,key)) == np.ndarray: set..
[TIP / Pytorch] torch class name 얻는 방법
·
분석 Python/Pytorch
torch class 이름을 얻고자 할 때, 삽질을 하였기 떄문에, 블로깅다른 사람들은 삽질을 덜 하셨으면 한다!layer = torch.nn.Linear(10,5) layer.__class__.__name__
[Python] self 에 대해서 알아보기 (__init__ , __new__)
·
분석 Python/구현 및 자료
광고 한 번씩 눌러주세요! 블로그 운영에 큰 힘이 됩니다 :) class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self, other): print(f'{self.name} says hello to {other}.') Person instance object가 어떻게 생성되는지에 대한 예시가 있다. person = Person('John Smith', 25) print(person.__dict__) `__dict__` 속성을 호출하면, 객체 `person`에는 `name` 과 `age`가 있다. What’s self in the __init__() method? 이 글 참고 person.greet('..
[ Python ] class에 동적으로 객체 추가하는 방법
·
분석 Python/구현 및 자료
python에서 class 내부에 특정 객체 값을 넣고 싶을 때가 있다. class test : def __init__(self,) : pass 넣으려면 저런 식으로. 을 찍고 넣어야 한다. 하지만 저렇게 하면 먼가 새로운 것을 추가해야 할 때마다 다 저런 식으로 해야 한다는 것이다. 그렇지만 동적으로 할당하는 방법에 대해서 필자는 알고 싶었고, 다음과 같은 방법을 사용하면 된다. 2가지 방법을 찾았다. class test : def __init__(self,) : pass def update(self, dict) : self.__dict__.update(dict) 아니면 setattr을 사용하여서 문자형으로 넣어줄 수도 있다. 더 다양한 방법이 많겠지만, 일단 찾은 것은 여기까지이다 ㅎㅎ
[ Python ] Kafka 유용한 Command Class로 만들기
·
개발/Kafka
import os from kafka import KafkaConsumer class Commad : def __init__(self, path = "/usr/local/kafka/bin/" ) : self.consumer = KafkaConsumer( bootstrap_servers=['localhost:9092'], auto_offset_reset='latest' , # 'earliest', enable_auto_commit= True ,) self.path = path def show_topic(self,) : """ topic 보여주기 """ print("Topic : " , list(self.consumer.topics())) def create_topic(self, partition , rep..

AI 도구

AI 도구 사이드 패널

아래 AI 서비스 중 하나를 선택하여 블로그를 보면서 동시에 사용해보세요.

API 키를 입력하세요API 키를 저장하려면 저장 버튼을 클릭하세요API 키가 저장되었습니다
API 키를 입력하세요API 키를 저장하려면 저장 버튼을 클릭하세요API 키가 저장되었습니다
API 키를 입력하세요API 키를 저장하려면 저장 버튼을 클릭하세요API 키가 저장되었습니다
URL과 모델을 입력하세요설정을 저장하려면 저장 버튼을 클릭하세요설정이 저장되었습니다