Difference between revisions of "Python"

From Quantum kot
Jump to navigation Jump to search
Line 55: Line 55:
 
It is a good practice to follow PEP8 standard when writing code.
 
It is a good practice to follow PEP8 standard when writing code.
 
In jupyter the PEP8 syntax checker may help highlight the errors.
 
In jupyter the PEP8 syntax checker may help highlight the errors.
 +
To load the checker into a jupyter notebook:
 +
<syntaxhighlight lang="python">
 +
%load_ext pycodestyle_magic
 +
</syntaxhighlight>
 +
 +
To switch on the checker:
 +
<syntaxhighlight lang="python">
 +
%pycodestyle_on
 +
</syntaxhighlight>
 +
To switch off the checker:
 +
<syntaxhighlight lang="python">
 +
%pycodestyle_off
 +
</syntaxhighlight>
  
 
===Main function ===
 
===Main function ===

Revision as of 16:28, 27 January 2023

Python Tips and Tricks


Array enumeration

For numpy

a = np.array([[1, 2], [3, 4]])
for index, x in np.ndenumerate(a):
    print(index, x)

For usual python

a = np.array([[1, 2], [3, 4]])
for index, x in enumerate(a):
    print(index, x)

Zip

For iterating along the several list it is a good practice to use zip:

a = [1,2,3,4]
b = [5,6,7,8]
    for av, bv in zip(a, b):
        print(av)
        print(bv)

Multiprocessing

from multiprocessing import Pool
def run_with_mp_map(items, do_work, processes=None, chunksize=None):
    print(f"running using multiprocessing with {processes=}, {chunksize=}")
    start_t = time.perf_counter()
    with Pool(processes=processes) as pool:
        results = pool.imap(do_work, items, chunksize=chunksize)
    end_t = time.perf_counter()
    wall_duration = end_t - start_t
    print(f"it took: {wall_duration:.2f}s")
    return results

Context manager

 with open(filename, "w") as f:
        f.write("hello!\n")
    # close automatic, even if exception


PEP8 Checker

It is a good practice to follow PEP8 standard when writing code. In jupyter the PEP8 syntax checker may help highlight the errors. To load the checker into a jupyter notebook:

%load_ext pycodestyle_magic

To switch on the checker:

%pycodestyle_on

To switch off the checker:

%pycodestyle_off

Main function

It is a good practice to indicate whether your python file will be executed as a script by including "if __name__ == '__main__':" statement:

def main():
    pass
# Do Something:

if __name__ == '__main__':
   main()


Measure execution timing

import time
start = time.perf_counter()
time.sleep(1)
end = time.perf_counter()
print(end - start)