programming unlimited "categories" in python ?

Stephen shriek at gmx.co.uk
Tue Oct 23 23:30:03 EDT 2001


I've used something identical to this (with 3 digits) for 
precisely the same thing, Grant ~ ie. when creating a threaded
discussion board ~ so I know precisely what you're describing.
And for all intents and purposes, I've never exceeded the 
limitations. 

Having just looked at the "sets" solution ~ referenced by Steve,
see dbmsmag ~ I can safely say that the "sets" solution is easier.

In fact, I wish I'd learned this 6 years ago.  Really, the number
of times I've hacked a different solution to this problem, I'd have
saved a lot of time. 
Is there a reference somewhere that contains this type
of problem that is too unique and small to be a "Pattern" 
but useful to have in one's programmer toolbox ?

Stephen.




> Something I've used for developing threaded discussion boards might help you
> here, but I'm not sure if it's more or less efficient, and can't truly go to
> great depths (without sacrificing space).
> 
> Assign each category a 2-digit code (if you need more categories, increase
> this to 3 or 4 digits). This code need only be unique on it's level. It does
> not need to be completely unique. Eg, ByLocation = '01', BySeverity = '02',
> Africa = '01', Europe = '02', Mozambique = '01', South Africa = '02',
> Critical = '01', Death='01', etc. ....
> 
> Then when assigning the category code to your record, concatenate all codes
> together, thus South Africa = '010102' (ByLocation+Africa+SouthAfrica).
> Death = '020101' (BySeverity+Critical+Death). As you can see, your codes
> could get pretty long, but the nice part comes in selecting stuff in a
> category. To find out if a particular record is in a particular category,
> test whether RecordCategorisationID.startswith(CategoryID). i.e Comparing a
> record with ID '010102' (i.e South Africa), to Africa - '0101' would be
> true, to BySeverity '02', it would be false.
> 
> I think you get the idea. I have this implemented with my id's as strings,
> but it would be far more efficient doing this by converting to binary, etc.
> The same principle applies, but you'll be dealing with numbers. I haven't
> even considered the maths, but I would imagine it could be made to be fairly
> efficient.
> 
> HTH
> 
> Cheers
> Grant Beasley
>



More information about the Python-list mailing list