Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import random
  2. from colored import fg
  3.  
  4. class RandomColorDict(dict):
  5. """Color storage class.
  6.  
  7. If the value for a key is 'random', obj[key] returns a random fg(color).
  8. For nonexistent keys, obj[notakey] will also return a random fg(color),
  9. instead of throwing a KeyError. Each instance of the class stores its
  10. own PRNG.
  11.  
  12. Use obj.get(key) if you need the real value (nonexistent keys will return
  13. None)."""
  14. def __init__(self, choices, *args, **kwargs):
  15. super(RandomColorDict, self).__init__(*args, **kwargs)
  16. self.random = random.Random()
  17. self.random.seed()
  18. self.choices = choices
  19.  
  20. def __getitem__(self, key):
  21. """Overload to return a random fg(color) if the value is 'random'
  22. or the key does not exist."""
  23. val = None
  24. try:
  25. val = super(RandomColor, self).__getitem__(key)
  26. except:
  27. # catch nonexistent keys and return a random value
  28. val = 'random'
  29.  
  30. if val == 'random':
  31. return fg(self.random.choice(self.choices))
  32. # could add other attributes or bg colors here but might be too much
  33. return val
  34. ### end RandomColorDict
  35.  
  36.  
  37. # sample use
  38. from colored import fg, bg, attr
  39. colors = list(range(19,231))
  40.  
  41. # define the theme
  42. mytheme = RandomColorDict( colors, {
  43. 'ui_info': fg('blue'), # UI (info msg)
  44. 'ui_error': fg('red')+attr('bold'), # UI (error msg)
  45. 'user_display': 'random', # user detail
  46. 'spoiler': fg('red'), # spoiler text
  47. 'timestamp': attr('dim'), # timestamps (general use)
  48. 'boosttoot': 'random', # timeline (header & content)
  49. 'toot_name': 'random', # timeline (name line)
  50. 'toot_content': fg('grey_82'), # timeline (content, summary)
  51. 'toot_reply': fg('blue'), # timeline (reply header)
  52. 'vis': fg('blue'), # timeline (id line)
  53. 'counts': fg('cyan'), # timeline (id line)
  54. 'id': fg('yellow'), # timeline (id line)
  55. 'mention': fg('magenta_2b'), # notifications
  56. 'favourite': fg('sea_green_2'), # notifications
  57. 'reblog': fg('yellow_2'), # notifications
  58. 'follow': fg('red_1'), # notifications
  59. # ... etc ...
  60. })
  61.  
  62. # now print the pieces
  63. # first line -- random name color, dimmed timestamp
  64. print( stylize(display_name, mytheme['toot_name']) + stylize(timestamp, mytheme['timestamp']) )
  65.  
  66. # second line -- cyan, yellow, blue
  67. print( stylize(counts, mytheme['counts']) + stylize(id, mytheme['id']) + stylize(vis, mytheme['vis']) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement