NamedTuple
NamedTuple - Imitates the behavior of the named tuple.
Class NamedTuple contains the following methods:
get- Return the value for key if key is in the dictionary, elseNone.update- Update a value of key.to_dict- Convert to the dictionary.items- Returns a generator of list ofNamedTupleelements grouped into tuples.keys- Get a generator of list of keys.values- Get a generator of list of values.has_key- Returns True if the key exists, otherwise False.has_value- Returns True if the value exists, otherwise False.
NamedTuple
¶
This class imitates the behavior of the named tuple.
Source code in src/xloft/types/named_tuple.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
__bool__()
¶
Called when checking for truth.
Examples:
Returns:
| Type | Description |
|---|---|
bool
|
Boolean value. |
Source code in src/xloft/types/named_tuple.py
__delattr__(name)
¶
__getattr__(name)
¶
Getter.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Key name. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value of key. |
Source code in src/xloft/types/named_tuple.py
__getitem__(key)
¶
Get value by [key_name].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name. |
required |
Examples:
Returns:
| Type | Description |
|---|---|
Any
|
Return the value for key, else KeyError. |
Source code in src/xloft/types/named_tuple.py
__len__()
¶
Get the number of elements in the tuple.
Examples:
Returns:
| Type | Description |
|---|---|
int
|
The number of elements in the tuple. |
Source code in src/xloft/types/named_tuple.py
__repr__()
¶
Called by the repr built-in function.
Examples:
>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> repr(nt)
'NamedTuple(x=10, y="Hello")'
Returns:
| Type | Description |
|---|---|
str
|
Returns raw data used for internal representation in python. |
Source code in src/xloft/types/named_tuple.py
__setattr__(name, value)
¶
__str__()
¶
Get a string representation of NamedTuple.
Examples:
Returns:
| Type | Description |
|---|---|
str
|
String representation of NamedTuple. |
Source code in src/xloft/types/named_tuple.py
get(key, default=None)
¶
Return the value for key if key is in the NamedTuple, else default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name. |
required |
default
|
Any
|
Returns if the value is missing. |
None
|
Examples:
Returns:
| Type | Description |
|---|---|
Any
|
Return the value for key if key is in the NamedTuple, else default. |
Source code in src/xloft/types/named_tuple.py
has_key(key)
¶
Check if the key exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name. |
required |
Examples:
Returns:
| Type | Description |
|---|---|
bool
|
True if the key exists, otherwise False. |
Source code in src/xloft/types/named_tuple.py
has_value(value)
¶
Check if the value exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value of key. |
required |
Examples:
Returns:
| Type | Description |
|---|---|
bool
|
True if the value exists, otherwise False. |
Source code in src/xloft/types/named_tuple.py
items()
¶
Returns a generator of list containing a tuple for each key-value pair.
This is convenient for use in a for loop.
If you need to get a list, do it list(instance.items()).
Examples:
>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> for key, val in nt.items():
... print(f"Key: {key}, Value: {val}")
"Key: x, Value: 10"
"Key: y, Value: Hello"
Returns:
| Name | Type | Description |
|---|---|---|
Any
|
Returns a list containing a tuple for each key-value pair. |
|
Type |
Any
|
|
Source code in src/xloft/types/named_tuple.py
keys()
¶
to_dict()
¶
update(key, value)
¶
Update a value of key.
Attention: This is an uncharacteristic action for the type tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name. |
required |
value
|
Any
|
Value of key. |
required |
Examples:
>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> nt.update("x", 20)
>>> nt.x
20
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/xloft/types/named_tuple.py
values()
¶
Get a generator of list of values.
If you need to get a list, do it list(instance.items()).
Examples:
>>> from xloft import NamedTuple
>>> nt = NamedTuple(x=10, y="Hello")
>>> list(nt.values())
[10, "Hello"]
Returns:
| Type | Description |
|---|---|
Any
|
List of values. |