tech:python-draft
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:python-draft [2025/04/15 15:32] – Jean-Baptiste | tech:python-draft [2025/05/26 18:56] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Draft Python3 | ||
| + | |||
| + | ## List comprehensions and generator expressions | ||
| + | |||
| + | | ||
| + | |||
| + | ~~~python | ||
| + | line_list = [' | ||
| + | |||
| + | # Generator expression -- returns iterator | ||
| + | stripped_iter = (line.strip() for line in line_list) | ||
| + | |||
| + | # List comprehension -- returns list | ||
| + | stripped_list = [line.strip() for line in line_list] | ||
| + | |||
| + | stripped_list = [line.strip() for line in line_list if line != "" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Autres | ||
| + | |||
| + | ~~~python | ||
| + | def inc(f, id): | ||
| + | """ | ||
| + | try : | ||
| + | global counter | ||
| + | counter = f(counter) | ||
| + | except NameError: | ||
| + | counter = 0 | ||
| + | return counter | ||
| + | ~~~ | ||
| + | |||
| + | ------------ | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ------------ | ||
| + | |||
| + | ## List unique remove duplicate | ||
| + | |||
| + | Source : | ||
| + | http:// | ||
| + | |||
| + | ~~~python | ||
| + | def unique(items): | ||
| + | found = set([]) | ||
| + | keep = [] | ||
| + | for item in items: | ||
| + | if item not in found: | ||
| + | found.add(item) | ||
| + | keep.append(item) | ||
| + | return keep | ||
| + | ~~~ | ||
| + | |||
| + | ------------ | ||
| + | |||
| + | ## How do you split a list into evenly sized chunks in Python? | ||
| + | |||
| + | Source : http:// | ||
| + | |||
| + | ~~~python | ||
| + | def chunks(l, n): | ||
| + | n = max(1, n) | ||
| + | return [l[i:i + n] for i in range(0, len(l), n)] | ||
| + | ~~~ | ||
| + | |||
| + | ------------- | ||
| + | |||
| + | ## Determine if variable is defined in Python | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ~~~python | ||
| + | try: | ||
| + | thevariable | ||
| + | except NameError: | ||
| + | print "well, it WASN'T defined after all!" | ||
| + | else: | ||
| + | print "sure, it was defined." | ||
| + | ~~~ | ||
| + | |||
| + | I think it's better to avoid the situation. It's cleaner and clearer to write: | ||
| + | |||
| + | ~~~python | ||
| + | a = None | ||
| + | if condition: | ||
| + | a = 42 | ||
| + | ~~~ | ||
| + | |||
| + | ------------ | ||
| + | |||
| + | ## Getting file size in Python | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ~~~python | ||
| + | def get_Size(file): | ||
| + | file.seek(0, | ||
| + | size = file.tell() | ||
| + | return size | ||
| + | ~~~ | ||
| + | |||
| + | Exemple : | ||
| + | ~~~python | ||
| + | with open(' | ||
| + | size=get_Size(file) | ||
| + | |||
| + | with open(' | ||
| + | # | ||
| + | for i in range(size): | ||
| + | un, deux = read_hexafile() | ||
| + | msg.append(un) | ||
| + | msg.append(deux) | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ''/ | ||
| + | ~~~python | ||
| + | fnull = open(os.devnull, | ||
| + | ~~~ | ||
| + | |||
| + | ----------------- | ||
| + | |||
| + | ## Message d' | ||
| + | |||
| + | How to print to stderr in Python? | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ~~~python | ||
| + | def warning(*objs): | ||
| + | print(*objs, | ||
| + | ~~~ | ||
| + | |||
| + | ## Conversion hexa | ||
| + | |||
| + | ~~~python | ||
| + | import numpy as np | ||
| + | |||
| + | carac=re.sub(' | ||
| + | carac= int(carac, 16) | ||
| + | print(carac) | ||
| + | file.write(np.byte(carac)) | ||
| + | |||
| + | |||
| + | with open(' | ||
| + | file.write(b' | ||
| + | | ||
| + | | ||
| + | hex(11) # ' | ||
| + | |||
| + | a = int(' | ||
| + | print(a) | ||
| + | print(' | ||
| + | |||
| + | import binascii | ||
| + | binascii.unhexlify(' | ||
| + | |||
| + | " | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | http:// | ||
| + | ~~~python | ||
| + | with open(myfile) as f: | ||
| + | iv = binascii.unhexlify(f.readline().strip()) | ||
| + | key = binascii.unhexlify(f.readline().strip()) | ||
| + | count = int(f.readline()) | ||
| + | a = np.fromiter((binascii.unhexlify(line.strip()) for line in f), dtype=' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ~~~python | ||
| + | hashlib.sha512(' | ||
| + | hex(int.from_bytes(' | ||
| + | ~~~ | ||
| + | |||
| + | -------- | ||
| + | |||
| + | Créer un Dictionnaire à partir de deux listes (l'une clef, l' | ||
| + | |||
| + | ~~~python | ||
| + | clef = [' | ||
| + | valeur = [1, 2, 3] | ||
| + | dictionnaire=dict(zip(clef, | ||
| + | ~~~ | ||
| + | |||
| + | ## Test | ||
| + | |||
| + | Voir : | ||
| + | * nox | ||
| + | * unittest | ||
| + | |||
| + | ## Exception | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ~~~python | ||
| + | try: | ||
| + | s | ||
| + | except NameError: | ||
| + | pass | ||
| + | else: # If no exception occured, do : | ||
| + | s.user.logout() | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Temps / time | ||
| + | |||
| + | Voir : | ||
| + | * timeit | ||
| + | |||
| + | ~~~python | ||
| + | start = time.time() | ||
| + | UnifiedJob.objects.filter(id=1096679).update(status=' | ||
| + | end = time.time() | ||
| + | |||
| + | print(end - start) | ||
| + | ~~~ | ||
| + | |||
| + | ## Strings | ||
| + | |||
| + | https:// | ||
| + | |||
| + | ## Debug | ||
| + | |||
| + | Source : https:// | ||
| + | |||
| + | Level 1 | ||
| + | ~~~python | ||
| + | f = open('/ | ||
| + | f.write(variable + ' | ||
| + | f.close() | ||
| + | ~~~ | ||
| + | |||
| + | Level 2 | ||
| + | ~~~python | ||
| + | from pprint import pprint | ||
| + | pprint(variable.__class__.__name__, | ||
| + | pprint(dir(variable), | ||
| + | pprint(vars(variable), | ||
| + | ~~~ | ||
| + | |||
| + | Level 3 (sur une exception) | ||
| + | ~~~python | ||
| + | import traceback | ||
| + | f.write(str(traceback.format_exc())) | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## map reduce filter | ||
| + | |||
| + | Voir : | ||
| + | * [[notes_python_multithreading|Alternatives Multi threads à map]] | ||
| + | |||
| + | |||
| + | ## Aures | ||
| + | |||
| + | A noter que sur RedHat 8 le chemin vers python est ''/ | ||
