Last active
August 29, 2015 13:57
-
-
Save junqueira/9919717 to your computer and use it in GitHub Desktop.
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 -*- | |
# create sorting python in list csv | |
from random import shuffle | |
import os | |
class DoorPrize(object): | |
def __init__(self, list_to_sort=[]): | |
self.list_to_sort = list(set(list_to_sort)) | |
self.winners = [] | |
def __getitem__(self, key): | |
return self.list_to_sort[key] | |
def __setitem__(self, key, value): | |
self.list_to_sort[key] = value | |
def __len__(self): | |
return len(self.list_to_sort) | |
def winner(self): | |
self.shuffle() | |
if not len(self.list_to_sort) == 0: | |
poped = self.list_to_sort.pop() | |
if not poped: return self.winner() | |
self.winners.append(poped) | |
return poped | |
def shuffle(self): | |
return shuffle(self.list_to_sort) | |
desc = '' | |
while not os.path.isfile(desc): | |
desc = raw_input('Qual o arquivo para sorteio ? => ') | |
with open(desc, 'rb') as ff: | |
contents = ff.read().split('\r\n') | |
#import pdb; pdb.set_trace() | |
try: | |
sorteio = DoorPrize(contents) | |
except: | |
pass | |
finally: | |
ff.close() | |
n = raw_input('Quantos sorteios ? => ') | |
while (not str(n).isdigit()) or (int(n) > len(sorteio)): | |
if str(n).isdigit() and int(n) > len(sorteio): | |
print "Lista contem somente %s item" % len(sorteio) | |
n = raw_input('Quantos sorteios ? => ') | |
for index in range(0, int(n) ): | |
sorteado = sorteio.winner() | |
if sorteado == None: break | |
print "O sorteado %s eh: %s" % (index + 1, sorteado) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment