Exemple #1
0
    def update_value(self, position, value, path, Iter=0):
        """update_value() takes a position [0-n] and a value to be updated and updates this
        value at the position. Default parameter is Iter=0. If Iter is set to 1, update_value()
        will replace the values with an iterated value. Output will be written to the
        parameter given in path."""

        try:
            record = self.read_field(position, 0, 0)
            lw = LineWriter(path)

            for j in record:

                    fields = j.split('\t')
                    if (iter==0):
                        fields[position] = value
                    else:
                        value += 1
                        fields[position] = str(value)
                    fields = '\t'.join(fields)
                    lw.write_line(fields)

            lw.closing()

        except IndexError:
            print "update_value() List error. Non valid position."
            print str
Exemple #2
0
    def read_behind(self, path, num_lines, size=-64, behind=2, printer=0):
        """read_behind() takes a number of lines to be read from the back of the file
        or the middle of the file. The default size in kilobytes is set to 64k,
        but this can be overwritten by assigning another value. Remember to put in
        minus before the size value. Takes args.: Path to file, number of lines, size,
        behind (if set to 2; reads from behind, if not; reads from the middle of the file)"""

        if (behind==2):
            self.dwca.seek((size*1024), behind)
            mylist = []
            k = self.dwca.readline()#reads truncated line
            k = self.dwca.readline()

            while k != '':
                mylist.append(k)
                k = self.dwca.readline()

            mylist.reverse()    #Sorts the list so the original order is preserved
        else:
            size = os.path.getsize(self.dwc_path)
            mid_size = size/2
            self.dwca.seek(mid_size, 0)
            mylist = []
            k = self.dwca.readline()    #reads truncated line
            k = self.dwca.readline()
            j = 0
            while j < num_lines:
                mylist.append(k)
                k = self.dwca.readline()
                j+= 1

        if (printer == 1):
            for j in mylist[:num_lines:]:
                print j,
        if (printer == 0):
            lw = LineWriter(path)
            for j in mylist[:num_lines:]:
                lw.write_line(j)
            lw.closing()
        if k == '':
            print 'end is reached'
Exemple #3
0
    def read_lines(self, number, path, printer=0):
        """Reads a number of lines and either prints them or writes to a file.
        Takes parameter no of lines, path to file, boolean print 1 or 0"""

        header = ''
        if (printer == 1):
            my_list = self.dwca.readline()
            my_list = my_list.split('\t')
            for j in my_list:
                header = header + "(%s)%s\t" %(my_list.index(j), j.strip())
                #print "(%s)%s" %(my_list.index(j), j),
            print header

            for j in range(0, number):
                print self.dwca.readline(),
        else:
            lw = LineWriter(path)

            for j in range(0 ,number):
                lw.write_line(self.dwca.readline())
            lw.closing()
Exemple #4
0
    def update_value(self, position, value, path, Iter=0):
        """update_value() takes a position [0-n] and a value to be updated and updates this
        value at the position. Default parameter is Iter=0. If Iter is set to 1, update_value()
        will replace the values with an iterated value. Output will be written to the
        parameter given in path."""

        try:
            record = self.read_field(position, 0, 0)
            lw = LineWriter(path)

            for j in record:

                fields = j.split('\t')
                if (iter == 0):
                    fields[position] = value
                else:
                    value += 1
                    fields[position] = str(value)
                fields = '\t'.join(fields)
                lw.write_line(fields)

            lw.closing()

        except IndexError:
            print "update_value() List error. Non valid position."
            print str
Exemple #5
0
    def read_behind(self, path, num_lines, size=-64, behind=2, printer=0):
        """read_behind() takes a number of lines to be read from the back of the file
        or the middle of the file. The default size in kilobytes is set to 64k,
        but this can be overwritten by assigning another value. Remember to put in
        minus before the size value. Takes args.: Path to file, number of lines, size,
        behind (if set to 2; reads from behind, if not; reads from the middle of the file)"""

        if (behind == 2):
            self.dwca.seek((size * 1024), behind)
            mylist = []
            k = self.dwca.readline()  #reads truncated line
            k = self.dwca.readline()

            while k != '':
                mylist.append(k)
                k = self.dwca.readline()

            mylist.reverse(
            )  #Sorts the list so the original order is preserved
        else:
            size = os.path.getsize(self.dwc_path)
            mid_size = size / 2
            self.dwca.seek(mid_size, 0)
            mylist = []
            k = self.dwca.readline()  #reads truncated line
            k = self.dwca.readline()
            j = 0
            while j < num_lines:
                mylist.append(k)
                k = self.dwca.readline()
                j += 1

        if (printer == 1):
            for j in mylist[:num_lines:]:
                print j,
        if (printer == 0):
            lw = LineWriter(path)
            for j in mylist[:num_lines:]:
                lw.write_line(j)
            lw.closing()
        if k == '':
            print 'end is reached'
Exemple #6
0
    def read_lines(self, number, path, printer=0):
        """Reads a number of lines and either prints them or writes to a file.
        Takes parameter no of lines, path to file, boolean print 1 or 0"""

        header = ''
        if (printer == 1):
            my_list = self.dwca.readline()
            my_list = my_list.split('\t')
            for j in my_list:
                header = header + "(%s)%s\t" % (my_list.index(j), j.strip())
                #print "(%s)%s" %(my_list.index(j), j),
            print header

            for j in range(0, number):
                print self.dwca.readline(),
        else:
            lw = LineWriter(path)

            for j in range(0, number):
                lw.write_line(self.dwca.readline())
            lw.closing()
Exemple #7
0
def main():

    path = "H:/dwctest/testtest.txt"
    lw = LineWriter(path)
    lw.write_line('Wakey wakey, eggs and baky')
    lw.closing()
Exemple #8
0
def main():

    path = "H:/dwctest/testtest.txt"
    lw = LineWriter(path)
    lw.write_line("Wakey wakey, eggs and baky")
    lw.closing()