Skip to content

Commit c89e58d

Browse files
committed
Fix up a lot of small issues in the tutorial.
[skip ci]
1 parent e244c35 commit c89e58d

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

docs/source/tutorials/prettyprinter.rst

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ The first step is to get a script up to accept a path from the command line.
3838
if __name__ == '__main__':
3939
main()
4040

41-
Now we need to use sc2reader to read that file into a :class:`Replay` object that contains all the information that we are looking for.
41+
Now we need to use sc2reader to read that file into a :class:`~sc2reader.resources.Replay` object that contains all the information that we are looking for.
4242

4343
::
4444

45-
from sc2reader import SC2Reader
45+
from sc2reader.factories import SC2Factory
4646

4747
def main():
4848
path = sys.argv[1]
49-
sc2 = SC2Reader()
49+
sc2 = SC2Factory()
5050
replay = sc2.load_replay(path)
5151

52-
In the code above, we imported the :class:`SC2Reader` class from the sc2reader package. This class is a factory class that is used to load replays. This factory is configurable in a variety of ways but sane defaults are provided so that most users probably won't need to do any substantial configuration. In fact, because many users will never need to configure the SC2Reader factory the package provides a default factory that can be used by operating directly on the sc2reader package.
52+
In the code above, we imported the :class:`~sc2reader.factories.SC2Factory` class from the ``sc2reader.factories`` package. This class is a factory class that is used to load replays. This factory is configurable in a variety of ways but sane defaults are provided so that most users probably won't need to do any substantial configuration. In fact, because many users will never need to configure the SC2Factory the package provides a default factory that can be used by operating directly on the sc2reader package.
5353

5454
::
5555

@@ -61,7 +61,7 @@ In the code above, we imported the :class:`SC2Reader` class from the sc2reader p
6161

6262
We'll use this short hand method for the rest of this tutorial.
6363

64-
The replay object itself is a dumb data structure; there are no access methods only attributes. For our script we will need the following set of attributes, a full list of attributes can be found on the :class:`Replay` reference page.
64+
The replay object itself is a dumb data structure; there are no access methods only attributes. For our script we will need the following set of attributes, a full list of attributes can be found on the :class:`~sc2reader.resources.Replay` reference page.
6565

6666
::
6767

@@ -71,13 +71,13 @@ The replay object itself is a dumb data structure; there are no access methods o
7171
'1.4.0.19679'
7272
>>> replay.category
7373
'Ladder'
74-
>>> replay.date
74+
>>> replay.end_time
7575
datetime.datetime(2011, 9, 20, 21, 8, 8)
7676
>>> replay.type
7777
'2v2'
7878
>>> replay.map_name
7979
'The Boneyard'
80-
>>> replay.length # string format is MM.SS
80+
>>> replay.game_length # string format is MM.SS
8181
Length(0, 1032)
8282
>>> replay.teams
8383
[<sc2reader.objects.Team object at 0x2b5e410>, <sc2reader.objects.Team object at 0x2b5e4d0>]
@@ -88,7 +88,7 @@ Many of the replay attributes are nested data structures which are generally all
8888

8989
>>> replay.teams[0].players[0].color.hex
9090
'B4141E'
91-
>>> replay.player['Remedy'].url
91+
>>> replay.player.name('Remedy').url
9292
'http://us.battle.net/sc2/en/profile/2198663/1/Remedy/'
9393

9494
Each of these nested structures can be found either on its own reference page or lumped together with the other minor structures on the Misc Structures page.
@@ -103,13 +103,13 @@ So now all we need to do is build the ouput using the available replay attribute
103103
{filename}
104104
--------------------------------------------
105105
SC2 Version {release_string}
106-
{category} Game, {date}
106+
{category} Game, {start_time}
107107
{type} on {map_name}
108-
Length: {length}
108+
Length: {game_length}
109109

