-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable areas to be an iterator throughout request.
We subclass dict so that we can create an iterator that json will use.
- Loading branch information
Showing
3 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# If you wanted iterdict() to be more generic, it'd be something like: | ||
# from django.utils import six | ||
# import itertools | ||
# if not args: | ||
# self.ITERABLE = iter() | ||
# elif isinstance(args[0], dict): | ||
# self.ITERABLE = six.iteritems(args[0]) | ||
# else: | ||
# self.ITERABLE = args[0] | ||
# if kwargs: | ||
# self.ITERABLE = itertools.chain(self.ITERABLE, six.iteritems(kwargs)) | ||
|
||
|
||
class iterdict(dict): | ||
"""This is a fake dict that sticks an iterable on items/iteritems. Why on | ||
earth would you want to do such a thing, I hear you cry? Well, if you want | ||
to output what you'd like to be a dict of c. 200,000 objects from a | ||
database, currently held in a lovely iterable, as JSON, but discover that | ||
json.iterencode() doesn't work with iterators, and that wouldn't be much | ||
help with a dict anyway, you either need to write your own iterator to | ||
output a JSON object, or trick iterencode() into thinking you're passing it | ||
a dict when you're not.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.ITERABLE = args[0] | ||
# Create a dict so that "if X" passes. | ||
super(iterdict, self).__init__({'Hack': True}) | ||
|
||
def iteritems(self): | ||
return self.ITERABLE | ||
|
||
def items(self): | ||
return self.ITERABLE | ||
|
||
|
||
class iterlist(list): | ||
"""This is a fake list that uses its own stored iterable. The built in json | ||
package, though it can work as an iterator using iterencode() or indeed | ||
dump(), can't take one as input, which is annoying if you're trying to save | ||
memory. This class can be passed to iterencode() and will work the same as | ||
a list, but won't require the list to exist first.""" | ||
|
||
def __init__(self, l): | ||
self.ITERABLE = l | ||
# Create a list so that "if X" passes. | ||
super(iterlist, self).__init__("Hack") | ||
|
||
def __iter__(self): | ||
return self.ITERABLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters