Created
November 8, 2011 23:09
-
-
Save martinusso/1349619 to your computer and use it in GitHub Desktop.
Just for fun #1 - random string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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