Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89969
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Step further with filebasedMessages |
| Organization | Decebal Computing |
| References | <87oalzh0d5.fsf@Equus.decebal.nl> <mailman.123.1430824107.12865.python-list@python.org> |
| Date | 2015-05-05 17:25 +0200 |
| Message-ID | <877fsngi7i.fsf@Equus.decebal.nl> (permalink) |
Op Tuesday 5 May 2015 13:08 CEST schreef Peter Otten:
> Cecil Westerhof wrote:
>
>> I now defined get_message_slice:
>> ### Add step
>> def get_message_slice(message_filename, start, end):
>
> Intervals are usually half-open in Python. I recommend that you
> follow that convention.
I will change it.
>> """
>> 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)
>
> You should raise an exception. While asserts are rarely switched off
> in Python you still have to be prepeared, and an IndexError would be
> a better fit anyway.
OK.
> Should you later decide that a database is a better fit you can
> change read_messages() to return a class that transparently accesses
> that database. Again, most of the work is already done:
>
> class Messages(collections.Sequence):
> def __init__(self, filename):
> self.filename = filename)
> def __getitem__(self, index):
> # read record(s) from db
> def __len__(self):
> # return num-records in db
>
> def read_messages(filename):
> return Messages(filename)
Another thing I have to look into. :-D
> By the way, where do you plan to use your functions? And where do
> the indices you feed them come from?
In my case from:
get_random_message
and:
dequeue_message
Both also in:
https://github.com/CecilWesterhof/PythonLibrary/blob/master/filebasedMessages.py
I should write some documentation with those functions. ;-)
I have a file with quotes and a file with tips. I want to place random
messages from those two (without them being repeated to soon) on my
Twitter page. This I do with ‘get_random_message’. I also want to put
the first message of another file and remove it from the file. For
this I use ‘dequeue_message’.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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