Skip to content

Instantly share code, notes, and snippets.

@martinusso
Created November 8, 2011 23:09
Show Gist options
  • Save martinusso/1349619 to your computer and use it in GitHub Desktop.
Save martinusso/1349619 to your computer and use it in GitHub Desktop.
Just for fun #1 - random string
# -*- coding: utf-8 -*-
import random
class RandomString:
__ALPHABETIC = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ'
__NUMBERS = '0123456789'
def random_char(self, allow_numbers = True):
return self.random_string(1, allow_numbers)
def random_string(self, lenght, allow_numbers = True):
text = self.__get_text(allow_numbers)
return ''.join(random.choice(text) for x in range(lenght))
def __get_text(self, allow_numbers):
text = self.__ALPHABETIC
if allow_numbers:
text = text.join(self.__NUMBERS)
return text
# demo
rs = RandomString()
print rs.random_char()
print rs.random_char(False)
print rs.random_string(5)
print rs.random_string(5, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment