/usr/share/pyshared/lazr.batchnavigator-1.2.10.egg-info/PKG-INFO is in python-lazr.batchnavigator 1.2.10-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | Metadata-Version: 1.1
Name: lazr.batchnavigator
Version: 1.2.10
Summary: A helper to navigate batched results in a web page.
Home-page: https://launchpad.net/lazr.batchnavigator
Author: LAZR Developers
Author-email: lazr-users@lists.launchpad.net
License: LGPL v3
Download-URL: https://launchpad.net/lazr.batchnavigator/+download
Description: ..
This file is part of lazr.batchnavigator.
lazr.batchnavigator is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3 of the License.
lazr.batchnavigator is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with lazr.batchnavigator. If not, see
<http://www.gnu.org/licenses/>.
Batch Navigation
****************
Batch navigation provides a way to navigate batch results in a web
page by providing URL links to the next, previous and numbered pages
of results.
It uses four query/POST arguments to control the batching:
- memo: A record of the underlying storage index pointer for the position of
the batch.
- direction: Indicates whether the memo is at the start or end of the batch.
- start: Cosmetic - used to calculate the apparent location (but note that
due to the concurrent nature of repeated visits to batches that the
true offset may differ - however the collection won't skip or show
items twice. For compatibility with saved URLs, if memo and
direction are both missing then start is used to do list slicing
into the collection.
- batch: Controls the amount of items we are showing per batch. It will only
appear if it's different from the default value set when the batch
is created.
These values can be overriden in the request, unless you also pass
force_start=True, which will make the start argument (again, defaulting to 0)
always chosen.
Imports:
>>> from lazr.batchnavigator import BatchNavigator, ListRangeFactory
>>> from zope.publisher.browser import TestRequest
>>> from zope.publisher.http import HTTPCharsets
>>> from zope.component import getSiteManager
>>> sm = getSiteManager()
>>> sm.registerAdapter(HTTPCharsets)
>>> def build_request(query_string_args=None, method='GET'):
... if query_string_args is None:
... query_string = ''
... else:
... if getattr(query_string_args, 'items', None) is not None:
... query_string_args = query_string_args.items()
... query_string = "&".join(
... ["%s=%s" % (k,v) for k,v in query_string_args])
... request = TestRequest(SERVER_URL='http://www.example.com/foo',
... method=method,
... environ={'QUERY_STRING': query_string})
... request.processInputs()
... return request
A dummy request object:
Some sample data.
>>> reindeer = ['Dasher', 'Dancer', 'Prancer', 'Vixen', 'Comet',
... 'Cupid', 'Donner', 'Blitzen', 'Rudolph']
Because slicing large collections can be very expensive, BatchNavigator offers
a non-slice protocol for determining the edge of batches. The range_factory
supplies an object implementing IRangeFactory and manages this protocol.
ListRangeFactory is a simple included implementation which BatchNavigator will
use if no range_factory is supplied.
>>> _ = BatchNavigator(reindeer, build_request(),
... range_factory=ListRangeFactory(reindeer))
For the examples in the documentation we let BatchNavigator construct a
range_factory implicitly:
>>> safe_reindeer = reindeer
>>> safe_reindeer_batch_navigator = BatchNavigator(
... safe_reindeer, build_request(), size=3)
An important feature of lazr.batchnavigator is its reluctance to
invoke len() on an underlying data set. len() can be an expensive
operation that provides little benefit, so this library tries hard to
avoid calling len() unless it's absolutely necessary. To show this
off, we'll define a subclass of Python's list type that explodes when
len() is invoked on it.
>>> class ListWithExplosiveLen(list):
... """A list subclass that doesn't like its len() being called."""
... def __len__(self):
... raise RuntimeError
Unless otherwise stated, we will use this list exclusively throughout
this test, to verify that len() is never called unless we want it to
be.
>>> explosive_reindeer = ListWithExplosiveLen(reindeer)
>>> reindeer_batch_navigator = BatchNavigator(
... explosive_reindeer, build_request(), size=3)
The BatchNavigator implements IBatchNavigator. We need to use the
'safe' batch navigator here, because verifyObject probes all methods
of the object it's passed, including __len__.
>>> from zope.interface.verify import verifyObject
>>> from lazr.batchnavigator.interfaces import IBatchNavigator
>>> verifyObject(IBatchNavigator, safe_reindeer_batch_navigator)
True
The BatchNavigator class provides IBatchNavigatorFactory. This can be used
to register a batch navigator factory as a utility, for instance.
>>> from lazr.batchnavigator.interfaces import IBatchNavigatorFactory
>>> verifyObject(IBatchNavigatorFactory, BatchNavigator)
True
You can ask the navigator for the chunk of results currently being shown
(e.g. to iterate over them for rendering in ZPT):
>>> list(reindeer_batch_navigator.currentBatch())
['Dasher', 'Dancer', 'Prancer']
You can ask for the first, previous, next and last results' links:
>>> reindeer_batch_navigator.firstBatchURL()
''
>>> reindeer_batch_navigator.prevBatchURL()
''
>>> reindeer_batch_navigator.nextBatchURL()
'http://www.example.com/foo?memo=3&start=3'
There's no way to get the URL to the final batch without knowing the
length of the entire list, so we'll use the safe batch navigator to
demonstrate lastBatchURL():
>>> safe_reindeer_batch_navigator.lastBatchURL()
'http://www.example.com/foo?direction=backwards&start=6'
The next link will be empty when there are no further results:
>>> request = build_request({"start": "3", "batch": "20"})
>>> last_reindeer_batch_navigator = BatchNavigator(reindeer, request=request)
>>> last_reindeer_batch_navigator.nextBatchURL()
''
The first and previous link should appear even when we start at a point between 0
and the batch size:
>>> request = build_request({"start": "2", "batch": "3"})
>>> last_reindeer_batch_navigator = BatchNavigator(reindeer, request=request)
Here, we can see too that the batch argument appears as part of the URL.
That's because the request asked for a different size than the default
one when we create the Batch object, by default, it's 5.
>>> last_reindeer_batch_navigator.firstBatchURL()
'http://www.example.com/foo?batch=3'
>>> last_reindeer_batch_navigator.prevBatchURL()
'http://www.example.com/foo?batch=3&direction=backwards&memo=2'
This all works with other values in the query string, too:
>>> request = build_request({'fnorb': 'bar',
... 'start': '3',
... 'batch': '3'})
>>> reindeer_batch_navigator_with_qs = BatchNavigator(
... reindeer, request, size=3)
>>> safe_reindeer_batch_navigator_with_qs = BatchNavigator(
... safe_reindeer, request, size=3)
In this case, we created the BatchNavigator with a default size of '3' and
the request is asking exactly that number of items per batch, and thus, we
don't need to show 'batch' as part of the URL.
>>> reindeer_batch_navigator_with_qs.firstBatchURL()
'http://www.example.com/foo?fnorb=bar'
>>> reindeer_batch_navigator_with_qs.prevBatchURL()
'http://www.example.com/foo?fnorb=bar&direction=backwards&memo=3'
>>> reindeer_batch_navigator_with_qs.nextBatchURL()
'http://www.example.com/foo?fnorb=bar&memo=6&start=6'
(Again, there's no way to get the last batch without knowing the size
of the entire list.)
>>> safe_reindeer_batch_navigator_with_qs.lastBatchURL()
'http://www.example.com/foo?fnorb=bar&direction=backwards&start=6'
The ``force_start`` argument allows you to ignore the start value in the
request. This can be useful when, for instance, a filter has changed, and the
desired behavior is to restart at 0.
>>> reindeer_batch_navigator_with_qs = BatchNavigator(
... reindeer, request, size=3, force_start=True)
>>> reindeer_batch_navigator_with_qs.currentBatch().start
0
>>> reindeer_batch_navigator_with_qs.nextBatchURL()
'http://www.example.com/foo?fnorb=bar&memo=3&start=3'
>>> reindeer[:3] == list(reindeer_batch_navigator_with_qs.currentBatch())
True
We ensure that batch arguments supplied in the URL are observed
for POST operations too:
>>> request = build_request({'fnorb': 'bar',
... 'start': '3',
... 'batch': '3'}, method='POST')
>>> reindeer_batch_navigator_post_with_qs = BatchNavigator(
... reindeer, request)
>>> reindeer_batch_navigator_post_with_qs.start
3
>>> reindeer_batch_navigator_post_with_qs.nextBatchURL()
'http://www.example.com/foo?fnorb=bar&batch=3&memo=6&start=6'
We ensure that multiple size and batch arguments supplied in the URL don't
blow up the application. The first one is preferred.
>>> request = build_request(
... [('batch', '1'), ('batch', '7'), ('start', '2'), ('start', '10')])
>>> navigator = BatchNavigator(reindeer, request=request)
>>> navigator.nextBatchURL()
'http://www.example.com/foo?batch=1&memo=3&start=3'
The batch argument must be positive. Other numbers are ignored, and the
default batch size is used instead.
>>> from cgi import parse_qs
>>> request = build_request({'batch': '0'})
>>> navigator = BatchNavigator(range(99), request=request)
>>> print 'batch' in parse_qs(navigator.nextBatchURL())
False
>>> request = build_request({'batch': '-1'})
>>> navigator = BatchNavigator(range(99), request=request)
>>> print 'batch' in parse_qs(navigator.nextBatchURL())
False
=============
Empty Batches
=============
You can also create an empty batch that will not have any items:
>>> null_batch_navigator = BatchNavigator(
... None, build_request(), size=3)
>>> null_batch_navigator.firstBatchURL()
''
>>> null_batch_navigator.nextBatchURL()
''
>>> null_batch_navigator.prevBatchURL()
''
>>> null_batch_navigator.lastBatchURL()
''
>>> null_batch_navigator = BatchNavigator(
... [], build_request(), size=3)
>>> null_batch_navigator.firstBatchURL()
''
>>> null_batch_navigator.nextBatchURL()
''
>>> null_batch_navigator.prevBatchURL()
''
>>> null_batch_navigator.lastBatchURL()
''
TODO:
- blowing up when start is beyond end
- orphans
- overlap
====================================
Supporting Results Without a __len__
====================================
Some result objects do not implement __len__ because generally Python code
assumes that __len__ is cheap. SQLObject and Storm result sets both have this
behavior, for instance, so that it is cleat that getting the length is a non-
trivial operation.
To support these objects, the batch looks for __len__ on the result set. If
it does not exist, it adapts the result to
zope.interface.common.sequence.IFiniteSequence and uses that __len__.
>>> class ExampleResultSet(object):
... def __init__(self, results):
... self.stub_results = results
... def count(self):
... # imagine this actually returned
... return len(self.stub_results)
... def __getitem__(self, ix):
... return self.stub_results[ix] # also works with slices
... def __iter__(self):
... return iter(self.stub_results)
...
>>> from zope.interface import implements
>>> from zope.component import adapts, getSiteManager
>>> from zope.interface.common.sequence import IFiniteSequence
>>> class ExampleAdapter(ExampleResultSet):
... adapts(ExampleResultSet)
... implements(IFiniteSequence)
... def __len__(self):
... return self.stub_results.count()
...
>>> sm = getSiteManager()
>>> sm.registerAdapter(ExampleAdapter)
>>> example = ExampleResultSet(safe_reindeer)
>>> example_batch_navigator = BatchNavigator(
... example, build_request(), size=3)
>>> example_batch_navigator.currentBatch().total()
9
========================
Only Gets What Is Needed
========================
It's also important for performance of batching large result sets that the
batch only gets a slice of the results, rather than accessing the entirety.
>>> class ExampleResultSet(ExampleResultSet):
... def __init__(self, results):
... super(ExampleResultSet, self).__init__(results)
... self.getitem_history = []
... def __getitem__(self, ix):
... self.getitem_history.append(ix)
... return super(ExampleResultSet, self).__getitem__(ix)
...
>>> example = ExampleResultSet(reindeer)
>>> example_batch_navigator = BatchNavigator(
... example, build_request(), size=3)
>>> reindeer[:3] == list(example_batch_navigator.currentBatch())
True
>>> example.getitem_history
[slice(0, 4, None)]
Note that although the batch is of the size requested, the underlying
list contains one more item than is necessary. This is to make it easy
to determine whether a given batch is the final one in the list,
without having to explicitly look up the length of the list
(potentially an expensive operation).
=========================
Adding callback functions
=========================
Sometimes it is useful to have a function called with the batched
values once they have been determined. This is the case when there
are subsequent queries that are needed to be executed for each batch,
and it is undesirable or overly expensive to execute the query for
every value in the entire result set.
The callback function must define two parameters. The first is the
batch navigator object itself, and the second it the current batch.
The callback function is called once and only once when the
BatchNavigator is constructed, and the current batch is determined.
>>> def print_callback(context, batch):
... for item in batch:
... print item
>>> reindeer_batch_navigator = BatchNavigator(
... reindeer, build_request(), size=3, callback=print_callback)
Dasher
Dancer
Prancer
>>> request = build_request({"start": "3", "batch": "20"})
>>> last_reindeer_batch_navigator = BatchNavigator(
... reindeer, request=request, callback=print_callback)
Vixen
Comet
Cupid
Donner
Blitzen
Rudolph
Most likely, the callback function will be bound to a view class.
By providing the batch navigator itself as the context for the
callback allows the addition of extra member variables. This is
useful as the BatchNavigator becomes the context in page templates
that are batched.
>>> class ReindeerView:
... def constructReindeerFromAtoms(self, context, batch):
... # some significantly slow process
... view.built_reindeer = list(batch)
... def batchedReindeer(self):
... return BatchNavigator(
... reindeer, build_request(), size=3,
... callback=self.constructReindeerFromAtoms)
>>> view = ReindeerView()
>>> batch_navigator = view.batchedReindeer()
>>> print view.built_reindeer
['Dasher', 'Dancer', 'Prancer']
>>> print list(batch_navigator.currentBatch())
['Dasher', 'Dancer', 'Prancer']
==================
Maximum batch size
==================
Since the batch size is exposed in the URL, it's possible for users to
tweak the batch parameter to retrieve more results. Since that may
potentially exhaust server resources, an upper limit is put on the batch
size. If the requested batch parameter is higher than this, an
InvalidBatchSizeError is raised.
>>> class DemoBatchNavigator(BatchNavigator):
... max_batch_size = 5
...
>>> request = build_request({"start": "0", "batch": "20"})
>>> DemoBatchNavigator(reindeer, request=request )
Traceback (most recent call last):
...
InvalidBatchSizeError: Maximum for "batch" parameter is 5.
==============
URL parameters
==============
Normally, any parameters passed in the current page's URL are
reproduced in the batch navigator's links. A "transient" parameter is
one that was only relevant for the current page request and shouldn't be
passed on to subsequent ones.
In this next batch navigator, two parameters occur in the page's URL:
"noisy" and "quiet."
>>> request_parameters = {
... 'quiet': 'ssht',
... 'noisy': 'HELLO',
... }
>>> request_with_parameters = build_request(request_parameters)
One parameter, "quiet," is transient. There is another transient
parameter called "absent," but it's not passed in our ongoing page
request.
>>> def build_navigator(list):
... return BatchNavigator(
... list, request_with_parameters, size=3,
... transient_parameters=['quiet', 'absent'])
>>> navigator_with_parameters = build_navigator(reindeer)
>>> safe_navigator_with_parameters = build_navigator(safe_reindeer)
Of these three parameters, only "noisy" recurs in the links produced by
the batch navigator.
>>> navigator_with_parameters.nextBatchURL()
'http://www.example.com/foo?noisy=HELLO&memo=3&start=3'
>>> safe_navigator_with_parameters.lastBatchURL()
'http://www.example.com/foo?noisy=HELLO&direction=backwards&start=6'
The transient parameter is omitted, and the one that was never passed in
in the first place does not magically appear.
==============
Batch headings
==============
The batched values are usually one kind of object such as bugs. The
BatchNavigator's heading property contains a description of the objects
for display.
>>> safe_reindeer_batch_navigator.heading
'results'
There is a special case for when there is only one item in the batch,
the singular version of the heading is returned.
>>> navigator = BatchNavigator(['only-one'], request=request)
>>> navigator.heading
'result'
(Accessing .heading causes len() to be called on the underlying list,
which is why we have to use the safe batch navigator. In theory, this
could be optimized, but there's no real point, since the heading is
invariably preceded by the actual length of the underlying list,
eg. "10 results". Since len() is called anyway, and its value is
cached, a second len() won't hurt performance.)
The heading can be set by passing a singular and a plural version of
the heading. The batch navigation will return the appropriate
header based on the total items in the batch.
>>> navigator = BatchNavigator(safe_reindeer, request=request)
>>> navigator.setHeadings('bug', 'bugs')
>>> navigator.heading
'bugs'
>>> navigator = BatchNavigator(['only-one'], request=request)
>>> navigator.setHeadings('bug', 'bugs')
>>> navigator.heading
'bug'
(Cleanup)
>>> sm.unregisterAdapter(HTTPCharsets)
True
>>> sm.unregisterAdapter(ExampleAdapter)
True
===============
Other Documents
===============
.. toctree::
:glob:
*
docs/*
============================
NEWS for lazr.batchnavigator
============================
1.2.10 (2011-09-14)
- delegate the calculation of the rough length of a result set to
IRangeFactory.
1.2.9 (2011-08-25)
- When a backwards batch is at first too short and when another chunk
from the result set is added, _Batch,sliced_list() does no longer
use the memo value for the already retrived chunk.
- don't use the parameter start to determine if a previous/next batch
exists; don't rely on len(resultset) and to determine the real size
of a batch.
- Avoid negative start index on empty result sets.
1.2.7 (2011-07-18)
==================
- retrieve slices of the result set in class _Batch only via methods
of the range factory.
1.2.6 (2011-07-28)
==================
- fixed an error in handling backwards batches which return less elements
than expected.
- URL-encode all query parameters in BatchNavigator.generateBatchURL()
1.2.5 (2011-07-13)
==================
- Permit changing all variable names with a single prefix.
1.2.4 (2011-04-11)
==================
- Permit overriding determineSize to control how the batch default and concrete
sizes are determined in subclasses.
- Listify (once we have sliced) rather than assuming batched slices will honour
the complete list protocol.
1.2.3 (2011-04-06)
==================
- Add IRangeFactory and the ability to use backend database hints for efficient
retrieval of pages.
- Remove terrible-scaling getBatchURLs method.
1.2.2 (2010-08-19)
==================
- Make len() cheap to call when the current batch is the last (or
only) batch.
- Avoid calling len() when generating navigator URLs.
1.2.1 (2010-08-12)
==================
- fix a bug in the len() of a batch when the batch had previously been
iterated over
1.2.0 (2010-08-05)
==================
- avoid calling len() on the underlying sequence when possible
- return None for endNumber when the batch is out of range
1.1.1 (2010-05-10)
==================
- Ignore negative batch sizes
1.1 (2009-08-31)
================
- Remove build dependencies on bzr and egg_info
- remove sys.path hack in setup.py for __version__
1.0 (2009-03-24)
================
- Initial release on PyPI
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
|