Skip to content

Commit

Permalink
random algorithms for shortening text
Browse files Browse the repository at this point in the history
  • Loading branch information
swfiua committed Dec 4, 2020
1 parent a4050cd commit 4f4ddb1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
3 changes: 2 additions & 1 deletion blume/examples/shortify.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
# Add a table at the top of the axes
table(
ax,
max_cell_width=6,
max_cell_width=8,
cellText=cell_text,
squash=' aeiou',
bbox=(0, 0, 1, 1))

if not sys.flags.interactive:
Expand Down
48 changes: 31 additions & 17 deletions blume/taybell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def summarise(self):
for key, values in self.data:
pass

def shortify(values, maxlen=None, ellipsis='...'):
def shortify(values, maxlen=None, ellipsis=None, squash=None):
""" Shorten the value
Ho-hum need to deal with long strings with new lines:
Expand All @@ -32,34 +32,47 @@ def shortify(values, maxlen=None, ellipsis='...'):
for value in values:
aline = []
for line in value.split('\n'):
aline.append(shortify_line(line, maxlen, ellipsis))
aline.append(shortify_line(line, maxlen, ellipsis, squash))

result.append('\n'.join(aline))

return result

def shortify_line(value, maxlen=None, ellipsis='...'):
def shortify_line(value, maxlen=None, ellipsis=None, squash=None):

ellipsis = ellipsis or '...'
size = len(value)
if maxlen is None or size < maxlen:
print(maxlen)
if maxlen is None or size <= maxlen:
return value

# need to shorten and insert ...
elen = len(ellipsis)

# try removing space -- maybe camel case too?
if squash:
for key in squash:
value = value.replace(key, '')
if len(value) <= maxlen:
return value

# update size
size = len(value)


# how many characters neet the chop?
ncut = (size-maxlen) + elen
ncut = size-maxlen

# give beginning and end
sluglen = (size-ncut)//2
while ncut < len(ellipsis) and ellipsis:
ellipsis = ellipsis[:-1]
elen = len(ellipsis)

# and if there is a spare character, take it at the beginning
spare = ncut - (2*sluglen)
# give equal to beginning and end
sluglen = (size-(ncut+elen))//2

print(ncut, sluglen, spare)

return value[:sluglen+spare] + '...' + value[-sluglen:]
# and if there is a spare character, take it at the beginning
spare = maxlen - ((2*sluglen) + elen)
print(size, ncut, sluglen, spare)
return value[:sluglen+spare] + ellipsis + value[-sluglen:]



Expand All @@ -73,6 +86,7 @@ def table(ax,
cell_width=None,
col_width=None,
row_width=None,
squash=None,
**kwargs):

if max_cell_width:
Expand All @@ -84,17 +98,17 @@ def table(ax,
# here we need to turn cells, rows and cols into strings, unless they already are.
if rowLabels:
fields = [str(x) for x in rowLabels]
rowLabels = shortify(fields, row_width)
rowLabels = shortify(fields, row_width, squash=squash)

if colLabels:
fields = [str(x) for x in colLabels]
colLabels = shortify(fields, col_width)
colLabels = shortify(fields, col_width, squash=squash)

if cellText:
cells = []

for row in cellText:
cells.append(shortify(row, cell_width))
cells.append(shortify(row, cell_width, squash=squash))

cellText = cells

Expand Down
Binary file added doc/images/short2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions doc/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ output, whilst not what intended, is better than expected:

.. image:: images/short.png

Fixed the bug, ruined the image:

.. image:: images/short2.png

Fixme?

Look for white space to delete?

Camelcase while we are at it?


Interactive Magic
-----------------
Expand Down

0 comments on commit 4f4ddb1

Please sign in to comment.