Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e2065c65
Kaydet (Commit)
e2065c65
authored
Şub 23, 2008
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
#1826: allow dotted attribute paths in operator.attrgetter.
üst
b0b0317b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
5 deletions
+78
-5
operator.rst
Doc/library/operator.rst
+8
-2
test_operator.py
Lib/test/test_operator.py
+20
-0
NEWS
Misc/NEWS
+2
-0
operator.c
Modules/operator.c
+48
-3
No files found.
Doc/library/operator.rst
Dosyayı görüntüle @
e2065c65
...
...
@@ -499,15 +499,21 @@ expect a function argument.
Return a callable object that fetches *attr* from its operand. If more than one
attribute is requested, returns a tuple of attributes. After,
``f
=
attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
``f
=
attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
``f
=
attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
``f
=
attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
b.date)``.
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
the call ``f(b)`` returns ``b.date.month``.
.. versionadded:: 2.4
.. versionchanged:: 2.5
Added support for multiple attributes.
.. versionchanged:: 2.6
Added support for dotted attributes.
.. function:: itemgetter(item[, args...])
...
...
Lib/test/test_operator.py
Dosyayı görüntüle @
e2065c65
...
...
@@ -386,6 +386,26 @@ class OperatorTestCase(unittest.TestCase):
raise
SyntaxError
self
.
failUnlessRaises
(
SyntaxError
,
operator
.
attrgetter
(
'foo'
),
C
())
# recursive gets
a
=
A
()
a
.
name
=
'arthur'
a
.
child
=
A
()
a
.
child
.
name
=
'thomas'
f
=
operator
.
attrgetter
(
'child.name'
)
self
.
assertEqual
(
f
(
a
),
'thomas'
)
self
.
assertRaises
(
AttributeError
,
f
,
a
.
child
)
f
=
operator
.
attrgetter
(
'name'
,
'child.name'
)
self
.
assertEqual
(
f
(
a
),
(
'arthur'
,
'thomas'
))
f
=
operator
.
attrgetter
(
'name'
,
'child.name'
,
'child.child.name'
)
self
.
assertRaises
(
AttributeError
,
f
,
a
)
a
.
child
.
child
=
A
()
a
.
child
.
child
.
name
=
'johnson'
f
=
operator
.
attrgetter
(
'child.child.name'
)
self
.
assertEqual
(
f
(
a
),
'johnson'
)
f
=
operator
.
attrgetter
(
'name'
,
'child.name'
,
'child.child.name'
)
self
.
assertEqual
(
f
(
a
),
(
'arthur'
,
'thomas'
,
'johnson'
))
def
test_itemgetter
(
self
):
a
=
'ABCDE'
f
=
operator
.
itemgetter
(
2
)
...
...
Misc/NEWS
Dosyayı görüntüle @
e2065c65
...
...
@@ -1196,6 +1196,8 @@ Library
Extension Modules
-----------------
- Patch #1826: operator.attrgetter() now supports dotted attribute paths.
- Patch #1957: syslogmodule: Release GIL when calling syslog(3)
- #2112: mmap.error is now a subclass of EnvironmentError and not a
...
...
Modules/operator.c
Dosyayı görüntüle @
e2065c65
...
...
@@ -495,6 +495,49 @@ attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
return
0
;
}
static
PyObject
*
dotted_getattr
(
PyObject
*
obj
,
PyObject
*
attr
)
{
char
*
s
,
*
p
;
#ifdef Py_USING_UNICODE
if
(
PyUnicode_Check
(
attr
))
{
attr
=
_PyUnicode_AsDefaultEncodedString
(
attr
,
NULL
);
if
(
attr
==
NULL
)
return
NULL
;
}
#endif
if
(
!
PyString_Check
(
attr
))
{
PyErr_SetString
(
PyExc_TypeError
,
"attribute name must be a string"
);
return
NULL
;
}
s
=
PyString_AS_STRING
(
attr
);
Py_INCREF
(
obj
);
for
(;;)
{
PyObject
*
newobj
,
*
str
;
p
=
strchr
(
s
,
'.'
);
str
=
p
?
PyString_FromStringAndSize
(
s
,
(
p
-
s
))
:
PyString_FromString
(
s
);
if
(
str
==
NULL
)
{
Py_DECREF
(
obj
);
return
NULL
;
}
newobj
=
PyObject_GetAttr
(
obj
,
str
);
Py_DECREF
(
str
);
Py_DECREF
(
obj
);
if
(
newobj
==
NULL
)
return
NULL
;
obj
=
newobj
;
if
(
p
==
NULL
)
break
;
s
=
p
+
1
;
}
return
obj
;
}
static
PyObject
*
attrgetter_call
(
attrgetterobject
*
ag
,
PyObject
*
args
,
PyObject
*
kw
)
{
...
...
@@ -504,7 +547,7 @@ attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
if
(
!
PyArg_UnpackTuple
(
args
,
"attrgetter"
,
1
,
1
,
&
obj
))
return
NULL
;
if
(
ag
->
nattrs
==
1
)
return
PyObject_GetA
ttr
(
obj
,
ag
->
attr
);
return
dotted_geta
ttr
(
obj
,
ag
->
attr
);
assert
(
PyTuple_Check
(
ag
->
attr
));
assert
(
PyTuple_GET_SIZE
(
ag
->
attr
)
==
nattrs
);
...
...
@@ -516,7 +559,7 @@ attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
for
(
i
=
0
;
i
<
nattrs
;
i
++
)
{
PyObject
*
attr
,
*
val
;
attr
=
PyTuple_GET_ITEM
(
ag
->
attr
,
i
);
val
=
PyObject_GetA
ttr
(
obj
,
attr
);
val
=
dotted_geta
ttr
(
obj
,
attr
);
if
(
val
==
NULL
)
{
Py_DECREF
(
result
);
return
NULL
;
...
...
@@ -531,7 +574,9 @@ PyDoc_STRVAR(attrgetter_doc,
\n
\
Return a callable object that fetches the given attribute(s) from its operand.
\n
\
After, f=attrgetter('name'), the call f(r) returns r.name.
\n
\
After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date)."
);
After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
\n
\
After, h=attrgetter('name.first', 'name.last'), the call h(r) returns
\n
\
(r.name.first, r.name.last)."
);
static
PyTypeObject
attrgetter_type
=
{
PyVarObject_HEAD_INIT
(
NULL
,
0
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment