forked from plotdevice/plotdevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
448 lines (367 loc) · 15.4 KB
/
__init__.py
File metadata and controls
448 lines (367 loc) · 15.4 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
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
import os
import re
import json
import csv
from codecs import open
from xml.parsers import expat
from collections import OrderedDict, defaultdict
from AppKit import NSFontManager, NSFont, NSMacOSRomanStringEncoding, NSItalicFontMask
from os.path import abspath, dirname, exists, join
from random import choice, shuffle
from plotdevice import DeviceError, INTERNAL as DEFAULT
from .http import GET
__all__ = ('grid', 'random', 'shuffled', 'choice', 'ordered', 'order', 'files', 'read', 'autotext', '_copy_attr', '_copy_attrs', 'odict', 'ddict', 'adict')
### Utilities ###
def grid(cols, rows, colSize=1, rowSize=1, shuffled=False):
"""Returns an iterator that contains coordinate tuples.
The grid can be used to quickly create grid-like structures. A common way to use them is:
for x, y in grid(10,10,12,12):
rect(x,y, 10,10)
"""
# Prefer using generators.
rowRange = xrange(int(rows))
colRange = xrange(int(cols))
# Shuffled needs a real list, though.
if (shuffled):
rowRange = list(rowRange)
colRange = list(colRange)
shuffle(rowRange)
shuffle(colRange)
for y in rowRange:
for x in colRange:
yield (x*colSize,y*rowSize)
def random(v1=None, v2=None, mean=None, sd=None):
"""Returns a random value.
This function does a lot of things depending on the parameters:
- If one or more floats is given, the random value will be a float.
- If all values are ints, the random value will be an integer.
- If one value is given, random returns a value from 0 to the given value.
This value is not inclusive.
- If two values are given, random returns a value between the two; if two
integers are given, the two boundaries are inclusive.
"""
import random
if v1 != None and v2 == None: # One value means 0 -> v1
if isinstance(v1, float):
return random.random() * v1
else:
return int(random.random() * v1)
elif v1 != None and v2 != None: # v1 -> v2
if isinstance(v1, float) or isinstance(v2, float):
start = min(v1, v2)
end = max(v1, v2)
return start + random.random() * (end-start)
else:
start = min(v1, v2)
end = max(v1, v2) + 1
return int(start + random.random() * (end-start))
elif mean != None and sd!= None:
return random.normalvariate(mean, sd)
else: # No values means 0.0 -> 1.0
return random.random()
def files(path="*", case=True):
"""Returns a list of files.
You can use wildcards to specify which files to pick, e.g.
f = files('~/Pictures/*.jpg')
For a case insensitive search, call files() with case=False
"""
from iglob import iglob
if type(path)==unicode:
path.encode('utf-8')
path = re.sub(r'^~(?=/|$)',os.getenv('HOME'),path)
return list(iglob(path.decode('utf-8'), case=case))
def autotext(sourceFile):
from plotdevice.util.kgp import KantGenerator
k = KantGenerator(sourceFile)
return k.output()
### Permutation sugar ###
def _as_sequence(seq):
if not hasattr(seq, '__getitem__'):
badtype = 'ordered, shuffled, and friends only work for strings, tuples and lists (not %s)' % type(seq)
raise DeviceError(badtype)
return list(seq)
def _as_before(orig, lst):
return "".join(lst) if isinstance(orig, basestring) else list(lst)
def _getter(seq, names):
from operator import itemgetter, attrgetter
is_dotted = any(['.' in name for name in names])
getter = attrgetter if is_dotted or hasattr(seq[0],names[0]) else itemgetter
return getter(*names)
def order(seq, *names, **kwargs):
lst = _as_sequence(seq)
if not names or not seq:
reordered = [(it,idx) for idx,it in enumerate(lst)]
else:
getter = _getter(lst, names)
reordered = [(getter(it), idx) for idx,it in enumerate(lst)]
reordered.sort(**kwargs)
return [it[1] for it in reordered]
def ordered(seq, *names, **kwargs):
"""Return a sorted copy of a list or tuple, optionally using a common item or
attr name of the elements within the sequence.
If included, *names should be one or more strings indicating which `fields` of
the sequence elements should be used in comparing their equality. If more than
one name is specified, the second field will be used to break `ties' based on
the first.
The return value will be ordered in an ascending fashion, but can be flipped
using the reverse=True keyword argument."""
lst = _as_sequence(seq)
if kwargs.get('perm') and lst:
return _as_before(seq, [lst[idx] for idx in kwargs['perm']])
if not names or not lst:
return _as_before(seq, sorted(lst, **kwargs))
return _as_before(seq, sorted(lst, key=_getter(lst, names), **kwargs))
def shuffled(seq):
"""Returns a random permutation of a list or tuple (without modifying the original)"""
lst = _as_sequence(seq)
shuffle(lst)
return _as_before(seq, lst)
### deepcopy helpers ###
def _copy_attr(v):
if v is None:
return None
elif hasattr(v, "copy"):
return v.copy()
elif isinstance(v, tuple):
if hasattr(v, '_fields'):
return v._replace() # don't demote namedtuples to tuples
return tuple(v)
elif isinstance(v, list):
return list(v)
elif isinstance(v, (int, str, unicode, float, bool, long)):
return v
else:
raise DeviceError, "Don't know how to copy '%s'." % v
def _copy_attrs(source, target, attrs):
for attr in attrs:
try:
setattr(target, attr, _copy_attr(getattr(source, attr)))
except AttributeError, e:
print "missing attr: %r"% attr, hasattr(source, attr), hasattr(target, attr)
raise e
### tuple/list de-nester ###
def _flatten(seq):
return sum( ([x] if not isinstance(x, (list,tuple)) else list(x) for x in seq), [] )
### repr decorator (tidies numbers) ###
def trim_zeroes(func):
return lambda slf: re.sub(r'\.?0+(?=[,\)\]])', '', func(slf))
### Dimension-aware number detector (replacement for isintance) ###
def numlike(obj):
return hasattr(obj, '__int__') or hasattr(obj, '__float__')
### give ordered- and default-dict a nicer repr ###
class BetterRepr(object):
def __repr__(self, indent=2):
result = '%s{'%self.__class__.__name__
for k,v in self.iteritems():
if isinstance(v, BetterRepr):
vStr = v.__repr__(indent + 2)
else:
vStr = v.__repr__()
result += "\n" + ' '*indent + k.__repr__() + ': ' + vStr
if not result.endswith('{'):
result += "\n"
result += '}'
return result
class odict(BetterRepr,OrderedDict):
"""Dictionary that remembers insertion order
Normal dict objects return keys in an arbitrary order. An odict will return them in
the order you add them. To initialize an odict and not lose the ordering in the process,
avoid using keyword arguments and instead pass a list of (key,val) tuples:
odict([ ('foo',12), ('bar',14), ('baz', 33) ])
or as part of a generator expression:
odict( (k,other[k]) for k in sorted(other.keys()) )
"""
pass
class ddict(BetterRepr,defaultdict):
"""Dictionary with default factory
Any time you access a key that was previously undefined, the factory function
is called and a default value is inserted in the dictionary. e.g.,
normal_dict = {}
normal_dict['foo'].append(42) # raises a KeyError
lst_dict = ddict(list)
lst_dict['foo'].append(42) # sets 'foo' to [42]
num_dict = ddict(int)
print num_dict['bar'] # prints '0'
num_dict['baz'] += 2 # increments 'baz' from 0 to 2
"""
pass
### the not very pythonic but often convenient dot-notation dict ###
class adict(BetterRepr, dict):
"""A dictionary object whose items may also be accessed with dot notation.
Items can be assigned using dot notation even if a dictionary method of the
same name exists. Subsequently, dot notation will still reference the method,
but the assigned value can be read out using traditional `d["name"]` syntax.
"""
def __init__(self, *args, **kw):
super(adict, self).__init__(*args, **kw)
self.__initialised = True
def __getattr__(self, key):
try:
return self[key]
except KeyError, k:
raise AttributeError, k
def __setattr__(self, key, value):
# this test allows attributes to be set in the __init__ method
if not self.__dict__.has_key('_adict__initialised'):
return dict.__setattr__(self, key, value)
self[key] = value
def __delattr__(self, key):
try:
del self[key]
except KeyError, k:
raise AttributeError, k
### datafile unpackers ###
class XMLParser(object):
_log = 0
def __init__(self, txt):
# configure the parsing machinery/callbacks
p = expat.ParserCreate()
p.StartElementHandler = self._enter
p.EndElementHandler = self._leave
p.CharacterDataHandler = self._chars
self._expat = p
# set up state attrs to record the parse results
self.stack = []
self.cursor = 0
self.regions = ddict(list)
self.body = []
try:
# parse the input xml string
if isinstance(txt, unicode):
txt = txt.encode('utf-8')
wrap = "<%s>" % ">%s</".join([DEFAULT]*2)
self._expat.Parse(wrap%txt, True)
except expat.ExpatError, e:
# go a little overboard providing context for syntax errors
line = self.xml.split('\n')[e.lineno-1]
self._expat_error(e, line)
@property
def text(self):
# returns the processed string (with all markup removed)
return "".join(self.body)
def _expat_error(self, e, line):
measure = 80
col = e.offset
start, end = len('<%s>'%DEFAULT), -len('</%s>'%DEFAULT)
line = line[start:end]
col -= start
# move the column range with the typo into `measure` chars
snippet = line
if col>measure:
snippet = snippet[col-measure:]
col -= col-measure
snippet = snippet[:max(col+12, measure-col)]
col = min(col, len(snippet))
# show which ends of the line are truncated
clipped = [snippet]
if not line.endswith(snippet):
clipped.append('...')
if not line.startswith(snippet):
clipped.insert(0, '...')
col+=3
caret = ' '*col + '^'
# raise the exception
msg = 'Text: ' + "\n".join(e.args)
stack = 'stack: ' + " ".join(['<%s>'%tag for tag in self.stack[1:]]) + ' ...'
xmlfail = "\n".join([msg, "".join(clipped), caret, stack])
raise DeviceError(xmlfail)
def log(self, s=None, indent=0):
if not isinstance(s, basestring):
if s is None:
return self._log
self._log = int(s)
return
if not self._log: return
if indent<0: self._log-=1
msg = (u' '*self._log)+(s if s.startswith('<') else repr(s))
print msg.encode('utf-8')
if indent>0: self._log+=1
def _enter(self, name, attrs):
self.stack.append(name)
self.log(u'<%s>'%(name), indent=1)
def _leave(self, name):
if name == DEFAULT:
self.body = u"".join(self.body)
self.stack.pop()
self.log(u'</%s>'%(name), indent=-1)
def _chars(self, data):
self.regions[tuple(self.stack)].append(tuple([self.cursor, len(data)]))
self.cursor += len(data)
self.body.append(data)
self.log(u'"%s"'%(data))
def csv_reader(pth, encoding, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
with open(pth, 'Urb', encoding) as unicode_csv_data:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def csv_dictreader(pth, encoding, dialect=csv.excel, cols=None, dict=dict, **kwargs):
if not isinstance(cols, (list, tuple)):
cols=None
with open(pth, 'Urb', encoding) as unicode_csv_data:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
if not cols:
cols = [unicode(cell, 'utf-8') for cell in row]
continue
# decode UTF-8 back to Unicode, cell by cell:
yield dict( (col, unicode(cell, 'utf-8')) for (col,cell) in zip(cols,row) )
def csv_dialect(fd):
dialect = csv.Sniffer().sniff(fd.read(1024))
fd.seek(0)
return dialect
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
def read(pth, format=None, encoding='utf-8', cols=None, **kwargs):
"""Returns the contents of a file into a string or format-dependent data
type (with special handling for json and csv files).
The format will either be inferred from the file extension or can be set
explicitly using the `format` arg. Text will be read using the specified
`encoding` or default to UTF-8.
JSON files will be parsed and an appropriate python type will be selected
based on the top-level object defined in the file. The optional keyword
argument `dict` can be set to `adict` or `odict` if you'd prefer not to use
the standard python dictionary for decoded objects.
CSV files will return a list of rows. By default each row will be an ordered
list of column values. If the first line of the file defines column names,
you can call read() with cols=True in which case each row will be a dictionary
using those names as keys. If the file doesn't define its own column names,
you can pass a list of strings as the `cols` parameter.
"""
if re.match(r'https?:', pth):
fd, _ = GET(pth)
else:
pth = re.sub(r'^~(?=/|$)',os.getenv('HOME'),pth)
fd = file(pth, 'Urb')
format = format.lstrip('.') if format else pth.rsplit('.',1)[-1]
dict_type = kwargs.get('dict', dict)
if format=='json':
return json.load(fd, object_pairs_hook=dict_type, encoding=encoding)
elif format=='csv' and not re.match(r'https?:', pth):
dialect = csv_dialect(pth)
if cols:
return list(csv_dictreader(pth, encoding, dialect=dialect, cols=cols, dict=dict_type))
return list(csv_reader(pth, encoding, dialect=dialect))
else:
return fd.read().decode(encoding)
### module data dir ###
def rsrc_path(resource=None):
"""Return absolute path of the rsrc directory (or a file within it)"""
module_root = abspath(dirname(dirname(__file__)))
rsrc_root = join(module_root, 'rsrc')
if not exists(rsrc_root):
# hack to run in-place in sdist
from glob import glob
for pth in glob(join(module_root, '../build/lib/plotdevice/rsrc')):
rsrc_root = abspath(pth)
break
else:
notfound = "Couldn't locate resources directory (try running `python setup.py build` before running from the source dist)."
raise RuntimeError(notfound)
if resource:
return join(rsrc_root, resource)
return rsrc_root