title: Designing a reusable parser in python
body: <p>I am writing a file parser and I want to be able to determine witch "data fields" it would return for me. </p>
  
  <p>I am starting to learn python and am still used to think like a Java programmer, so this question is more about how to design my module than about how specifically parse the file.</p>
  
  <p>Contextualizing, each line of the file has a fixed number of characters and each information is contained between specific indexes. Eg.:</p>
  
  <pre><code>XX20120101NAME1CITYA
  XY20120101NAME2CITYB
  </code></pre>
  
  <p>In this fictional example, from index 0 until 2 you have one information, from 2 to 10 another, and so on... </p>
  
  <p>Using Java, I would normally create an enumerator representing the different pieces of information, each "storing" the <em>start index</em> and the <em>end index</em>. In my parsing class, I would then make an method available design to accept <em>n</em> different enums. Eg.:</p>
  
  <pre><code>enum FileInformation {
      INFO01(0,2), INFO02(2,10), INFO03(10,15), INFO04(15,20);
      int startIndex;
      int endIndex;
  
      public FileInformation(int si, int ei)  {
          this.startIndex = si;
          this.endIndex = ei;
      }
  
      public int getStartIndex() { return si; }
      public int getEndIndex() { return ei; }
  }
  
  public Whatever parse(FileInformation... infos) {
      // Here I would iterate through infos[], 
      // using its start and end index to retrieve only what I need.
  }
  </code></pre>
  
  <p>I know that I probably should not use the same line of though in <em>python</em>, specially because the language wouldn't allow it (no enums in python) and because I imagine <em>python</em> can be much less verbose, but I have no idea of a good design practice to achieve this same result.</p>
  
  <p>It is valid to mention that I don't want to expose the module's user to unnecessary complexity, or to force him to know the indexes for each information. The module's user should preferably be able to determine witch information he wants and its order. </p>
  
  <p>So, do you have any insights about solving this requisites in an elegant manner?
  Thanks in advance</p>