μμ±μ : μ ν¬μ¬
λ³Έ μλ£λ 'νμ΄μ¬ λμμΈ ν¨ν΄ 2/e (Chetan Giridhar)' μ±
μ ν λλ‘ μμ±λμμ΅λλ€. λ°λΌμ λͺ¨λ μμ μ½λμλ μ μλ₯Ό νμνλ __author__ = 'Chetan'
μ΄ ν¬ν¨λ©λλ€.
Table of Contents
μ±κΈν€ λμμΈ ν¨ν΄μ κΈλ‘λ²νκ² μ κ·Ό κ°λ₯ν λ¨ ν κ°μ κ°μ²΄λ§μ νμ©νλ ν¨ν΄μ΄λ€.
- ν΄λμ€μ λν λ¨μΌ κ°μ²΄ μμ±
- μ μ κ°μ²΄ μ 곡
- 곡μ λ 리μμ€μ λν λμ μ κ·Ό μ μ΄
__author__ = 'Chetan'
class Singleton(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Singleton, cls).__new__(cls)
return cls.instance
s = Singleton()
print("Object created", s)
s1 = Singleton()
print("Object created", s1)
''' <μ€ν κ²°κ³Ό>
Object created <__main__.Singleton object at 0x1005c12b0>
Object created <__main__.Singleton object at 0x1005c12b0>
'''
- ν κ°μ instance ν΄λμ€ μΈμ€ν΄μ€λ₯Ό μμ±νλ€.
- μ΄λ―Έ μμ±λ μΈμ€ν΄μ€κ° μλ€λ©΄ μ¬μ¬μ©νλ€.
μΈμ€ν΄μ€κ° κΌ νμν μν©μΌ λ μ΄κΈ°νλ₯Ό νλ€. μ¬μ©ν μ μλ 리μμ€κ° μ νμ μΌ λ μ¬μ©νλ λ°©μμ΄λ€.
__author__ = 'Chetan'
class Singleton:
__instance = None
def __init__(self):
if not Singleton.__instance:
print(" __init__ method called..")
else:
print("Instance already created:", self.getInstance())
@classmethod
def getInstance(cls):
if not cls.__instance:
cls.__instance = Singleton()
return cls.__instance
s = Singleton()
## ν΄λμ€λ μ΄κΈ°ν λμμ§λ§ μμ§ κ°μ²΄λ μμ±λμ§ μμλ€.
print("Object created", Singleton.getInstance())
## κ°μ²΄λ₯Ό μμ±νλ€.
s1 = Singleton()
## κ°μ²΄λ μ΄λ―Έ μμ±λμλ€.
''' <μ€ν κ²°κ³Ό>
__init__ method called..
__init__ method called..
Object created <__main__.Singleton object at 0x10ca025c0>
Instance already created: <__main__.Singleton object at 0x10ca025c0>
'''
getInstance()
λ‘ μ»μ΄μ€λ μ£Όμκ° μΌμΉνλ κ²μ λ³Ό μ μλ€.
νμ΄μ¬μμ λͺ¨λ λͺ¨λμ κΈ°λ³Έμ μΌλ‘ μ±κΈν€μ΄λ€. ν κ°μ κ°μ²΄λ§ μ μ§νκ³ λ°ννλ μ±κΈν€ λ°©μμ΄λ€. νμ΄μ¬μ λͺ¨λ μν¬νΈ λ°©μμ λ€μκ³Ό κ°λ€.
- νμ΄μ¬ λͺ¨λμ΄ μν¬νΈλλμ§ νμΈνλ€.
- μν¬νΈ λλ€λ©΄ ν΄λΉ κ°μ²΄λ₯Ό λ°ννλ€.
- μν¬νΈλμ§ μμλ€λ©΄ μν¬νΈνκ³ μΈμ€ν΄μ€ν νλ€.
- λͺ¨λμ μν¬νΈμ λμμ μ΄κΈ°νλλ€. νμ§λ§ κ°μ λͺ¨λμ λ€μ μν¬νΈνλ©΄ μ΄κΈ°ννμ§ μλλ€.
import sys
import sys
μ΄μ²λΌ κ°μ λͺ¨λμ μ€λ³΅ν΄μ μν¬νΈν΄λ sysκ°μ²΄λ νλλ§ μμ±λλ€.
μ±κΈν€ ν¨ν΄κ³Ό λ¬λ¦¬ λ κ° μ΄μμ κ°μ²΄κ° μμ±λ μ μμ§λ§ λͺ¨λ κ°μ²΄κ° μνλ₯Ό 곡μ νλ€.
__author__ = 'Chetan'
class Borg:
__shared_state = {}
def __init__(self):
self.x = 1
self.__dict__ = self.__shared_state
pass
b = Borg()
b1 = Borg()
b.x = 3
print("Borg Object 'b': ", b)
print("Borg Object 'b1': ", b1)
## bμ b1μ μλ‘ λ€λ₯Έ κ°μ²΄λ€.
print("Object State 'b':", b.__dict__)
print("Object State 'b1':", b1.__dict__)
## κ·Έλ¬λ μλ‘ stateλ₯Ό 곡μ νλ€.
''' <μ€ν κ²°κ³Ό>
Borg Object 'b': <__main__.Borg object at 0x10d6d95c0>
Borg Object 'b1': <__main__.Borg object at 0x10d6d95f8>
Object State 'b': {'x': 3}
Object State 'b1': {'x': 3}
'''
λ©νν΄λμ€λ ν΄λμ€μ ν΄λμ€λ€. μ¦ ν΄λμ€λ μμ μ λ©νν΄λμ€μ μΈμ€ν΄μ€λ€. ν΄λμ€λ λ©νν΄λμ€κ° μ μνλ€.
class A ꡬ문μΌλ‘ ν΄λμ€λ₯Ό μμ±νλ©΄ νμ΄μ¬μ A = type(name, bases, dict) λ₯Ό μ€ννλ€. μ΄λ―Έ μ μλ λ©νν΄λμ€κ° μλ€λ©΄ νμ΄μ¬μ A = MetaKls(name, bases, dict) λ₯Ό μ€νν΄ ν΄λμ€λ₯Ό μμ±νλ€.
name
: ν΄λμ€λͺbase
: λ² μ΄μ€ ν΄λμ€dict
: μμ± κ°
__author__ = 'Chetan'
class MyInt(type):
def __call__(cls, *args, **kwds):
print("***** Here's My int *****", args)
print("Now do whatever you want with these objects...")
return type.__call__(cls, *args, **kwds)
class int(metaclass=MyInt):
def __init__(self, x, y):
self.x = x
self.y = y
pass
i = int(4,5)
i = int(7,8)
''' <μ€ν κ²°κ³Ό>
***** Here's My int ***** (4, 5)
Now do whatever you want with these objects...
***** Here's My int ***** (7, 8)
Now do whatever you want with these objects...
'''
__call__
λ©μλλ μ΄λ―Έ μ‘΄μ¬νλ ν΄λμ€μ κ°μ²΄λ₯Ό μμ±ν λ νΈμΆλλ νμ΄μ¬μ νΉμ λ©μλλ€.- κ°μ²΄ μμ±μ λ©νν΄λμ€κ° μ μ΄νλ€.
- λ©νν΄λμ€κ° ν΄λμ€μ κ°μ²΄ μμ±μ μ μ΄νκΈ° λλ¬Έμ μ±κΈν€μ μμ±νλ μ©λλ‘ μ¬μ©ν μ μλ€.
__author__ = 'Chetan'
class MetaSingleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Logger(metaclass=MetaSingleton):
pass
logger1 = Logger()
logger2 = Logger()
print(logger1)
print(logger2)
''' <μ€ν κ²°κ³Ό>
<__main__.Logger object at 0x1059635c0>
<__main__.Logger object at 0x1059635c0>
'''
ν κ°μ DBλ₯Ό 곡μ νλ ν΄λΌμ°λ μλΉμ€μ μ.
- λ°μ΄ν°λ² μ΄μ€μ μΌκ΄μ±μ 보쑴ν΄μΌ νλ€. μ°μ° κ°μ μΆ©λμ΄ μμ΄μΌ νλ€.
- λ€μμ DB μ°μ°μ μ²λ¦¬νλ €λ©΄ λ©λͺ¨λ¦¬μ CPUλ₯Ό ν¨μ¨μ μΌλ‘ μ¬μ©ν΄μΌ νλ€.
__author__ = 'Chetan'
import sqlite3
class MetaSingleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(MetaSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Database(metaclass=MetaSingleton):
connection = None
def connect(self):
if self.connection is None:
self.connection = sqlite3.connect("db.sqlite3")
self.cursorobj = self.connection.cursor()
return self.cursorobj
db1 = Database().connect()
db2 = Database().connect()
print ("Database Objects DB1", db1)
print ("Database Objects DB2", db2)
''' <μ€ν κ²°κ³Ό>
Database Objects DB1 <sqlite3.Cursor object at 0x1005d9c00>
Database Objects DB2 <sqlite3.Cursor object at 0x1005d9c00>
'''
Database
ν΄λμ€λMetaSingleton
λ©νν΄λμ€μ λμμΌλ‘ μ±κΈν€ μν μ νλ€. λ¨ ν κ°μDatabase
ν΄λμ€ κ°μ²΄λ§ μμ±λλ€.- μΉ μ±μ΄ DB μ°μ°μ μμ²ν λλ§λ€
Database
ν΄λμ€λ₯Ό μμ±νμ§λ§ λ΄λΆμ μΌλ‘ ν κ°μ κ°μ²΄λ§ μμ±λλ€. λ°λΌμ λ°μ΄ν°λ² μ΄μ€μ λκΈ°νκ° λ³΄μ₯λλ€. 리μμ€λ₯Ό μ κ² μ¬μ©ν΄ λ©λͺ¨λ¦¬μ CPUμ¬μ©λμ μ΅μ νν μ μλ€.
μΈνλΌ μνλ₯Ό νμΈνλ μλΉμ€μ μ.
HealthCheck
ν΄λμ€λ₯Ό μ±κΈν€μΌλ‘ ꡬννλ€.- μνλ₯Ό νμΈν΄μΌ νλ μλ²μ λͺ©λ‘μ λ§λ€κ³ λͺ©λ‘μμ μ κ±°λ μλ²μ μνλ νμΈνμ§ μμμΌ νλ€.
__author__ = 'Chetan'
class HealthCheck:
_instance = None
def __new__(cls, *args, **kwargs):
if not HealthCheck._instance:
HealthCheck._instance = super(HealthCheck, cls).__new__(cls, *args, **kwargs)
return HealthCheck._instance
def __init__(self):
self._servers = []
def addServer(self):
self._servers.append("Server 1")
self._servers.append("Server 2")
self._servers.append("Server 3")
self._servers.append("Server 4")
def changeServer(self):
self._servers.pop()
self._servers.append("Server 5")
hc1 = HealthCheck()
hc2 = HealthCheck()
hc1.addServer()
print("Schedule health check for servers (1)..")
for i in range(4):
print("Checking ", hc1._servers[i])
hc2.changeServer()
print("Schedule health check for servers (2)..")
for i in range(4):
print("Checking ", hc2._servers[i])
''' <μ€ν κ²°κ³Ό>
Schedule health check for servers (1)..
Checking Server 1
Checking Server 2
Checking Server 3
Checking Server 4
Schedule health check for servers (2)..
Checking Server 1
Checking Server 2
Checking Server 3
Checking Server 5
'''
addServer()
λ©μλλ μλ²λ₯Ό λͺ©λ‘μ μΆκ°νλ€.changeServer()
λ©μλλ μλ²λ₯Ό λͺ©λ‘μμ μ κ±°νκ³ μλ‘μ΄ μλ²λ₯Ό μΆκ°νλ€.h1
κ³Όh2
λ μ±κΈν€μΌλ‘ μμ±λ κ°μ²΄μ΄λ―λ‘ λμΌν κ°μ²΄λ€.
- μ μ λ³μμ κ°μ΄ μ€μλ‘ λ³κ²½λ κ²μ λͺ¨λ₯΄κ³ μ ν리μΌμ΄μ μμ μ¬μ©λ μ μλ€.
- κ°μ κ°μ²΄μ λν μ¬λ¬ μ°Έμ‘°μκ° μκΈΈ μ μλ€. μ±κΈν€μ ν κ°μ κ°μ²΄λ§μ λ§λ€κΈ° λλ¬Έμ κ°μ κ°μ²΄μ λν μ¬λ¬ κ°μ μ°Έμ‘°μκ° μκΈ΄λ€.
- μ μ λ³μμ μ’ μμ μΈ λͺ¨λ ν΄λμ€ κ° μνΈκ΄κ³κ° 볡μ‘ν΄μ§λ€. μ μ λ³μ μμ μ΄ μλμΉ μκ² λ€λ₯Έ ν΄λμ€μλ μν₯μ μ€ μ μλ€.