[frames] | no frames]

# Source Code for Module gmisclib.nicknames

``` 1
2
3
4 -def nicknames(namelist):
5          """Takes a list of names and trims off junk from the beginning
6          and ends of the names to produce a set of reasonable nicknames
7          that are relatively compact and easy to read.
8
9          It returns a map from the names to the nicknames, along with
10          the stuff trimmed off from the edges.
11          """
12
13          namelist = list(namelist)
14          assert len(namelist) > 0
15          left = ''
16          right = ''
17          ll = 1
18          while ll <= len(namelist[0]):
19                  left = namelist[0][:ll]
20                  failed = False
21                  for nm in namelist:
22                          if not nm.startswith(left):
23                                  failed = True
24                                  break
25                  if failed:
26                          ll -= 1
27                          left = left[:ll]
28                          break
29                  ll += 1
30          rr = 1
31          while rr <= len(namelist[0]):
32                  right = namelist[0][-rr:]
33                  failed = False
34                  for nm in namelist:
35                          if not nm[ll:].endswith(right):
36                                  failed = True
37                                  break
38                  if failed:
39                          right = right[1:]
40                          rr -= 1
41                          break
42                  rr += 1
43          map = {}
44          for nm in namelist:
45                  map[nm] = nm[ll:len(nm)-rr]
46          return (map, left, right)
47
48
49
50 -def test():
51          m, l, r = nicknames(['football.', 'fooberry.', 'fools gold.'])
52          assert l == 'foo'
53          assert r == '.'
54          mi = m.items()
55          mi.sort()
56          assert mi == [('fooberry.', 'berry'), ('fools gold.', 'ls gold'), ('football.', 'tball')]
57          assert l + m['football.'] + r == 'football.'
58
59          m, l, r = nicknames(['abc', 'def'])
60          assert l=='' and r==''
61          assert m['abc'] == 'abc'
62          assert l + m['abc'] + r == 'abc'
63
64          m, l, r = nicknames(['abc'])
65          assert l + m['abc'] + r == 'abc'
66
67          m, l, r = nicknames(['val', 'str'])
68          print m, l, r
69
70
71
72
73  if __name__ == '__main__':
74          test()
75
<!--
expandto(location.href);
// -->

```

 Generated by Epydoc 3.0.1 on Thu Sep 22 04:25:13 2011 http://epydoc.sourceforge.net