Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 1.19. Checking a String for Any of Multiple Endings


Credit: Michele
Simionato


Problem


For a certain string s, you must check
whether s has any of several endings; in
other words, you need a handy, elegant equivalent of
s.endswith(end1) or s.endswith(end2) or
s.endswith(end3)
and so on.


Solution


The itertools.imap function is just as handy for
this task as for many of a similar nature:

import itertools
def anyTrue(predicate, sequence):
return True in itertools.imap(predicate, sequence)
def endsWith(s, *endings):
return anyTrue(s.endswith, endings)


Discussion


A typical use for endsWith might be to print all
names of image files in the current directory:

import os
for filename in os.listdir('.'):
if endsWith(filename, '.jpg', '.jpeg', '.gif'):
print filename

The same general idea shown in this recipe's
Solution is easily applied to other tasks related to checking a
string for any of several possibilities. The auxiliary function
anyTrue is general and fast, and you can pass it as
its first argument (the predicate) other bound
methods, such as s.startswith or s._
_contains_ _
. Indeed, perhaps it would be better to do
without the helper function endsWithafter
all, directly coding

    if anyTrue(filename.endswith, (".jpg", ".gif", ".png")):

seems to be already readable enough.


Bound Methods



Whenever a Python object supplies a method,
you can get the method, already bound to the
object, by just accessing the method on the
object. (For example, you can assign it, pass it as an argument,
return it as a function's result, etc.) For example:

L = ['fee', 'fie', 'foo']
x = L.append

Now, name x refers to a bound
method
of list object L.
Calling, say, x('fum') is the same as calling
L.append('fum'): either call mutates object
L into ['fee', 'fie',
'foo', 'fum'].


If
you access a method on a type or class, rather than an instance of
the type or class, you get an unbound method,
not "attached" to any particular
instance of the type or class: when you call it, you need to pass as
its first argument an instance of that type or class. For example, if
you set y = list.append, you cannot just call
y('I')Python couldn't
possibly guess which list you want to append
I to! You can, however, call y(L,
'I')
, and that is just the same as calling
L.append('I') (as long as isinstance(L,
list)
).

This recipe originates from a discussion on

See Also


Library Reference and Python in a
Nutshell
docs for itertools and string
methods.


/ 394