-
-
Save OriPekelman/eb0e7a3fee954b922d8c to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
# coding=UTF-8 | |
import re | |
import unicodedata | |
import string | |
import random | |
first_cap_re = re.compile('(.)([A-Z][a-z]+)') | |
all_cap_re = re.compile('([a-z0-9])([A-Z])') | |
def slugify(value): | |
""" | |
Convert to ASCII convert spaces to hyphens. Remove characters that aren't | |
alphanumerics, or hyphens. Convert to lowercase. Camel case | |
to hyphen. Also strip leading and trailing whitespace. | |
""" | |
# normalized unicode to ascii | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') | |
#CamelCase to hypens | |
value = de_camel_case(value) | |
#remove all non alphanumeric and hypen, strip whitespace and make lower | |
value = re.sub('[^a-zA-Z0-9-]', '', value) | |
#remove any double hyphens, we use those in mangling | |
value = re.sub('--', '', value) | |
#remove leading and trailing hyphen | |
return value.strip("-") | |
def de_camel_case(value): | |
s1 = first_cap_re.sub(r'\1-\2', value) | |
return all_cap_re.sub(r'\1-\2', s1).lower() | |
def id_generator(size=3, chars=string.ascii_lowercase + string.digits): | |
return ''.join(random.choice(chars) for _ in range(size)) | |
def slug(value): | |
return (slugify(value) + "-" + id_generator()).strip("-") | |
print(slug(u"/a/")) | |
print(slug(u":")) | |
print(slug(u"")) | |
print(slug(u"-")) | |
print(slug(u" -changeMe or don't \ | • We-🚔A.re/You/âé--ï_$#@% -")) | |
print(slug(u"--- -changeMe or don't \ | • We-🚔A.re/You/âé--ï_$#@% -| - - - --- ---- --")) | |
print(slug(u" ChangeMe --- ChangeMe ")) | |
print(slug(u"-changeMe or don't \ | • We-🚔A.re/You/âé-ï_$#@%-")) | |
#which gives us: | |
#a-8z9 | |
#5b2 | |
#por | |
#3m5 | |
#change-meordont-we-are-youaei-hvb | |
#change-meordont-we-are-youaei-m5k | |
#change-mechange-me-12v | |
#change-meordont-we-are-youae-i-5rq |
Or maybe we can go heroku style... and generate nice names like
# coding=UTF-8
import random
def random_names():
attributes = "active", "adaptable", "adventurous", "affable", "affectionate", "agreeable", "ambitious", "amiable", "amicable", "amusing", "balanced", "brave", "bright", "broad-minded", "calm", "candid", "careful", "charming", "cheerful", "communicative", "compassionate", "competitive", "conscientious", "considerate", "consistent", "convivial", "cooperative", "courageous", "courteous", "creative", "curious", "decisive", "determined", "devoted", "diligent", "diplomatic", "discreet", "dynamic", "easy going", "easygoing", "emotional", "energetic", "enterprising", "enthusiastic", "entrepreneurial", "exciting", "exuberant", "facilitator", "fair-minded", "faithful", "fast", "fearless", "flexible", "focused", "forceful", "forgiving", "frank", "friendly", "funny", "generous", "gentle", "genuine", "good", "gregarious", "hard-working", "helpful", "honest", "humorous", "imaginative", "impartial", "incredible", "independent", "industrious", "initiator", "insightful", "intellectual", "intelligent", "interesting", "intuitive", "inventive", "kind", "knowledgeable", "literate", "logical", "loving", "loyal", "mediator", "meditative", "modest", "neat", "nice", "open minded", "optimistic", "organised", "organized", "original", "outgoing", "particular", "passionate", "patient", "perceptive", "persistent", "personable", "persuasive", "philosophical", "pioneering", "placid", "pleasant", "plucky", "polite", "positive", "powerful", "practical", "pro-active", "proactive", "productive", "professional", "quick", "quick-witted", "quiet", "quirky", "quixotic", "racy", "rational", "rebellious", "reliable", "reserved", "resourceful", "responsible", "romantic", "sensible", "sensitive", "sensuous", "shy", "sincere", "skilled", "sociable", "solid", "sporty", "straightforward", "sympathetic", "thoughtful", "tidy", "tough", "trustworthy", "unassuming", "understanding", "versatile", "warm", "warmhearted", "willing", "wise", "witty",
animal_modifiers = "white", "black", "gray", "blue", "green", "red", "yellow", "brown", "striped", "common", "rare","mountain", "sea", "velvet", "tiny", "giant"
animals = "aardvark", "albatross", "alligator", "alpaca", "amphibian", "anaconda", "ant", "anteater", "antelope", "antlion", "ape", "aphid", "armadillo", "asp", "ass", "baboon", "badger", "bandicoot", "barnacle", "barracuda", "basilisk", "bass", "bat", "bear", "beaver", "bedbug", "bee", "beetle", "bird", "bison", "blackbird", "bluejay", "boar", "boa", "bobcat", "buffalo", "butterfly", "buzzard", "camel", "capybara", "cardinal", "caribou", "carp", "cassowary", "cat", "caterpillar", "catfish", "centipede", "chameleon", "chamois", "cheetah", "chickadee", "chicken", "chimpanzee", "chinchilla", "chipmunk", "chough", "clam", "clownfish", "coati", "cobra", "cockroach", "cod", "condor", "constrictor", "coral", "cormorant", "cougar", "coyote", "crab", "crane", "crawdad", "crayfish", "cricket", "crocodile", "crow", "cuckoo", "curlew", "damselfly", "deer", "dingo", "dinosaur", "dog", "dogfish", "dolphin", "donkey", "dormouse", "dotterel", "dove", "dragonfly", "duck", "dugong", "dung beetle", "dunlin", "eagle", "earthworm", "earwig", "echidna", "eel", "egret", "eland", "elephant", "elephant seal", "elk", "emu", "ermine", "f", "falcon", "ferret", "finch", "firefly", "fish", "flamingo", "flea", "fly", "flyingfish", "fox", "frog", "fruit bat", "gaur", "gazelle", "gecko", "gerbil", "giant panda", "giant squid", "gibbon", "gila monster", "giraffe", "gnat", "gnu", "goat", "goldfinch", "goldfish", "goose", "gopher", "gorilla", "goshawk", "grasshopper", "grizzly bear", "grouse", "guanaco", "guinea-fowl", "guinea pig", "gull", "guppy", "haddock", "halibut", "hamster", "hare", "hawk", "hedgehog", "hermit-crab", "heron", "herring", "hippopotamus", "hookworm", "hornet", "horse", "hummingbird", "hyena", "ibex", "ibis", "iguana", "impala", "jackal", "jaguar", "jay", "jellyfish", "kangaroo", "kingfisher", "kinkajou", "kiwi", "koala", "koi", "komodo", "kookabura", "kouprey", "krill", "kudu", "ladybug", "lamprey", "lapwing", "lark", "leech", "lemming", "lemur", "leopard", "lion", "lizard", "llama", "lobster", "locust", "loon", "loris", "louse", "lynx", "lyrebird", "macaw", "mackerel", "magpie", "mallard", "manatee", "mandrill", "manta-ray", "mantis", "marlin", "marmoset", "marmot", "marsupial", "marten", "meadowlark", "meerkat", "mink", "minnow", "mite", "mockingbird", "mole", "mollusk", "mongoose", "monkey", "moose", "mosquito", "moth", "mountain-goat", "mouse", "mule", "muskox", "narwhal", "newt", "nightingale", "ocelot", "octopus", "okapi", "opossum", "orangutan", "orca", "oryx", "ostrich", "otter", "owl", "oyster", "panda", "panther", "parrot", "partridge", "peafowl", "pelican", "penguin", "pheasant", "pig", "pigeon", "polar-bear", "pony", "porcupine", "porpoise", "prairie dog", "quail", "quelea", "quetzal", "rabbit", "raccoon", "rail", "ram", "rat", "raven", "reindeer", "rhinoceros", "rodent", "rook", "salamander", "salmon", "sand dollar", "sandpiper", "sardine", "scorpion", "seahorse", "seal", "shark", "sheep", "shrew", "skunk", "sloth", "snail", "snake", "sparrow", "spider", "spoonbill", "squid", "squirrel", "starling", "stingray", "stinkbug", "stork", "swallow", "swan", "tapir", "tarantula", "tarsier", "termite", "tiger", "toad", "trout", "turkey", "turtle", "vicuña", "viper", "vulture", "wallaby", "walrus", "wasp", "weasel", "whale", "wildcat", "wolf", "wolverine", "wombat", "woodcock", "woodpecker", "worm", "wren", "yak", "zebra"
return random.choice(attributes) + "-" + random.choice(animal_modifiers) + "-" + random.choice(animals)
for x in range(0, 50):
print random_names()
which gives us:
skilled-tiny-bobcat
sincere-red-elephant-seal
discreet-red-flea
active-red-mongoose
passionate-black-salamander
industrious-velvet-mongoose
straightforward-brown-eland
conscientious-giant-amphibian
original-rare-porcupine
amiable-striped-sparrow
straightforward-blue-giant panda
frank-tiny-gull
considerate-gray-jackal
reserved-yellow-camel
particular-yellow-wolf
fair-minded-sea-elephant-seal
fearless-brown-gila-monster
careful-common-fox
impartial-black-ocelot
original-mountain-turtle
rebellious-tiny-gerbil
modest-black-grouse
romantic-black-guinea-fowl
logical-white-beetle
racy-black-lynx
quiet-tiny-mandrill
loyal-blue-alligator
balanced-gray-rodent
incredible-green-camel
witty-white-mantis
neat-red-iguana
placid-brown-hookworm
persuasive-striped-carp
competitive-common-donkey
proactive-gray-orca
facilitator-black-viper
warmhearted-gray-chicken
solid-giant-porcupine
emotional-yellow-swan
meditative-blue-minnow
enterprising-black-wildcat
candid-green-tarsier
rebellious-blue-mockingbird
courageous-gray-chicken
bright-common-horse
good-rare-leopard
inventive-gray-mallard
ambitious-striped-mandrill
considerate-red-owl
determined-black-wildcat
And why not instead of random chars random stars.