changeset 4855:ffc03a1d60c3

fix sample scripts for Python3
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 17 Dec 2021 13:36:27 +0100
parents 8bd1f0d9506e
children 5c1805499dec
files OrthancServer/Resources/Samples/Python/ArchiveStudiesInTimeRange.py OrthancServer/Resources/Samples/Python/ContinuousPatientAnonymization.py
diffstat 2 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Resources/Samples/Python/ArchiveStudiesInTimeRange.py	Thu Dec 16 16:13:03 2021 +0100
+++ b/OrthancServer/Resources/Samples/Python/ArchiveStudiesInTimeRange.py	Fri Dec 17 13:36:27 2021 +0100
@@ -36,7 +36,7 @@
 
 def CheckIsDate(date):
     if len(date) != 8 or not date.isdigit():
-        print '"%s" is not a valid date!\n' % date
+        print('"%s" is not a valid date!\n' % date)
         exit(-1)
 
 
@@ -83,7 +83,9 @@
                                             GetTag(study, 'StudyDescription'))
 
         # Remove any non-ASCII character in the filename
-        filename = filename.encode('ascii', errors = 'replace').translate(None, r"'\/:*?\"<>|!=").strip()
+        filename = filename.encode('ascii', errors = 'replace')
+        filename = filename.translate(None, b"'\/:*?\"<>|!=")
+        filename = filename.decode('ascii').strip()
 
         # Download the ZIP archive of the study
         print('Downloading %s' % filename)
--- a/OrthancServer/Resources/Samples/Python/ContinuousPatientAnonymization.py	Thu Dec 16 16:13:03 2021 +0100
+++ b/OrthancServer/Resources/Samples/Python/ContinuousPatientAnonymization.py	Fri Dec 17 13:36:27 2021 +0100
@@ -24,7 +24,7 @@
 import time
 import sys
 import RestToolbox
-import md5
+import hashlib
 
 
 ##
@@ -66,13 +66,15 @@
         
         # The PatientID after anonymization is taken as the 8 first
         # characters from the MD5 hash of the original PatientID
-        anonymizedID = md5.new(patientID).hexdigest()[:8]
+        h = hashlib.md5(patientID.encode('ascii'))
+        anonymizedID = h.hexdigest()[:8]
         anonymizedName = 'Anonymized patient %d' % COUNT
         COUNT += 1
 
         RestToolbox.DoPost(URL + path + '/anonymize',
                            { 'Replace' : { 'PatientID' : anonymizedID,
-                                           'PatientName' : anonymizedName } })
+                                           'PatientName' : anonymizedName },
+                             'Force' : True })
 
         # Delete the source patient after the anonymization
         RestToolbox.DoDelete(URL + change['Path'])