From 9c2d09dfcdc908443f494a512a6e83ce5a4a1a97 Mon Sep 17 00:00:00 2001 From: Matheus Felipe <50463866+matheusfelipeog@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:25:31 -0300 Subject: [PATCH 1/3] Upd to python 3 in explicit code section --- docs/writing/style.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/writing/style.rst b/docs/writing/style.rst index 3bf029911..718a6b4dd 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -65,9 +65,9 @@ it is bad practice to have two disjointed statements on the same line of code. .. code-block:: python - print 'one'; print 'two' + print('one'); print('two') - if x == 1: print 'one' + if x == 1: print('one') if and : # do something @@ -76,11 +76,11 @@ it is bad practice to have two disjointed statements on the same line of code. .. code-block:: python - print 'one' - print 'two' + print('one') + print('two') if x == 1: - print 'one' + print('one') cond1 = cond2 = From ed755c1e6328589ef04be91c8dbb3e068be159d5 Mon Sep 17 00:00:00 2001 From: Matheus Felipe <50463866+matheusfelipeog@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:35:31 -0300 Subject: [PATCH 2/3] Change xrange function to range function --- docs/writing/style.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/writing/style.rst b/docs/writing/style.rst index 718a6b4dd..cd62d81f4 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -357,9 +357,7 @@ Instead, use a list comprehension: .. code-block:: python - four_lists = [[] for __ in xrange(4)] - -Note: Use range() instead of xrange() in Python 3. + four_lists = [[] for __ in range(4)] Create a string from a list ~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 5796581b4fbd581052601b3de9fef42755e64805 Mon Sep 17 00:00:00 2001 From: Matheus Felipe <50463866+matheusfelipeog@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:47:10 -0300 Subject: [PATCH 3/3] Upd of print func to py3 syntax in conventions section --- docs/writing/style.rst | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/writing/style.rst b/docs/writing/style.rst index cd62d81f4..9858112de 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -582,10 +582,10 @@ list of what is considered false. .. code-block:: python if attr == True: - print 'True!' + print('True!') if attr == None: - print 'attr is None!' + print('attr is None!') **Good**: @@ -593,15 +593,15 @@ list of what is considered false. # Just check the value if attr: - print 'attr is truthy!' + print('attr is truthy!') # or check for the opposite if not attr: - print 'attr is falsey!' + print('attr is falsey!') # or, since None is considered false, explicitly check for it if attr is None: - print 'attr is None!' + print('attr is None!') Access a Dictionary Element ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -615,9 +615,9 @@ or pass a default argument to :py:meth:`dict.get`. d = {'hello': 'world'} if d.has_key('hello'): - print d['hello'] # prints 'world' + print(d['hello']) # prints 'world' else: - print 'default_value' + print('default_value') **Good**: @@ -625,12 +625,12 @@ or pass a default argument to :py:meth:`dict.get`. d = {'hello': 'world'} - print d.get('hello', 'default_value') # prints 'world' - print d.get('thingy', 'default_value') # prints 'default_value' + print(d.get('hello', 'default_value')) # prints 'world' + print(d.get('thingy', 'default_value')) # prints 'default_value' # Or: if 'hello' in d: - print d['hello'] + print(d['hello']) Short Ways to Manipulate Lists ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -781,7 +781,7 @@ Use :py:func:`enumerate` keep a count of your place in the list. a = [3, 4, 5] for i, item in enumerate(a): - print i, item + print(i, item) # prints # 0 3 # 1 4 @@ -802,7 +802,7 @@ files for you. f = open('file.txt') a = f.read() - print a + print(a) f.close() **Good**: @@ -811,7 +811,7 @@ files for you. with open('file.txt') as f: for line in f: - print line + print(line) The ``with`` statement is better because it will ensure you always close the file, even if an exception is raised inside the ``with`` block.