Merge branch 'master' into subfolders-8.3
[pyTivo.git] / Cheetah / Tools / MondoReportDoc.txt
blob8685eb1e637cf5ccb153d324443c4dfa715ac5f4
1 MondoReport Documentation  \r
2 Version 0.01 alpha 24-Nov-2001.  iron@mso.oz.net or mso@oz.net.\r
3 Copyright (c) 2001 Mike Orr.  License: same as Python or Cheetah.\r
4 \r
5 * * * * *\r
6 STATUS:  previous/next batches and query string are not implemented yet.\r
7 Sorting not designed yet.  Considering "click on this column header to sort by\r
8 this field" and multiple ascending/descending sort fields for a future version.\r
9 \r
10 Tested with Python 2.2b1.  May work with Python 2.1 or 2.0.\r
12 * * * * *\r
13 OVERVIEW\r
15 MondoReport -- provide information about a list that is useful in generating\r
16 any kind of report.  The module consists of one main public class, and some\r
17 generic functions you may find useful in other programs.  This file contains an\r
18 overview, syntax reference and examples.  The module is designed both for\r
19 standalone use and for integration with the Cheetah template system\r
20 (http://www.cheetahtemplate.org/), so the examples are in both Python and\r
21 Cheetah.  The main uses of MondoReport are: \r
23 (A) to iterate through a list.  In this sense MR is a for-loop enhancer,\r
24 providing information that would be verbose to calculate otherwise.\r
26 (B) to separate a list into equal-size "pages" (or "batches"--the two terms are\r
27 interchangeable) and only display the current page, plus limited information\r
28 about the previous and next pages.\r
30 (C) to extract summary statistics about a certain column ("field") in the list.\r
32 * * * * *\r
33 MAIN PUBLIC CLASS\r
35 To create a MondoReport instance, supply a list to operate on.\r
37     mr = MondoReport(origList)\r
39 The list may be a list of anything, but if you use the 'field' argument in any\r
40 of the methods below, the elements must be instances or dictionaries.\r
42 MondoReport assumes it's operating on an unchanging list.  Do not modify the\r
43 list or any of its elements until you are completely finished with the\r
44 ModoReport object and its sub-objects.  Otherwise, you may get an exception or\r
45 incorrect results.\r
47 MondoReport instances have three methods:\r
49     .page(size, start, overlap=0, orphan=0\r
50         sort=None, reverse=False)                => list of (r, a, b).\r
52 'size' is an integer >= 1.  'start', 'overlap' and 'orphan' are integers >= 0.\r
53 The list returned contains one triple for each record in the current page.  'r'\r
54 is the original record.  'a' is a BatchRecord instance for the current record\r
55 in relation to all records in the origList.  'b' is a BatchRecord instance for\r
56 the current record in relation to all the records in that batch/page.  (There\r
57 is a .batch method that's identical to .page.)\r
59 The other options aren't implemented yet, but 'overlap' duplicates this many\r
60 records on adjacent batches.  'orphan' moves this many records or fewer, if\r
61 they are on a page alone, onto the neighboring page.  'sort' (string) specifies\r
62 a field to sort the records by.  It may be suffixed by ":desc" to sort in\r
63 descending order.  'reverse' (boolean) reverses the sort order.  If both\r
64 ":desc" and 'reverse' are specified, they will cancel each other out.  This\r
65 sorting/reversal happens on a copy of the origList, and all objects returned\r
66 by this method use the sorted list, except when resorting the next time.\r
67 To do more complicated sorting, such as a hierarchy of columns, do it to the\r
68 original list before creating the ModoReport object.\r
70     .all(sort=None, reverse=False)              => list of (r, a).\r
72 Same, but the current page spans the entire origList.\r
74     .summary()                                  => Summary instance.\r
76 Summary statistics for the entire origList.\r
78 In Python, use .page or .all in a for loop:\r
80     from Cheetah.Tools.MondoReport import MondoReport\r
81     mr = MondoReport(myList)\r
82     for r, a, b in mr.page(20, 40):\r
83         # Do something with r, a and b.  The current page is the third page,\r
84         # with twenty records corresponding to origList[40:60].\r
85     if not myList:\r
86         # Warn the user there are no records in the list.\r
88 It works the same way in Cheetah, just convert to Cheetah syntax.  This example\r
89 assumes the template doubles as a Webware servlet, so we use the servlet's\r
90 '$request' method to look up the CGI parameter 'start'.  The default value is 0\r
91 for the first page.\r
93     #from Cheetah.Tools.MondoReport import MondoReport\r
94     #set $mr = $MondoReport($bigList)\r
95     #set $start = $request.field("start", 0)\r
96     #for $o, $a, $b in $mr.page(20, $start)\r
97         ... do something with $o, $a and $b ...\r
98     #end for\r
99     #unless $bigList\r
100         This is displayed if the original list has no elements.\r
101         It's equivalent to the "else" part Zope DTML's <dtml-in>.\r
102     #end unless\r
104 * * * * *\r
105 USING 'r' RECORDS\r
107 Use 'r' just as you would the original element.  For instance:\r
109     print r.attribute     # If r is an instance.\r
110     print r['key']        # If r is a dictionary.\r
111     print r               # If r is numeric or a string.\r
113 In Cheetah, you can take advantage of Universal Dotted Notation and autocalling:\r
115     $r.name        ## 'name' may be an attribute or key of 'r'.  If 'r' and/or\r
116                    ## 'name' is a function or method, it will be called without\r
117                    ## arguments.\r
118     $r.attribute\r
119     $r['key']\r
120     $r\r
121     $r().attribute()['key']()\r
123 If origList is a list of name/value pairs (2-tuples or 2-lists), you may\r
124 prefer to do this:\r
126     for (key, value), a, b in mr.page(20, 40):\r
127         print key, "=>", value\r
128     \r
129     #for ($key, $value), $a, $b in $mr.page(20, $start)\r
130         $key =&gt; $value\r
131     #end for\r
133 * * * * *\r
134 STATISTICS METHODS AND FIELD VALUES\r
136 Certain methods below have an optional argument 'field'.  If specified, \r
137 MondoReport will look up that field in each affected record and use its value\r
138 in the calculation.  MondoReport uses Cheetah's NameMapper if available,\r
139 otherwise it uses a minimal NameMapper substitute that looks for an attribute\r
140 or dictionary key called "field".  You'll get an exception if any record is a\r
141 type without attributes or keys, or if one or more records is missing that\r
142 attribute/key.\r
144 If 'field' is None, MondoReport will use the entire record in its\r
145 calculation.  This makes sense mainly if the records are a numeric type.\r
147 All statistics methods filter out None values from their calculations, and\r
148 reduce the number of records accordingly.  Most filter out non-numeric fields\r
149 (or records).  Some raise NegativeError if a numeric field (or record) is\r
150 negative.\r
153 * * * * *\r
154 BatchRecord METHODS\r
156 The 'a' and 'b' objects of MondoReport.page() and MondoReport.all() provide\r
157 these methods.\r
159     .index()\r
161 The current subscript.  For 'a', this is the true subscript into origList.\r
162 For 'b', this is relative to the current page, so the first record will be 0.\r
163 Hint: In Cheetah, use autocalling to skip the parentheses: '$b.index'.\r
165     .number()\r
167 The record's position starting from 1.  This is always '.index() + 1'.\r
169     .Letter()\r
171 The letter ("A", "B", "C") corresponding to .number().  Undefined if .number()\r
172 > 26.  The current implementation just adds the offset to 'a' and returns\r
173 whatever character it happens to be.  \r
175 To make a less dumb implementation (e.g., "Z, AA, BB" or "Z, A1, B1"):\r
176 1) Subclass BatchRecord and override the .Letter method. \r
177 2) Subclass MondoReport and set the class variable .BatchRecordClass to your\r
178 new improved class.\r
180     .letter()\r
182 Same but lower case.\r
184     .Roman()\r
186 The Roman numeral corresponding to .number().\r
188     .roman()\r
190 Same but lower case.\r
192     .even()\r
194 True if .number() is even.\r
196     .odd()\r
198 True if .number() is odd.\r
200     .even_i()\r
202 True if .index() is even.\r
204     .odd_i()\r
206 True if .index() is odd.\r
208     .length()\r
210 For 'a', number of records in origList.  For 'b', number of records on this\r
211 page.\r
213     .item()\r
215 The record itself.  You don't need this in the normal case since it's the same\r
216 as 'r', but it's useful for previous/next batches.\r
218     .size()\r
220 The 'size' argument used when this BatchRecord was created.  \r
221 'a.size() == b.size()'.\r
223     .first()\r
225 True if this is the first record.\r
227     .last()\r
229 True if this is the last record.\r
231     .firstValue(field=None)\r
233 True if there is no previous record, or if the previous field/record has a \r
234 different value.  Used for to print section headers.  For instance, if you\r
235 are printing addresses by country, this will be true at the first occurrance\r
236 of each country.  Or for indexes, you can have a non-printing field showing\r
237 which letter of the alphablet this entry starts with, and then print a "B"\r
238 header before printing the first record starting with "B".\r
240     .lastValue(field=None)\r
242 True if this is the last record containing the current value in the\r
243 field/record.\r
245     .percentOfTotal(field=None, suffix="%", default="N/A", decimals=2)\r
247 Returns the percent that the current field/record is of all fields/records.\r
248 If 'suffix' is None, returns a number; otherwise it returns a string with\r
249 'suffix' suffixed.  If the current value is non-numeric, returns 'default'\r
250 instead (without 'suffix').  'decimals' tells the number of decimal places to\r
251 return; if 0, there will be no decimal point.\r
253     .prev()\r
255 Returns a PrevNextBatch instance for the previous page.  If there is no\r
256 previous page, returns None.  [Not implemented yet.]\r
258     .next()\r
260 Returns a PrevNextBatch instance for the next page.  If there is no next page,\r
261 returns None.  [Not implemented yet.]\r
263     .prevPages()\r
265 Returns a list of PrevNextPage instances for every previous page, or [] if no\r
266 previous pages.  [Not implemented yet.]\r
268     .nextPages()\r
270 Returns a list of PrevNextPage instances for every next page, or [] if no next\r
271 pages.  [Not implemented yet.]\r
273     .query(start=None, label=None, attribName="start", attribs=[])\r
275 [Not implemented yet.]\r
277 With no arguments, returns the HTML query string with start value removed (so\r
278 you can append a new start value in your hyperlink).  The query string is taken\r
279 from the 'QUERY_STRING' environmental variable, or "" if missing.  (This is\r
280 Webware compatible.)  \r
282 With 'start' (an integer >= 0), returns the query string with an updated start\r
283 value, normally for the next or previous batch.\r
285 With 'label' (a string), returns a complete HTML hyperlink:\r
286 '<A HREF="?new_query_string">label</A>'.  You'll get a TypeError if you specify\r
287 'label' but not 'start'.\r
289 With 'attribName' (a string), uses this attribute name rather than "start".\r
290 Useful if you have another CGI parameter "start" that's used for something\r
291 else.\r
293 With 'attribs' (a dictionary), adds these attributes to the hyperlink.\r
294 For instance, 'attribs={"target": "_blank"}'.  Ignored unless 'label' is \r
295 specified too.\r
297 This method assumes the start parameter is a GET variable, not a POST variable.\r
299     .summary()\r
301 Returns a Summary instance.  'a.summary()' refers to all records in the\r
302 origList, so it's the same as MondoReport.summary().  'b.summary()' refers only\r
303 to the records on the current page.  [Not implemented yet.]\r
305 * * * * *\r
306 PrevNextPage INSTANCES\r
308 [Not implemented yet.]\r
310 PrevNextPage instances have the following methods:\r
312     .start()\r
314 The index (true index of origList) that that page starts at.  You may also use\r
315 '.start().index()', '.start().number()', etc.  Also\r
316 '.start().item(field=None)'.  (Oh, so *that*'s what .item is for!)\r
318     .end()\r
320 The index (true index of origList) that that page ends at.  You may also use\r
321 '.end().index()', '.end().number()', etc.   Also\r
322 '.end().item(field=None)'.\r
324     .length()\r
326 Number of records on that page.\r
328     .query(label=None, attribName="start", attribs={}, before="", after="")\r
330 [Not implemented yet.]\r
332 Similar to 'a.query()' and 'b.query()', but automatically calculates the start\r
333 value for the appropriate page.  \r
335 For fancy HTML formatting, 'before' is prepended to the returned text and \r
336 'after' is appended.  (There was an argument 'else_' for if there is no such\r
337 batch, but it was removed because you can't even get to this method at all in\r
338 that case.)\r
340 * * * * * *\r
341 SUMMARY STATISTICS\r
343 These methods are supported by the Summary instances returned by\r
344 MondoReport.Summary():\r
346     .sum(field=None)\r
348 Sum of all numeric values in a field, or sum of all records.\r
350     .total(field=None)\r
352 Same.\r
354     .count(field=None)\r
356 Number of fields/records with non-None values.\r
358     .min(field=None)\r
360 Minimum value in that field/record.  Ignores None values.\r
362     .max(field=None)\r
364 Maximum value in that field/record.  Ignores None values.\r
366     .mean(field=None)\r
368 The mean (=average) of all numeric values in that field/record.\r
370     .average(field=None)\r
372 Same.\r
374     .median(field=None)\r
376 The median of all numeric values in that field/record.  This is done by sorting\r
377 the values and taking the middle value.\r
379     .variance(field=None), .variance_n(field=None)\r
380     .standardDeviation(field=None), .standardDeviation_n(field=None)\r
382 [Not implemented yet.]\r
385 * * * * *\r
386 To run the regression tests (requires unittest.py, which is standard with\r
387 Python 2.2), run MondoReportTest.py from the command line.  The regression test\r
388 double as usage examples.\r
391 # vim: shiftwidth=4 tabstop=4 expandtab textwidth=79\r