format
method: user = "Jane Doe" action = "buy" log_message = 'User {} has logged in and did an action {}.'.format( user, action ) print(log_message) # User Jane Doe has logged in and did an action buy.
format
method, supports format strings ( f-strings , f-strings). They are a flexible tool for performing various string manipulations. Here is what the previous example looks like, rewritten using format strings: user = "Jane Doe" action = "buy" log_message = f'User {user} has logged in and did an action {action}.' print(log_message) # User Jane Doe has logged in and did an action buy.
from pathlib import Path root = Path('post_sub_folder') print(root) # post_sub_folder path = root / 'happy_user' # print(path.resolve()) # /home/weenkus/Workspace/Projects/DataWhatNow-Codes/how_your_python3_should_look_like/post_sub_folder/happy_user
def sentence_has_animal(sentence: str) -> bool: return "animal" in sentence sentence_has_animal("Donald had a farm without animals") # True
Enum
class, a simple mechanism for working with enumerations . Enumerations are useful for storing lists of constants. Constants, otherwise, are randomly scattered in the code. from enum import Enum, auto class Monster(Enum): ZOMBIE = auto() WARRIOR = auto() BEAR = auto() print(Monster.ZOMBIE) # Monster.ZOMBIE
for monster in Monster: print(monster) # Monster.ZOMBIE # Monster.WARRIOR # Monster.BEAR
import time def fib(number: int) -> int: if number == 0: return 0 if number == 1: return 1 return fib(number-1) + fib(number-2) start = time.time() fib(40) print(f'Duration: {time.time() - start}s') # Duration: 30.684099674224854s
lru_cache
to optimize this function (this optimization technique is called memoization ). As a result, the execution time of the function, which was previously measured in seconds, is now measured in nanoseconds. from functools import lru_cache @lru_cache(maxsize=512) def fib_memoization(number: int) -> int: if number == 0: return 0 if number == 1: return 1 return fib_memoization(number-1) + fib_memoization(number-2) start = time.time() fib_memoization(40) print(f'Duration: {time.time() - start}s') # Duration: 6.866455078125e-05s
range(5)
command fall into the variables head
and tail
. Everything that is between the first and the last value falls into the body
variable. head, *body, tail = range(5) print(head, body, tail) # 0 [1, 2, 3] 4 py, filename, *cmds = "python3.7 script.py -n 5 -l 15".split() print(py) print(filename) print(cmds) # python3.7 # script.py # ['-n', '5', '-l', '15'] first, _, third, *_ = range(10) print(first, third) # 0 2
dataclass
decorator automatically generates special methods such as __init__()
and __repr__()
. In the official text of the corresponding sentence, they are described as “mutable named tuples with default values”. Here is an example of creating a class without using the dataclass
decorator: class Armor: def __init__(self, armor: float, description: str, level: int = 1): self.armor = armor self.level = level self.description = description def power(self) -> float: return self.armor * self.level armor = Armor(5.2, "Common armor.", 2) armor.power() # 10.4 print(armor) # <__main__.Armor object at 0x7fc4800e2cf8>
dataclass
: from dataclasses import dataclass @dataclass class Armor: armor: float description: str level: int = 1 def power(self) -> float: return self.armor * self.level armor = Armor(5.2, "Common armor.", 2) armor.power() # 10.4 print(armor) # Armor(armor=5.2, description='Common armor.', level=2)
__init__.py
file). Here is an example from the official documentation: sound/ __init__.py sound formats/ __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ __init__.py echo.py surround.py reverse.py ... filters/ __init__.py equalizer.py vocoder.py karaoke.py ...
__init__.py
. Thanks to this file, the folder is perceived as a Python package. In Python 3, with the advent of the Implicit Namespace Packages feature, the presence of such files in folders is no longer mandatory. sound/ __init__.py sound formats/ wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ echo.py surround.py reverse.py ... filters/ equalizer.py vocoder.py karaoke.py ...
__init__.py
file is still needed for regular packages. If you remove it from the folder, the package becomes a so-called namespace package , to which additional restrictions apply.Source: https://habr.com/ru/post/452564/
All Articles