[issue9685] tuples should remember their hash value
New submission from David Albert Torpey : Dictionary keys are commonly numbers, strings, or tuples. Python has optimized numbers and strings to remember their hash values on successive calls. Tuples should do this too since their recursive hash function can take a long time to compute. Tuples are Python's official record type and the one obvious way of making non-scalar dictionary keys. The code to do this in stringobject.c is short and sweet, so this major speed boost should be an easy thing to. static long string_hash(PyStringObject *a) { register Py_ssize_t len; register unsigned char *p; register long x; if (a->ob_shash != -1) <== return a->ob_shash; <== len = Py_SIZE(a); p = (unsigned char *) a->ob_sval; x = *p << 7; while (--len >= 0) x = (103*x) ^ *p++; x ^= Py_SIZE(a); if (x == -1) <== x = -2; <== a->ob_shash = x; return x; } The code in tupleobject.c would just need to add the four lines marked above. Here's what is looks like now. static long tuplehash(PyTupleObject *v) { register long x, y; register Py_ssize_t len = Py_SIZE(v); register PyObject **p; long mult = 103L; x = 0x345678L; p = v->ob_item; while (--len >= 0) { y = PyObject_Hash(*p++); if (y == -1) return -1; x = (x ^ y) * mult; /* the cast might truncate len; that doesn't change hash stability */ mult += (long)(82520L + len + len); } x += 97531L; if (x == -1) x = -2; return x; } Thank you guys for all of your work. *David -- messages: 114929 nosy: dtorp priority: normal severity: normal status: open title: tuples should remember their hash value type: resource usage versions: Python 2.6 ___ Python tracker <http://bugs.python.org/issue9685> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11756] bytes.hex()
New submission from David Albert Torpey : Floats have fromhex() and hex() to round-trip from and to hexadecimal, but bytes only have fromhex(), so it's hard to reliably round-trip. -- messages: 132892 nosy: dtorp priority: normal severity: normal status: open title: bytes.hex() type: feature request versions: Python 3.3 ___ Python tracker <http://bugs.python.org/issue11756> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11967] Left shift and Right shift for floats
New submission from David Albert Torpey : I would like to left and right shift floats as a fast way to multiply or divide by a power of 2 without rounding error. The only way to do that now is t=frexp(x) and y=ldexp(t[0],t[1]+2). But would be better to type y=x<<2. Thank you. -- messages: 134897 nosy: dtorp priority: normal severity: normal status: open title: Left shift and Right shift for floats versions: Python 3.3 ___ Python tracker <http://bugs.python.org/issue11967> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1771] Remove cmp parameter to list.sort() and builtin.sorted()
David Albert Torpey added the comment: > sorted(tree, cmp=lambda x, y: 1 if x in tree[y] else -1 if y in tree[x] else > 0) > > and it gets ['A', 'C', 'B', 'E', 'D']. That cmp function is nonsense and isn't even close to being correct: >>> from random import shuffle >>> for i in range(10): ... t = list(tree) ... shuffle(t) ... print sorted(t, cmp=lambda x, y: 1 if x in tree[y] else -1 if y in tree[x] else 0) ['E', 'C', 'B', 'D', 'A'] ['A', 'D', 'C', 'B', 'E'] ['C', 'B', 'E', 'D', 'A'] ['E', 'D', 'A', 'C', 'B'] ['A', 'B', 'D', 'E', 'C'] ['D', 'A', 'E', 'C', 'B'] ['C', 'D', 'A', 'B', 'E'] ['A', 'C', 'B', 'D', 'E'] ['A', 'C', 'B', 'E', 'D'] ['A', 'C', 'B', 'D', 'E'] > how to convert cmp to key really confused > me and it surely need more typing time. Just cut and paste the recipe. Simple. -- nosy: +dtorp ___ Python tracker <http://bugs.python.org/issue1771> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue8551] Start argument for str.rfind used incorrectly
New submission from David Albert Torpey : The purpose of the start argument in str.find() and str.rfind() is to allow for repeated searches. >>> def find_third_occurrence(s, value): ... p = s.find(value) ... p = s.find(value, p+1) ... return s.find(value, p+1) ... >>> find_third_occurrence('scientific american', 'c') 16 The rfind() method is meant for searching from the right, but its start argument is used internally as if it were searching from the left. This bug makes it useless for repeated searches from the right. >>> 'scientific american'.rfind('c') 16 >>> 'scientific american'.rfind('c', 15) 16 >>> 'scientific american'.rfind('c', 14) 16 Having found the first 'c' from the right at position 16, there is no way to tell it to search further from the right and find the second 'c' at position 9. -- components: Interpreter Core messages: 104375 nosy: dtorp priority: normal severity: normal status: open title: Start argument for str.rfind used incorrectly type: behavior versions: Python 2.6, Python 3.1 ___ Python tracker <http://bugs.python.org/issue8551> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2138] Factorial
New submission from David Albert Torpey: Add a factorial method. Everybody understands what it means before they are out of high school and it comes up all the time in statistics and combinatorics. Ruby has a factorial method and heck even basic calculators have a factorial key. print n.factorial() Maybe raise ValueError if n is negative. -- components: Interpreter Core messages: 62520 nosy: dtorp severity: normal status: open title: Factorial type: rfe versions: Python 2.6 __ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue2138> __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue2138] Factorial
David Albert Torpey added the comment: Mr. Dickinson thank you for doing this. I do not know how to help with a patch. If it helps, here is the code I use in python: def factorial(n, _known=[1]): assert isinstance(n, int), "Need an integer. This isn't a gamma" assert n >= 0, "Sorry, can't factorilize a negative" assert n < 1000, "No way! That's too large" try: return _known[n] except IndexError: pass for i in range(len(_known), n+1): _known.append(_known[-1] * i) return _known[n] When the assertions are turned-off, this runs pretty fast. __ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue2138> __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32656] writing to stdout prints extraneous size character
New submission from David Albert Torpey : $ python3.5 Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 08:49:46) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout.write('python') python6 -- messages: 310631 nosy: dtorp priority: normal severity: normal status: open title: writing to stdout prints extraneous size character versions: Python 3.5 ___ Python tracker <https://bugs.python.org/issue32656> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com