Obsah

Python

#!/usr/bin/env python

Funkce

# Příklad funkce s return annotation (říká, co by to vracet mělo)
def add(first, second) -> int:
  return first+second
 
# Volání funkce  
add(5,10)
def add(first: int = 5, second: int = 10) -> int:
  return first+second
 
add()
add(second=6)
# Uložení reference na funkci
add = lambda first, second: first+second
 
# Volání funkce
add(5,10)
 
# Definice a přímé použití
(lambda first, second: first+second)(5,10)

OOP

class MyClass():
 
  # Konstruktor
  def __init__(self):
    self._count = 0
 
  # Metoda tridy
  @classmethod
  def add(number):
    _count += number
 
  # Ekvivalentni zapis bez pouziti dekoratoru
'''
  def add(self,number):
    self._count += number
'''
 
  # Getter pomoci dekorátoru
  @property
  def count():
    return self._count
class MyClass(MyParentClass):
 
  def __init__(self):
    super.__init__()
class MyException(Exception):
    pass

Scapy

from scapy.all import *
 
pkt = Ether()/IP()  // vytvoří objekt, dvě vrstvy: Ethernet, TCP
pkt[Ether].src = my_mac
pkt[Ether].dst = "ff:ff:ff:ff:ff:ff"
pkt[IP].src = "192.168.1.100"
pkt[IP].dst = "192.168.1.255"
 
sendp(pkt)
>>> ls(Ether)
dst        : DestMACField         = (None)
src        : SourceMACField       = (None)
type       : XShortEnumField      = (0)
mypkt2=Ether()/Dot1Q(vlan=42)/IP(dst="1.2.3.4")/Raw("example")
sendp(mypkt2, iface="eth0")

setuptools (setup.py)

python setup.py bdist_rpm
python setup.py bdist_wheel
pip wheel .
python -m build --wheel