MCPcopy Create free account
hub / github.com/ActiveState/code / maybe_ord

Function maybe_ord

recipes/Python/578319_bubblebabble/recipe-578319.py:22–41  ·  view source on GitHub ↗

If x is a string of length 1, return the ascii value. If it's a longer string, return an iterator of values. Otherwise, coerce it to an int() >>> maybe_ord('A') 65 >>> tuple(maybe_ord('lol')) (108, 111, 108) >>> maybe_ord(242) 242 >>> maybe_ord(242.2222) 242

(x)

Source from the content-addressed store, hash-verified

20vowels = "aeiouy"
21
22def maybe_ord(x):
23 """If x is a string of length 1, return the ascii value. If it's a
24 longer string, return an iterator of values. Otherwise, coerce it
25 to an int()
26 >>> maybe_ord('A')
27 65
28 >>> tuple(maybe_ord('lol'))
29 (108, 111, 108)
30 >>> maybe_ord(242)
31 242
32 >>> maybe_ord(242.2222)
33 242
34 """
35 try:
36 if len(x) is 1 and len(x[0]) is 1:
37 return ord(x)
38 else:
39 return itertools.imap(maybe_ord, x)
40 except:
41 return int(x)
42
43
44def babble(digest, seed=1):

Callers 1

babbleFunction · 0.85

Calls 1

imapMethod · 0.45

Tested by

no test coverage detected