Posts for: #Python

Asyncio e Coroutines

Continuando a série sobre o módulo asyncio do Python 3.4, vou apresentar as corotinas e como elas simplificam a escrita de nossos programas com o loop de eventos.

Modificamos também a chamada de execução da corotina, pois agora utilizamos loop.run_until_complete para iniciar nossa corotina principal. Aproveitamos para colocar tudo entre um try-finally para terminar a execução do loop corretamente (mesmo em caso de exceção). Perceba que no exemplo anterior, com call_soon, passamos a função e seus parâmetros, mas não executamos a função em si. No caso de run_until_complete, estamos passando o retorno da chamada de print_and_repeat que é uma corotina, uma vez que a marcamos com o decorador @asyncio.coroutine.

Read more

Asyncio e Coroutines

Continuando a série sobre o módulo asyncio do Python 3.4, vou apresentar as corotinas e como elas simplificam a escrita de nossos programas com o loop de eventos.

Modificamos também a chamada de execução da corotina, pois agora utilizamos loop.run_until_complete para iniciar nossa corotina principal. Aproveitamos para colocar tudo entre um try-finally para terminar a execução do loop corretamente (mesmo em caso de exceção). Perceba que no exemplo anterior, com call_soon, passamos a função e seus parâmetros, mas não executamos a função em si. No caso de run_until_complete, estamos passando o retorno da chamada de print_and_repeat que é uma corotina, uma vez que a marcamos com o decorador @asyncio.coroutine.

Read more

Asyncio - Asynchronous Methods in Python

With the release of Python 3.4, I updated my book on Introduction to Programming with Python. Some topics fall outside the scope of the book, which is intended for beginners. I’m going to start writing a series of short posts about some interesting topics that I think are worth discussing and might even be the basis for a new book.

One of the new features in Python 3.4 is the asyncio module, which brings various routines for calling asynchronous methods in Python. Asynchronous programming is a bit different from what we’re used to writing in Python, but it’s an excellent alternative to using threads and a good choice for solving problems with many inputs or outputs (I/O).

Read more

Asyncio - Asynchronous Methods in Python

With the release of Python 3.4, I updated my book on Introduction to Programming with Python. Some topics fall outside the scope of the book, which is intended for beginners. I’m going to start writing a series of short posts about some interesting topics that I think are worth discussing and might even be the basis for a new book.

One of the new features in Python 3.4 is the asyncio module, which brings various routines for calling asynchronous methods in Python. Asynchronous programming is a bit different from what we’re used to writing in Python, but it’s an excellent alternative to using threads and a good choice for solving problems with many inputs or outputs (I/O).

Read more

Python Computer and Raspberry Pi

Preparation

On July 30, I received my first Raspberry Pi, purchased at Element 14. After a one-month wait, it arrived in the mail, albeit late from Belgium – 9 pm. In reality, it arrived around lunchtime, but since I wasn’t home, the postman left it as regular mail.

P1060883

The box is small, like a credit card. In fact, I found the computer amidst other letters.

[Open-mouthed smile]

Read more

Python Computer and Raspberry Pi

Preparation

On July 30, I received my first Raspberry Pi, purchased at Element 14. After a one-month wait, it arrived in the mail, albeit late from Belgium – 9 pm. In reality, it arrived around lunchtime, but since I wasn’t home, the postman left it as regular mail.

P1060883

The box is small, like a credit card. In fact, I found the computer amidst other letters.

[Open-mouthed smile]

Read more

Why UTF-8 and Not ASCII for Portuguese? (PART II)

Continuation of the post, originally made in the Python-Brasil list:

I’ll try again, as the thread has already discussed three different things:

  1. Code to use in Python programs: why UTF-8 is highly recommended
  2. Encodings in general and problems caused and solved by it
  3. A bug in Python on Windows, when the prompt is set to page 65001

I’ll try to explain for everyone, as it’s a recurring topic.

But before getting back to these topics, we have to go back to files.

Read more

Why UTF-8 and Not ASCII for Portuguese? (PART II)

Continuation of the post, originally made in the Python-Brasil list:

I’ll try again, as the thread has already discussed three different things:

  1. Code to use in Python programs: why UTF-8 is highly recommended
  2. Encodings in general and problems caused and solved by it
  3. A bug in Python on Windows, when the prompt is set to page 65001

I’ll try to explain for everyone, as it’s a recurring topic.

But before getting back to these topics, we have to go back to files.

Read more

Why UTF-8 and Not ASCII for Portuguese? (PART I)

A fellow blogger on Python-Brasil:

The colleagues have already talked about why UTF-8.

I just want to remind that the subject is more complicated than it seems, for example in Python 2.7:

# -*- coding: utf-8 -*-
print "Accents: áéíóúãõç"
print u"Accents2: áéíóúãõç"

Run the program above on Windows, either through IDLE or console:

C:\Users\nilo\Desktop>\Python27\python.exe test.py
Accents: ├í├®├¡├│├║├º├ú├Á
Accents2: áéíóúçãõ

You should have obtained good results only on the Accents2 line. If the string is not marked with unicode, it will be simply printed as a sequence of bytes, without translation. If you have u in front, like in accents2, Python gets that it needs to translate from unicode to cp850, in this case of console here at home. Already on Linux, both lines produce correct results!

Read more

Why UTF-8 and Not ASCII for Portuguese? (PART I)

A fellow blogger on Python-Brasil:

The colleagues have already talked about why UTF-8.

I just want to remind that the subject is more complicated than it seems, for example in Python 2.7:

# -*- coding: utf-8 -*-
print "Accents: áéíóúãõç"
print u"Accents2: áéíóúãõç"

Run the program above on Windows, either through IDLE or console:

C:\Users\nilo\Desktop>\Python27\python.exe test.py
Accents: ├í├®├¡├│├║├º├ú├Á
Accents2: áéíóúçãõ

You should have obtained good results only on the Accents2 line. If the string is not marked with unicode, it will be simply printed as a sequence of bytes, without translation. If you have u in front, like in accents2, Python gets that it needs to translate from unicode to cp850, in this case of console here at home. Already on Linux, both lines produce correct results!

Read more