본문 바로가기

python79

[PyQt6] Getting Started In this posting, we will learn how to create the desktop application using PyQt6. Installation $ pip install pyqt6 Now, we prepared for using PyQt6. Create Application Using Function import sys from PyQt6.QtWidgets import QApplication, QWidget def main(): w = QWidget() w.resize(320, 240) w.setWindowTitle('PyQt6 Example') w.show() sys.exit(app.exec()) if __name__ == '__main__': app = QApplication.. 2021. 8. 10.
[Python3] typing module We can use a type in Python3. It is not enforced like other strong typed languages, but it can help us. It can be a communication tool with other developers and can prevent mistakes. Syntax The default syntax is like this. # def func_name(param: param_type) -> return_type # func_body def say_hello(name: str) -> str: return f"Hello {name}" Python3 supports bool, int, str, tuple, set, list, and di.. 2021. 7. 3.
[Python] Decorator Decorator allows you to dynamically add actions to a function or object without changing the behavior of the function or object. In Python, Decorator can be applied to a function and executed before and after the surrounding function. Simple Example This example changes the input string to the upper case. def to_upper_case(func): text = func() if not isinstance(text, str): raise TypeError("Not a.. 2021. 6. 29.
[Python] Draw shapes using tkinter tkinter is the standard Python interface to the Tk GUI tookit. In my opinion, I recommend PyQt for GUI application, and tkinter for drawing application. Most of Python3 include the tkinter package. Now, let's learn how to draw shapes using tkinter. Import Module import tkinter as tk Create Empty UI class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.canvas.. 2021. 5. 29.
[Python] itertools module accumulate(iterable[, func, *, initial=None]) This function makes an iterator that returns accumulated sums, or accumulated results of other binary functions. The default func is operator.add function. from itertools import accumulate import operator print(list(accumulate([1, 2, 3, 4]))) # [1, 3, 6, 10] print(list(accumulate([1, 2, 3, 4], operator.mul))) # [1, 2, 6, 24] print(list(accumulate([1,.. 2021. 5. 15.
[Python] functools module The functools module is for higher-order functions. @functools.cache(func) Simple lightweight unbounded function cache. Sometimes called memoize. Returns the same as lru_cache(maxsize=None). This function is smaller and faster than lru_cache() with a size limit because it never needs to evict old values. v3.9 or later from functools import cache @cache def factorial(n): return n * factorial(n - .. 2021. 5. 8.
[Python] ChainMap in collections ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. from collections import ChainMap dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} cm = ChainMap(dict1, dict2) print(cm) # ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) print(list(cm.keys())) # ['b', 'c', 'a'] print(list(cm.values())) # [2, 4, 1] print(list(cm.items())) # [('b', 2), ('c', .. 2021. 5. 2.
[Python] namedtuple in collections namedtuple can give a name to each element of tuple. from collections import namedtuple Point = namedtuple('Name', ['x', 'y']) pt = Point(1, 2) print(pt) # Name(x=1, y=2) print(pt.x, pt.y) # 1 2 Attributes _fields Tuple of strings listing the field names. print(pt._fields) # ('x', 'y') _field_defaults Dictionary mapping field names to default values. Point = namedtuple('Name', ['x', 'y'], defaul.. 2021. 5. 2.
[Python] Counter in collections Counter is a subclass of dict for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. from collections import Counter d = Counter("I have one dog and one cat".split()) print(d) # Counter({'one': 2, 'I': 1, 'have': 1, 'dog': 1, 'and.. 2021. 5. 1.
[Python] OrderedDict in collections OrderedDict is like dict but have some extra capabilities relating to ordering operation. OrderedDict is designed to be good at reordering operations. Space efficiency, iteration speed, and the performance of update operations were secondary. from collections import OrderedDict d = OrderedDict() d['apple'] = 1 d['banana'] = 2 d['cherry'] = 3 for k, v in d.items(): print(k, v) # apple 1 # banana .. 2021. 5. 1.
[Python] defaultdict in collections defaultdict is a subclass of the built-in dict class. dict returns KeyError if you get value with nonexistence key. In contrast, defaultdict returns default value even with the nonexistence key. Usage Default Value with Built-in Class from collections import defaultdict d = defaultdict(int) d['apple'] = 3 print(d['banana']) # 0 print(d) # defaultdict(, {'apple': 3, 'banana': 0}) Default Value wi.. 2021. 5. 1.
[Python] deque in collections Deque is a Double-Ended Queue. Deque supports thread-safe, memory-efficient appends and pops from either side. Import from collections import deque Attributes maxlen Maximum size of a deque or None if unbounded. Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end. d = deque([1, 2, 3], 3) print(d) # deque([1, 2, 3], ma.. 2021. 4. 30.