Skip to content

Commit 19e9b65

Browse files
committed
Fixed UFT8 problems since Python3 handles strings differently
1 parent 2bce845 commit 19e9b65

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

scripts/smart_dispatch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ def main():
125125
jobs_id = []
126126
for pbs_filename in pbs_filenames:
127127
qsub_output = check_output('{launcher} {pbs_filename}'.format(launcher=LAUNCHER if args.launcher is None else args.launcher, pbs_filename=pbs_filename), shell=True)
128+
129+
if isinstance(qsub_output, bytes):
130+
qsub_output = qsub_output.decode("utf-8")
131+
128132
jobs_id += [qsub_output.strip()]
129133

130134
with utils.open_with_lock(pjoin(path_job, "jobs_id.txt"), 'a') as jobs_id_file:

smartdispatch/utils.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import re
44
import fcntl
5+
import binascii
56
import logging
67
import hashlib
78
import unicodedata
@@ -40,13 +41,13 @@ def yes_no_prompt(query, default=None):
4041

4142
def chunks(sequence, n):
4243
""" Yield successive n-sized chunks from sequence. """
43-
for i in xrange(0, len(sequence), n):
44+
for i in range(0, len(sequence), n):
4445
yield sequence[i:i + n]
4546

4647

4748
def generate_uid_from_string(value):
4849
""" Create unique identifier from a string. """
49-
return hashlib.sha256(value).hexdigest()
50+
return hashlib.sha256(value.encode()).hexdigest()
5051

5152

5253
def slugify(value):
@@ -59,15 +60,21 @@ def slugify(value):
5960
---------
6061
https://github.com/django/django/blob/1.7c3/django/utils/text.py#L436
6162
"""
62-
value = unicodedata.normalize('NFKD', unicode(value, "UTF-8")).encode('ascii', 'ignore').decode('ascii')
63+
try:
64+
value = unicode(value, "UTF-8")
65+
except NameError:
66+
pass # In Python 3 all strings are already unicode.
67+
68+
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
6369
value = re.sub('[^\w\s-]', '', value).strip().lower()
6470
return str(re.sub('[-\s]+', '_', value))
6571

6672

6773
def encode_escaped_characters(text, escaping_character="\\"):
6874
""" Escape the escaped character using its hex representation """
6975
def hexify(match):
70-
return "\\x{0}".format(match.group()[-1].encode("hex"))
76+
# Reference: http://stackoverflow.com/questions/18298251/python-hex-values-to-convert-to-a-string-integer
77+
return "\\x" + binascii.hexlify(match.group()[-1].encode()).decode()
7178

7279
return re.sub(r"\\.", hexify, text)
7380

@@ -78,7 +85,7 @@ def decode_escaped_characters(text):
7885
return ''
7986

8087
def unhexify(match):
81-
return match.group()[2:].decode("hex")
88+
return binascii.unhexlify(match.group()[2:]).decode()
8289

8390
return re.sub(r"\\x..", unhexify, text)
8491

0 commit comments

Comments
 (0)