randomize nips

This commit is contained in:
viennet 2020-10-14 10:07:35 +02:00
parent f1f3112e7b
commit 6f8ce4e6a8
1 changed files with 54 additions and 23 deletions

View File

@ -3,56 +3,87 @@
"""Outils pour environnements de démo.
Change aléatoirement les identites (nom, prenom) d'un ensemble d'étudiants.
Change aléatoirement les identites (nip, sexe, nom, prenom) des étudiants d'un semestre.
Le NIP est choisi alétoirement (nombre entier à 8 chiffres).
Les noms et prénoms sont issus des fichiers noms.txt, prenoms-h.txt, prenoms-f.txt
(ce sont simlement les plus fréquemment rencontrés en France).
Le sexe est choisi aléatoirement 50-50.
"""
import sys
import random
import psycopg2
DBCNXSTRING='dbname=SCODEMO'
def usage():
print(f"Usage: {sys.argv[0]} dbname formsemestre_id")
sys.exit(1)
if len(sys.argv) != 3:
usage()
dbname = sys.argv[1]
formsemestre_id = sys.argv[2]
DBCNXSTRING = f"dbname={dbname}"
# Noms et prénoms les plus fréquents en France:
NOMS = [ x.strip() for x in open('noms.txt').readlines() ]
PRENOMS_H = [ x.strip() for x in open('prenoms-h.txt').readlines() ]
PRENOMS_F = [ x.strip() for x in open('prenoms-f.txt').readlines() ]
NOMS = [x.strip() for x in open("noms.txt").readlines()]
PRENOMS_H = [x.strip() for x in open("prenoms-h.txt").readlines()]
PRENOMS_F = [x.strip() for x in open("prenoms-f.txt").readlines()]
def nomprenom(sexe):
"""un nom et un prenom au hasard"""
if 'e' in sexe.lower():
if "e" in sexe.lower():
prenom = random.choice(PRENOMS_F)
else:
prenom = random.choice(PRENOMS_H)
return random.choice(NOMS), prenom
def usage():
print(f'Usage: {sys.argv[0]} formsemestre_id')
sys.exit(1)
if len(sys.argv) != 2:
usage()
formsemestre_id = sys.argv[1]
# Liste des etudiants inscrits à ce semestre
cnx = psycopg2.connect( DBCNXSTRING )
cnx = psycopg2.connect(DBCNXSTRING)
cursor = cnx.cursor()
cursor.execute( """select i.etudid
cursor.execute(
"select count(*) from notes_formsemestre where formsemestre_id=%(formsemestre_id)s",
{"formsemestre_id": formsemestre_id},
)
nsem = cursor.fetchone()[0]
if nsem != 1:
print(f"{nsem} formsemestre matching {formsemestre_id} in {dbname}")
sys.exit(2)
cursor.execute(
"""select i.etudid
from identite i, notes_formsemestre_inscription ins
where i.etudid=ins.etudid and ins.formsemestre_id=%(formsemestre_id)s
""",
{ 'formsemestre_id' : formsemestre_id})
""",
{"formsemestre_id": formsemestre_id},
)
wcursor = cnx.cursor()
n = 0
for (etudid,) in cursor:
sexe = random.choice( ('M.', 'MME') )
sexe = random.choice(("M.", "MME"))
nom, prenom = nomprenom(sexe)
print(f'{etudid}: {nom}\t{prenom}')
args = { 'nom' : nom, 'prenom' : prenom, 'sexe' : sexe, 'etudid' : etudid }
print(f"{etudid}: {nom}\t{prenom}")
args = {
"nom": nom,
"prenom": prenom,
"sexe": sexe,
"etudid": etudid,
"code_nip": random.randrange(10000000, 99999999),
}
req = "update identite set nom=%(nom)s, prenom=%(prenom)s, sexe=%(sexe)s where etudid=%(etudid)s"
#print( req % args)
wcursor.execute( req, args )
# print( req % args)
wcursor.execute(req, args)
n += 1
cnx.commit()
cnx.close()
print(f"changed {n} identities", file=sys.stderr)