Tutorial by Examples

You can create a TCP server using the socketserver library. Here's a simple echo server. Server side from sockerserver import BaseRequestHandler, TCPServer class EchoHandler(BaseRequestHandler): def handle(self): print('connection from:', self.client_address) while True:...
A UDP server is easily created using the socketserver library. a simple time server: import time from socketserver import BaseRequestHandler, UDPServer class CtimeHandler(BaseRequestHandler): def handle(self): print('connection from: ', self.client_address) # Get message and cli...
The vast majority of Python memory management is handled with reference counting. Every time an object is referenced (e.g. assigned to a variable), its reference count is automatically increased. When it is dereferenced (e.g. variable goes out of scope), its reference count is automatically decreas...
The only time the garbage collector is needed is if you have a reference cycle. The simples example of a reference cycle is one in which A refers to B and B refers to A, while nothing else refers to either A or B. Neither A or B are accessible from anywhere in the program, so they can safely be dest...
Removing a variable name from the scope using del v, or removing an object from a collection using del v[item] or del[i:j], or removing an attribute using del v.name, or any other way of removing references to an object, does not trigger any destructor calls or any memory being freed in and of itsel...
use std::io::{Read, Result as IoResult}; use std::fs::File; struct Config(u8); fn read_config() -> IoResult<String> { let mut s = String::new(); let mut file = File::open(&get_local_config_path()) // or_else closure is invoked if Result is Err. .or_els...
EXTENDED_ARG = 145 # All opcodes greater than this have 2 operands HAVE_ARGUMENT = 90 # All opcodes greater than this have at least 1 operands cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is ... # A list of comparator id's. The indecies are used as opera...
Python is a hybrid interpreter. When running a program, it first assembles it into bytecode which can then be run in the Python interpreter (also called a Python virtual machine). The dis module in the standard library can be used to make the Python bytecode human-readable by disassembling classes, ...
To disassemble a Python module, first this has to be turned into a .pyc file (Python compiled). To do this, run python -m compileall <file>.py Then in an interpreter, run import dis import marshal with open("<file>.pyc", "rb") as code_f: code_f.read(8) # M...
The iloc (short for integer location) method allows to select the rows of a dataframe based on their position index. This way one can slice dataframes just like one does with Python's list slicing. df = pd.DataFrame([[11, 22], [33, 44], [55, 66]], index=list("abc")) df # Out: # 0...
Inventory is the Ansible way to track all the systems in your infrastructure. Here is a simple inventory file containing a single system and the login credentials for Ansible. [targethost] 192.168.1.1 ansible_user=mrtuovinen ansible_ssh_pass=PassW0rd
[targethost] 192.168.1.1 ansible_user=mrtuovinen ssh_private_key_file=~/.ssh/custom_key
[targethost] 192.168.1.1 ansible_user=mrtuovinen ansible_port=2222
This example creates a new file named "NewFile.txt", then writes "Hello World!" to its body. If the file already exists, CreateFile will fail and no data will be written. See the dwCreationDisposition parameter in the CreateFile documentation if you don't want the function to fa...
Trap expressions don't have to be individual functions or programs, they can be more complex expressions as well. By combining jobs -p and kill, we can kill all spawned child processes of the shell on exit: trap 'jobs -p | xargs kill' EXIT
Using the timeit module from the command line: > python -m timeit 'for x in xrange(50000): b = x**3' 10 loops, best of 3: 51.2 msec per loop > python -m timeit 'from math import pow' 'for x in xrange(50000): b = pow(x,3)' 100 loops, best of 3: 9.15 msec per loop The built-in ** operato...
The typing.TypeVar is a generic type factory. It's primary goal is to serve as a parameter/placeholder for generic function/class/method annotations: import typing T = typing.TypeVar("T") def get_first_element(l: typing.Sequence[T]) -> T: """Gets the first ele...
Enum constants are instantiated when an enum is referenced for the first time. Therefore, that allows to implement Singleton software design pattern with a single-element enum. public enum Attendant { INSTANCE; private Attendant() { // perform some initialization routine ...
To get basic information about a DataFrame including the column names and datatypes: import pandas as pd df = pd.DataFrame({'integers': [1, 2, 3], 'floats': [1.5, 2.5, 3], 'text': ['a', 'b', 'c'], 'ints with None': [1, None, 3]}) ...
df = pd.DataFrame({'old_name_1': [1, 2, 3], 'old_name_2': [5, 6, 7]}) print(df) # Output: # old_name_1 old_name_2 # 0 1 5 # 1 2 6 # 2 3 7 To rename one or more columns, pass the old names and new names as a dictionary: df.r...

Page 216 of 1336