Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Python 3
- # Randomly Draw Players
- import sys
- import pathlib
- import random
- if len(sys.argv) < 2:
- raise Exception("Missing argument: input_file_name")
- script_file_name = sys.argv[0]
- input_file_path = pathlib.Path(sys.argv[1])
- with open(file=str(input_file_path.absolute()),
- mode='r',
- buffering=-1,
- encoding='utf-8'
- ) as input_file: # {
- players_dict = dict()
- input_file.lineno = 0
- for input_line in input_file: # {
- input_file.lineno += 1
- try:
- player_name, player_id = input_line.strip().split('\t')
- except ValueError:
- raise Exception("File error {!r} at line {}!".format( input_file_path.name, input_file.lineno ))
- else:
- players_dict.update({player_name: player_id})
- # }
- # } with open
- if not len(players_dict):
- raise Exception("Missing players")
- output_file_path = input_file_path
- i = 0
- while output_file_path.exists(): # { Find an no-existing file name
- i += 1
- output_file_path = pathlib.Path( # Folder + name_base + name_extension
- input_file_path.parent,
- input_file_path.stem + " output {}".format(i) + input_file_path.suffix
- )
- # }
- with open(file=str(output_file_path.absolute()),
- mode='w',
- buffering=-1,
- encoding='utf-8'
- ) as output_file: # {
- while len(players_dict): # { Extract randomly each players
- player_chosen_name = random.choice(list(players_dict.keys())) # Choose randomly a player name
- player_chosen_id = players_dict.pop(player_chosen_name) # Extract and remove from dictionary
- output_file.writelines( "{}\t{}\n".format(player_chosen_name, player_chosen_id) )
- # }
- # } with open
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement