Merge pull request #39196 from badone/wip-doc-python3-api

doc/rados/api: Update code samples to python3

Reviewed-by: Zac Dover <zac.dover@gmail.com>
This commit is contained in:
zdover23 2021-02-01 17:18:35 +10:00 committed by GitHub
commit e9a74d6951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 60 deletions

View File

@ -422,18 +422,18 @@ into exceptions.
try:
cluster = rados.Rados(conffile='')
except TypeError as e:
print 'Argument validation error: ', e
print('Argument validation error: {}'.format(e))
raise e
print "Created cluster handle."
print("Created cluster handle.")
try:
cluster.connect()
except Exception as e:
print "connection error: ", e
print("connection error: {}".format(e))
raise e
finally:
print "Connected to the cluster."
print("Connected to the cluster.")
Execute the example to verify that it connects to your cluster. ::
@ -838,42 +838,43 @@ Python Example
.. code-block:: python
print "\n\nI/O Context and Object Operations"
print "================================="
print("\n\nI/O Context and Object Operations")
print("=================================")
print "\nCreating a context for the 'data' pool"
print("\nCreating a context for the 'data' pool")
if not cluster.pool_exists('data'):
raise RuntimeError('No data pool exists')
ioctx = cluster.open_ioctx('data')
print "\nWriting object 'hw' with contents 'Hello World!' to pool 'data'."
ioctx.write("hw", "Hello World!")
print "Writing XATTR 'lang' with value 'en_US' to object 'hw'"
ioctx.set_xattr("hw", "lang", "en_US")
print("\nWriting object 'hw' with contents 'Hello World!' to pool 'data'.")
ioctx.write("hw", b"Hello World!")
print("Writing XATTR 'lang' with value 'en_US' to object 'hw'")
ioctx.set_xattr("hw", "lang", b"en_US")
print "\nWriting object 'bm' with contents 'Bonjour tout le monde!' to pool 'data'."
ioctx.write("bm", "Bonjour tout le monde!")
print "Writing XATTR 'lang' with value 'fr_FR' to object 'bm'"
ioctx.set_xattr("bm", "lang", "fr_FR")
print("\nWriting object 'bm' with contents 'Bonjour tout le monde!' to pool
'data'.")
ioctx.write("bm", b"Bonjour tout le monde!")
print("Writing XATTR 'lang' with value 'fr_FR' to object 'bm'")
ioctx.set_xattr("bm", "lang", b"fr_FR")
print "\nContents of object 'hw'\n------------------------"
print ioctx.read("hw")
print("\nContents of object 'hw'\n------------------------")
print(ioctx.read("hw"))
print "\n\nGetting XATTR 'lang' from object 'hw'"
print ioctx.get_xattr("hw", "lang")
print("\n\nGetting XATTR 'lang' from object 'hw'")
print(ioctx.get_xattr("hw", "lang"))
print "\nContents of object 'bm'\n------------------------"
print ioctx.read("bm")
print("\nContents of object 'bm'\n------------------------")
print(ioctx.read("bm"))
print "Getting XATTR 'lang' from object 'bm'"
print ioctx.get_xattr("bm", "lang")
print("\n\nGetting XATTR 'lang' from object 'bm'")
print(ioctx.get_xattr("bm", "lang"))
print "\nRemoving object 'hw'"
print("\nRemoving object 'hw'")
ioctx.remove_object("hw")
print "Removing object 'bm'"
print("Removing object 'bm'")
ioctx.remove_object("bm")
@ -981,10 +982,10 @@ Python Example
.. code-block:: python
print "\nClosing the connection."
print("\nClosing the connection.")
ioctx.close()
print "Shutting down the handle."
print("Shutting down the handle.")
cluster.shutdown()
PHP Example

View File

@ -89,18 +89,18 @@ information about the cluster.
import rados, sys
cluster = rados.Rados(conffile='ceph.conf')
print "\nlibrados version: " + str(cluster.version())
print "Will attempt to connect to: " + str(cluster.conf_get('mon host'))
print("\nlibrados version: {}".format(str(cluster.version())))
print("Will attempt to connect to: {}".format(str(cluster.conf_get('mon host'))))
cluster.connect()
print "\nCluster ID: " + cluster.get_fsid()
print("\nCluster ID: {}".format(cluster.get_fsid()))
print "\n\nCluster Statistics"
print "=================="
print("\n\nCluster Statistics")
print("==================")
cluster_stats = cluster.get_cluster_stats()
for key, value in cluster_stats.iteritems():
print key, value
for key, value in cluster_stats.items():
print(key, value)
By default, Ceph authentication is ``on``. Your application will need to know
@ -128,33 +128,32 @@ pool.
:linenos:
:emphasize-lines: 6, 13, 18, 25
print "\n\nPool Operations"
print "==============="
print("\n\nPool Operations")
print("===============")
print "\nAvailable Pools"
print "----------------"
print("\nAvailable Pools")
print("----------------")
pools = cluster.list_pools()
for pool in pools:
print pool
print(pool)
print "\nCreate 'test' Pool"
print "------------------"
print("\nCreate 'test' Pool")
print("------------------")
cluster.create_pool('test')
print "\nPool named 'test' exists: " + str(cluster.pool_exists('test'))
print "\nVerify 'test' Pool Exists"
print "-------------------------"
print("\nPool named 'test' exists: {}".format(str(cluster.pool_exists('test'))))
print("\nVerify 'test' Pool Exists")
print("-------------------------")
pools = cluster.list_pools()
for pool in pools:
print pool
print(pool)
print "\nDelete 'test' Pool"
print "------------------"
print("\nDelete 'test' Pool")
print("------------------")
cluster.delete_pool('test')
print "\nPool named 'test' exists: " + str(cluster.pool_exists('test'))
print("\nPool named 'test' exists: {}".format(str(cluster.pool_exists('test'))))
Input/Output Context
@ -186,7 +185,7 @@ that you close the connection. For example:
.. code-block:: python
:linenos:
print "\nClosing the connection."
print("\nClosing the connection.")
ioctx.close()
@ -203,13 +202,13 @@ from the cluster. You may also remove objects from the cluster. For example:
:linenos:
:emphasize-lines: 2, 5, 8
print "\nWriting object 'hw' with contents 'Hello World!' to pool 'data'."
print("\nWriting object 'hw' with contents 'Hello World!' to pool 'data'.")
ioctx.write_full("hw", "Hello World!")
print "\n\nContents of object 'hw'\n------------------------\n"
print ioctx.read("hw")
print("\n\nContents of object 'hw'\n------------------------\n")
print(ioctx.read("hw"))
print "\nRemoving object 'hw'"
print("\nRemoving object 'hw'")
ioctx.remove_object("hw")
@ -223,11 +222,11 @@ the object and read XATTRs from the object. For example:
:linenos:
:emphasize-lines: 2, 5
print "\n\nWriting XATTR 'lang' with value 'en_US' to object 'hw'"
print("\n\nWriting XATTR 'lang' with value 'en_US' to object 'hw'")
ioctx.set_xattr("hw", "lang", "en_US")
print "\n\nGetting XATTR 'lang' from object 'hw'\n"
print ioctx.get_xattr("hw", "lang")
print("\n\nGetting XATTR 'lang' from object 'hw'\n")
print(ioctx.get_xattr("hw", "lang"))
Listing Objects
@ -239,19 +238,22 @@ For example:
.. code-block:: python
:linenos:
:emphasize-lines: 1, 6, 7
:emphasize-lines: 1, 6, 7, 13
object_iterator = ioctx.list_objects()
while True :
try :
rados_object = object_iterator.next()
print "Object contents = " + rados_object.read()
rados_object = object_iterator.__next__()
print("Object contents = {}".format(rados_object.read()))
except StopIteration :
break
# Or alternatively
[print("Object contents = {}".format(obj.read())) for obj in ioctx.list_objects()]
The ``Object`` class provides a file-like interface to an object, allowing
you to read and write content and extended attributes. Object operations using
the I/O context provide additional functionality and asynchronous capabilities.