python-modernize

This commit is contained in:
Emmanuel Viennet 2021-07-09 21:44:39 +02:00
parent 16be3e8fc9
commit 427eb169aa
6 changed files with 65 additions and 127 deletions

View File

@ -7,26 +7,29 @@
Emmanuel Viennet, 2007 - 2014 Emmanuel Viennet, 2007 - 2014
""" """
from __future__ import print_function
import pdb, os, sys import pdb, os, sys
import psycopg2 import psycopg2
from six.moves import input
if len(sys.argv) != 4: if len(sys.argv) != 4:
print 'Usage: %s database ancien_utilisateur nouvel_utilisateur' % sys.argv[0] print("Usage: %s database ancien_utilisateur nouvel_utilisateur" % sys.argv[0])
print 'Exemple: change_enseignant.py SCOGEII toto tata' print("Exemple: change_enseignant.py SCOGEII toto tata")
sys.exit(1) sys.exit(1)
dbname = sys.argv[1] dbname = sys.argv[1]
OLD_ID = sys.argv[2] OLD_ID = sys.argv[2]
NEW_ID = sys.argv[3] NEW_ID = sys.argv[3]
DBCNXSTRING = 'dbname=%s' % dbname DBCNXSTRING = "dbname=%s" % dbname
# Confirmation # Confirmation
ans = raw_input("Remplacer le l'utilisateur %s par %s dans toute la base du departement %s ?" ans = input(
% (OLD_ID, NEW_ID, dbname)).strip() "Remplacer le l'utilisateur %s par %s dans toute la base du departement %s ?"
if not ans or ans[0].lower() not in 'oOyY': % (OLD_ID, NEW_ID, dbname)
print 'annulation' ).strip()
if not ans or ans[0].lower() not in "oOyY":
print("annulation")
sys.exit(-1) sys.exit(-1)
@ -34,24 +37,21 @@ cnx = psycopg2.connect( DBCNXSTRING )
cursor = cnx.cursor() cursor = cnx.cursor()
req = "update %s set %s=%%(new_id)s where %s=%%(old_id)s" req = "update %s set %s=%%(new_id)s where %s=%%(old_id)s"
args = { 'old_id' : OLD_ID, 'new_id' : NEW_ID } args = {"old_id": OLD_ID, "new_id": NEW_ID}
tables_attr = { tables_attr = {
'notes_formsemestre' : 'responsable_id', "notes_formsemestre": "responsable_id",
'entreprise_contact' : 'enseignant', "entreprise_contact": "enseignant",
'admissions' : 'rapporteur', "admissions": "rapporteur",
'notes_moduleimpl' : 'responsable_id', "notes_moduleimpl": "responsable_id",
'notes_modules_enseignants' : 'ens_id', "notes_modules_enseignants": "ens_id",
'notes_notes' : 'uid', "notes_notes": "uid",
'notes_notes_log' : 'uid', "notes_notes_log": "uid",
'notes_appreciations' : 'author', "notes_appreciations": "author",
} }
for (table, attr) in tables_attr.items(): for (table, attr) in tables_attr.items():
cursor.execute(req % (table, attr, attr), args) cursor.execute(req % (table, attr, attr), args)
print 'table %s: %s' % (table, cursor.statusmessage) print("table %s: %s" % (table, cursor.statusmessage))
cnx.commit() cnx.commit()

View File

