โœŒ๏ธ
Studylog
See More
Studylog
Studylog
  • INTRO
  • AWS
    • AWS101
      • Virtualization and the AWS structures
      • AWS account and free-tier
      • AWS IAM
      • AWS EC2
        • EC2 basic
        • ENI and EIP
        • Elastic Block Storage
        • Auto Scaling
        • Elastic Load Balancer
  • IaC
    • Terraform
      • License Change
      • Tutorial
      • Module
      • Versioning
  • Airflow
    • Airflow-Ninja
      • Introduction and Goal
      • Tutorial: Settings
      • Tutorial: Module Import, Alert
  • Docker
    • Production with Docker
      • Customizing root directory
  • Network
    • Network-Handbook
      • Introduction and Goal
      • OSI7Layer
      • DNS
      • SSL/TLS
  • Comupter Architecture
    • Basic
      • Introduction and Goal
      • Chapter 1. The Internal Language of Computers
      • Chapter 2. Combinatorial Logic
      • Chapter 3. The Essentials of Memory and Disk Sequential Logic
    • Hands-on
      • Introduction and Goal
      • theory
        • Chapter 1. Logic Gates
        • Chapter 2. ALU
      • project
        • Project 1. Elementary logic gates implement
        • Project 2. Boolean Arithmetic
  • Java
    • Readable Code
      • ํ•™์Šต ๋ชฉ์ 
      • ์ถ”์ƒํ™”
      • ๋…ผ๋ฆฌ์  ์‚ฌ๊ณ  ํ๋ฆ„
      • ๊ฐ์ฒด์ง€ํ–ฅ ์ด๋ก 
      • ๊ฐ์ฒด์ง€ํ–ฅ ์ฝ”๋“œ ์ ์šฉํ•˜๊ธฐ
      • ์ฝ”๋“œ ๋‹ค๋“ฌ๊ธฐ
      • ์ฝ๊ธฐ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ๋„์™€์ค„ ์กฐ์–ธ๋“ค
  • Spring Boot
    • Practical Testing
      • ํ…Œ์ŠคํŠธ ์‚ฌ์ „ ์ง€์‹
      • ์Šคํ”„๋ง ๋ ˆ์ด์–ด๋“œ ์•„ํ‚คํ…์ฒ˜ ํ…Œ์ŠคํŠธํ•˜๊ธฐ
        • Persistence Layer
        • Business Layer
    • ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ
      • ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง
      • ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ ์ดํ•ด
        • ์˜ˆ์ œ ๋งŒ๋“ค๊ธฐ
        • ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ
        • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ์Šคํ”„๋ง ๋นˆ
  • Python
    • Effective Python
      • Introduction and Goal
      • Closure: Lazy Evaluation And Eager Evaluation
      • Python public attributes are better getter-setter
      • No refactoring attribute, we can use property decorator
      • You can do it, but it means you don't have to
  • Playgrounds
    • Java Playground
      • ํ•™์Šต ๋ชฉ์ 
      • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„
        • ํ•™์Šต ํ…Œ์ŠคํŠธ
        • ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ
        • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„ ๊ตฌํ˜„
        • ์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜๊ธฐ
      • ์ž๋™์ฐจ ๊ฒฝ์ฃผ
        • ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ
        • ์ž๋™์ฐจ ๊ฒฝ์ฃผ ๋ฏธ์…˜ ๊ตฌํ˜„
      • ์ขŒํ‘œ ๊ณ„์‚ฐ๊ธฐ
        • ์—ฐ๋ฃŒ ์ฃผ์ž…
        • ์ขŒํ‘œ ๊ณ„์‚ฐ๊ธฐ ๋ฏธ์…˜ ๊ตฌํ˜„
    • Infra Playground
      • VPC: ๋ง๋ถ„๋ฆฌ ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ
      • ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์ „ ์ง€์‹
      • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„  ์ „ ํ•™์Šต ํ…Œ์ŠคํŠธ
      • SSM: Session Manager
      • SQL, ์ด ์ •๋„๋Š” ์•Œ์•„์•ผ์ง€ ๐Ÿ˜Ž
      • Subway-Map
        • ๋ง ๊ตฌ์„ฑํ•˜๊ธฐ
        • ์„œ๋ฒ„ ๊ตฌ์„ฑํ•˜๊ธฐ
        • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„ ํ•˜๊ธฐ
      • Conference Platform
        • ๋ง ๊ตฌ์„ฑํ•˜๊ธฐ
        • ์„œ๋ฒ„ ๊ตฌ์„ฑํ•˜๊ธฐ
        • ํ™”๋ฉด ์„ฑ๋Šฅ ๊ฐœ์„ ํ•˜๊ธฐ
  • Tools
    • SOPS
    • Bruno
    • ๐Ÿ–ฅ๏ธFCK-NAT
    • ๐ŸงŠPulumi
Powered by GitBook
On this page
  1. Python
  2. Effective Python

You can do it, but it means you don't have to

PreviousNo refactoring attribute, we can use property decoratorNextJava Playground

Last updated 9 months ago

Properties

2024.08.22

CHAPTER 5. ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์€ ์•„๋‹ˆ๋‹ค

5.2 Monkey patch

  • page. 188


