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
2b03d45b
Kaydet (Commit)
2b03d45b
authored
Eyl 18, 2007
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
üst
de37a8ce
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
9 deletions
+33
-9
collections.rst
Doc/library/collections.rst
+18
-1
collections.py
Lib/collections.py
+9
-8
test_collections.py
Lib/test/test_collections.py
+6
-0
No files found.
Doc/library/collections.rst
Dosyayı görüntüle @
2b03d45b
...
...
@@ -364,7 +364,7 @@ Setting the :attr:`default_factory` to :class:`set` makes the
--------------------------------------------
.. function:: NamedTuple(typename, fieldnames)
.. function:: NamedTuple(typename, fieldnames
, [verbose]
)
Returns a new tuple subclass named *typename*. The new subclass is used to
create tuple-like objects that have fields accessable by attribute lookup as
...
...
@@ -412,6 +412,23 @@ Setting the :attr:`default_factory` to :class:`set` makes the
>>> print Color(*m.popitem())
Color(name='blue', code=3)
If *verbose* is true, the *NamedTuple* call will print the class definition::
>>> Point = NamedTuple('Point', 'x y', verbose=True)
class Point(tuple):
'Point(x, y)'
__slots__ = ()
__fields__ = ('x', 'y')
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))
def __repr__(self):
return 'Point(x=%r, y=%r)' % self
def __replace__(self, field, value):
'Return a new Point object replacing one field with a new value'
return Point(**dict(zip(('x', 'y'), self) + [(field, value)]))
x = property(itemgetter(0))
y = property(itemgetter(1))
In addition to the methods inherited from tuples, named tuples support
an additonal method and an informational read-only attribute.
...
...
Lib/collections.py
Dosyayı görüntüle @
2b03d45b
...
...
@@ -4,7 +4,7 @@ from _collections import deque, defaultdict
from
operator
import
itemgetter
as
_itemgetter
import
sys
as
_sys
def
NamedTuple
(
typename
,
s
):
def
NamedTuple
(
typename
,
s
,
verbose
=
False
):
"""Returns a new subclass of tuple with named fields.
>>> Point = NamedTuple('Point', 'x y')
...
...
@@ -28,25 +28,26 @@ def NamedTuple(typename, s):
"""
field_names
=
tuple
(
s
.
replace
(
','
,
' '
)
.
split
())
# names separated by spaces and/or commas
field_names
=
tuple
(
s
.
replace
(
','
,
' '
)
.
split
())
# names separated by spaces and/or commas
if
not
''
.
join
((
typename
,)
+
field_names
)
.
replace
(
'_'
,
''
)
.
isalnum
():
raise
ValueError
(
'Type names and field names can only contain alphanumeric characters and underscores'
)
argtxt
=
', '
.
join
(
field_names
)
argtxt
=
repr
(
field_names
)
.
replace
(
"'"
,
""
)[
1
:
-
1
]
# tuple repr without parens or quotes
reprtxt
=
', '
.
join
(
'
%
s=
%%
r'
%
name
for
name
in
field_names
)
template
=
'''class
%(typename)
s(tuple):
'
%(typename)
s(
%(argtxt)
s)'
__slots__ = ()
__fields__ =
%(field_names)
r
def __new__(cls,
%(argtxt)
s):
return tuple.__new__(cls, (
%(argtxt)
s
,
))
return tuple.__new__(cls, (
%(argtxt)
s))
def __repr__(self):
return '
%(typename)
s(
%(reprtxt)
s)'
%%
self
def __replace__(self, field, value):
'Return a new
%(typename)
s object replacing one field with a new value'
return
%(typename)
s(**dict(zip(
%(field_names)
r, self) + [(field, value)]))
'''
%
locals
()
return
%(typename)
s(**dict(zip(
%(field_names)
r, self) + [(field, value)]))
\n
'''
%
locals
()
for
i
,
name
in
enumerate
(
field_names
):
template
+=
'
\n
%
s = property(itemgetter(
%
d))
\n
'
%
(
name
,
i
)
template
+=
'
%
s = property(itemgetter(
%
d))
\n
'
%
(
name
,
i
)
if
verbose
:
print
template
m
=
dict
(
itemgetter
=
_itemgetter
)
exec
template
in
m
result
=
m
[
typename
]
...
...
@@ -62,7 +63,7 @@ def NamedTuple(typename, s):
if
__name__
==
'__main__'
:
# verify that instances can be pickled
from
cPickle
import
loads
,
dumps
Point
=
NamedTuple
(
'Point'
,
'x, y'
)
Point
=
NamedTuple
(
'Point'
,
'x, y'
,
True
)
p
=
Point
(
x
=
10
,
y
=
20
)
assert
p
==
loads
(
dumps
(
p
))
...
...
Lib/test/test_collections.py
Dosyayı görüntüle @
2b03d45b
...
...
@@ -58,6 +58,12 @@ class TestNamedTuple(unittest.TestCase):
self
.
assertRaises
(
AttributeError
,
eval
,
'p.z'
,
locals
())
def
test_odd_sizes
(
self
):
Zero
=
NamedTuple
(
'Zero'
,
''
)
self
.
assertEqual
(
Zero
(),
())
Dot
=
NamedTuple
(
'Dot'
,
'd'
)
self
.
assertEqual
(
Dot
(
1
),
(
1
,))
def
test_main
(
verbose
=
None
):
import
collections
as
CollectionsModule
test_classes
=
[
TestNamedTuple
]
...
...
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