diff --git a/_images/crying-on-sofa1.gif b/_images/crying-on-sofa1.gif new file mode 100644 index 0000000..066388b Binary files /dev/null and b/_images/crying-on-sofa1.gif differ diff --git a/_images/logo-horizontal-claro.png b/_images/logo-horizontal-claro.png new file mode 100644 index 0000000..ef3e9d4 Binary files /dev/null and b/_images/logo-horizontal-claro.png differ diff --git a/_images/logo-horizontal-escuro.png b/_images/logo-horizontal-escuro.png new file mode 100644 index 0000000..f91abd8 Binary files /dev/null and b/_images/logo-horizontal-escuro.png differ diff --git a/_images/snake-six.jpg b/_images/snake-six.jpg new file mode 100644 index 0000000..059e7cb Binary files /dev/null and b/_images/snake-six.jpg differ diff --git a/python23six.html b/python23six.html index 548393a..26a1670 100644 --- a/python23six.html +++ b/python23six.html @@ -1,145 +1,162 @@ -
- - -python -3 [script]
2to3
2to3
arrumaprint
não vai funcionar (é função)iter*
não vai funcionarurlib
mudou tudourlparse
mudou tudoxrange
agora é range
e o velho range
não existe mais.raw_input
mudou(%
para formatar strings
- retornou, 2to3
não muda para format()
)
- ... mas agora o código pode ter ficado incompatível com - Python 2... -
-python -3 [script]
2to3
2to3
arrumaprint
não vai funcionar (é função)iter*
não vai funcionarurlib
mudou tudourlparse
mudou tudoxrange
agora é range
e o velho range
não existe mais.raw_input
mudou(%
para formatar strings
+ retornou, 2to3
não muda para format()
)
__future__
!+ ... mas agora o código pode ter ficado incompatível com + Python 2... +
+import collections
+
+
+ "Minha infra usa Centos 4 e não tem Python 3 pra Centos!"
+
+
+
+
+ "Eu mantenho uma aplicação Python 2.6!"
+
+
+
+
+ Six ao resgate!
+
+ e seu fiel companheiro __future__
!
+
+ ...mas apenas para aqueles com Python 2.7.
+
+
+
+ Exemplo (estúpido)
+ import collections
class Model(object):
def __init__(self, word):
@@ -155,177 +172,234 @@ class Model(object):
def word(self, word):
self._word = word
self._count = collections.Counter(word)
-
+
-
-
+
+
@property
def letters(self):
return self._count
def __getitem__(self, pos):
return self._count[pos]
-
-
+
+
-
-
+
+
if __name__ == "__main__":
word = Model('This is an ex-parrot')
for letter, count in word.letters.iteritems():
print letter, count
-
-
-
-
- E Python 3?
+
+
- class Model(object):
-
- Todas as classes são new style, mas Python 3 não
- reclama disso.
-
class Model(object):
Todas as classes são new style, mas Python 3 não + reclama disso.
+for letter, count in word.letters.iteritems():
iteritems()
não existe mais no Python 3.
Python 3 tem apenas items()
,
- que funciona de forma diferente no Python 2.
items()
no Python 2 gera uma
- lista inteira nova ao invés de ser um iterador.
for letter, count in word.letters.iteritems():
iteritems()
não existe mais no Python 3.
Python 3 tem apenas items()
,
+ que funciona de forma diferente no Python 2.
items()
no Python 2 gera uma
+ lista inteira nova ao invés de ser um iterador.
print letter, count
print letter, count
print
agora é uma função.
print
agora é uma função.
Algumas funcionalidades mudaram (por exemplo, não há - mais espaço entre itens).
-Algumas funcionalidades mudaram (por exemplo, não há + mais espaço entre itens).
+
+
class Model(object):
-
+
- Não precisa fazer nada.
-Não precisa fazer nada.
+
+
print letter, count
-
+
-
+
from __future__ import print_function
[...]
print('{} {}'.format(letter, count))
-
-
+
print('{letter} {count}'.format(letter=letter,
count=count))
-
-
-
-
+
import six
[...]
six.print_('{letter} {count}'.format(letter=letter,
count=count))
-
+
-
-
+
for letter, count in word.letters.iteritems():
-
+
-
+
import six
[...]
for letter, count in six.iteritems(word.letters):
-
-
... o código era estúpido mesmo...
-... o código era estúpido mesmo...
+import urllib
+import urlparse
+
+def add_querystring(url, querystring, value):
+ frags = list(urlparse.urlsplit(url))
+ query = frags[3]
+ query_frags = urlparse.parse_qsl(query)
+ query_frags.append((querystring, value))
+ frags[3] = urllib.urlencode(query_frags)
+ return urlparse.urlunsplit(frags)
+
+if __name__ == "__main__":
+ print add_querystring('http://python.org', 'doc', 'urllib')
+ print add_querystring('http://python.org?doc=urllib',
+ 'page', '2')
+ urlparse
foi "juntada" com urllib
.
Coisas da urllib
mudaram de lugar.
import six
+
+def add_querystring(url, querystring, value):
+ frags = list(six.moves.urllib.parse.urlsplit(url))
+ query = frags[3]
+ query_frags = six.moves.urllib.parse.parse_qsl(query)
+ query_frags.append((querystring, value))
+ frags[3] = six.moves.urllib.parse.urlencode(query_frags)
+ return six.moves.urllib.parse.urlunsplit(frags)
+
+if __name__ == "__main__":
+ six.print_(add_querystring('http://python.org', 'doc', 'urllib'))
+ six.print_(add_querystring('http://python.org?doc=urllib',
+ 'page', '2'))
+ E antes que alguém pergunte, + é 2 *3, não 2 +3.
+