Python help
    Erik Price 
    erikprice at mac.com
       
    Sat Feb  7 09:33:19 EST 2004
    
    
  
On Feb 5, 2004, at 10:47 AM, Paul Lussier wrote:
> I'm not overly interesting in shell, perl, tcl, or other language 
> solutions
> to this problem, since I already know how to write this in the first 3.
> (a java or c implementation might be interesting :)
Here's the Java implementation.  You can see that the way I went about 
it is just like the Python version, except Java requires even more 
verbosity.  It took longer to write, even though I had already 
prototyped the design in Python (the two designs are nearly identical), 
and IMHO would also be more work to modify/extend.  That said, if 
handed a several million-line application written by some other 
development team, I would rather the application be written in Java 
than Python.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
class Group {
     private static final int NAME_INDEX = 0;
     private static final int GID_INDEX = 2;
     private static final int MEMBERS_INDEX = 3;
     private String name;
     private int gid;
     private List members;
     Group(String name, int gid, List members) {
         this.name = name;
         this.gid = gid;
         this.members = members;
     }
     Group (String lineFromFile) {
         String[] record = lineFromFile.split(":");
         this.name = record[NAME_INDEX];
         this.gid = Integer.parseInt(record[GID_INDEX]);
         if (hasMembers(record)) {
             String[] membersArray =
                 record[MEMBERS_INDEX].split(",");
             this.members = Arrays.asList(membersArray);
         }
     }
     private boolean hasMembers(String[] record) {
         return (record.length == MEMBERS_INDEX + 1);
     }
     String toLdifFormat() {
         String memberString = "";
         if (this.members != null) {
             SortedSet sorted = new TreeSet(this.members);
             for (Iterator i = sorted.iterator(); i.hasNext();) {
                 memberString += (String)i.next();
                 if (i.hasNext()) {
                     memberString += ",";
                 }
             }
         }
         return this.name + ":" + this.gid + ":" + memberString;
     }
}
public class EtcGroupToLdif {
     public static void main(String[] args) throws IOException {
         File etcGroup = new File(args[0]);
         BufferedReader br =
             new BufferedReader(new FileReader(etcGroup));
         String line;
         while ((line = br.readLine()) != null) {
             if (line.startsWith("#")) {
                 continue;
             }
             Group group = new Group(line);
             System.out.println(group.toLdifFormat());
         }
         br.close();
     }
}
    
    
More information about the gnhlug-discuss
mailing list