-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·51 lines (38 loc) · 1.14 KB
/
Copy path__init__.py
File metadata and controls
executable file
·51 lines (38 loc) · 1.14 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# File for storing code and data common to the whole project.
def dimensioner(item, dim):
try:
return item[dim]
except (TypeError, IndexError):
if dim > 0:
return TooFewDimensions
return item
class DoesNotFitWarning(Warning):
pass
class TooFewDimensions(Warning):
pass
class GoTo(Warning):
pass
class PersistentGenerator(object):
"""Generic generator which does not die after raising `StopIteration`.
Useful for ensuring online packers are polymorphic with offline packers
(in a syntactically convenient manner).
"""
def __init__(self, *items, **kwargs):
self.key = kwargs.get('key', lambda x:x)
self.binsize = kwargs.get('binsize', 800)
self.bins = kwargs.get('bins', [[]])
self.unpacked_items=list(items)
def __iter__(self):
return self
def next(self):
self.bins.sort(self.key)
if self.bins:
return self.bins.pop(0)
else:
raise StopIteration
def __call__(self, *items):
self.bins += items
return self