File: text_randomcase.py

package info (click to toggle)
inkscape 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,944 kB
  • sloc: cpp: 532,425; ansic: 59,393; python: 51,398; javascript: 3,864; xml: 2,708; sh: 2,426; perl: 614; makefile: 431
file content (32 lines) | stat: -rwxr-xr-x 859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
"""Randomise the case of the letters."""

import random
import inkex


class RandomCase(inkex.TextExtension):
    """Randomise the case of the text (with bias)"""

    previous_case = 1

    def map_char(self, char):
        # bias the randomness towards inversion of the previous case:
        # We use this weird way to get from a random set because
        # python2 and python3 have different ways of seeding
        if self.previous_case > 0:
            case = [-2, -1, 1][int(random.random() * 3)]
        else:
            case = [-1, 1, 2][int(random.random() * 3)]

        if char.isalpha():
            self.previous_case = case
            if case > 0:
                return char.upper()
            elif case < 0:
                return char.lower()
        return char


if __name__ == "__main__":
    RandomCase().run()