@@ -97,18 +97,35 @@ def fs_isRunning(subjects_dir, subject_id, mtime_tol=86400, logger=None):
97
97
return subjects_dir
98
98
99
99
100
- def hash_list (lst : list [str ], digest_size : int = 2 ) -> str :
100
+ def stringify_sessions (lst : list [str ], max_length : int = 10 , digest_size : int = 2 ) -> str :
101
101
"""
102
- Hash a list of strings into a string.
102
+ Convert a list of session into a string identifier.
103
+ If the list has only one element, it returns that element.
104
+ If the list has more than one element, it concatenates them with '+'.
105
+ If the concatenated string exceeds `max_length`, it returns a
106
+ string starting with 'multi+' followed by a BLAKE2b hash of the concatenated string.
103
107
104
108
Example
105
109
-------
106
- >>> hash_list(['a', 'b', 'c'])
107
- '1279'
108
- >>> hash_list(['a', 'b', 'c'], digest_size=3)
109
- 'fb7bf6'
110
+ >>> stringify_sessions(['a'])
111
+ 'a'
112
+ >>> stringify_sessions(['a', 'b', 'c'])
113
+ 'a+b+c'
114
+ >>> stringify_sessions(['a', 'b', 'toolong'])
115
+ 'multi+7c22'
116
+ >>> stringify_sessions(['a', 'b', 'toolong'], max_length=12)
117
+ 'a+b+toolong'
118
+ >>> stringify_sessions(['a', 'b', 'toolong'], digest_size=4)
119
+ 'multi+dd8bb349'
120
+ >>>
110
121
111
122
"""
112
- from hashlib import blake2b
123
+ if len (lst ) == 1 :
124
+ return lst [0 ]
113
125
114
- return blake2b (',' .join (lst ).encode ('utf-8' ), digest_size = digest_size ).hexdigest ()
126
+ ses_str = '+' .join (lst )
127
+ if len (ses_str ) > max_length :
128
+ from hashlib import blake2b
129
+
130
+ ses_str = f'multi+{ blake2b (ses_str .encode (), digest_size = digest_size ).hexdigest ()} '
131
+ return ses_str
0 commit comments