background preloader

Dev python

Facebook Twitter

Python: "subject" not shown when sending email using smtplib module. L’encoding en Python, une bonne fois pour toute. J’avais oublié la zik, je rajoute: Vous avez tous un jour eu l’erreur suivante : UnicodeDecodeError: 'machine' codec can't decode character 'trucmuche' in position x: ordinal not in range(z) Et là, pour vous en sortir, vous en avez chié des ronds de pâté.

L’encoding en Python, une bonne fois pour toute

Le problème vient du fait que la plupart du temps, ignorer l’encoding marche : nous travaillons dans des environnements homogènes et toujours avec des données dans le même format, ou un format plus ou moins compatible. Mais le texte, c’est compliqué, terriblement compliqué, et le jour où ça se gâte, si vous ne savez pas ce que vous faites, vous ne vous en sortirez pas. C’est d’autant plus vrai en Python car : Par défaut, Python plante sur les erreurs d’encoding là où d’autres langages (comme le PHP) se débrouillent pour vous sortir un truc (qui ne veut rien dire, qui peut corrompre toute votre base de données, mais qui ne plante pas).Python est utilisé dans des environnements hétérogènes.

Règle numéro 1 : Le texte brut n’existe pas. Python Regular Expressions. A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern.

Python Regular Expressions

Regular expressions are widely used in UNIX world. The module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression.

The match Function This function attempts to match RE pattern to string with optional flags. Here is the syntax for this function − re.match(pattern, string, flags=0) Here is the description of the parameters: The re.match function returns a match object on success, None on failure. Example #! Python Networking Programming. Python provides two levels of access to network services.

Python Networking Programming

At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols. Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. This chapter gives you understanding on most famous concept in Networking - Socket Programming. Python Networking Programming. Python Object Oriented. Python has been an object-oriented language since it existed.

Python Object Oriented

Because of this, creating and using classes and objects are downright easy. Python Sending Email using SMTP. Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers.

Python Sending Email using SMTP

Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail − import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) Unicode encoding and decoding — Plone Documentation v4.3. Introduction: Why unicode is difficult?

Unicode encoding and decoding — Plone Documentation v4.3

Python 2.x does not make a clear distinction between: 8-bit strings (byte data)16-bit unicode strings (character data) Developers use these two formats interchangeably, because it is so easy and Python does not warn you about this. However, it will only work as long as the input does not encounter any international, non-ASCII, characters. When 8-bit encoded string data and 16-bit raw Unicode string data gets mixed up, by being run through encoding first, really nasty things start to happen.

Step 011 - Keys, Rotating and Zoom [The Python Game Book] Code Discussion Pressed Keys Surfaces can not only moved around, but also rotated and -unlike beer mats- zoomed.

Step 011 - Keys, Rotating and Zoom [The Python Game Book]

The next source code examples introduce a new method of keyboard control. Instead of checking a queued event with pygame.event.get() the function pygame.key.get_pressed() delivers the actual state of the complete keyboard. This state is represented by a tuple of 0/1 values for each key. Pressedkeys = pygame.key.get_pressed()if pressedkeys[pygame.K_x]: do_something() checks if the key “x” is pressed. In the source code example bleow, the cursor keys ← ↑ → ↓ are used to move the snake surface, while the keys W and S zoom/shrink the snake and the keys A and D rotate the snake. It is possible to press several keys together, like left and right cursor, and the program will move the correctly (not at all in this case): dx, dy = 0, 0 # no cursor key, no movement if pressedkeys[pygame.K_LEFT]: dx -= speed if pressedkeys[pygame.K_RIGHT]: dx += speed.

Log watcher (tail -F *.log) « Python recipes « ActiveState Code. Basic usage.

Log watcher (tail -F *.log) « Python recipes « ActiveState Code

Audio - Play a Sound with Python. For Beginners. Welcome! Are you completely new to programming? If not then we presume you will be looking for information about why and how to get started with Python. Fortunately an experienced programmer in any programming language (whatever it may be) can pick up Python very quickly. It's also easy for beginners to use and learn, so jump in! Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information.

If you have a question, it's a good idea to try the FAQ, which answers the most commonly asked questions about Python. If you want to help to develop Python, take a look at the developer area for further information. What python library to use for non-blocking audio I/O on OSX?