@ -21,15 +21,18 @@ et postgresql lance
Emmanuel Viennet, 2007-2020 Emmanuel Viennet, 2007-2020
""" """
import pdb, os, sys, psycopg2 from __future__ import print_function
import pdb
import sys
import psycopg2
if len(sys.argv) != 4: if len(sys.argv) != 4:
print "Usage: %s database orig_etudid dest_etudid" % sys.argv[0] print("Usage: %s database orig_etudid dest_etudid" % sys.argv[0])
print " Fusionne l'étudiant orig dans l'étudiant dest." print(" Fusionne l'étudiant orig dans l'étudiant dest.")
print " L'identité, adresse et admission de dest restent inchangées." print(" L'identité, adresse et admission de dest restent inchangées.")
print " Les inscriptions, notes, absences etc. d'orig sont associées à dest." print(" Les inscriptions, notes, absences etc. d'orig sont associées à dest.")
print " Après cette operation, orig_etudid peut être supprimé." print(" Après cette operation, orig_etudid peut être supprimé.")
print "Exemple: change_etudid.py SCOGEII E1234 E87654" print("Exemple: change_etudid.py SCOGEII E1234 E87654")
sys.exit(1) sys.exit(1)
dbname = sys.argv[1] dbname = sys.argv[1]
@ -69,6 +72,6 @@ tables = (
for table in tables: for table in tables:
cursor.execute(req % table, args) cursor.execute(req % table, args)
print "table %s: %s" % (table, cursor.statusmessage) print("table %s: %s" % (table, cursor.statusmessage))
cnx.commit() cnx.commit()

View File

@ -6,14 +6,17 @@
(erreur passage GEA, fev 2007) (erreur passage GEA, fev 2007)
""" """
import pdb,os,sys,psycopg from __future__ import print_function
import csv import csv
import pdb
import sys
import psycopg2
DBCNXSTRING = 'host=localhost user=scogea dbname=SCOXXXX password=XXXXX' DBCNXSTRING = "host=localhost user=scogea dbname=SCOXXXX password=XXXXX"
SCO_ENCODING = 'utf-8' SCO_ENCODING = "utf-8"
cnx = psycopg.connect( DBCNXSTRING ) cnx = psycopg2.connect(DBCNXSTRING)
cursor = cnx.cursor() cursor = cnx.cursor()
cursor.execute("select * from identite i order by nom") cursor.execute("select * from identite i order by nom")
@ -21,18 +24,23 @@ R = cursor.dictfetchall()
nzero = 0 nzero = 0
nhomonoins = 0 nhomonoins = 0
print 'etudid, nom, prenom, nb_inscriptions' print("etudid, nom, prenom, nb_inscriptions")
for e in R: for e in R:
cursor.execute("select count(*) from notes_formsemestre_inscription where etudid=%(etudid)s", { 'etudid' : e['etudid'] } ) cursor.execute(
"select count(*) from notes_formsemestre_inscription where etudid=%(etudid)s",
{"etudid": e["etudid"]},
)
nbins = cursor.fetchone()[0] nbins = cursor.fetchone()[0]
if nbins == 0: if nbins == 0:
nzero += 1 nzero += 1
# recherche homonyme # recherche homonyme
cursor.execute("select * from identite i where nom=%(nom)s and prenom=%(prenom)s", e ) cursor.execute(
"select * from identite i where nom=%(nom)s and prenom=%(prenom)s", e
)
H = cursor.dictfetchall() H = cursor.dictfetchall()
if len(H) == 2: if len(H) == 2:
nhomonoins += 1 nhomonoins += 1
print e['etudid'], e['nom'], e['prenom'], nbins print(e["etudid"], e["nom"], e["prenom"], nbins)
# etudiant non inscrit ayant un homonyme exact: # etudiant non inscrit ayant un homonyme exact:
# il doit etre supprimé !!! # il doit etre supprimé !!!
# cursor.execute("delete from admissions where etudid=%(etudid)s", e) # cursor.execute("delete from admissions where etudid=%(etudid)s", e)
@ -40,4 +48,4 @@ for e in R:
cnx.commit() cnx.commit()
print '= %d etudiants, %d jamais inscrits, %d avec homo' % (len(R), nzero, nhomonoins) print("= %d etudiants, %d jamais inscrits, %d avec homo" % (len(R), nzero, nhomonoins))

View File

@ -33,7 +33,10 @@ CSV: utf-8/Euro, tab, no text delimiter
(le faire avec OpenOffice ou NeoOffice...) (le faire avec OpenOffice ou NeoOffice...)
""" """
import sys, pdb from __future__ import print_function
import sys
import app.scodoc.sco_utils as scu
sourcefile = sys.argv[1] # fichier CSV sourcefile = sys.argv[1] # fichier CSV
@ -75,8 +78,6 @@ rules_source_file='%s'
sourcefile, sourcefile,
) )
import sco_utils as scu
def _fmt(s): def _fmt(s):
if not s: if not s:
@ -134,4 +135,4 @@ def genrules(csv):
) )
print(genrules(open(sourcefile).readlines())) print((genrules(open(sourcefile).readlines())))

