analitics

Pages

Tuesday, October 2, 2012

Tuples can put you in difficulty.

They are two examples of sequence data types in python.

Today I will tell about tuples.

A tuple consists of a number of values separated by commas.

The biggest problem is when we do not know the number of these values ​​or type.

Let me illustrate with a function that returns a tuple.

This is a known tuple but we will use like an unknown tuple.

Let's see the python code and some errors:

>>> import sys
>>> if hasattr(sys, 'version_info'):
...     print sys.version_info()
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable

Is very correct to got this error.

And the result using the print function:

>>> if hasattr(sys, 'version_info'):
...     print "%s" % str(sys.version_info)
... 
(2, 6, 4, 'final', 0) 

>>> if hasattr(sys, 'version_info'):
...     sys.stderr.write("%s" % str(sys.version_info))
... 
(2, 6, 4, 'final', 0)>>> 

or if you can use the repr function.


>>> if hasattr(sys, 'version_info'):
...     sys.stderr.write("%s" % repr(sys.version_info))
... 
(2, 6, 4, 'final', 0)>>> 

The official Python documentation says __repr__ is used to compute the “official” string representation of an object and __str__ is used to compute the “informal” string representation of an object.

In my opinion , the correct way it's to use repr. For example:

>>> import datetime
>>> today = datetime.datetime.now()
>>> str(today)
'2012-10-02 22:45:57.634977'
>>> repr(today)
'datetime.datetime(2012, 10, 2, 22, 45, 57, 634977)'

As we see all values from tuples it's show.

Sunday, September 23, 2012

Setting up Vim to work with Python - part 2

You can customize Vim for editing a specific file.

In this case is our python script files.

To do that you can use modelines, or auto commands, or filetype plugins.

For example , if you want to see the number on rows then just set the boolean number option to true.

set number

The keys PageUp and PageDown are mapped to Ctr+U and Ctr+D.

The next changes of the vimrc file will move cursor only by half of screen.


map <PageUp> <C-U>
map <PageDown> <C-D>
imap <PageUp> <C-O><C-U>
imap <PageDown> <C-O><C-D>

To move the cursor to the end of the screen they have to be triggered twice:

map <PageUp> <C-U><C-U>
map <PageDown> <C-D><C-D>
imap <PageUp> <C-O><C-U><C-O><C-U>
imap <PageDown> <C-O><C-D><C-O><C-D>

Then you can add next line to prevent the cursor from changing the current column when jumping to other lines within the window.

set nostartofline

To add some lines on your python file.

For example many python files start with this:

#!/usr/bin/env python 
#-*- coding: utf-8 -*-

Just add the next line in your vimrc file.

:iabbrev newpy  #!/usr/bin/env python <cr>#-*- coding: utf-8 -*-

On your first line from your python file in the insert mode , write newpy and press Enter key.

Also will be a good idea to take a look at the official .vimrc for following PEP 7 & 8 conventions.

...and the result of this changes.