Merge branch 'master' into bbmaster
This commit is contained in:
		
						commit
						8fb5102aa8
					
				
					 6 changed files with 120 additions and 6 deletions
				
			
		|  | @ -162,7 +162,7 @@ | ||||||
|                            style python: |                            style python: | ||||||
|                                 'width: %s' % (width and str(width)+'px' or '');"> |                                 'width: %s' % (width and str(width)+'px' or '');"> | ||||||
|       <option tal:repeat="item python: |       <option tal:repeat="item python: | ||||||
|                     field.getVocabularyItems(instance.context, request)" |                     field.getVocabularyItems(instance, request)" | ||||||
|               tal:content="item/title" |               tal:content="item/title" | ||||||
|               tal:attributes="value item/token; |               tal:attributes="value item/token; | ||||||
|                               selected python: |                               selected python: | ||||||
|  |  | ||||||
|  | @ -129,7 +129,10 @@ class Field(Component): | ||||||
|     def getTitleValue(self): |     def getTitleValue(self): | ||||||
|         return self.title or self.name |         return self.title or self.name | ||||||
| 
 | 
 | ||||||
|     def getVocabularyItems(self, context=None, request=None): |     def getVocabularyItems(self, instance=None, request=None): | ||||||
|  |         context = None | ||||||
|  |         if instance is not None: | ||||||
|  |             context = instance.context | ||||||
|         voc = (self.vocabulary or '') |         voc = (self.vocabulary or '') | ||||||
|         if isinstance(voc, basestring): |         if isinstance(voc, basestring): | ||||||
|             terms = self.getVocabularyTerms(voc, context, request) |             terms = self.getVocabularyTerms(voc, context, request) | ||||||
|  | @ -138,7 +141,7 @@ class Field(Component): | ||||||
|             voc = voc.splitlines() |             voc = voc.splitlines() | ||||||
|             return [dict(token=t, title=t) for t in voc if t.strip()] |             return [dict(token=t, title=t) for t in voc if t.strip()] | ||||||
|         elif IContextSourceBinder.providedBy(voc): |         elif IContextSourceBinder.providedBy(voc): | ||||||
|             source = voc(context) |             source = voc(instance) | ||||||
|             terms = component.queryMultiAdapter((source, request), ITerms) |             terms = component.queryMultiAdapter((source, request), ITerms) | ||||||
|             if terms is not None: |             if terms is not None: | ||||||
|                 termsList = [terms.getTerm(value) for value in source] |                 termsList = [terms.getTerm(value) for value in source] | ||||||
|  | @ -395,7 +398,7 @@ class CheckBoxesFieldInstance(ListFieldInstance): | ||||||
| class DropdownFieldInstance(FieldInstance): | class DropdownFieldInstance(FieldInstance): | ||||||
| 
 | 
 | ||||||
|     def display(self, value): |     def display(self, value): | ||||||
|         items = self.context.getVocabularyItems(self.clientContext, self.request) |         items = self.context.getVocabularyItems(self.clientInstance, self.request) | ||||||
|         for item in items: |         for item in items: | ||||||
|             if item['token'] == value: |             if item['token'] == value: | ||||||
|                 return item['title'] |                 return item['title'] | ||||||
|  |  | ||||||
|  | @ -120,7 +120,8 @@ class GridFieldInstance(ListFieldInstance): | ||||||
|             if fi.default is not None: |             if fi.default is not None: | ||||||
|                 if value == fi.default: |                 if value == fi.default: | ||||||
|                     continue |                     continue | ||||||
|             item[fi.name] = value |             if value: | ||||||
|  |                 item[fi.name] = value | ||||||
|         return item |         return item | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -189,7 +190,7 @@ class KeyTableFieldInstance(RecordsFieldInstance): | ||||||
|         for row in value: |         for row in value: | ||||||
|             item = self.unmarshallRow(row) |             item = self.unmarshallRow(row) | ||||||
|             if item: |             if item: | ||||||
|                 result[item.pop(self.keyName)] = [item.get(name) |                 result[item.pop(self.keyName)] = [item.get(name) or u'' | ||||||
|                                                   for name in self.dataNames] |                                                   for name in self.dataNames] | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										55
									
								
								util/iterate.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								util/iterate.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,55 @@ | ||||||
|  | # | ||||||
|  | #  Copyright (c) 2012 Helmut Merz helmutm@cy55.de | ||||||
|  | # | ||||||
|  | #  This program is free software; you can redistribute it and/or modify | ||||||
|  | #  it under the terms of the GNU General Public License as published by | ||||||
|  | #  the Free Software Foundation; either version 2 of the License, or | ||||||
|  | #  (at your option) any later version. | ||||||
|  | # | ||||||
|  | #  This program 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 General Public License for more details. | ||||||
|  | # | ||||||
|  | #  You should have received a copy of the GNU General Public License | ||||||
|  | #  along with this program; if not, write to the Free Software | ||||||
|  | #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | ||||||
|  | # | ||||||
|  | 
 | ||||||
