-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimportexport.py
More file actions
62 lines (54 loc) · 2.56 KB
/
importexport.py
File metadata and controls
62 lines (54 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import csv
from hangman import Hangman, HangmanResult
from player import Player
class CsvImport:
@classmethod
def import_words(cls, filepath):
with open(filepath) as source:
words_to_import = csv.DictReader(source)
for word in words_to_import:
Hangman.add_word(word['word'].upper())
@classmethod
def import_players(cls, filepath):
with open(filepath) as source:
players_to_import = csv.DictReader(source)
for player in players_to_import:
Player.add_new_player(player['nick'])
@classmethod
def import_results(cls, filepath):
with open(filepath) as source:
results_to_import = csv.DictReader(source)
for result in results_to_import:
player = Player.get_player(result['player_nick'])
HangmanResult.add_result(result['word_to_guess'], result['lives'], result['attempts'], player)
@classmethod
def all_imports(cls, words_file_path='Data/words.csv', player_file_path='Data/players.csv', results_file_path='Data/results.csv'):
cls.import_words(words_file_path)
cls.import_players(player_file_path)
cls.import_results(results_file_path)
@classmethod
def export_players(cls, filepath):
with open(filepath, 'w') as player_csv:
fieldnames = ['nick']
player_writer = csv.DictWriter(player_csv, fieldnames)
player_writer.writeheader()
player_list = Player.get_players_list()
for player in player_list:
player_dict = {'nick': player.get_nick()}
player_writer.writerow(player_dict)
@classmethod
def export_results(cls, filepath):
with open(filepath, 'w') as results_csv:
fieldnames = ['player_nick', 'word_to_guess', 'lives', 'attempts']
results_writer = csv.DictWriter(results_csv, fieldnames)
results_writer.writeheader()
results_list = HangmanResult.get_results()
for result in results_list:
player_nick = result.get_player().get_nick()
results_dict = {'player_nick': player_nick, 'word_to_guess': result.get_word_to_guess(),
'lives': result.get_saved_lives(), 'attempts': result.get_attempts()}
results_writer.writerow(results_dict)
@classmethod
def all_exports(cls, player_file_path='Data/players.csv', results_file_path='Data/results.csv'):
cls.export_players(player_file_path)
cls.export_results(results_file_path)