Skip to content
Permalink
Browse files
Fix code example blocks in QgsFeature documentation
  • Loading branch information
nyalldawson committed Jun 25, 2021
1 parent 711a615 commit 3c3059c9381d38063c5d9877fb47d5cd31036700
Showing with 221 additions and 49 deletions.
  1. +107 −30 python/core/auto_generated/qgsfeature.sip.in
  2. +114 −19 src/core/qgsfeature.h
@@ -180,11 +180,17 @@ Sets the feature ``id`` for this feature.
%Docstring
Returns the feature's attributes.

.. seealso:: :py:func:`setAttributes`
Alternatively, in Python it is possible to directly iterate over a feature in order to retrieve
its attributes:

.. note::
.. code-block:: python

feature = QgsFeature()
feature.setAttributes([11, 'string value', 55.5])
for attr in feature:
print(attr)

Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode
.. seealso:: :py:func:`setAttributes`

.. versionadded:: 2.9
%End
@@ -225,15 +231,25 @@ Sets an attribute's value by field index.

If the attribute was successfully set then the feature will be automatically marked as valid (see :py:func:`~QgsFeature.isValid`).

Alternatively, in Python it is possible to directly set a field's value via the field's index:

.. code-block:: python

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)

# set the "name" field value
feature[1] = "my name"
# set the "my_id" field value
feature[0] = 55

:param field: the index of the field to set
:param attr: the value of the attribute

:raises KeyError: if the field index does not exist

.. note::

Alternatively in Python: @code feature[field] = attr @endcode

.. seealso:: :py:func:`setAttributes`
%End
%MethodCode
@@ -297,15 +313,29 @@ Resizes the attributes attached to this feature by appending the specified ``cou

void deleteAttribute( int field );
%Docstring
Deletes an attribute and its value.
Clear's an attribute's value by its index.

:param field: the index of the field

:raises KeyError: if the field is not found
Alternatively, in Python it is possible to directly `del` an attribute via its index:

.. note::
.. code-block:: python

feature = QgsFeature()
feature.setAttributes([11, 'my feature', 55.5])

# will print [11, 'my feature', 55.5]
print(feature.attributes())

# clear the second attribute
del feature[1]

Alternatively in Python: @code del feature[field] @endcode
# will now print [11, NONE]
print(feature.attributes())



:raises KeyError: if the field is not found

.. seealso:: :py:func:`setAttribute`
%End
@@ -452,15 +482,25 @@ Field map must be associated using :py:func:`~QgsFeature.setFields` before this

Calling this method will automatically set the feature as valid (see :py:func:`~QgsFeature.isValid`).

Alternatively, in Python it is possible to directly set a field's value via the field's name:

.. code-block:: python

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)

# set the "name" field value
feature['name'] = "my name"
# set the "my_id" field value
feature['my_id'] = 55

:param name: The name of the field to set
:param value: The value to set

:raises KeyError: if the attribute name could not be converted to an index

.. note::

Alternatively in Python: @code feature[name] = attr @endcode

.. seealso:: :py:func:`setFields`
%End
%MethodCode
@@ -486,17 +526,33 @@ Calling this method will automatically set the feature as valid (see :py:func:`~

bool deleteAttribute( const QString &name );
%Docstring
Removes an attribute value by field ``name``.
Clear's an attribute's value by its field ``name``.

Field map must be associated using setFields()before this method can be used.
Field map must be associated using :py:func:`~QgsFeature.setFields` before this method can be used.

:param name: The name of the field to delete
Alternatively, in Python it is possible to directly `del` an attribute via its name:

:raises KeyError: if attribute name could not be converted to index
.. code-block:: python

.. note::
fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))

feature = QgsFeature(fields)
feature.setAttributes([11, 'my feature'])

# will print [11, 'my feature']
print(feature.attributes())

# clear the 'name' attribute
del feature['name']

Alternatively in Python: @code del feature[name] @endcode
# will now print [11, NULL]
print(feature.attributes())

:param name: The name of the field to clear

:raises KeyError: if attribute name could not be converted to index

.. seealso:: :py:func:`setFields`
%End
@@ -522,16 +578,27 @@ Lookup attribute value by attribute ``name``.

Field map must be associated using :py:func:`~QgsFeature.setFields` before this method can be used.

Alternatively, in Python it is possible to directly retrieve a field's value via the field's name:

.. code-block:: python

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)
feature.setAttributes([11, 'my feature'])

# print the "name" field value
print(feature['name'])
# print the "my_id" field value
print(feature['my_id'])

:param name: The name of the attribute to get

:return: The value of the attribute

:raises KeyError: if the field is not found

.. note::

Alternatively in Python: @code feature[name] @endcode

.. seealso:: :py:func:`setFields`
%End
%MethodCode
@@ -553,18 +620,28 @@ Field map must be associated using :py:func:`~QgsFeature.setFields` before this
%Docstring
Lookup attribute value from its index.

Field map must be associated using :py:func:`~QgsFeature.setFields` before this method can be used.
Alternatively, in Python it is possible to directly retrieve a field's value via its index:

.. code-block:: python

feature = QgsFeature()
feature.setAttributes([11, 'my feature', 55.5])

# will print 11
print(feature[0])

# will print 'my feature'
print(feature[1])

# will print 55.5
print(feature[2])

:param fieldIdx: The index of the attribute to get

:return: The value of the attribute

:raises KeyError: if the field is not found

.. note::

Alternatively in Python: @code feature[fieldIdx] @endcode

.. seealso:: :py:func:`setFields`
%End
%MethodCode

0 comments on commit 3c3059c

Please sign in to comment.