Archives mensuelles : mars 2022

lambda,dynamic function calls and comprehensions in python

Comprehension A concise way to create a new list or a new dictionary from an existing iterable or sequence. Syntax : It consists of brackets (for list comprehension) and of braces(for dict comprehension) containing an expression followed by a for … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

python: common string functions

chapters transformation functions finding functions checking functions transformation functions transformations functions doesn’t modify the current string since strings are imitable.Instead of,they return a copy. remove leading and trailing whitespace:strip() remove leading whitespace:lstrip() remove trailing whitespace:rstrip() split a string into a … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

Iterations with python

Loop for in statement: iterate over any sequences The sequence can be a list,a string,a tuple and so for… in python this statement is very very very common. In fact for most of things we need to iterate we will … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

execute a command with python

Execute shell process with subprocess module (Python 3.5) goal and general advices goal: The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

regex in python

Raw regex It is very different from the java approach that doesn’t have that capacity. It allows to escape in a clever way: we escape only when needed. Example here with classic regex: two main ways to use regex – … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

python imports

Definitions Module: a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module name (as a string) is available as the value of the global variable __name__. … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

File and path in python with pathlib api

common functions and variables Functions to create or retrieve path objects Create a path pointing to the current working directory (as returned by os.getcwd()) : cwd: Path = Path.cwd() Create a path by concatenating a Path with a String object: … Continuer la lecture

Publié dans Non classé | Laisser un commentaire

write and read file in python

Read text files By default python reading file API preserves the break-line character (\n on Linux for example). in the example below we mean by « cleaned lines »,lines that we removed the break line characters Read lines in memory: def read_lines_in_memory(filename: … Continuer la lecture

Publié dans Non classé | Laisser un commentaire