from pprint import pprint
input = open('example_ignition.txt').read()
hands = input.split('\n\n\n')

class Hand:
    def __init__(self, se=None, p=None, f=None, t=None, r=None, su=None):
        self.seats = se
        self.preflop = p
        self.flop = f
        self.turn = t
        self.river = r
        self.summary = su
    def __repr__(self):
        return str(self.__dict__)

for i, h in enumerate(hands):
    segments = "seats preflop flop turn river".split()
    s = h.split('\n*** ')
    hands[i] = Hand()
    while len(s) > 1:
        # We don't always have flop, turn, riv, but last element is
        # always Summary.
        k = segments.pop(0)
        v = s.pop(0).splitlines()
        hands[i].__dict__[k] = v
    hands[i].summary = s.pop(0).splitlines()
    assert len(s) == 0

## [ { s:[] p:[] f:[] s:[] }  { s:[] p:[] f:[] t:[] r:[] s:[] }  {}  {} ]

print(hands[0])

from pprint import pprint

class Hand:
    def __init__(self, string):
        segments = "seats preflop flop turn river".split()
        self.seats = None
        self.preflop = None
        self.flop = None
        self.turn = None
        self.river = None
        self.summary = None
        ## step 2: split each hand into segments
        s = string.split('\n*** ')
        while len(s) > 1:
            # We don't always have flop, turn, riv, but last element is
            # always Summary.
            k = segments.pop(0)
            v = s.pop(0).splitlines()
            self.__dict__[k] = v
        ## step 3: split each segment into lines
        self.summary = s.pop(0).splitlines()
        assert len(s) == 0
    def __repr__(self):
        return str(self.__dict__)


## main

input = open('example_ignition.txt').read()

## step 1: split flat file into hands
hands = input.split('\n\n\n')

for i, h in enumerate(hands):
    hands[i] = Hand(h)

## [ { s:[] p:[] f:[] s:[] }  { s:[] p:[] f:[] t:[] r:[] s:[] }  {}  {} ]

print(hands[0])

Move parsing loop into the class itself.
Move parsing loop into the class itself.

  from pprint import pprint
- input = open('example_ignition.txt').read()
- hands = input.split('\n\n\n')
  
  class Hand:
-     def __init__(self, se=None, p=None, f=None, t=None, r=None, su=None):
+     def __init__(self, string):
+         segments = "seats preflop flop turn river".split()
-         self.seats = se
+         self.seats = None
-         self.preflop = p
+         self.preflop = None
-         self.flop = f
+         self.flop = None
-         self.turn = t
+         self.turn = None
-         self.river = r
+         self.river = None
-         self.summary = su
+         self.summary = None
+         ## step 2: split each hand into segments
+         s = string.split('\n*** ')
+         while len(s) > 1:
+             # We don't always have flop, turn, riv, but last element is
+             # always Summary.
+             k = segments.pop(0)
+             v = s.pop(0).splitlines()
+             self.__dict__[k] = v
+         ## step 3: split each segment into lines
+         self.summary = s.pop(0).splitlines()
+         assert len(s) == 0
      def __repr__(self):
          return str(self.__dict__)
  
+ 
+ ## main
+ 
+ input = open('example_ignition.txt').read()
+ 
+ ## step 1: split flat file into hands
+ hands = input.split('\n\n\n')
+ 
  for i, h in enumerate(hands):
-     segments = "seats preflop flop turn river".split()
-     s = h.split('\n*** ')
-     hands[i] = Hand()
+     hands[i] = Hand(h)
-     while len(s) > 1:
-         # We don't always have flop, turn, riv, but last element is
-         # always Summary.
-         k = segments.pop(0)
-         v = s.pop(0).splitlines()
-         hands[i].__dict__[k] = v
-     hands[i].summary = s.pop(0).splitlines()
-     assert len(s) == 0
  
  ## [ { s:[] p:[] f:[] s:[] }  { s:[] p:[] f:[] t:[] r:[] s:[] }  {}  {} ]
  
  print(hands[0])
  
Move parsing loop into the class itself.
## Code Before:
from pprint import pprint
input = open('example_ignition.txt').read()
hands = input.split('\n\n\n')

class Hand:
    def __init__(self, se=None, p=None, f=None, t=None, r=None, su=None):
        self.seats = se
        self.preflop = p
        self.flop = f
        self.turn = t
        self.river = r
        self.summary = su
    def __repr__(self):
        return str(self.__dict__)

for i, h in enumerate(hands):
    segments = "seats preflop flop turn river".split()
    s = h.split('\n*** ')
    hands[i] = Hand()
    while len(s) > 1:
        # We don't always have flop, turn, riv, but last element is
        # always Summary.
        k = segments.pop(0)
        v = s.pop(0).splitlines()
        hands[i].__dict__[k] = v
    hands[i].summary = s.pop(0).splitlines()
    assert len(s) == 0

## [ { s:[] p:[] f:[] s:[] }  { s:[] p:[] f:[] t:[] r:[] s:[] }  {}  {} ]

print(hands[0])

## Instruction:
Move parsing loop into the class itself.
## Code After:
from pprint import pprint

class Hand:
    def __init__(self, string):
        segments = "seats preflop flop turn river".split()
        self.seats = None
        self.preflop = None
        self.flop = None
        self.turn = None
        self.river = None
        self.summary = None
        ## step 2: split each hand into segments
        s = string.split('\n*** ')
        while len(s) > 1:
            # We don't always have flop, turn, riv, but last element is
            # always Summary.
            k = segments.pop(0)
            v = s.pop(0).splitlines()
            self.__dict__[k] = v
        ## step 3: split each segment into lines
        self.summary = s.pop(0).splitlines()
        assert len(s) == 0
    def __repr__(self):
        return str(self.__dict__)


## main

input = open('example_ignition.txt').read()

## step 1: split flat file into hands
hands = input.split('\n\n\n')

for i, h in enumerate(hands):
    hands[i] = Hand(h)

## [ { s:[] p:[] f:[] s:[] }  { s:[] p:[] f:[] t:[] r:[] s:[] }  {}  {} ]

print(hands[0])
