Debian Codegraph @

petl/util/misc.py

raw
1 from __future__ import absolute_import, print_function, division
2
3
4 from petl.util.base import values, header, Table
5
6
7 def typeset(table, field):
8 """
9 Return a set containing all Python types found for values in the given
10 field. E.g.::
11
12 >>> import petl as etl
13 >>> table = [['foo', 'bar', 'baz'],
14 ... ['A', 1, '2'],
15 ... ['B', u'2', '3.4'],
16 ... [u'B', u'3', '7.8', True],
17 ... ['D', u'xyz', 9.0],
18 ... ['E', 42]]
19 >>> sorted(etl.typeset(table, 'foo'))
20 ['str']
21 >>> sorted(etl.typeset(table, 'bar'))
22 ['int', 'str']
23 >>> sorted(etl.typeset(table, 'baz'))
24 ['NoneType', 'float', 'str']
25
26 The `field` argument can be a field name or index (starting from zero).
27
28 """
29
30 s = set()
31 for v in values(table, field):
32 try:
33 s.add(type(v).__name__)
34 except IndexError:
35 pass # ignore short rows
36 return s
37
38
39 Table.typeset = typeset
40
41
42 def diffheaders(t1, t2):
43 """
44 Return the difference between the headers of the two tables as a pair of
45 sets. E.g.::
46
47 >>> import petl as etl
48 >>> table1 = [['foo', 'bar', 'baz'],
49 ... ['a', 1, .3]]
50 >>> table2 = [['baz', 'bar', 'quux'],
51 ... ['a', 1, .3]]
52 >>> add, sub = etl.diffheaders(table1, table2)
53 >>> add
54 {'quux'}
55 >>> sub
56 {'foo'}
57
58 """
59
60 t1h = set(header(t1))
61 t2h = set(header(t2))
62 return t2h - t1h, t1h - t2h
63
64
65 Table.diffheaders = diffheaders
66
67
68 def diffvalues(t1, t2, f):
69 """
70 Return the difference between the values under the given field in the two
71 tables, e.g.::
72
73 >>> import petl as etl
74 >>> table1 = [['foo', 'bar'],
75 ... ['a', 1],
76 ... ['b', 3]]
77 >>> table2 = [['bar', 'foo'],
78 ... [1, 'a'],
79 ... [3, 'c']]
80 >>> add, sub = etl.diffvalues(table1, table2, 'foo')
81 >>> add
82 {'c'}
83 >>> sub
84 {'b'}
85
86 """
87
88 t1v = set(values(t1, f))
89 t2v = set(values(t2, f))
90 return t2v - t1v, t1v - t2v
91
92
93 Table.diffvalues = diffvalues
94
95
96 def strjoin(s):
97 """
98 Return a function to join sequences using `s` as the separator. Intended
99 for use with :func:`petl.transform.conversions.convert`.
100
101 """
102
103 return lambda l: s.join(map(str, l))
104
105
106 def nthword(n, sep=None):
107 """
108 Construct a function to return the nth word in a string. E.g.::
109
110 >>> import petl as etl
111 >>> s = 'foo bar'
112 >>> f = etl.nthword(0)
113 >>> f(s)
114 'foo'
115 >>> g = etl.nthword(1)
116 >>> g(s)
117 'bar'
118
119 Intended for use with :func:`petl.transform.conversions.convert`.
120
121 """
122
123 return lambda s: s.split(sep)[n]
124
125
126 def coalesce(*fields, **kwargs):
127 """
128 Return a function which accepts a row and returns the first non-missing
129 value from the specified fields. Intended for use with
130 :func:`petl.transform.basics.addfield`.
131
132 """
133 missing = kwargs.get('missing', None)
134 default = kwargs.get('default', None)
135
136 def _coalesce(row):
137 for f in fields:
138 v = row[f]
139 if v is not missing:
140 return v
141 return default
142
143 return _coalesce