You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/tutorials/prettyprinter.rst
+25-24Lines changed: 25 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,18 +38,18 @@ The first step is to get a script up to accept a path from the command line.
38
38
if __name__ == '__main__':
39
39
main()
40
40
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.
42
42
43
43
::
44
44
45
-
from sc2reader import SC2Reader
45
+
from sc2reader.factories import SC2Factory
46
46
47
47
def main():
48
48
path = sys.argv[1]
49
-
sc2 = SC2Reader()
49
+
sc2 = SC2Factory()
50
50
replay = sc2.load_replay(path)
51
51
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.
53
53
54
54
::
55
55
@@ -61,7 +61,7 @@ In the code above, we imported the :class:`SC2Reader` class from the sc2reader p
61
61
62
62
We'll use this short hand method for the rest of this tutorial.
63
63
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.
65
65
66
66
::
67
67
@@ -71,13 +71,13 @@ The replay object itself is a dumb data structure; there are no access methods o
71
71
'1.4.0.19679'
72
72
>>> replay.category
73
73
'Ladder'
74
-
>>> replay.date
74
+
>>> replay.end_time
75
75
datetime.datetime(2011, 9, 20, 21, 8, 8)
76
76
>>> replay.type
77
77
'2v2'
78
78
>>> replay.map_name
79
79
'The Boneyard'
80
-
>>> replay.length # string format is MM.SS
80
+
>>> replay.game_length # string format is MM.SS
81
81
Length(0, 1032)
82
82
>>> replay.teams
83
83
[<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
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
103
103
{filename}
104
104
--------------------------------------------
105
105
SC2 Version {release_string}
106
-
{category} Game, {date}
106
+
{category} Game, {start_time}
107
107
{type} on {map_name}
108
-
Length: {length}
108
+
Length: {game_length}
109
109
110
110
""".format(**replay.__dict__)
111
111
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!
113
113
114
114
Similar formatting written in a more verbose and less pythonic way:
115
115
@@ -119,12 +119,12 @@ Similar formatting written in a more verbose and less pythonic way:
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:
128
128
129
129
::
130
130
@@ -162,9 +162,9 @@ So lets put it all together into the final script, ``prettyPrinter.py``:
@@ -182,10 +182,10 @@ So lets put it all together into the final script, ``prettyPrinter.py``:
182
182
Making Improvements
183
183
---------------------------
184
184
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:
186
186
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.
189
189
190
190
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:
191
191
@@ -203,13 +203,13 @@ Any time that you start dealing with directories or collections of files you run
203
203
* exclude: Default []. When recursing directories you can choose to exclude directories from the file search by directory name (not full path).
204
204
* followlinks: Default false. When recursing directories, enables or disables the follow symlinks behavior.
205
205
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.
@@ -243,6 +243,7 @@ There are 4 available load levels:
243
243
* 0: Parses the replay header for version, build, and length information
244
244
* 1: Also parses the replay.details, replay.attribute.events and replay.initData files for game settings, map, and time information
245
245
* 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.
247
248
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