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


Groups > comp.lang.python > #85870 > unrolled thread

Re: What behavior would you expect?

Started byDave Angel <davea@davea.name>
First post2015-02-19 01:29 -0500
Last post2015-02-19 01:29 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What behavior would you expect? Dave Angel <davea@davea.name> - 2015-02-19 01:29 -0500

#85870 — Re: What behavior would you expect?

FromDave Angel <davea@davea.name>
Date2015-02-19 01:29 -0500
SubjectRe: What behavior would you expect?
Message-ID<mailman.18866.1424327362.18130.python-list@python.org>
On 02/19/2015 12:10 AM, Chris Angelico wrote:
> On Thu, Feb 19, 2015 at 3:44 PM, Jason Friedman <jsf80238@gmail.com> wrote:
>> I have need to search a directory and return the name of the most recent
>> file matching a given pattern.  Given a directory with these files and
>> timestamps,
>>
>> q.pattern1.abc Feb 13
>> r.pattern1.cdf  Feb 12
>> s.pattern1.efg  Feb 10
>> t.pattern2.abc Feb 13
>> u.pattern2.xyz  Feb 14
>> v.pattern2.efg  Feb 10
>>
>> calling my_function("/path/to/dir", "pattern1") will return q.pattern1.abc
>> and calling my_function("/path/to/dir", "pattern2") will return
>> u.pattern2.xyz.
>
> That seems reasonable, and well-defined.
>
>> My question is, what would be a reasonable behavior/result/return value if:
>> 1. "/path/to/dir" does not exist or is not readable
>
> Raise an exception. Or, better still, just allow the exception to bubble.
>
>> 2. no files match the given pattern
>
> Either return None, or raise an exception, depending on how "normal"
> this state is. Is it simply a matter of "you asked for something, but
> you got nothing"? Then return None. Is it a really unusual situation
> that should basically never happen? Then raise an exception, so you
> get an instant report with no wondering "why am I getting these
> strange TypeErrors".
>
>> Also, what would be a reasonable name for such a function?
>
> newest_file_matching() seems decent. Remember, it doesn't have to be
> especially short.
>

Consider returning a list of files, sorted by datestamp.  Return an 
empty list if there are no matches.  That way, the user can find the 
newest, the oldest, can check if any match, and so on, all by how it 
manipulates the list.

    if result:     check whether there are any matches
    newest = result[0]
    oldest = result[-1]

Your code probably has to build the list anyway, to find the newest.  So 
why not just return it, and let the caller decide which part(s) to keep.

-- 
DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web