We have wiki groups and wiki dicts, but no wiki lists. I recently had the requirement of having a list of things that is worked on from the theme code and started using a wiki group until I realised that due to it's dict usage it wouldn't keep the order correct.
Here's a patch to add a wiki List class that is just like a group except for having another function items to get the list in order.
diff -r 2fbb179f3518 MoinMoin/wikidicts.py --- a/MoinMoin/wikidicts.py Tue Oct 31 12:22:38 2006 +0100 +++ b/MoinMoin/wikidicts.py Tue Jan 02 14:31:53 2007 +0100 @@ -18,6 +18,70 @@ from MoinMoin.logfile.editlog import Edi # Please increment if you have changed the structure DICTS_PICKLE_VERSION = 5 + +class List: + """List of items + + How a List definition page should look like: + + any text ignored + * item1 + * ignored, too + * item2 + * .... + * itemN + any text ignored + + if there are any free links using ["free link"] notation, the markup + is stripped from the member + """ + # * Item - ignore all but first level list items, strip whitespace + # Strip free links markup if exists + regex = re.compile(r'^ \* +(?:\[\")?(?P<member>.+?)(?:\"\])? *$', re.MULTILINE | re.UNICODE) + + def __init__(self, request, name): + """ Initialize, starting from <nothing>. + + Create a dict from a wiki page. + """ + self.name = name + + # Get text from page named 'name' + p = Page.Page(request, name) + text = p.get_raw_body() + self.initFromText(text) + + def initFromText(self, text): + """Create list from items in text + + Invoked by __init__, also useful for testing without a page. + """ + self._list = [] + for match in self.regex.finditer(text): + self._list.append(match.group('member')) + + def __getitem__(self, i): + return self._list[i] + + def __iter__(self): + return iter(self._list) + + def __len__(self): + return len(self._list) + + def __cmp__(self, other): + if isinstance(other, List): + return cmp(self._list, other._list) + elif isinstance(other, list): + return cmp(self._list, other) + else: + return NotImplemented + + def __contains__(self, item): + return item in self._list + + def __repr__(self): + return "<List name=%r items=%r>" % (self.name, self._list) class DictBase: """ Base class for wiki dicts
I guess we don't need a separate List thing for that, but we could use some "Ordered Dict" thing in the existing classes. initfromtext and addmembers could get some slight optimisations.
- I agree, but fear that such ordered dicts have an unnecessary overhead for most use cases. I'll do the proposed optimisations and update the patch.
- In fact, I think that the List posted above isn't quite right. A List should be allowed to contain a certain item twice. And when you take that into account an ordered dict implementation becomes useless.
wiki dicts are now ordered. -- JohannesBerg 2008-03-18 02:28:03
- In fact, I think that the List posted above isn't quite right. A List should be allowed to contain a certain item twice. And when you take that into account an ordered dict implementation becomes useless.