Coverage for gpaw/quiz.py: 40%

53 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-07-19 00:19 +0000

1"""Developer quiz.""" 

2from __future__ import annotations 

3 

4import contextlib 

5import io 

6from dataclasses import dataclass 

7from random import shuffle 

8from textwrap import wrap 

9 

10with contextlib.redirect_stdout(io.StringIO()): 

11 from this import d 

12 

13 

14@dataclass 

15class Question: 

16 text: str 

17 choises: list[str] | None = None 

18 solutions: list[str] | None = None 

19 shuffle: bool = True 

20 extra_check: bool = False 

21 

22 def ask(self) -> bool: 

23 """Ask question and check result.""" 

24 print() 

25 text = self.text.rstrip('?') + '?' 

26 for line in text.splitlines(): 

27 print('\n'.join(wrap(line))) 

28 if self.choises: 

29 N = len(self.choises) 

30 indices = list(range(N)) 

31 if self.shuffle: 

32 shuffle(indices) 

33 i0 = len(self.text) - len(text) 

34 n0 = indices.index(i0) + 1 

35 for n, i in enumerate(indices, start=1): 

36 print(f'{n}: {self.choises[i]}') 

37 answer = input(f'[1-{N}]: ') 

38 if answer != str(n0): 

39 return False 

40 if self.extra_check: 

41 return Question(text='Really?', choises=['yes', 'no']).ask() 

42 return True 

43 

44 answer = input() 

45 return self.solutions is None or encode(answer) in self.solutions 

46 

47 

48def encode(text: str) -> str: 

49 """See "The Zen of Python" (this.py).""" 

50 return ''.join(d.get(c, c) for c in text) 

51 

52 

53dev = 'https://gpaw.readthedocs.io/devel' 

54 

55questions = [ 

56 Question(text='What, ... is your name?'), 

57 Question(text='What, ... is your quest?', 

58 choises=['To become a GPAW-developer']), 

59 Question(text='What, ... is your favourite programming language?', 

60 choises=['Python', 'Other'], 

61 shuffle=False), 

62 Question('What, ... is the air-speed velocity of an unladen swallow???', 

63 choises=['10 m/s', 

64 '20 miles an hour', 

65 'depends on the kind of swallow (African or European)']), 

66 Question(text='Here is some important developer information:\n\n' 

67 f' {dev}/workflow.html\n' 

68 f' {dev}/testing.html\n' 

69 f' {dev}/writing_documentation.html\n\n' 

70 'Did you read all of these three pages??', 

71 choises=['no', 'yes'], 

72 extra_check=True), 

73 Question(text='What framework does GPAW use for its test suite??', 

74 choises=['home-made system', 

75 'pytest', 

76 'unittest']), 

77 Question(text='What is the name of the environment variable that points ' 

78 'at the folder(s) where PAW dataset files are stores?', 

79 solutions=['TCNJ_FRGHC_CNGU', '$TCNJ_FRGHC_CNGU'])] 

80 

81 

82def main() -> None: 

83 """Ask questions.""" 

84 print('To become a gpaw developer, you must answer these ' 

85 f'{len(questions)} questions ...') 

86 shuffle(questions) 

87 for question in questions: 

88 ok = question.ask() 

89 if ok: 

90 print('\033[92mCorrect!\033[0m') 

91 else: 

92 print('\033[91mAuuuuuuuugh!\033[0m') 

93 return 

94 print('\nRight. Off you go.') 

95 

96 

97if __name__ == '__main__': 

98 main()