|
| 1 | +# Author: Daniela Grothe |
| 2 | +# GitHub: https://github.com/DGrothe-PhD |
| 3 | + |
| 4 | +# Replace blanks in a text with an asterisk, replace words of the text with blanks |
| 5 | +# to create a (pseudo-)random snow pattern |
| 6 | + |
| 7 | +class Scatter: |
| 8 | + __piece = ["*"] |
| 9 | + __textsample = "" |
| 10 | + __lineslist = [] |
| 11 | + def __init__(self, tx): |
| 12 | + self.__textsample = tx |
| 13 | + def SetTextSample(self, tx): |
| 14 | + self.__textsample = tx |
| 15 | + |
| 16 | + #choose your own symbol or string to be scattered |
| 17 | + def SetPrintableObject(self, p): |
| 18 | + self.__piece.append(p) |
| 19 | + def ClearPrintableObjects(self): |
| 20 | + self.__piece = [] |
| 21 | + |
| 22 | + # Split (roughly all of) the text in portions (lines) of linelength (integer) characters |
| 23 | + # note: TextCarpet ignores the last len(text) modulo linelength characters. |
| 24 | + # For splitting readable text, use SplitToShort instead. |
| 25 | + def TextCarpet(self, linelength): |
| 26 | + self.__lineslist = [] |
| 27 | + for i in range(0,len(self.__textsample)//linelength): |
| 28 | + self.__lineslist.append(self.__textsample[i*linelength:(i+1)*linelength]) |
| 29 | + |
| 30 | + def SplitToShort(self, tx, linelength): |
| 31 | + self.__lineslist = [] |
| 32 | + for i in range(0,len(__textsample)//linelength): |
| 33 | + self.__lineslist.append(self.__textsample[i*linelength:(i+1)*linelength]) |
| 34 | + self.__lineslist.append(self.__textsample[(i+1)*linelength+1:len(tx)]) |
| 35 | + return self.__lineslist |
| 36 | + # |
| 37 | + def scatter(self): |
| 38 | + foo = [] |
| 39 | + i=0 |
| 40 | + if len(self.__lineslist)<2: |
| 41 | + pass#return "Too low number of lines or text too short." |
| 42 | + for x in self.__lineslist: |
| 43 | + if i<100: # don't generate too large output |
| 44 | + buf= x.split() |
| 45 | + a = "" |
| 46 | + k = len(self.__piece) |
| 47 | + for j in range(0,len(buf)): |
| 48 | + a+= (" "*len(buf[j])) |
| 49 | + a+=self.__piece[j%k] |
| 50 | + foo.append(a) |
| 51 | + i+=1 |
| 52 | + print("\n".join(foo)) |
0 commit comments