Demos
Adding logs (adding threadname in case of multithreading) | import logging FORMAT="%(asctime)s %(message)s %(levelname)s %(funcName)s:%(lineno)d" logging.basicConfig(filename='myfile.log', level=logging.DEBUG, format=FORMAT) inside each module: logger=logging.getLogger(__name__) logger.debug("my message") |
Get information from a user | myresult=input("message to the user") |
print information to the console | print("hello") |
comment | # |
comment on several lines | triple double quote at the beginning and at the end |
convert string to integer | int(mystring) |
convert integer to string | str(myinteger) |
get a variable's type | type(myvariable) |
check if a variable is an int | isinstance(myvariable,int) |
get a random number between x and y | import random myvar=random.randrange(x, y+1) |
Assign a value to a parameter, with a default result if the first assignment is null | myvariable=firstresult or secondresult |
Assign a value to a parameter inside an if statement, and use the parameter further on | if (myvariable :=myvalue ) and len(myvariable): myparameter=myvariable |
Best practices to define python programs called from the outside | def main(): put the program here ..... if __name__=="__main__": main() |
get the nth character in a string | mystring[n-1] |
get the length of a string | len(mystring) |
condition: is string within a string | if stringtosearch in mystring: |
get string from position x to y | mystring[x-1:y] |
get string from the start to position y | mystring[:y] |
get string from position y to the end | mystring[x-1:] |
get string from position x from the end to position y from the end | mystring[-x:-y] |
convert to uppercase | mystring.upper() |
take off spaces before and after in string | mystring.strip() |
replace x by y in a string | newstring=mystring.replace(x,y) |
split a string using a separator | resutlingarray=mystring.split(separator) |
build a string using a template, with the order of parameter being used to choose the parameter | mytemplate="hello{1}how are you{0}" resultingstring=mytemplate.format(var0,var1) |
build a string using a template, with the name of parameter being used to choose the parameter | mytemplate="hello{param1}how are you{param2}" resultingstring=mytemplate.format(param1=val1,param2=val2) |
convert string to lowercase | mystring.lower() |
string template: take nth variable as double of m digits after zero | {n-a:.mf} |
new line in string | |
carriage return | \r |
concatenate string in loop ( best option when the string is growing bigger and bigger at each loop) | Use an array to accumulate pieces of string in the loop using append at the end, transform to string: in this example pieces of string will be separated by ",", you can also use "" ','.join(full_output_object_array) |
Array's length | len(myarray) |
Initialize a list | mylist=[item1,item2,{"param1":"value1"}] |
Types of arrays | List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered and unindexed. No duplicate members. Dictionary is a collection which is unordered and changeable. No duplicate members. |
Property of lists | Ordered, changeable, allows duplicates |
Same as list but unchangeable | Tuple |
Similar to an object | Dictionary |
Unordered and unindexed. No duplicate member | Set |
Check if value in a list | if value in mylist: |
Insert element in position x in a list | mylist.insert(x-1) |
Add an item at the end of the list | mylist.append(newvalue) |
Add a list at the end of another list or tupple | mainlist.extend(otherlist) |
Take off a value in a list | mylist.remove(myvalue) |
Remove nth value in a list | mylist.pop(n-1) |
Remove the last item in a list | mylist.pop() |
Delete the entire list | del mylist |
Take off the entire content from a list | mylist.clear() |
Loop through a list | for x in mylist: |
Loop through a list with the possibility to use the index number | for i in range(len(mylist)): print(mylist[i]) |
Create a list of number from 0 to n | mylist=range(n) |
Create a list of number from x to y, with an increment of k | mylist=range(x,y,k) |
Short hand of loop on list with operation: print all elements in a list | [print(x) for x in mylist] it is called also a list comprehension |
Short hand of loop on list with operation: put all elements of list1 containing string str1 in list2 | list2=[ x for x in list1 if str1 in x] |
Short hand of loop on list with operation: put all elements of list1 in list2 but transform to upper case | list2=[ upper(x) for x in list1 if str1 in x] |
Short hand of loop on list with operation: put all elements of list1 in list2 only if not equal to str1, else hardcode and put str2 | list2=[ x if x!=str1 else str2 for x in list1] |
Short hand of loop on elements of a dictionary with operation: multiply the value of fields by 2 | transformed_object={k:v*2 for k,v in my_object.items()} it is called also a dictionary comprehension |
Sort a list ascending | mylist.sort() |
Sort a list descending | mylist.sort(reverse=true) |
Custom sort function | def myfunc(x): ... return y mylist.sort(key=myfunc) the bigger is the return value ( y ) the higher it is in the hierarchy |
Case insensitive sort | mylist.sort(key=str.lower) |
Reverse the order of a list | mylist.reverse() |
Make a copy of the list ( give two examples ) | secondlist=firstlist.copy() you can also do: secondlist=list(firstlist) |
Put a list at the end of another list | resultinglist=firstlist.extend(secondlist) |
Put an element at the end of a list | resultinglist=firstlist.append(secondlist) |
Concatenate two tuples | resultingtuple=tuple1 + tuple2 |
Add an item to a set | myset.add(newvalue) |
Add a set to another set | firstset.update(secondset) |
Taking off a value from a set, not raising any error if the value is not there | myset.discard(myvalue) |
Put two sets into one set | newset=set1.union(set2) |
Leave in a set only elements that are also in another set | myset.intersection_update(otherset) |
Create a set having elements that are both in set1 and set2 | newset=set1.intersection(set2) |
Leave in a set only elements that are not in another set | myset.symmetric_difference_update(otherset) |
Create a set having elements that not common between set1 and set2 | newset=set1.symmetric_difference(set2) |
Access the value of an element of a dictionary | myvalue=mydictionnary["mykey"] you can also use ( safer in if statements) : myvalue=mydictionnary.get("mykey") |
Access the value of an element of a dictionary, giving a default value if the element does not exist | myvalue=mydictionnary.get("mykey","mydefaultvalue") |
Get the list of keys in a dictionnary | for x in mydictionnary.keys(): print(mydictionnary[x]) |
Get the list of values in a dictionnary | mydictionnary.values() |
Convert list of keys in a dictionnary to be able to take element number n | results=list(mydictionnary.keys()) result=results[n] |
Get the list of values pairs in a dictionnary | mylist=mydictionnary.items() |
Faster way of initializing a dictionnary | mydictionnary={param1:"value1","param2":{"param21":"value21"}} param1 is a parameter containing a string. Parameters can be put in the place of keys |
Check if a key is in a dictionnary | if mykey in mydictionnary: |
Remove item from a dictionnary using a key | mydictionnary.pop(mykey) |
Add elements from one dictionary to another | mydictionnary.update(additionaldictionnary) |
Empty a dictionnary | mydictionnary.clear() |
Loop through all the keys in the dictionnary | for x in mydictionnary: print(mydictionnary[x]) |
initialize a list | mylist=["val1","val2"] |
initialize a tuple | mytuple=("val1","val2") |
initialize a dictionary | mydictionnary={"var1":"value1","var2":"value2"} |
initialize a set | myset={"val1","val2"} |
Else if in python | elif |
Go to the next iteration in a while loop | continue |
Stop a loop | break |
If else if else | if condition: ... elif condition2: ... else: ... |
Execute code at the end of a loop | else: .... |
Shorthand for if else | print("hello") if condition else print("something else") |
Condition: field is null | myfield==None Careful in some cases the value is 'null' (must check when debugging) myfield=='null' |
condition : is alphanumeric | isalnum() |
condition : only letter | isalpha() |
condition : only numbers | isnumeric() |
condition: is a list | isinstance(mylist, list): |
Function with a variable | def myfunc(x): ... return y |
Function with number of variables unknown, use the nth argument | def myfunc(*x): print(x[n-a]) |
Call function by giving the variable names and values | def myfunc(var1,var2): ... myfunc(var1=value1,val2=value2) |
Call function with number of variable unknow by giving the variable names and values, in the function use value of var1 | def myfunc(**myvars): print(myvars[val1]) myfunc(var1=value1,val2=value2) |
Call function with number of variable unknow without giving the variable names and values, in the function use value of the first variable | def myfunc(*args): print(args[0]) myfunc(value1,value2) |
Define function with default value to param1 | def myfunc(var1=defaultvalue): |
Create a lambda that multiplies x by y and use it as argument to print | mylambda=lambda x,y: x* y print(mylambda(val1,val2)) |
Function returning a parameterized lambda ( multiplying input by the parameter) | def mylambdacreator(param1): return lambda x : x * param1 mylambda=mylambdacreator(3) print(mylambda(4)) |
Create a lambda in such a way that you can cope with the situation where you don't know if it will be called with one argument or two | lambda *args: args[0] if len(args)<=1 else args[0]+args[1] |
What can local function do that non local functions cannot do | They can reference parameters outside of the local function but within the function (main function) including this local function. This applies also to the main function's input parameters This is a closure |
Example of use of local function to create a factory of functions | def augment(myfactor): def multiply(inputnumber,myfactor): return inputnumber*myfactor return multiply newfunction=augment(2) myresult=newfunction(4) -> gives 8 |
non-local scope | parameter defined in function (and not returned) stays in the scope of the function and is out of scope outside of the function. parameter nonlocal makes so that it is still in scope outside of the function. At the same time it is not global, so if this function has a parent function, the parameter will out of scope after the parent function is called |
global scope | If you declare : global myvariable, and you assign a value to it, the scope will be outside of any called function's scope If you have func1 setting myvariable=1 , calling func11, and inside func11 you define global myvariable, and set myvariable=2 , then: inside func1 value will be 1, outside func1 ( global scope ), value will be 2 |
example of use of local function and non local scope | Timer function that creates a timer, with local function to calculate time elapsed since last call ( last call is a non local variable inialized to None in the timer function) declaring last call as nonlocal Call to timer function (creation of the timer) will initialize the last call time to None, but at each call of elapsed(), the last call setup during last call of elapsed will be kept in memory at the timer() function level |
Principle of decorators | Modify or enhance functions without changing their definition Takes a function as argument and return another function that modifies the original function |
Simple example of decorators | def convert_to_string(f): def wrapper_function(*args,**kwargs): result_before_modification=f(*args,**kwargs) return str(result_before_modification) return wrapper_function @convert_to_string def multiply_by_two(*args,**kwargs): return 2*kwargs.get("input") print(multiply_by_two(input=2)) |
How a class must be structured to be able to be used as a decorator | class MyClass(): __init__(self,f): self.f=f self.count=0 __call_(self): make use of self.f (function to modify) self.count+=1 return self.f(args*,kwargs**) can be used for example to increment a counter each time the modified function is called: mymodifiedfunction.call() mymodifiedfunction.call() mymodifiedfunction.count You can add also use wrappers to login information, or to validate arguments passed to the function |
Use of more than one decorator | @decorator2 @decorator1 def mymodifiedfunction(): careful, the decorator applied first is the one nearer the function name (decorator1 in this example). Then decorator2 is applied to the function returned by decorator1 |
Have the decorator inherite meta data from the modified function | Add @functools.wrap(f) before the definition of the function that the decorator will return def convert_to_string(f): @functools.wrap(f) def wrapper_function(*args,**kwargs): result_before_modification=f(*args,**kwargs) return str(result_before_modification) return wrapper_function |
Decorator and parameters | def convert(type): def convertor(f): def wrapper_function(*args,**kwargs): result_before_modification=f(*args,**kwargs) if type=="string": return str(result_before_modification) elif type=="integer": return int(result_before_modification) return wrapper_function return convertor @convert("integer") def multiply_by_two(*args,**kwargs): return 2*kwargs.get("input") |
Decorating a function belonging to a class, and self | def convert_to_string(f): @functools.wrap(f) def wrapper_function(self,*args,**kwargs): result_before_modification=f(self,*args,**kwargs) return str(result_before_modification) return wrapper_function |
Class with constructor and function | class MyClass: myvar=myvalue def __init__(self,param1,param2): self.param1=param1 self.param2=param2 def myfunction(self,param3): ... |
Instantiate a class | myobject=MyClass(param1,param2) |
Delete an object | del myobject |
Create a child class, with constructor using parent class and adding a variable | class MyChildClass(MyParentClass): def __init__(self, oldParam,newParam): super().__init__(oldParam) self.newParam=newParam |
Create an interface | from abc import ABC, abstractmethod class MyInterface(ABC): @abstractmethod def __init__(self,myparams): pass @abstractmethod def myfunctions(self, myparams): pass |
Implement an interface | class MyClass(MyInterface): |
Access an element of a module | In mymodule.py: mymodulevar=myvalue In the calling script: import mymodule myvar=mydule.mymodulevar |
List elements belonging to a module | mylist=dir(mymodule) |
Import only a given element from a module | from mymodule import myelement |
import from folder | from foldername import filenamewithoutextension You may need to put an empty file in the folder with the following name: __init__.py |
Get the current date and time | import datetime x=datetime.now() |
Create a date object representing March 8th, 2005 | import datetime x=datetime(2005,3,8) |
Format a date object to show YYYY-MM-DD hour:minutes:second:microsecond | x.strftime("%Y-%m-%d %H:%M:%S:%f") |
Format a date object to show Month in English, day of the week in English | x.strftime("%B %A") |
Round up | ceil |
Round down | floor |
Convert json to a dictionnary | import json mydictionnary=json.load(myjson) |
Convert dictionnary to json | myjson=json.dump(mydictionnary) |
Convert dictionnary to json, giving an indent of 4 characters | myjson=json.dump(mydictionnary,indent=4) |
Convert dictionnary to json, sorting keys | myjson=json.dump(mydictionnary,sort_keys=true) |
Condition using a regular expression | import re if re.search(mypattern,mytext): |
Get the first element when using a search | import re myresult=re.search(mypattern,mytext) if myresult: myresult.start() |
Pattern: my string starts with x and end with y | mypattern="^x.*y$" |
Get a list containing all the matches of a pattern | mylist=re.findall(mypattern,mytext) |
Split a string in a list using a regular expression | mylist=re.split(mypattern,mytext) |
Split a string in a list using a regular expression but split only a the first occurrence | mylist=re.split(mypattern,mytext,1) |
Perform a replacement using a regular expression. Replace first n occurences | myresultingstring=re.sub(mypattern,replacingstring,mytext,n) |
Regular expression with groups : find all matching pattern and for each match, get first group and second group | mytuples=re.findall(r'mypattern', mystring) for matchingtuple in mytuples: print matchingtuple [0] print matchingtuple [1] |
Regular expression: if the pattern contains parenthesis ( forming groups ), find the different groups | result=re.search(mypattern, mystring) myfirstgroupvalue=result.group(1) |
Regular expression : in if statement, condition that match is found | if re.search(mypattern, mystring): |
Regulary expression : any character any number of times, but greedy | .*? |
Match letter digit or underscore | \w |
Beginning or end of word | \b |
Match a whitespace character | \s |
Match a number | \d |
Match only at start of string | \A |
Match only at end of string | \Z |
Repetition: zero or one | ? |
Repetition: one or more | + |
Repetition: zero or more | * |
Repetition: from n to m times | {n,m} |
Repetition: n times | {n} |
Repetition: more than n times | {,n} |
Match any character | . |
Match word1 or word2 | [word1|word2] |
Match any character except a,b,c | [^abc] |
List of characters to escape | . ^ $ * + ?{[ ] \ | ( ) |
Get version of pip | pip --version |
Install a module | pip install mymodule |
Install a module, using version greater than n | pip install 'mymodule>n' |
Install a module, using version n | pip install mymodule==n |
Install the last version of a module | pip install -U mymodule |
Install the last version of pip itself | pip install -U pip |
purpose of setup.py | file called when installing the module with pip inside you have for example install_requires=[... that gives the list of modules required |
Find a package | https://pypi.org/ |
Remove a package | pip uninstall mymodule |
List the installed packages | pip list |
Create a virtual environment | pip3 install virtualenv create a directory for virtual environments go to directory and type: virtualenv -p python3 virtualenvname or "C:\Users\sesa201795\AppData\Local\Programs\Python\Python38\python.exe" -m venv virtualenvname inside Scripts, : activate.bat you will see your env name in parenthesis on the left ( before the prompt) you will be in a virtual env. From there you have your own python exe and pip exe. from there install packages with pip you can launch your script from there (first go to the directory where you script is) , it will use packages you installed from this virtual env. keep virtual environment folder separate from separate project |
Go out of the virtual environment | Type "deactivate" and press enter |
Create a requirements file with a description of all packages installed | pip freeze > requirements.txt (inside the main directory, from the virtual env directory you created, and after activating the virtual env with activate) |
Install all packages based on the requirements file | in another environment: pip install -r requirements.txt |
Run your program in visual studio code using your virtual environment | When running your program with code editor, choose to run with your virtual environment by selecting the python.exe located in a virtual environment folder In visual studio code: Ctrl+Shift+P=> type python:s |
Run your program in pycharm using your virtual environment | File, project structure, modules tab on the left, select the current module you work on, then on the right select the tab Dependencies. In module sdk combo box choose add sdk, python sdk, existing environment ( if you have already prepared the environment), then click on button with 3 dots and select the python.exe file in the venv/scripts folder |
Try catch | try: ... except Exception as e: raise Exception( "My error: %s" % str(e) ) from None ... you can also log the information instead of raising the exception in case you need to still go on with the process. |
Try catch, and execute commands whether there is an error or not (for example to close a connection or a file) | try: ... except: ... finally: ... |
Raise an exception | raise Exception(mymessage) |
Open a text file for writing from scratch and write a line | myfile=open("myfile.txt","w") myfile.write("mycontent"+"/n") myfile.close() |
Open a text file for reading | myfile=open("myfile.txt","r") |
Open a text file for writing at the end of the file | myfile=open("myfile.txt","a") |
Read the full text of a file | myfile.read() |
Loop through the file, line by line | for myline in myfile: print(myline) |
Close a file | close(myfile) |
Check if a file or folder exist | import os if os.path.exists(myfile): |
Remove a file | import os os.remove(myfile) |
Remove a folder | import os os.rmdir(myfolder) |
Extract data from pdf | import pdfplumber def read_pdf_file(path): full_extract=[] with pdfplumber.open(path) as pdf: for page in pdf.pages: page_text=page.extract_text().replace(" "," ") full_extract.append(page_text) full_document_extract=" ".join(full_extract) return full_document_extract |
Get answer from soap request | headers={'content-type': 'application/xml'} body=""" ... as per specification """ response=requests.post(url,data=body,headers=headers) mycontent=response.content |
Conditional breakpoint | Add a breakpoint, then right click on the break point and in "condition" add a condition ( piece of code as you would write on the code itself, for example: start > max ) |
Format code | Ctrl+Alt+L |
Go to the places where the function was called | Ctrl+B |
Go to the place where the function was defined ( coded ) | Ctrl+alt+B |
Show the list of functions on a page with links to go directly to them ( file structure ) | Ctrl + F12 |
Go directly to a class knowing its name | Ctrl + N and start entering the class name |
Refactor / rename shortcut | Shift + F6 |
Select an area of code ( for example everything under an if statement or a while loop) | Ctrl + w ( do it several times if there are parent areas) |
Change python version used on the python console | File, settings, Build execution ..., python console: python interpreter |
Change python version used when checking the syntax in the project | File, settings, Project:....., Python interpreter |
Search for elements in the code | ctrl+alt+f -> can click on small button on right to turn on regex + use button to pin the window. |
While the program is stopped at a breakpoint, use complex execution to get a value | On the debug tab at the bottom, on the console tab within ( where print() message are shown), enter the logic where the >>> is as if you were typing a python program after entering the command python. In case of if statement, press enter twice at the end of the statement |
Show the git information and functions on the left hand side ( commit tab ) | File, settings, version control, commit: use non-modal commit interface |
Setup to be able to display documentation of functions and classes when hovering | File, settings, tools, python integrated tools, docstring, docstring format: Google, check "Analyze Python code in docstrings" and "render .... " |
Document a function or class | def function_documented(): """ what is the function doings Args: myarg (type): my explanation Returns: myarg1 (type): my explanation myarg2 (type): my explanation >>>myreturn=class_documented(myparamvalue) ...myreturn.function_documented(myotherparamvalue) 'expectedvalue' """ For bullet points: leave a blank line before the first bullet point, and indent with 4 spaces |
Install a plugin | File, settings, Plugin, search for plugin, eventually click on search on the marketplace |
Useful plugins | Kite AI code autocomplete: autocomplete mypy To use mypy: mypy tab right click on file or folder: inspect code |
See branches graphically | alt+9 |
What to do if you forgot to pull changes from master before created your branche, and then you worked on this branch. As a result you missed a lot of changes on master | On the bottom left of Pycharm, you have your branch selected. Click on it, select master, then update ( to update master). Then reclick on Master, merge into current. If you get a list of files with conflicts, click on button Merge you get a screen with 3 column: left: your code, center: result of merge, right: master everything from left or right should be either rejected or accepted. If conflict is too complicated, change result column manually, and then reject on left and right. At the end click on apply Other tactic: checkout master: git fetch origin ( or git pull from master ) checkout yournewbranch: click on your branch on top down of pycharm. Then remote branch, origin/master -> rebase onto selected (select master) |
Make a scratch file for proof of concept | ctrl+alt+shift+insert, then choose the language |
move file from project to scratch | If scratch directory is not there, create any scratch file to have this directory automatically created. Then drag and drop your file to this newly created Scratches folder under "Scratches and console" |
Select entire columns | Ctrl+insert or, if you do not have an insert key: Press Ctrl+Shift+A (Find Action feature) and type column. The first result is Column Selection Mode, click on On to turn it on. Select text with keyboard (by holding Shift) Once done, using Find Action Feature, turn it off. |
Shortcut to see difference between expected result and real result in unit test | click on the window showing the results, and ctrl+D |
basic unit test | import unittest class ThisIsMyTest(unittest.TestCase): def test_this_thing(self): ... self.assertEqual(expected_thing,actual_thing) |
Unit test with nose2 and a parameter | File must be called test_.... and be in folder test from nose2.tools import params @params(param1,param2) def test_is_ok(value): assert value.startswith('a word') Then on root of project, on console, run nose2 |
Unit test coverage with branch testing | pip install coverage From the root of the project ( to check coverage of tests in folder test): coverage run --source=./sourcecodefolder --branch -m unittest discover -s test/ && coverage report To get the missed coverage: coverage run --source=./sourcecodefolder --branch -m unittest discover -s test/ && coverage html Then go to htmlcov folder, open index.html in browser, then click on the file having coverage not 100% |
Run unit test in Pycharm | Edit configuration, then add, choose python unit test, choose pytest in bin select the folder where your tests are go to File| Settings | Tools | Python Integrated Tools , choose pytest as default test runner. If you get the warning “No pytest runner found in selected interpreter”, click the Fix button. |
Best practices unit test | Name of test function says exactly why is tested Per test function, only one group of{preparation, running, asserting) |
Simple stub in python, without using any framework | In the function using the function to stub: class MyClass: def __init__(self,stubbingobject=false): myresult=stubbingobject or objecttostubb() In the test using the function: mytest=MyClass(stubbingobject=MyStubbingObject()) |
Dummy versus stub versus fake | Dummy usually a null value parameter Stub gives a hard coded answer, and is always passing the test Fake is more ellaborate, although not production ready, and can fail the test if the input is not correct |
Record a print to be able to compare it to an expected print | import sys from io import StringIO captured_output=StringIO() sys.stdout=captured_output print("hello world") sys.stdout=sys.__stdout__ captured_value=captured_output .getvalue() print ('Captured:', captured_value ) |
Get a configuration object from a yaml file | import yaml def get_config_from_yaml(path): with open(path, "r") as ymlfile: cfg=yaml.load(ymlfile, Loader=yaml.FullLoader) return cfg cfg will be like a dictionnary. Lists will be like lists within the dictionnary |
Configuration with one parameter, one parent and child parameters, and one list | myparameter: myvalue myparent: mychild: mychildvalue mylist: - myvalue1 - myvalue2 |
CPU intensive tasks | String operation search algorithms graphics processing number crunching algorithms |
Tasks involving waiting | Database reads, writes web service calls copying, downloading, uploading data |
When parallel programming best | Parallel programming best for CPU intensive tasks |
When asynchronous best | Tasks involving waiting |
Package to run tasks in parallel | Multiprocessing package |
Package for asynchronous programming | async.io |
What is a thread | smallest sequence of instruction that can be executed by the operating system |
Run two tasks in parallel | import threading class MyStuffToDo(threading,Thread): def __init__(self,myparam): Thread.__init__(self) self.myparam=myparam def run(self): .... myfirsttak=MyStuffToDo(myval1) mysecondtask=MyStuffToDo(myval2) myfirsttak.run() mysecondtask.run() myfirsttak.join() mysecondtask.join() |
Create thread inside a loop | threads=[] for myvar in mylist: t=threading.Thread(target=self.myfunctiontocall,args=(myparam,)) # carefull to put the , after the last parameter t.start() threads.append(t) for t in threads: t.join() |
Create thread that never stops | threading.Thread(daemon=true) |
Create a thread with an undefinite list of arguments | threading.Thread(kwargs={param1,param2,....}) |
Launch asynchronous function from main program (synchronous) | import asyncio mymainloop=asyncio.new_event_loop() asyncio.set_event_loop(mymainloop) myresult=mymainloop.run_until_complete( launcher.myasynchronousfunction(myparameters) ) |
Declare asynchronous function | async def myfunction(myparameters): |
Launch asynchronous function from main program (synchronous) | myresult=await myfunction(myparameters) |
Define a semaphore to limit the use of a ressource to one | semaphore=threading.Semaphore() |
Define a semaphore to limit the use of a ressource to n | In the main program: semaphore=threading.BoundedSemaphore(n) |
Reserve a shared ressource when working with threads | At the beginning of the action for which you want to prevent more than n run at the same time: semaphore.acquire() |
Unreserve a shared ressource when working with threads | At the end of the action semaphore.release() |