110110
""".format(**replay.__dict__)
111111

112-
In the code above we are taking advantage of the dump data structure design of the :class:`Replay` objects and unpacking its internal dictionary into the string.format method. If you aren't familiar with some of these concepts they are well worth reading up on; string templates are awesome!
112+
In the code above we are taking advantage of the dump data structure design of the :class:`~sc2reader.resources.Replay` objects and unpacking its internal dictionary into the ``string.format`` method. If you aren't familiar with some of these concepts they are well worth reading up on; string templates are awesome!
113113

114114
Similar formatting written in a more verbose and less pythonic way:
115115

@@ -119,12 +119,12 @@ Similar formatting written in a more verbose and less pythonic way:
119119
output = replay.filename+'\n'
120120
output += "--------------------------------------------\n"
121121
output += "SC2 Version "+replay.release_string+'\n'
122-
output += replay.category+" Game, "+str(replay.date)+'\n'
122+
output += replay.category+" Game, "+str(replay.start_time)+'\n'
123123
output += replay.type+" on "+replay.map_name+'\n'
124-
output += "Length: "+str(replay.length)
124+
output += "Length: "+str(replay.game_length)
125125
return output
126126

127-
Next we need to format the teams for display. The :class:`Team` and :class:`Player` data structures are pretty straightforward as well so we can apply a bit of string formatting and produce this:
127+
Next we need to format the teams for display. The :class:`~sc2reader.objects.Team` and :class:`~sc2reader.objects.Player` data structures are pretty straightforward as well so we can apply a bit of string formatting and produce this:
128128

129129
::
130130

@@ -162,9 +162,9 @@ So lets put it all together into the final script, ``prettyPrinter.py``:
162162
{filename}
163163
--------------------------------------------
164164
SC2 Version {release_string}
165-
{category} Game, {date}
165+
{category} Game, {start_time}
166166
{type} on {map_name}
167-
Length: {length}
167+
Length: {game_length}
168168

169169
{formattedTeams}
170170
""".format(formattedTeams=formatTeams(replay), **replay.__dict__)
@@ -182,10 +182,10 @@ So lets put it all together into the final script, ``prettyPrinter.py``:
182182
Making Improvements
183183
---------------------------
184184

185-
So our script works fine for single files, but what if you want to handle multiple files or directories? sc2reader provides two functions for loading replays: :meth:`~SC2Reader.load_replay` and :meth:`~SC2Reader.load_replays` which return a single replay and a list respectively. :meth:`~SC2Reader.load_replay` was used above for convenience but :meth:`~SC2Reader.load_replays` is much more versitile. Here's the difference:
185+
So our script works fine for single files, but what if you want to handle multiple files or directories? sc2reader provides two functions for loading replays: :meth:`~sc2reader.factories.SC2Factory.load_replay` and :meth:`~sc2reader.factories.SC2Factory.load_replays` which return a single replay and a list respectively. :meth:`~sc2reader.factories.SC2Factory.load_replay` was used above for convenience but :meth:`~sc2reader.factories.SC2Factory.load_replays` is much more versitile. Here's the difference:
186186

187-
* :meth:`~SC2Reader.load_replay`: accepts a file path or an opened file object.
188-
* :meth:`~SC2Reader.load_replays`: accepts a collection of opened file objects or a path (both directory and file paths acceptable) or a collection of paths. You could even pass in a collection of mixed paths and opened file objects.
187+
* :meth:`~sc2reader.factories.SC2Factory.load_replay`: accepts a file path or an opened file object.
188+
* :meth:`~sc2reader.factories.SC2Factory.load_replays`: accepts a collection of opened file objects or file paths. Can also accept a single path to a directory; files will be pulled from the directory using :func:`~sc2reader.utils.get_files` and the given options.
189189

190190
With this in mind, lets make a slight change to our main function to support any number of paths to any combination of files and directories:
191191

@@ -203,13 +203,13 @@ Any time that you start dealing with directories or collections of files you run
203203
* exclude: Default []. When recursing directories you can choose to exclude directories from the file search by directory name (not full path).
204204
* followlinks: Default false. When recursing directories, enables or disables the follow symlinks behavior.
205205

206-
Remember above that the short hand notation that we have been using works with a default instance of the SC2Reader class. :class:`SC2Reader` factories can be customized either on construction or with the :meth:`~SC2Reader.configure` method.
206+
Remember above that the short hand notation that we have been using works with a default instance of the SC2Factory class. :class:`sc2reader.factories.SC2Factory` objects can be customized either on construction or with the :meth:`~sc2reader.factories.SC2Factory.configure` method.
207207

208208
::
209209

210-
from sc2reader import SC2Reader
210+
from sc2reader.factories import SC2Factory
211211

212-
sc2 = SC2Reader(
212+
sc2 = SC2Factory(
213213
directory='~/Documents/Starcraft II/Multiplayer/Replays',
214214
exclude=['Customs','Pros'],
215215
followlinks=True
@@ -243,6 +243,7 @@ There are 4 available load levels:
243243
* 0: Parses the replay header for version, build, and length information
244244
* 1: Also parses the replay.details, replay.attribute.events and replay.initData files for game settings, map, and time information
245245
* 2: Also parses the replay.message.events file and constructs game teams and players.
246-
* 3: Also parses the replay.game.events file for player action events.
246+
* 3: Also parses the replay.tracker.events file
247+
* 4: Also parses the replay.game.events file for player action events.
247248

248-
So that's it! An ideal prettyPrinter script might let the user configure these options as arguments using the argparse library. Such an expansion is beyond the scope of sc2reader though, so we'll leave it that one to you.
249+
So that's it! An ideal prettyPrinter script might let the user configure these options as arguments using the ``argparse`` library. Such an expansion is beyond the scope of sc2reader though, so we'll leave it that one to you.

0 commit comments

Comments
 (0)