Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89946

Step further with filebasedMessages

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Step further with filebasedMessages
Organization Decebal Computing
Date 2015-05-05 10:52 +0200
Message-ID <87oalzh0d5.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


I now defined get_message_slice:
    ### Add step
    def get_message_slice(message_filename, start, end):
        """
        Get a slice of messages, where 0 is the first message
        Works with negative indexes
        The values can be ascending and descending
        """

        message_list    = []
        real_file       = expanduser(message_filename)
        nr_of_messages  = get_nr_of_messages(real_file)
        if start < 0:
            start += nr_of_messages
        if end < 0:
            end += nr_of_messages
        assert (start >= 0) and (start < nr_of_messages)
        assert (end   >= 0) and (end   < nr_of_messages)
        if start > end:
            tmp             = start
            start           = end
            end             = tmp
            need_reverse    = True
        else:
            need_reverse    = False
        with open(real_file, 'r') as f:
            for message in islice(f, start, end + 1):
                message_list.append(message.rstrip())
        if need_reverse:
            message_list.reverse()
        return message_list

Is that a good way?

I also had:
    def get_indexed_message(message_filename, index):
        """
        Get index message from a file, where 0 gets the first message
        A negative index gets messages indexed from the end of the file
        Use get_nr_of_messages to get the number of messages in the file
        """

        real_file       = expanduser(message_filename)
        nr_of_messages  = get_nr_of_messages(real_file)
        if index < 0:
            index += nr_of_messages
        assert (index >= 0) and (index < nr_of_messages)
        with open(real_file, 'r') as f:
            [line] = islice(f, index, index + 1)
            return line.rstrip()

But changed it to:
    def get_indexed_message(message_filename, index):
        """
        Get index message from a file, where 0 gets the first message
        A negative index gets messages indexed from the end of the file
        Use get_nr_of_messages to get the number of messages in the file
        """

        return get_message_slice(message_filename, index, index)[0]

Is that acceptable? I am a proponent of DRY.
Or should I at least keep the assert in it?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Step further with filebasedMessages Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 10:52 +0200
  Re: Step further with filebasedMessages Chris Angelico <rosuav@gmail.com> - 2015-05-05 19:20 +1000
    Re: Step further with filebasedMessages Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 12:14 +0200
  Re: Step further with filebasedMessages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-05 20:41 +1000
    Re: Step further with filebasedMessages Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 13:27 +0200
      Re: Step further with filebasedMessages Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-05 07:57 -0600
        Re: Step further with filebasedMessages Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 16:51 +0200
  Re: Step further with filebasedMessages Peter Otten <__peter__@web.de> - 2015-05-05 13:08 +0200
    Re: Step further with filebasedMessages Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 17:25 +0200
      Re: Step further with filebasedMessages Dave Angel <davea@davea.name> - 2015-05-05 13:10 -0400

csiph-web