Skip to content

Instantly share code, notes, and snippets.

@mattcarp
Created July 24, 2012 22:09
Show Gist options
  • Select an option

  • Save mattcarp/3173004 to your computer and use it in GitHub Desktop.

Select an option

Save mattcarp/3173004 to your computer and use it in GitHub Desktop.
Generates a random length string of non-control Unicode characters
#!/usr/bin/python
import random
import unicodedata
def unicode_fuzz(lower_limit=0, upper_limit=60):
unicode_glyphs = ''.join(
unichr(char)
for char in xrange(65533)
# use the unicode categories that don't include control codes
if unicodedata.category(unichr(char))[0] in ('LMNPSZ')
)
rand_length = random.randint(lower_limit, upper_limit)
# generate it
utf_string = ''.join([random.choice(unicode_glyphs).encode('utf-8')
for i in xrange(rand_length)])
return utf_string
# call it with some upper and lower string lengths, or use the defaults
# print unicode_fuzz(0, 250)
@FooqX

FooqX commented Sep 27, 2021

Copy link
Copy Markdown

This doesn't work! I tried fixing it but it didn't output anything. Here is my fixed version:

from random import randint, choice
from unicodedata import category

import pyparsing
from cffi.backend_ctypes import xrange


def gen_unicode(lower_limit, upper_limit):
    unicode_glyphs = ''.join(
        pyparsing.unichr(char) for char in xrange(65533) if category(pyparsing.unichr(char))[0] in 'LMNPSZ')

    rand_length = randint(lower_limit, upper_limit)
    utf_string = ''.join([choice(unicode_glyphs).encode('utf-8') for _ in xrange(rand_length)])

    return utf_string


print(gen_unicode(0, 30))

@VBPROGER

VBPROGER commented Apr 7, 2022

Copy link
Copy Markdown

This doesn't work! I tried fixing it but it didn't output anything. Here is my fixed version:

from random import randint, choice
from unicodedata import category

import pyparsing
from cffi.backend_ctypes import xrange


def gen_unicode(lower_limit, upper_limit):
    unicode_glyphs = ''.join(
        pyparsing.unichr(char) for char in xrange(65533) if category(pyparsing.unichr(char))[0] in 'LMNPSZ')

    rand_length = randint(lower_limit, upper_limit)
    utf_string = ''.join([choice(unicode_glyphs).encode('utf-8') for _ in xrange(rand_length)])

    return utf_string


print(gen_unicode(0, 30))

Error: TypeError: sequence item 0: expected str instance, bytes found

@mattcarp

mattcarp commented Oct 11, 2022 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment