Skip to content

Commit c93f21b

Browse files
committed
PY3 vector
1 parent 0de5865 commit c93f21b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

pattern/vector/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def decode_string(v, encoding="utf-8"):
102102
return unicode(v)
103103

104104

105+
# TODO use one of these (rather than copy and paste in each file)
106+
# ... if we really have to use any at all
105107
def encode_string(v, encoding="utf-8"):
106108
"""Returns the given value as a Python byte string (if possible)."""
107109
if isinstance(encoding, basestring):
@@ -113,7 +115,12 @@ def encode_string(v, encoding="utf-8"):
113115
except:
114116
pass
115117
return v
116-
return str(v)
118+
if isinstance(v, bytes):
119+
return v
120+
else:
121+
# TODO Is this ever the correct behaviour (see coverage)
122+
raise ValueError()
123+
#return str(v)
117124

118125
decode_utf8 = decode_string
119126
encode_utf8 = encode_string
@@ -3478,10 +3485,17 @@ def _train(self):
34783485
H2 = dict((w, i + 1) for i, w in enumerate(self.classes))
34793486
# Class reversed hash.
34803487
H3 = dict((i + 1, w) for i, w in enumerate(self.classes))
3488+
34813489
# Hashed vectors.
3482-
x = map(lambda v: dict(map(lambda k: (H1[k], v[k]), v)), M)
3490+
x = list(map(lambda v: dict(map(lambda k: (H1[k], v[k]), v)), M))
3491+
# TODO use this more efficient version?
3492+
# x = [dict(((H1[k], v[k]), v) for k in v) for v in M]
3493+
34833494
# Hashed classes.
3484-
y = map(lambda v: H2[v[0]], self._vectors)
3495+
y = list(map(lambda v: H2[v[0]], self._vectors))
3496+
# TODO use this more efficient version?
3497+
# y = [H2[v[0]] for v in self._vectors]
3498+
34853499
# For linear SVC, use LIBLINEAR which is faster.
34863500
# For kernel SVC, use LIBSVM.
34873501
if self.extension == LIBLINEAR:

pattern/web/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ def encode_string(v, encoding="utf-8"):
189189
"""Returns the given value as a Python byte string (if possible)."""
190190
if isinstance(encoding, basestring):
191191
encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore"))
192-
if isinstance(v, unicode):
193192
for e in encoding:
194193
try:
195194
return v.encode(*e)
@@ -698,7 +697,7 @@ def redirect(self, timeout=10):
698697
return self.__dict__["_redirect"] or None
699698

700699
def __str__(self):
701-
return bytestring(self.string)
700+
return self._string
702701

703702
def __unicode__(self):
704703
# The string representation includes the query attributes with HTTP

0 commit comments

Comments
 (0)