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


Groups > comp.lang.python > #85870

Re: What behavior would you expect?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'matches': 0.07; 'none,': 0.07; 'subject:would': 0.07; 'caller': 0.09; 'exception,': 0.09; 'exception.': 0.09; 'happen?': 0.09; 'newest': 0.09; 'function?': 0.16; 'none.': 0.16; 'readable': 0.16; 'result[-1]': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'thu,': 0.19; 'seems': 0.21; 'feb': 0.22; 'header:User-Agent:1': 0.23; '"you': 0.24; 'decide': 0.24; 'question': 0.24; 'header:In-Reply- To:1': 0.27; 'wondering': 0.29; 'chris': 0.29; 'on,': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'matching': 0.30; 'especially': 0.30; 'asked': 0.31; 'code': 0.31; 'getting': 0.31; 'file': 0.32; 'probably': 0.32; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'returning': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'jason': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'is.': 0.60; 'most': 0.60; 'matter': 0.61; 'simply': 0.61; 'name': 0.63; 'such': 0.63; 'situation': 0.65; 'charset:windows-1252': 0.65; 'received:74.208': 0.68; 'unusual': 0.74; '2015': 0.84; 'newest,': 0.84; 'timestamps,': 0.84; 'subject:you': 0.87; 'remember,': 0.93; 'instant': 0.97
Date Thu, 19 Feb 2015 01:29:06 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: What behavior would you expect?
References <CANy1k1g+wU6VUifZpV5+DWendT2LqS-PhRA-KC5hmq2xQ_uy+A@mail.gmail.com> <CAPTjJmqPKvc6RWU7Y90Cngbe+SontxPzaFm+ZOjXjcnALTqPQw@mail.gmail.com>
In-Reply-To <CAPTjJmqPKvc6RWU7Y90Cngbe+SontxPzaFm+ZOjXjcnALTqPQw@mail.gmail.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:RBc67cJBR/K+CQ8qVo6hbzkk+qc7QnNydrOf/U9Hjvh 86+QXTRnnntyIyVPVlkk++12uvhtCT1kAWK8J8IhftUhvVf4+E xaKnUmUV6MCSoPITnl/firTwHLoctBa4DxtOenF7XB8F0OHSjV DlVlGtFtGxktBmxellaYmfEQFmXPgAUKSa9d/cmJhKenOndGaw /pUXi6W2MfmdAjIdNZI4U141EtyHStbN2huAQLBCipmVroHgA7 HYx1x3A7rosblFLDklR/iCXcwxJV3W6J8wYsSIzokhVK6qmjcl TlhDQ6ph91/CqVvvR/Ogehk50xQwCuOfCmvhizS87p3ROSKYwR QFknMVNX/c2rn9A3sqBI=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18866.1424327362.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424327362 news.xs4all.nl 2920 [2001:888:2000:d::a6]:43041
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85870

Show key headers only | View raw


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

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


Thread

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

csiph-web