๋™์ ์ธ ํ™˜๊ฒฝ์—์„œ ์‰ฝ๊ฒŒ ์ ‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•์œผ๋กœ Ruby ์—์„œ ๋งŽ์ด ์ฐจ์šฉ๋˜์–ด ์“ฐ์—ฌ์™”๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ์‹์€ ๊ธฐ์กด ์†Œ์Šค์ฝ”๋“œ์˜ ์‹คํ–‰ ๋™์ž‘์„ ๋ง์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์„ ์ž์ œ ํ•˜๋Š” ํŽธ์ด๋‚˜, Ruby on Rails ํ”„๋ ˆ์ž„์›Œํฌ ๋‚ด๋ถ€ ์ฝ”๋“œ๋Š” ๋Œ€๊ฐœ ๋งŽ์€ ๋™์ž‘๋“ค์ด MonkeyPatch ํ˜•์‹์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ต์ฒดํ•˜์ง€ ๋ชปํ•˜๋Š” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

Why do not use?

MonkeyPatch ์˜ ์‹คํ–‰ ๋™์ž‘์„ ์‚ดํŽด ๋ณด์ž๋ฉด ์•„๋ž˜ ์˜ˆ์‹œ์™€ ๊ฐ™๋‹ค.

"""
monkey_patch.py

monkey patch module
"""

import re
from re import match as _match

def match_positive(pat, s, flags=0):
	return _match(rf"\+{pat}", s, flags)

re.match = match_positive
"""
accounts.py

print has 5k balance
"""

import monkey_patch
import re
import sys

def get_5k_balances(rows):
	for row in rows:
		if re.match("5\d{3}\.\d{2}", row):
			balance, account_num, owner = row.split(",")
			yield (f"Account: {account_num}\n"
				f" Owner: {owner.strip()}\n"
				f"Balance: ${float(balance):,.2f}\n")


if __name__ == "__main__":
	for account in get_5k_balances(open(sys.argv[1])):
		print(account)

์—ฌ๊ธฐ๊นŒ์ง€ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ์ž‘๋™ํ•˜๋˜ ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๊ณ , ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ‘์ž‘์Šค๋ ˆ ๋ณ€๊ฒฝ ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌํ•œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์†Œ์Šค์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ ์—†์ด ๋™์ž‘์„ ์œ ์ง€ ์‹œํ‚ค๋ ค๋ฉด ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐธ์กฐ ํ•˜๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์›ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž๊ฒŒ ๋ฎ์–ด์”Œ์šฐ๋Š” ํ˜•ํƒœ๋กœ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒƒ์ด MonkeyPatch ๋กœ ๋ถˆ๋ฆฌ์šฐ๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ์‹ค์ œ ์ž˜ ๋™์ž‘ํ•˜๋˜ ๋‹ค๋ฅธ ์ฝ”๋“œ๋„ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํƒ€์ธ์ด ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ ๋™์ž‘์„ ์œ ์ถ”ํ•˜๊ธฐ ์–ด๋ ต๋‹ค ๋Š” ๋‹จ์ ์ด ๋ถ„๋ช…ํ•˜๊ฒŒ ์กด์žฌ ํ•˜๊ธฐ์— ์‚ฌ์šฉ ํ•˜์ง€ ๋ง์•„์•ผํ•œ๋‹ค.

"""
accounts2.py

Error!
"""
import monkey_patch
import re
import argparse  

def get_5k_balances(rows):
	for row in rows:
		if re.match("5\d{3}\.\d{2}", row):
			balance, account_num, owner = row.split(",")
			yield (f"Account: {account_num}\n"
				f" Owner: {owner.strip()}\n"
				f"Balance: ${float(balance):,.2f}\n")


if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	parser.add_argument("datafile", type=str, help="CSV data file")
	parser.add_argument("-d", "--datafile", type=str, help="CSV data file")
	parser.add_argument("-f", "--function", action="store", default="get_5k_balances")
	args = parser.parse_args()
	func = eval(args.function)
	
	for account in get_5k_balances(open(args.datafile)):
		print(account)

์œ„์—์„  ์ •์ƒ ๋™์ž‘ ํ•˜๋Š” ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ ๋˜์—ˆ์ง€๋งŒ ์‹ค์ œ ์ด์™€ ๊ฐ™์ด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ƒํ™ฉ์„ ๋ณด๋ฉด, ํ”ํžˆ ์‚ฌ์šฉ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ MonkeyPatch๋กœ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ชจ๋“ˆ ์กฐ์ฐจ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ์ƒํ™ฉ์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ ์ด ์žˆ๋‹ค.

What's the conclusion

MonkeyPatch ๋ฅผ ํ”ผํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋ฅผ ์ดํ•ด ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ์‹ค์ œ ์ฝ”๋“œ์—์„œ ๊ณผ์—ฐ ๋ˆ„๊ฐ€ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ด๋ฆ„์„ ๊ทธ๋Œ€๋กœ ์ฃผ์ž…ํ•˜์—ฌ ์‹คํ–‰ ๋™์ž‘์„ ๋ณ€๊ฒฝ ์‹œํ‚ฌ ๊ฒƒ์ธ๊ฐ€? ์ด๋ ‡๊ฒŒ ์ธ์œ„์ ์ธ ์ƒํ™ฉ์„ ์˜ˆ์‹œ๋กœ ๋“ค์—ˆ์ง€๋งŒ MonkeyPatch ๋ฅผ ํ”ผํ•˜๊ณ  ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ•˜๋Š” ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๐Ÿ“
๐Ÿ“„
๐Ÿ“Ž
๐Ÿ”–
๋˜‘๋˜‘ํ•˜๊ฒŒ ์ฝ”๋”ฉํ•˜๋Š”๋ฒ• ์ฝ”๋”ฉ์˜ ๊ธฐ์ˆ  51