View File

@ -14,15 +14,15 @@ import psycopg2
if len(sys.argv) != 4: if len(sys.argv) != 4:
print( 'Usage: %s database formsemestre_id user_name' % sys.argv[0]) print("Usage: %s database formsemestre_id user_name" % sys.argv[0])
print( 'Exemple: reset_sem_ens.py SCOGEII SEM34534 toto') print("Exemple: reset_sem_ens.py SCOGEII SEM34534 toto")
sys.exit(1) sys.exit(1)
dbname = sys.argv[1] dbname = sys.argv[1]
formsemestre_id = sys.argv[2] formsemestre_id = sys.argv[2]
user_name = sys.argv[3] user_name = sys.argv[3]
DBCNXSTRING = 'dbname=%s' % dbname DBCNXSTRING = "dbname=%s" % dbname
cnx = psycopg2.connect(DBCNXSTRING) cnx = psycopg2.connect(DBCNXSTRING)
@ -31,10 +31,5 @@ cursor = cnx.cursor()
print('affecting all modules of semestre %s to "%s"' % (formsemestre_id, user_name)) print('affecting all modules of semestre %s to "%s"' % (formsemestre_id, user_name))
req = "update notes_moduleimpl set responsable_id=%(responsable_id)s where formsemestre_id=%(formsemestre_id)s" req = "update notes_moduleimpl set responsable_id=%(responsable_id)s where formsemestre_id=%(formsemestre_id)s"
cursor.execute(req, {'formsemestre_id':formsemestre_id, 'responsable_id': user_name}) cursor.execute(req, {"formsemestre_id": formsemestre_id, "responsable_id": user_name})
cnx.commit() cnx.commit()

View File

@ -1,69 +0,0 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
"""List Zope published methods (helps redesign ScoDoc's API).
Usage:
scotests/scointeractive.sh RT misc/zopelistmethods.py
(replace RT by an existing departement id)
E. Viennet 2020-01-26
"""
from __future__ import print_function
from types import FunctionType, MethodType
from pprint import pprint as pp
import inspect
from ZScoDoc import ZScoDoc
from ZScolar import ZScolar
from ZNotes import ZNotes
from ZAbsences import ZAbsences
from ZScoUsers import ZScoUsers
from ZEntreprises import ZEntreprises
RESFILENAME = "publishedmethods.csv"
def get_methods_description(klass):
D = klass.__dict__
M = []
for method_name in D:
o = D[method_name]
if method_name[0] != "_" and type(o) == FunctionType and o.__doc__:
argspec = inspect.getargspec(D[method_name])
defaults = argspec.defaults or []
if defaults:
args = argspec.args[: -len(defaults)]
else:
args = argspec.args
for a in defaults:
args.append("%s=%s" % (argspec.args[len(args)], repr(a)))
M.append((method_name, ", ".join(args)))
M.sort()
return M
published_by_module = {}
lines = []
for klass in ZScoDoc, ZScolar, ZNotes, ZAbsences, ZEntreprises, ZScoUsers:
module_name = klass.__name__
published_by_module[module_name] = []
M = get_methods_description(klass)
for m in M:
published_by_module[module_name].append(m)
lines.append((module_name, m[0], m[1]))
print("%s = %d published methods" % (module_name, len(M)))
print("---\nModule \t #methods")
N = 0
for module_name in published_by_module:
n = len(published_by_module[module_name])
print(module_name, "\t", n)
N += n
print("Total: \t ", N)
print("Writing %s" % RESFILENAME)
with open(RESFILENAME, "w") as f:
f.write("\n".join(["\t".join(l) for l in lines]))