From e08f35cb8027a1aaf5c3db8dbc04f877a10d88be Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 11 Jul 2021 22:32:01 +0200 Subject: [PATCH] modernized type checking --- app/decorators.py | 6 +++--- app/scodoc/notes_table.py | 7 +++---- app/scodoc/sco_apogee_csv.py | 3 +-- app/scodoc/sco_codes_parcours.py | 2 +- app/scodoc/sco_compute_moy.py | 3 +-- app/scodoc/sco_excel.py | 15 +++++++-------- app/scodoc/sco_formsemestre_status.py | 7 +++++-- app/scodoc/sco_formsemestre_validation.py | 4 ++-- app/scodoc/sco_formulas.py | 11 +++++------ app/scodoc/sco_groups_view.py | 2 +- app/scodoc/sco_import_etuds.py | 2 +- app/scodoc/sco_parcours_dut.py | 7 +++---- app/scodoc/sco_pvpdf.py | 2 +- app/scodoc/sco_tag_module.py | 2 +- app/scodoc/sco_utils.py | 4 ++-- app/scodoc/sco_vdi.py | 2 +- 16 files changed, 38 insertions(+), 41 deletions(-) diff --git a/app/decorators.py b/app/decorators.py index af4223b6..d1c4745b 100644 --- a/app/decorators.py +++ b/app/decorators.py @@ -174,7 +174,7 @@ def scodoc7func(context): pos_arg_values.append(context) else: # XXX Convert to regular string for ScoDoc8/Python 2 #py3 - if type(req_args[arg_name]) == str: + if isinstance(req_args[arg_name], str): pos_arg_values.append(req_args[arg_name].encode("utf-8")) else: pos_arg_values.append(req_args[arg_name]) @@ -187,7 +187,7 @@ def scodoc7func(context): elif arg_name in req_args: # set argument kw optionnel # XXX Convert to regular string for ScoDoc8/Python 2 #py3 - if type(req_args[arg_name]) == str: + if isinstance(req_args[arg_name], str): kwargs[arg_name] = req_args[arg_name].encode("utf-8") else: kwargs[arg_name] = req_args[arg_name] @@ -236,7 +236,7 @@ class ScoDoc7Context(object): logging.getLogger(__name__).info("populating context %s" % self) for k in globals_dict: if (not k.startswith("_")) and ( - type(globals_dict[k]) == types.FunctionType + isinstance(globals_dict[k], types.FunctionType) ): setattr(self, k, globals_dict[k].__get__(self)) diff --git a/app/scodoc/notes_table.py b/app/scodoc/notes_table.py index 6f91befd..c5974ee5 100644 --- a/app/scodoc/notes_table.py +++ b/app/scodoc/notes_table.py @@ -27,7 +27,6 @@ """Calculs sur les notes et cache des resultats """ -from types import FloatType import time import pdb import inspect @@ -349,7 +348,7 @@ class NotesTable(object): (self.moy_ue[ue_id][etudid], etudid) for etudid in self.moy_ue[ue_id] ] ue_eff = len( - [x for x in val_ids if type(x[0]) == FloatType] + [x for x in val_ids if isinstance(x[0], float)] ) # nombre d'étudiants avec une note dans l'UE val_ids.sort(key=row_key) ue_rangs[ue_id] = ( @@ -845,7 +844,7 @@ class NotesTable(object): mu["event_date"] = event_date # - ECTS ? ("pot" pour "potentiels" car les ECTS ne seront acquises qu'apres validation du jury if ( - type(mu["moy"]) == FloatType + isinstance(mu["moy"], float) and mu["moy"] >= self.parcours.NOTES_BARRE_VALID_UE ): mu["ects_pot"] = ue["ects"] or 0.0 @@ -1182,7 +1181,7 @@ class NotesTable(object): ] ue_cap["moy_ue"] = moy_ue_cap if ( - type(moy_ue_cap) == FloatType + isinstance(moy_ue_cap, float) and moy_ue_cap >= self.parcours.NOTES_BARRE_VALID_UE ): if not cnx: diff --git a/app/scodoc/sco_apogee_csv.py b/app/scodoc/sco_apogee_csv.py index 03af2d70..d86a282a 100644 --- a/app/scodoc/sco_apogee_csv.py +++ b/app/scodoc/sco_apogee_csv.py @@ -81,7 +81,6 @@ XXX A vérifier: """ import collections -from types import FloatType import re import time import datetime @@ -154,7 +153,7 @@ def code_scodoc_to_apo(code): def _apo_fmt_note(note): "Formatte une note pour Apogée (séparateur décimal: ',')" - if not note and type(note) != FloatType: + if not note and isinstance(note, float): return "" try: val = float(note) diff --git a/app/scodoc/sco_codes_parcours.py b/app/scodoc/sco_codes_parcours.py index a22fb4fc..251a7ada 100644 --- a/app/scodoc/sco_codes_parcours.py +++ b/app/scodoc/sco_codes_parcours.py @@ -238,7 +238,7 @@ class TypeParcours(object): ue_status for ue_status in ues_status if ue_status["coef_ue"] > 0 - and type(ue_status["moy"]) == float + and isinstance(ue_status["moy"], float) and ue_status["moy"] < self.get_barre_ue(ue_status["ue"]["type"]) ] diff --git a/app/scodoc/sco_compute_moy.py b/app/scodoc/sco_compute_moy.py index cb0f2738..746494d3 100644 --- a/app/scodoc/sco_compute_moy.py +++ b/app/scodoc/sco_compute_moy.py @@ -30,7 +30,6 @@ import traceback import pprint -from types import FloatType import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -355,7 +354,7 @@ def do_moduleimpl_moyennes(context, nt, mod): if etudid in eval_rattr["notes"]: note = eval_rattr["notes"][etudid]["value"] if note != None and note != NOTES_NEUTRALISE and note != NOTES_ATTENTE: - if type(R[etudid]) != FloatType: + if isinstance(R[etudid], float): R[etudid] = note else: note_sur_20 = note * 20.0 / eval_rattr["note_max"] diff --git a/app/scodoc/sco_excel.py b/app/scodoc/sco_excel.py index ea771088..50925516 100644 --- a/app/scodoc/sco_excel.py +++ b/app/scodoc/sco_excel.py @@ -29,9 +29,9 @@ """ Excel file handling """ import time, datetime -from types import IntType, FloatType, LongType -from pyExcelerator import * +# #sco8 #py3 XXX TODO A revoir utiliser d'autres modules +# from pyExcelerator import * import app.scodoc.sco_utils as scu from app.scodoc import notesdb @@ -226,10 +226,9 @@ class ScoExcelSheet(object): co = 0 for c in l: # safety net: allow only str, int and float - if type(c) == LongType: - c = int(c) # assume all ScoDoc longs fits in int ! - elif type(c) not in (IntType, FloatType): - c = str(c).decode(scu.SCO_ENCODING) + # #py3 #sco8 A revoir lors de la ré-écriture de ce module + # XXX if type(c) not in (IntType, FloatType): + # c = str(c).decode(scu.SCO_ENCODING) ws0.write(li, co, c, self.get_cell_style(li, co)) co += 1 @@ -263,9 +262,9 @@ def Excel_SimpleTable(titles=[], lines=[[]], SheetName="feuille", titlesStyles=[ for it in l: cell_style = default_style # safety net: allow only str, int and float - if type(it) == LongType: + if isinstance(it, LongType): # XXX it = int(it) # assume all ScoDoc longs fits in int ! - elif type(it) not in (IntType, FloatType): + elif type(it) not in (IntType, FloatType): # XXX A REVOIR it = str(it).decode(scu.SCO_ENCODING) cell_style = text_style ws0.write(li, col, it, cell_style) diff --git a/app/scodoc/sco_formsemestre_status.py b/app/scodoc/sco_formsemestre_status.py index 698125cb..a0e554f8 100644 --- a/app/scodoc/sco_formsemestre_status.py +++ b/app/scodoc/sco_formsemestre_status.py @@ -508,7 +508,7 @@ def retreive_formsemestre_from_request(context, REQUEST): formsemestre_id = group["formsemestre_id"] elif group_ids: if group_ids: - if type(group_ids) == str: + if isinstance(group_ids, str): group_id = group_ids else: # prend le semestre du 1er groupe de la liste: @@ -781,7 +781,10 @@ def _make_listes_sem(context, sem, REQUEST=None, with_absences=True): query_args = cgi.parse_qs(REQUEST.QUERY_STRING) if "head_message" in query_args: del query_args["head_message"] - destination = "%s?%s" % (REQUEST.URL, six.moves.urllib.parse.urlencode(query_args, True)) + destination = "%s?%s" % ( + REQUEST.URL, + six.moves.urllib.parse.urlencode(query_args, True), + ) destination = destination.replace( "%", "%%" ) # car ici utilisee dans un format string ! diff --git a/app/scodoc/sco_formsemestre_validation.py b/app/scodoc/sco_formsemestre_validation.py index b4c8beb1..b75ae1cb 100644 --- a/app/scodoc/sco_formsemestre_validation.py +++ b/app/scodoc/sco_formsemestre_validation.py @@ -991,7 +991,7 @@ def formsemestre_fix_validation_ues(context, formsemestre_id, REQUEST=None): moy_ue = ue_status["moy"] if valid_semestre: if ( - type(moy_ue) == FloatType + isinstance(moy_ue, float) and ue_status["moy"] >= nt.parcours.NOTES_BARRE_VALID_UE ): code_ue = ADM @@ -1001,7 +1001,7 @@ def formsemestre_fix_validation_ues(context, formsemestre_id, REQUEST=None): if not decision_sem["assidu"]: code_ue = AJ elif ( - type(moy_ue) == FloatType + isinstance(moy_ue, float) and ue_status["moy"] >= nt.parcours.NOTES_BARRE_VALID_UE ): code_ue = ADM diff --git a/app/scodoc/sco_formulas.py b/app/scodoc/sco_formulas.py index fed0ebac..7dda7b90 100644 --- a/app/scodoc/sco_formulas.py +++ b/app/scodoc/sco_formulas.py @@ -29,7 +29,6 @@ """ import operator -from types import FloatType, IntType, LongType from functools import reduce @@ -125,17 +124,17 @@ class NoteVector(object): return binary_op(x, self.v, operator.floordiv) -def isScalar(x): - return isinstance(x, FloatType) or isinstance(x, IntType) or isinstance(x, LongType) +def is_scalar(x): + return isinstance(x, (float, int)) def binary_op(x, y, op): - if isScalar(x): - if isScalar(y): + if is_scalar(x): + if is_scalar(y): x, y = [x], [y] else: x = [x] * len(y) - if isScalar(y): + if is_scalar(y): y = [y] * len(x) if len(x) != len(y): diff --git a/app/scodoc/sco_groups_view.py b/app/scodoc/sco_groups_view.py index 29a5d8f7..3c218fb1 100644 --- a/app/scodoc/sco_groups_view.py +++ b/app/scodoc/sco_groups_view.py @@ -317,7 +317,7 @@ class DisplayedGroupsInfos(object): REQUEST=None, ): # log('DisplayedGroupsInfos %s' % group_ids) - if type(group_ids) == str: + if isinstance(group_ids, str): if group_ids: group_ids = [group_ids] # cas ou un seul parametre, pas de liste else: diff --git a/app/scodoc/sco_import_etuds.py b/app/scodoc/sco_import_etuds.py index ca456bef..2d8732da 100644 --- a/app/scodoc/sco_import_etuds.py +++ b/app/scodoc/sco_import_etuds.py @@ -779,7 +779,7 @@ def adm_get_fields(titles, formsemestre_id): def adm_convert_text(v): - if type(v) == float: + if isinstance(v, float): return "{:g}".format(v) # evite "1.0" return v diff --git a/app/scodoc/sco_parcours_dut.py b/app/scodoc/sco_parcours_dut.py index 2eaf52e0..83d5f3f2 100644 --- a/app/scodoc/sco_parcours_dut.py +++ b/app/scodoc/sco_parcours_dut.py @@ -27,7 +27,6 @@ """Semestres: gestion parcours DUT (Arreté du 13 août 2005) """ -from types import FloatType import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -417,7 +416,7 @@ class SituationEtudParcoursGeneric(object): self.etudid ) self.moy_gen = self.nt.get_etud_moy_gen(self.etudid) - self.barre_moy_ok = (type(self.moy_gen) == FloatType) and ( + self.barre_moy_ok = (isinstance(self.moy_gen, float)) and ( self.moy_gen >= (self.parcours.BARRE_MOY - scu.NOTES_TOLERANCE) ) # conserve etat UEs @@ -921,11 +920,11 @@ def formsemestre_validate_ues( else: # log('%s: %s: ue_status=%s' % (formsemestre_id,ue_id,ue_status)) if ( - type(ue_status["moy"]) == FloatType + isinstance(ue_status["moy"], float) and ue_status["moy"] >= nt.parcours.NOTES_BARRE_VALID_UE ): code_ue = ADM - elif type(ue_status["moy"]) != FloatType: + elif isinstance(ue_status["moy"], float): # aucune note (pas de moyenne) dans l'UE: ne la valide pas code_ue = None elif valid_semestre: diff --git a/app/scodoc/sco_pvpdf.py b/app/scodoc/sco_pvpdf.py index 73db208e..11b9d99c 100644 --- a/app/scodoc/sco_pvpdf.py +++ b/app/scodoc/sco_pvpdf.py @@ -835,7 +835,7 @@ def _pvjury_pdf_type( def _format_pv_cell(x): """convert string to paragraph""" - if type(x) == bytes: + if isinstance(x, bytes): return Paragraph(SU(x), cell_style) else: return x diff --git a/app/scodoc/sco_tag_module.py b/app/scodoc/sco_tag_module.py index 20be758c..f9b54ff0 100644 --- a/app/scodoc/sco_tag_module.py +++ b/app/scodoc/sco_tag_module.py @@ -246,7 +246,7 @@ def module_tag_set(context, module_id="", taglist=[], REQUEST=None): # if not taglist: taglist = [] - elif type(taglist) == bytes: + elif isinstance(taglist, bytes): taglist = taglist.split(",") taglist = [t.strip() for t in taglist] log("module_tag_set: module_id=%s taglist=%s" % (module_id, taglist)) diff --git a/app/scodoc/sco_utils.py b/app/scodoc/sco_utils.py index 797809d8..9e765b1b 100644 --- a/app/scodoc/sco_utils.py +++ b/app/scodoc/sco_utils.py @@ -132,7 +132,7 @@ def fmt_note(val, note_max=None, keep_numeric=False): return "EXC" # excuse, note neutralise if val == NOTES_ATTENTE: return "ATT" # attente, note neutralisee - if type(val) == float or type(val) == int: + if isinstance(val, float) or isinstance(val, int): if note_max != None and note_max > 0: val = val * 20.0 / note_max if keep_numeric: @@ -434,7 +434,7 @@ def unescape_html_dict(d): indices = list(range(len(d))) for k in indices: v = d[k] - if type(v) == bytes: + if isinstance(v, bytes): d[k] = unescape_html(v) elif isiterable(v): unescape_html_dict(v) diff --git a/app/scodoc/sco_vdi.py b/app/scodoc/sco_vdi.py index fd7c35df..c9e3ff5b 100644 --- a/app/scodoc/sco_vdi.py +++ b/app/scodoc/sco_vdi.py @@ -63,7 +63,7 @@ class ApoEtapeVDI(object): """ if other is None: return -1 - if type(other) == str: + if isinstance(other, str): other = ApoEtapeVDI(other) if self.vdi and other.vdi: