[Python] f-string trick (1)
ยท
๐Ÿ Python
Python f-string์„ ํ™œ์šฉํ•œ ๊น”๋”ํ•œ ์ถœ๋ ฅ 1) int ๋ฌธ์ž์—ด ์ถœ๋ ฅ f-string์œผ๋กœ ์ˆซ์ž๋ฅผ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ํ˜•ํƒœ๋กœ ๊ตฌ๋ถ„์ž ์„ ํƒ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. n : int = 1000000 print(f"{n}") # 1000000 print(f"{n:_}") # 1_000_000 print(f"{n:,}") # 1,000,000 2) ๋ฌธ์ž์—ด ์ถœ๋ ฅ blank space ์ฑ„์šฐ๊ธฐ ํŠน์ • ํ•ญ๋ชฉ์„ ์ถœ๋ ฅํ•  ๋•Œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ฌ๋ผ ๋ณด๊ธฐํž˜๋“  ๊ฒฝ์šฐ ์ผ์ผํžˆ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ๋นˆ์นธ์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ f-string์œผ๋กœ ๋ณด์ •ํ•  ์ˆ˜์žˆ๋‹ค. type1 : str = 'color' type2 : str = 'size' print(f"{type1:
[Python] Command Line Flags
ยท
๐Ÿ Python
Python Command Flag python -c - (command) ์ง์ ‘ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด Python ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ex) python -c "import pandas as pd" python -m - (module) Python ๋ชจ๋“ˆ์„ ์Šคํฌ๋ฆฝํŠธ๋กœ ์‹คํ–‰. ex) 'python -m http.server' ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ HTTP ์„œ๋ฒ„ ์‹คํ–‰ python -i - (interactive) ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํ›„ ๋Œ€ํ™”ํ˜• ๋ชจ๋“œ๋กœ ์ง„์ž… ex) ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํ›„ interpreter๋กœ ์ง„์ž… python -O - (Optimize) ์ตœ์ ํ™” ๋ชจ๋“œ๋ฅผ ํ™œ์„ฑํ™”, assert๋ฌธ์„ ์ œ๊ฑฐํ•˜๊ณ  `__debug__`๋ฅผ 'False'๋กœ ์„ค์ • python -B - (bytecode) .pyc ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€. ๋ณดํ†ต ๊ฐœ๋ฐœ ์ค‘..
[Version] 3.12 rc (Release Candidates) (2) - kwargs, override
ยท
๐Ÿ Python
1 ) kwargs Python 3.12 ๋ถ€ํ„ฐ ๋ณ€์ˆ˜ Type์— ๊ด€ํ•œ ๋ชจ๋“ˆ์ด ๋งŽ์ด ๊ฐœ์„ ๋˜์—ˆ๋‹ค. ๊ทธ ์ค‘ Typing์— NotRequired, TypedDict, Unpack์„ ์‚ฌ์šฉํ•ด์„œ class ์ •์˜์™€ ํ•จ์ˆ˜ ๋ณ€์ˆ˜ ๊ด€๋ฆฌ๊ฐ€ ์ข€๋” ์šฉ์ดํ•ด์กŒ๋‹ค. from typing import NotRequired, TypedDict, Unpack class Kwargs(TypedDict): name : str age : NotRequired[int] def profile(**kwargs: Unpack[Kwargs]) -> None: for k, v in kwargs.items(): print(f"{k}: {v}") if __name__ == "__main__": profile(name="Ethan", age=25) Kwargs์˜..
[Python] importlib ์‚ฌ์šฉ์ž ์ •์˜ ๋ฆฌ๋กœ๋“œ
ยท
๐Ÿ Python
Sol 1) importlib์˜ reload( ) ํ•จ์ˆ˜ ์‚ฌ์šฉ (3.12 ์ดํ›„ imp ์ง€์› X) import example import importlib # make changes to example.py file importlib.reload(example) ๊ธฐ์กด์˜ sys.path.module์— ๋“ฑ๋ก๋œ module์„ reload ์‹œํ‚จ๋‹ค. ์ฃผ์˜ ํ•ด์•ผํ•  ๊ฒƒ์€ ์ ˆ๋Œ€ ๊ฒฝ๋กœ์— ์—†๋Š” module์ผ ๊ฒฝ์šฐ ์ ˆ๋Œ€ ๊ฒฝ๋กœ์— ๋“ฑ๋ก์„ ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค. Sol 2) JupyterNote Book์—์„œ Magic Command๋กœ reload ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋”๋ณด๊ธฐ https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html autoreload — IPython 8.14..
[leetcode-347] Top K Frequent Elements
ยท
๐Ÿข One step
LeetCode-347 Top K Frequent Elements : Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. note : time complexity must be better than O(n log n), where n is the array s size.Over * :::python class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: if len(set(nums))==1: return [nums[0]] answer = [] count =..
Python Interpreter Lock (a.k.a GIL)
ยท
๐Ÿ Python
Python ์ „์—ญ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฝ GIL ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๋ฝ(Python Interpreter Lock, GIL)์€ ํŒŒ์ด์ฌ์—์„œ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. GIL์€ CPython ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์‹คํ–‰๋˜๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ๋‹ด๋‹นํ•˜๋Š” ์ „์—ญ ๋ฝ์ด๋‹ค. ์ด ๋ฝ์€ ๋ชจ๋“  ํŒŒ์ด์ฌ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ด์ฌ ๋ฐ”์ดํŠธ์ฝ”๋“œ์—์„œ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ ์‹คํ–‰ํ•˜๋„๋ก ๋ณด์žฅํ•œ๋‹ค. Wiki๋ฐœ ์ž๋ฃŒ๋ฅผ ๋ณด๋ฉด 1994๋…„ 1์›” 26์ผ ๋ฐœํ‘œ๋กœ ์ƒ๋‹นํžˆ ์˜ค๋ž˜๋œ ๊ตฌ์กฐ์ฒด์ด๋‹ค. ์ด ์‹œ๊ธฐ์— ๋‹จ์ผ ์ฝ”์–ด์—์„œ ์ž‘๋™ํ•˜๋Š” ์ปดํ“จํ„ฐ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•ด ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ ์—†์ด ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ํŒŒ์ด์ฌ์˜ ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ์— ์ œํ•œ์ด ์ƒ๊ฒผ๋‹ค. GIL์˜ ๋ฌธ์ œ์ ์€ ๋ฉ€ํ‹ฐ์ฝ”์–ด CPU์—์„œ CPU ๋ฐ”์šด๋“œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ฑ๋Šฅ์„ ์ €..
[Mysql] python executemany ์‚ฌ์šฉ์‹œ TypeError: not all arguments converted during string ...
ยท
๐Ÿ›ข๏ธ Database
cursor.executemany(sql, data)๋กœ ๋‹ค์ˆ˜์˜ ๋กœ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ INSERT ํ•  ์ˆ˜ ์žˆ๋‹ค. sql : INSERT ์ฟผ๋ฆฌ ๋ฌธ์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค. sql = """INSERT INTO TBL_NAME (COL1_NAME, COL2_NAME, COL3_NAME) VALUES(%s,%s,%s)""" data : sql ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ tuple๋กœ ๋งŒ๋“ค๊ณ  list๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ๋œ๋‹ค. data = [(parm1,parm2,parm3),(parm1,parm2,parm3),(parm1,parm2,parm3)....] [ERROR] TypeError: not all arguments converted during string sql๋ฌธ์—์„œ ON DUPLICATE KEY UPDATE ์‚ฌ์šฉ์‹œ ์ฃผ์˜ [์ž˜๋ชป๋œ ๋ฐฉ์‹] s..
๋‹คํ–ˆ๋‹ค
'python' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)