tech:python-draft
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| tech:python-draft [2025/03/24 15:06] – créée - modification externe 127.0.0.1 | tech:python-draft [2025/05/26 18:56] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| {{tag> | {{tag> | ||
| - | = Draft Python3 | + | # Draft Python3 |
| + | ## List comprehensions and generator expressions | ||
| - | === Autres | + | |
| - | < | + | ~~~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): | def inc(f, id): | ||
| """ | """ | ||
| Ligne 15: | Ligne 32: | ||
| counter = 0 | counter = 0 | ||
| return counter | return counter | ||
| - | </ | + | ~~~ |
| ------------ | ------------ | ||
| Ligne 23: | Ligne 40: | ||
| ------------ | ------------ | ||
| - | == List unique remove duplicate | + | ## List unique remove duplicate |
| Source : | Source : | ||
| http:// | http:// | ||
| - | < | + | ~~~python |
| def unique(items): | def unique(items): | ||
| found = set([]) | found = set([]) | ||
| Ligne 37: | Ligne 54: | ||
| keep.append(item) | keep.append(item) | ||
| return keep | return keep | ||
| - | </ | + | ~~~ |
| ------------ | ------------ | ||
| - | == How do you split a list into evenly sized chunks in Python? | + | ## How do you split a list into evenly sized chunks in Python? |
| Source : http:// | Source : http:// | ||
| - | < | + | ~~~python |
| def chunks(l, n): | def chunks(l, n): | ||
| n = max(1, n) | n = max(1, n) | ||
| return [l[i:i + n] for i in range(0, len(l), n)] | return [l[i:i + n] for i in range(0, len(l), n)] | ||
| - | </ | + | ~~~ |
| ------------- | ------------- | ||
| - | == Determine if variable is defined in Python | + | ## Determine if variable is defined in Python |
| http:// | http:// | ||
| - | < | + | ~~~python |
| try: | try: | ||
| thevariable | thevariable | ||
| Ligne 64: | Ligne 81: | ||
| else: | else: | ||
| print "sure, it was defined." | print "sure, it was defined." | ||
| - | </ | + | ~~~ |
| I think it's better to avoid the situation. It's cleaner and clearer to write: | I think it's better to avoid the situation. It's cleaner and clearer to write: | ||
| - | < | + | ~~~python |
| a = None | a = None | ||
| if condition: | if condition: | ||
| a = 42 | a = 42 | ||
| - | </ | + | ~~~ |
| ------------ | ------------ | ||
| - | == Getting file size in Python | + | ## Getting file size in Python |
| http:// | http:// | ||
| - | < | + | ~~~python |
| def get_Size(file): | def get_Size(file): | ||
| file.seek(0, | file.seek(0, | ||
| size = file.tell() | size = file.tell() | ||
| return size | return size | ||
| - | </ | + | ~~~ |
| Exemple : | Exemple : | ||
| - | < | + | ~~~python |
| with open(' | with open(' | ||
| size=get_Size(file) | size=get_Size(file) | ||
| Ligne 98: | Ligne 115: | ||
| msg.append(un) | msg.append(un) | ||
| msg.append(deux) | msg.append(deux) | ||
| - | </code> | + | ~~~ |
| + | |||
| + | |||
| + | '' | ||
| + | ~~~python | ||
| + | fnull = open(os.devnull, | ||
| + | ~~~ | ||
| ----------------- | ----------------- | ||
| - | == Message d' | + | ## Message d' |
| How to print to stderr in Python? | How to print to stderr in Python? | ||
| Ligne 108: | Ligne 131: | ||
| http:// | http:// | ||
| - | <code bash> | + | ~~~python |
| def warning(*objs): | def warning(*objs): | ||
| - | print(*objs, | + | print(*objs, |
| - | </ | + | ~~~ |
| - | == Conversion hexa | + | ## Conversion hexa |
| - | < | + | ~~~python |
| import numpy as np | import numpy as np | ||
| Ligne 139: | Ligne 162: | ||
| " | " | ||
| - | </ | + | ~~~ |
| http:// | http:// | ||
| - | < | + | ~~~python |
| with open(myfile) as f: | with open(myfile) as f: | ||
| iv = binascii.unhexlify(f.readline().strip()) | iv = binascii.unhexlify(f.readline().strip()) | ||
| Ligne 148: | Ligne 171: | ||
| count = int(f.readline()) | count = int(f.readline()) | ||
| a = np.fromiter((binascii.unhexlify(line.strip()) for line in f), dtype=' | a = np.fromiter((binascii.unhexlify(line.strip()) for line in f), dtype=' | ||
| - | </ | + | ~~~ |
| - | < | + | ~~~python |
| hashlib.sha512(' | hashlib.sha512(' | ||
| hex(int.from_bytes(' | hex(int.from_bytes(' | ||
| - | </ | + | ~~~ |
| -------- | -------- | ||
| Ligne 160: | Ligne 183: | ||
| Créer un Dictionnaire à partir de deux listes (l'une clef, l' | Créer un Dictionnaire à partir de deux listes (l'une clef, l' | ||
| - | <code bash> | + | ~~~python |
| clef = [' | clef = [' | ||
| valeur = [1, 2, 3] | valeur = [1, 2, 3] | ||
| dictionnaire=dict(zip(clef, | dictionnaire=dict(zip(clef, | ||
| - | </ | + | ~~~ |
| - | == Test | + | ## Test |
| Voir : | Voir : | ||
| Ligne 172: | Ligne 195: | ||
| * unittest | * unittest | ||
| - | == Exception | + | ## Exception |
| http:// | http:// | ||
| - | < | + | ~~~python |
| try: | try: | ||
| s | s | ||
| Ligne 183: | Ligne 206: | ||
| else: # If no exception occured, do : | else: # If no exception occured, do : | ||
| s.user.logout() | s.user.logout() | ||
| - | </ | + | ~~~ |
| - | == Temps / time | + | ## Temps / time |
| Voir : | Voir : | ||
| * timeit | * timeit | ||
| - | < | + | ~~~python |
| start = time.time() | start = time.time() | ||
| UnifiedJob.objects.filter(id=1096679).update(status=' | UnifiedJob.objects.filter(id=1096679).update(status=' | ||
| Ligne 198: | Ligne 221: | ||
| print(end - start) | print(end - start) | ||
| - | </ | + | ~~~ |
| - | == Strings | + | ## Strings |
| https:// | https:// | ||
| - | == Debug | + | ## Debug |
| Source : https:// | Source : https:// | ||
| Level 1 | Level 1 | ||
| - | < | + | ~~~python |
| f = open('/ | f = open('/ | ||
| f.write(variable + ' | f.write(variable + ' | ||
| f.close() | f.close() | ||
| - | </ | + | ~~~ |
| Level 2 | Level 2 | ||
| - | < | + | ~~~python |
| from pprint import pprint | from pprint import pprint | ||
| pprint(variable.__class__.__name__, | pprint(variable.__class__.__name__, | ||
| pprint(dir(variable), | pprint(dir(variable), | ||
| pprint(vars(variable), | pprint(vars(variable), | ||
| - | </ | + | ~~~ |
| Level 3 (sur une exception) | Level 3 (sur une exception) | ||
| - | < | + | ~~~python |
| import traceback | import traceback | ||
| f.write(str(traceback.format_exc())) | f.write(str(traceback.format_exc())) | ||
| - | </ | + | ~~~ |
| - | == map reduce filter | + | ## map reduce filter |
| Voir : | Voir : | ||
| Ligne 236: | Ligne 259: | ||
| - | == Aures | + | ## Aures |
| A noter que sur RedHat 8 le chemin vers python est ''/ | A noter que sur RedHat 8 le chemin vers python est ''/ | ||
tech/python-draft.1742825205.txt.gz · Dernière modification : de 127.0.0.1
