1
0
forked from ScoDoc/ScoDoc

python-modernize + code cleaning

This commit is contained in:
Emmanuel Viennet 2021-07-09 16:29:45 +02:00
parent 0c33d363ca
commit 53cba824eb
7 changed files with 23 additions and 58 deletions

View File

@ -30,9 +30,8 @@
from __future__ import absolute_import
from flask import g, url_for
from . import listhistogram
import app.scodoc.sco_utils as scu
from . import listhistogram
def horizontal_bargraph(value, mark):

View File

@ -1,7 +1,8 @@
from __future__ import absolute_import
import math
def ListHistogram(L, nbins, minmax=None, normalize=None):
def ListHistogram(L, nbins, minmax=None):
"""Compute histogram of a list.
Does not use Numeric or numarray.

View File

@ -31,6 +31,7 @@
NOTA: inutilisable dans une instance Zope (can't pickle functions)
"""
from __future__ import absolute_import
from app.scodoc.notes_log import log
@ -45,7 +46,7 @@ class CacheFunc(object):
self.cache = {} # { arguments : function result }
def __call__(self, *args):
if self.cache.has_key(args):
if args in self.cache:
# log('cache hit %s' % str(args))
return self.cache[args]
else:

View File

@ -1,6 +1,7 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import re
import inspect
@ -67,7 +68,7 @@ def retreive_request(skip=0):
"""
def search(frame):
if frame.f_locals.has_key("REQUEST"):
if "REQUEST" in frame.f_locals:
return frame.f_locals["REQUEST"]
if frame.f_back:
return search(frame.f_back)
@ -99,9 +100,9 @@ def retreive_dept():
# Alarms by email:
def sendAlarm(context, subj, txt):
import sco_utils
import sco_emails
import sco_preferences
from . import sco_utils
from . import sco_emails
from . import sco_preferences
msg = MIMEMultipart()
subj = Header(subj, sco_utils.SCO_ENCODING)

View File

@ -27,6 +27,7 @@
"""Calculs sur les notes et cache des resultats
"""
from __future__ import absolute_import
from types import StringType, FloatType
import time
import pdb
@ -121,7 +122,7 @@ def get_sem_ues_modimpls(context, formsemestre_id, modimpls=None):
if not mod["ue_id"] in uedict:
ue = sco_edit_ue.do_ue_list(context, args={"ue_id": mod["ue_id"]})[0]
uedict[ue["ue_id"]] = ue
ues = uedict.values()
ues = list(uedict.values())
ues.sort(key=lambda u: u["numero"])
return ues, modimpls
@ -259,7 +260,7 @@ class NotesTable:
self.comp_ue_capitalisees()
# Liste des moyennes de tous, en chaines de car., triées
self._ues = uedict.values()
self._ues = list(uedict.values())
self._ues.sort(key=lambda u: u["numero"])
T = []
@ -421,7 +422,7 @@ class NotesTable:
def get_etud_etat(self, etudid):
"Etat de l'etudiant: 'I', 'D', DEF ou '' (si pas connu dans ce semestre)"
if self.inscrdict.has_key(etudid):
if etudid in self.inscrdict:
return self.inscrdict[etudid]["etat"]
else:
return ""
@ -1081,7 +1082,7 @@ class NotesTable:
)
decisions_jury_ues = {}
for (etudid, ue_id, code, event_date) in cursor.fetchall():
if not decisions_jury_ues.has_key(etudid):
if etudid not in decisions_jury_ues:
decisions_jury_ues[etudid] = {}
# Calcul des ECTS associes a cette UE:
ects = 0.0

View File

@ -1,12 +1,12 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import pdb, os, sys, string
import traceback
import psycopg2
import psycopg2.pool
import psycopg2.extras
import thread
from flask import g
@ -36,27 +36,6 @@ def unquote(s):
return s.replace("&", "&")
# Ramene une connexion a la base de donnees scolarite
# pour l'instance donnee par context
# La connexion est unique (réutilisée) pour chaque thread
# et est par défaut en autocommit
# _pools = {}
#
#
# def GetDBConnexion(autocommit=True):
# """connexion to the DB of a departement"""
# pool = _pools.get(scu.get_db_cnx_string(), None)
# if not pool:
# pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=scu.get_db_cnx_string())
# _pools[scu.get_db_cnx_string()] = pool
# # log('GetDBConnexion: created pool for "%s"' % scu.get_db_cnx_string())
# cnx = pool.getconn(key=(thread.get_ident(), autocommit))
# # log('GetDBConnexion: autocommit=%s cnx=%s' % (autocommit,cnx))
# if cnx.autocommit != autocommit:
# cnx.autocommit = autocommit
# return cnx
def open_dept_connection():
"""Open a connection to the current dept db"""
# log("open_dept_connection to " + scu.get_db_cnx_string()) # XXX
@ -75,22 +54,6 @@ def GetDBConnexion(autocommit=True): # on n'utilise plus autocommit
return g.db_conn
# Same for users:
_users_pools = {}
def GetUsersDBConnexion(context, autocommit=True):
pool = _users_pools.get(scu.get_db_cnx_string(), None)
if not pool:
pool = psycopg2.pool.ThreadedConnectionPool(2, 8, dsn=scu.get_db_cnx_string())
_users_pools[scu.get_db_cnx_string()] = pool
log('GetUsersDBConnexion: created pool for "%s"' % scu.get_db_cnx_string())
cnx = pool.getconn(key=(thread.get_ident(), autocommit))
if cnx.autocommit != autocommit:
cnx.autocommit = autocommit
return cnx
# Nota: on pourrait maintenant utiliser psycopg2.extras.DictCursor
class ScoDocCursor(psycopg2.extensions.cursor):
"""A database cursor emulating some methods of psycopg v1 cursors"""
@ -130,7 +93,7 @@ def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1):
if vals[col] == "":
vals[col] = None
# open('/tmp/vals','a').write( str(vals) + '\n' )
cols = vals.keys()
cols = list(vals.keys())
colnames = ",".join(cols)
fmt = ",".join(["%%(%s)s" % col for col in cols])
# print 'insert into %s (%s) values (%s)' % (table,colnames,fmt)
@ -329,13 +292,13 @@ class EditableTable:
def create(self, cnx, args, has_uniq_values=False):
"create object in table"
vals = dictfilter(args, self.dbfields, self.filter_nulls)
if vals.has_key(self.id_name) and not self.allow_set_id:
if self.id_name in vals and not self.allow_set_id:
del vals[self.id_name]
if self.html_quote:
quote_dict(vals) # quote all HTML markup
# format value
for title in vals.keys():
if self.input_formators.has_key(title):
if title in self.input_formators:
vals[title] = self.input_formators[title](vals[title])
# insert
oid = DBInsertDict(cnx, self.table_name, vals, commit=True)
@ -405,7 +368,7 @@ class EditableTable:
if v is None and self.convert_null_outputs_to_empty:
v = ""
# format value
if not disable_formatting and self.output_formators.has_key(k):
if not disable_formatting and k in self.output_formators:
try: # XXX debug "isodate"
v = self.output_formators[k](v)
except:
@ -422,7 +385,7 @@ class EditableTable:
quote_dict(vals) # quote HTML
# format value
for title in vals.keys():
if self.input_formators.has_key(title):
if title in self.input_formators:
try:
vals[title] = self.input_formators[title](vals[title])
except:
@ -491,7 +454,7 @@ def dictfilter(d, fields, filter_nulls=True):
# returns a copy of d with only keys listed in "fields" and non null values
r = {}
for f in fields:
if d.has_key(f) and (d[f] != None or not filter_nulls):
if f in d and (d[f] != None or not filter_nulls):
try:
val = d[f].strip()
except:
@ -656,7 +619,7 @@ def UniqListofDicts(L, key):
d = {}
for item in L:
d[item[key]] = item
return d.values()
return list(d.values())
#

View File

@ -90,7 +90,6 @@ def is_valid_password(cleartxt):
# ---------------
# ---------------
# XXX TODO supprimer ndb.GetUsersDBConnexion
def index_html(context, REQUEST, all_depts=False, with_inactives=False, format="html"):