
Steve Howell wrote:
--- Ron Adam <rrr@ronadam.com> wrote:
Steve Howell wrote:
--- Ron Adam <rrr@ronadam.com> wrote:
# Tests
def a_g(s): return s[0].lower() in "abcdefg"
def h_m(s): return s[0].lower() in "hijklm"
def n_z(s): return s[0].lower() in "nopqrstuvwxyz"
decmps = [a_g, h_m, n_z] ag, hm, nz, other = decomp(src, *decmps)
print 'ag =', ag print 'hm =', hm print 'nz =', nz print 'other =', other
-------------------
ag = ['c8WQe60G6J', 'EMY7O8qzTg'] hm = ['lDunyeOM98', 'LJuPg8ncZd'] nz = ['uhhuhd9YdO', 'qAuQvfTc6N', 'vpJz47pkP5', 'YOq6m4IXBn'] other = ['8JE6PuXxBz', '4ttyMdpuQY']
Am I misunderstanding (de-comprehensing) something here? How does the code above return those result sets? Or, more specifically, why does ag include 'T' in its results set? The data in this case simulates 10 digit partnumbers which can include a-z, A-Z, and 0-9.
It doesn't alter the data, it just sorts it into smaller groups according to some predefined tests. In this case.. it's only testing the first letter of each item.
What is tested is entirely up to you. You could have lists of records as your data and test fields and divide the data according to that.
Ok, apologies for quoting away the parts of your code that probably answer my own question.
But to your bigger question--I think you can set up a list comprehension that does partitioning by having the list comprension or generator expression simply return a list of tuples where the first element in the tuple is a value that suggest where it fits in the partition, then feed that tuple to dict() or whatever.
But I don't have a specific code example to prove it.
That would depend on what level of abstraction you want. I find python already handles the simple things fairly well, so I tend to look for the next level up now. That makes it a bit harder to find the balance between being too specific and too general.
For simple binary partitions, there is the bool function.
Or more likely you may have a method in a class that tests for a particular condition. pass, fail = decomp(list_of_classes, lambda x: x.test()) Cheers, Ron