~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/python/pygrass/shell/conversion.py

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
Created on Sun Jun 23 13:40:19 2013
 
4
 
 
5
@author: pietro
 
6
"""
 
7
 
 
8
 
 
9
dcont = """    <tr>
 
10
      <td>{key}</td>
 
11
      <td>{value}</td>
 
12
    </tr>"""
 
13
 
 
14
 
 
15
def dict2html(dic, keys=None, border='',
 
16
              kfmt='%s', kdec='', kfun=None,
 
17
              vfmt='%s', vdec='', vfun=None):
 
18
    """Return a html repr of a dictionary.
 
19
 
 
20
    :param dict dic: dictionary or object with `keys` and `items` methods
 
21
    :param keys: iterable objectwith only the keys that we want to display
 
22
    :param str border: could be: "0", "1", etc.
 
23
    :param str kfmt: string to format the key string (i.e. "%r", etc.)
 
24
    :param str kdec: string to decorate the key (i.e. "b", "i", etc.)
 
25
    :param str vfmt: string to format the value string (i.e. "%r", etc.)
 
26
    :param str vdec: string to decorate the value (i.e. "b", "i", etc.)
 
27
 
 
28
    >>> dic = {'key 0': 0, 'key 1': 1}
 
29
    >>> print dict2html(dic)
 
30
    <table>
 
31
        <tr>
 
32
          <td>key 0</td>
 
33
          <td>0</td>
 
34
        </tr>
 
35
        <tr>
 
36
          <td>key 1</td>
 
37
          <td>1</td>
 
38
        </tr>
 
39
    </table>
 
40
    >>> print dict2html(dic, border="1")
 
41
    <table border='1'>
 
42
        <tr>
 
43
          <td>key 0</td>
 
44
          <td>0</td>
 
45
        </tr>
 
46
        <tr>
 
47
          <td>key 1</td>
 
48
          <td>1</td>
 
49
        </tr>
 
50
    </table>
 
51
    >>> print dict2html(dic, kdec='b', vfmt='%05d', vdec='i')
 
52
    <table>
 
53
        <tr>
 
54
          <td><b>key 0</b></td>
 
55
          <td><i>00000</i></td>
 
56
        </tr>
 
57
        <tr>
 
58
          <td><b>key 1</b></td>
 
59
          <td><i>00001</i></td>
 
60
        </tr>
 
61
    </table>
 
62
    >>> dic = {'key 0': (2, 3), 'key 1': (10, 5)}
 
63
    >>> print dict2html(dic, kdec='b', vdec='i',
 
64
    ...                 vfun=lambda x: "%d<sup>%.1f</sup>" % x)
 
65
    <table>
 
66
        <tr>
 
67
          <td><b>key 0</b></td>
 
68
          <td><i>2<sup>3.0</sup></i></td>
 
69
        </tr>
 
70
        <tr>
 
71
          <td><b>key 1</b></td>
 
72
          <td><i>10<sup>5.0</sup></i></td>
 
73
        </tr>
 
74
    </table>
 
75
    """
 
76
    def fun(x):
 
77
        return x
 
78
 
 
79
    keys = keys if keys else sorted(dic.keys())
 
80
    header = "<table border=%r>" % border if border else "<table>"
 
81
    kd = "<%s>%s</%s>" % (kdec, kfmt, kdec) if kdec else kfmt
 
82
    vd = "<%s>%s</%s>" % (vdec, vfmt, vdec) if vdec else vfmt
 
83
    kfun = kfun if kfun else fun
 
84
    vfun = vfun if vfun else fun
 
85
    content = [dcont.format(key=kd % kfun(k), value=vd % vfun(dic[k]))
 
86
               for k in keys]
 
87
    return '\n'.join([header, ] + content + ['</table>', ])