|  | """ | ||||||
|  | Iterator and generator utilities. | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | from itertools import islice | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class BatchIterator(object): | ||||||
|  | 
 | ||||||
|  |     def __init__(self, data, limit=20, start=0): | ||||||
|  |         self.data = iter(data) | ||||||
|  |         self.limit = limit | ||||||
|  |         self.start = start | ||||||
|  |         self.count = 0 | ||||||
|  |         self.batch = 0 | ||||||
|  |         self.exhausted = False | ||||||
|  | 
 | ||||||
|  |     def __iter__(self): | ||||||
|  |         return self | ||||||
|  | 
 | ||||||
|  |     def next(self): | ||||||
|  |         if self.count >= (self.batch + 1) * self.limit: | ||||||
|  |             raise StopIteration | ||||||
|  |         if self.start: | ||||||
|  |             for i in islice(self.data, 0, self.start*self.limit): | ||||||
|  |                 pass | ||||||
|  |             self.start = 0 | ||||||
|  |         self.count += 1 | ||||||
|  |         try: | ||||||
|  |             return self.data.next() | ||||||
|  |         except StopIteration: | ||||||
|  |             self.exhausted = True | ||||||
|  |             raise | ||||||
|  | 
 | ||||||
|  |     def advance(self, batches=1): | ||||||
|  |         self.batch += batches | ||||||
|  |         return not self.exhausted | ||||||
							
								
								
									
										54
									
								
								util/iterate.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								util/iterate.txt
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | ||||||
|  | ================================ | ||||||
|  | Iterator and Generator Utilities | ||||||
|  | ================================ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | Batch Iterator | ||||||
|  | ============== | ||||||
|  | 
 | ||||||
|  | A batch iterator only provides a limited number of items in one | ||||||
|  | series of access steps. | ||||||
|  | 
 | ||||||
|  |   >>> from cybertools.util.iterate import BatchIterator | ||||||
|  | 
 | ||||||
|  | We create a BatchIterator upon a base iterator. The BatchIterator | ||||||
|  | only gives us a limited portion of the values provided by the base | ||||||
|  | iterator. | ||||||
|  | 
 | ||||||
|  |   >>> it = BatchIterator(xrange(30)) | ||||||
|  |   >>> list(it) | ||||||
|  |   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] | ||||||
|  |   >>> list(it) | ||||||
|  |   [] | ||||||
|  | 
 | ||||||
|  | Now we advance to the next batch. The return value tells us that the | ||||||
|  | base iterator is not exhausted yet. | ||||||
|  | 
 | ||||||
|  |   >>> it.advance() | ||||||
|  |   True | ||||||
|  |   >>> list(it) | ||||||
|  |   [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] | ||||||
|  | 
 | ||||||
|  | Advancing would not help if the base iterator is exhausted. | ||||||
|  | 
 | ||||||
|  |   >>> it.advance() | ||||||
|  |   False | ||||||
|  |   >>> list(it) | ||||||
|  |   [] | ||||||
|  | 
 | ||||||
|  | We can also immediately start at the second batch by providing the ``start`` | ||||||
|  | argument to the BatchIterator constructor. | ||||||
|  | 
 | ||||||
|  |   >>> it = BatchIterator(xrange(30), start=1) | ||||||
|  |   >>> list(it) | ||||||
|  |   [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] | ||||||
|  | 
 | ||||||
|  | We can use another limit (i.e. the batch size) via the BatchIterator constructor. | ||||||
|  | 
 | ||||||
|  |   >>> it = BatchIterator(xrange(30), start=1, limit=8) | ||||||
|  |   >>> list(it) | ||||||
|  |   [8, 9, 10, 11, 12, 13, 14, 15] | ||||||
|  |   >>> it.advance() | ||||||
|  |   True | ||||||
|  |   >>> list(it) | ||||||
|  |   [16, 17, 18, 19, 20, 21, 22, 23] | ||||||
|  | @ -25,6 +25,7 @@ def test_suite(): | ||||||
|         doctest.DocFileSuite('defer.txt', optionflags=flags), |         doctest.DocFileSuite('defer.txt', optionflags=flags), | ||||||
|         doctest.DocFileSuite('format.txt', optionflags=flags), |         doctest.DocFileSuite('format.txt', optionflags=flags), | ||||||
|         doctest.DocFileSuite('html.txt', optionflags=flags), |         doctest.DocFileSuite('html.txt', optionflags=flags), | ||||||
|  |         doctest.DocFileSuite('iterate.txt', optionflags=flags), | ||||||
|         doctest.DocFileSuite('multikey.txt', optionflags=flags), |         doctest.DocFileSuite('multikey.txt', optionflags=flags), | ||||||
|         doctest.DocFileSuite('property.txt', optionflags=flags), |         doctest.DocFileSuite('property.txt', optionflags=flags), | ||||||
|         doctest.DocFileSuite('json.txt', optionflags=flags), |         doctest.DocFileSuite('json.txt', optionflags=flags), | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue