11"""
2- Functions for converting between ways of storing games.
2+ Utilities for converting between representations of games.
3+
4+ Currently supports reading and writing the GameTracer `.gam` text format
5+ [1]_.
36
47Examples
58--------
6-
7- Create a QuantEcon NormalFormGame from a .gam file storing
8- a 3-player Minimum Effort Game
9+ Create a QuantEcon NormalFormGame from a .gam file storing a 3-player
10+ Minimum Effort Game:
911
1012>>> import os
1113>>> import quantecon.game_theory as gt
1214>>> filepath = os.path.dirname(gt.__file__)
13- >>> filepath += "/tests/game_files/minimum_effort_game.gam"
15+ >>> filepath = os.path.join(filepath, 'tests', 'game_files',
16+ ... 'minimum_effort_game.gam')
1417>>> nfg = gt.from_gam(filepath)
1518>>> print(nfg)
16193-player NormalFormGame with payoff profile array:
2629 [[-19., -9., 1.], [ -8., 2., 2.], [ -8., 2., -8.]],
2730 [[-19., -19., 1.], [ -8., -8., 2.], [ 3., 3., 3.]]]]
2831
32+ References
33+ ----------
34+ .. [1] Ben Blum, Daphne Koller, Christian Shelton, "Game Theory:
35+ GameTracer," http://dags.stanford.edu/Games/gametracer.html
36+
2937"""
3038import io
3139import sys
@@ -169,6 +177,7 @@ def _str2num(s):
169177 -------
170178 int or float
171179 Integer if no decimal point, otherwise float.
180+
172181 """
173182 if '.' in s :
174183 return float (s )
@@ -177,46 +186,14 @@ def _str2num(s):
177186
178187class GAMReader :
179188 """
180- Reader object that converts a game in GameTracer .gam format into
181- a NormalFormGame.
189+ Parser for the GameTracer .gam format.
182190
183191 """
184192 @classmethod
185193 def from_file (cls , file_path ):
186194 """
187195 Read from a .gam format file.
188196
189- Parameters
190- ----------
191- file_path : str
192- Path to .gam file.
193-
194- Returns
195- -------
196- NormalFormGame
197-
198- Examples
199- --------
200- Save a .gam format string in a temporary file:
201-
202- >>> import tempfile
203- >>> fname = tempfile.mkstemp()[1]
204- >>> with open(fname, mode='w') as f:
205- ... f.write(\" \" \" \\
206- ... 2
207- ... 3 2
208- ...
209- ... 3 2 0 3 5 6 3 2 3 2 6 1\" \" \" )
210-
211- Read the file:
212-
213- >>> g = GAMReader.from_file(fname)
214- >>> print(g)
215- 2-player NormalFormGame with payoff profile array:
216- [[[3, 3], [3, 2]],
217- [[2, 2], [5, 6]],
218- [[0, 3], [6, 1]]]
219-
220197 """
221198 with open (file_path , 'r' ) as f :
222199 string = f .read ()
@@ -238,29 +215,6 @@ def from_string(cls, string):
238215 """
239216 Read from a .gam format string.
240217
241- Parameters
242- ----------
243- string : str
244- String in .gam format.
245-
246- Returns
247- -------
248- NormalFormGame
249-
250- Examples
251- --------
252- >>> string = \" \" \" \\
253- ... 2
254- ... 3 2
255- ...
256- ... 3 2 0 3 5 6 3 2 3 2 6 1\" \" \"
257- >>> g = GAMReader.from_string(string)
258- >>> print(g)
259- 2-player NormalFormGame with payoff profile array:
260- [[[3, 3], [3, 2]],
261- [[2, 2], [5, 6]],
262- [[0, 3], [6, 1]]]
263-
264218 """
265219 return cls ._parse (string )
266220
@@ -304,22 +258,13 @@ def _parse(string):
304258
305259class GAMWriter :
306260 """
307- Writer object that converts a NormalFormgame into a game in
308- GameTracer .gam format.
261+ Serializer for the GameTracer .gam format.
309262
310263 """
311264 @classmethod
312265 def to_file (cls , g , file_path ):
313266 """
314- Save the GameTracer .gam format string representation of the
315- NormalFormGame `g` to a file.
316-
317- Parameters
318- ----------
319- g : NormalFormGame
320-
321- file_path : str
322- Path to the file to write to.
267+ Write `g` to a file in GameTracer .gam` format.
323268
324269 """
325270 with open (file_path , 'w' ) as f :
@@ -328,17 +273,7 @@ def to_file(cls, g, file_path):
328273 @classmethod
329274 def to_string (cls , g ):
330275 """
331- Return a GameTracer .gam format string representing the
332- NormalFormGame `g`.
333-
334- Parameters
335- ----------
336- g : NormalFormGame
337-
338- Returns
339- -------
340- str
341- String representation in .gam format.
276+ Return the GameTracer ``.gam`` string representation of `g`.
342277
343278 """
344279 return cls ._dump (g )
@@ -370,34 +305,91 @@ def _dump(g):
370305
371306def from_gam (filename : str ) -> NormalFormGame :
372307 """
373- Makes a QuantEcon Normal Form Game from a .gam file.
374-
375- Gam files are described by GameTracer [1]_.
308+ Read a GameTracer .gam file and return a NormalFormGame.
376309
377310 Parameters
378311 ----------
379312 filename : str
380- path to .gam file.
313+ Path to .gam file.
381314
382315 Returns
383316 -------
384317 NormalFormGame
385- The QuantEcon Normal Form Game described by the .gam file.
386-
387- References
388- ----------
389- .. [1] Bem Blum, Daphne Kohler, Christian Shelton
390- http://dags.stanford.edu/Games/gametracer.html
318+ The game described by the .gam file.
319+
320+ Examples
321+ --------
322+ Save a .gam format string in a temporary file:
323+
324+ >>> import tempfile
325+ >>> fname = tempfile.mkstemp()[1]
326+ >>> with open(fname, mode='w') as f:
327+ ... _ = f.write(\" \" \" \\
328+ ... 2
329+ ... 3 2
330+ ...
331+ ... 3 2 0 3 5 6 3 2 3 2 6 1\" \" \" )
332+
333+ Read the file:
334+
335+ >>> g = from_gam(fname)
336+ >>> print(g)
337+ 2-player NormalFormGame with payoff profile array:
338+ [[[3, 3], [3, 2]],
339+ [[2, 2], [5, 6]],
340+ [[0, 3], [6, 1]]]
391341
392342 """
393343 return GAMReader .from_file (filename )
394344
395345
396346def from_gam_string (string ):
347+ """
348+ Read a .gam format string and return a NormalFormGame.
349+
350+ Parameters
351+ ----------
352+ string : str
353+ String in .gam format.
354+
355+ Returns
356+ -------
357+ NormalFormGame
358+ The game described by the .gam string.
359+
360+ Examples
361+ --------
362+ >>> string = \" \" \" \\
363+ ... 2
364+ ... 3 2
365+ ...
366+ ... 3 2 0 3 5 6 3 2 3 2 6 1\" \" \"
367+ >>> g = from_gam_string(string)
368+ >>> print(g)
369+ 2-player NormalFormGame with payoff profile array:
370+ [[[3, 3], [3, 2]],
371+ [[2, 2], [5, 6]],
372+ [[0, 3], [6, 1]]]
373+
374+ """
397375 return GAMReader .from_string (string )
398376
399377
400378def from_gam_url (url ):
379+ """
380+ Read a GameTracer .gam file from a URL and return a NormalFormGame.
381+
382+ Parameters
383+ ----------
384+ url : str
385+ String containing a URL of the .gam file.
386+
387+ Returns
388+ -------
389+ NormalFormGame
390+ The game described by the .gam file.
391+
392+ """
401393 return GAMReader .from_url (url )
402394
403395
@@ -413,6 +405,10 @@ def to_gam(g, file_path=None):
413405 Path to the file to write to. If None, the result is returned as
414406 a string.
415407
408+ Returns
409+ -------
410+ None or str
411+
416412 """
417413 if file_path is None :
418414 return GAMWriter .to_string (g )
0 commit comments