Tuesday, July 31, 2012

Microsoft Word 2007: Automatic content table

Go to References tab > Content Table > insert content table.
 PD: you have to handle the styles to be able to generate automatic content table

Here is a video tutorial about that:
http://www.youtube.com/watch?v=8sMzWVGiBQ4

Monday, July 30, 2012

Python: split string by given separator

use .split() method:


myString = "1-2-3-4-5-6-7-8"
mySplittedStringArray = myString.split('-')
print mySplittedStringArray # ['1', '2', '3', '4', '5', '6', '7', '8']


Resource:
http://www.tutorialspoint.com/python/string_split.htm
http://jolthgs.wordpress.com/2012/02/12/metodos-join-y-split-en-python/

MSSQL 2008 : Cannot open database requested by the login. The login failed

I got this error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "ICBF_GDOC_TESTS" requested by the login. The login failed. (4060) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "ICBF_GDOC_TESTS" requested by the login. The login failed. (4060)') None None.

In my case, the issue was that the database name was wrong , that was all :/

Wednesday, July 25, 2012

PHP: decode HTML special Chars


<?php
$str = "<p>this -&gt; &quot;</p>\n";


echo htmlspecialchars_decode($str); // <p>this -> "</p>


// note that here the quotes aren't converted
echo htmlspecialchars_decode($str, ENT_NOQUOTES); // <p>this -> &quot;</p>
?>


Reference:
http://php.net/manual/en/function.htmlspecialchars-decode.php

Tuesday, July 24, 2012

Python: Pass a method as a parameter


def method1():
    return 'hello world'


def method2(methodToRun):
    result = methodToRun()
    return result


method2(method1)


Reference:
http://stackoverflow.com/questions/706721/how-do-i-pass-a-method-as-a-parameter-in-python

Python: weird SyntaxError: invalid syntax

The code was apparently normal:

def myFunctionTestOK():
   return "hi there ok"

def myFunctionTestFail():
   return "hi there fail"

and the error was:

Traceback (most recent call last):  
  File "C:\mytest.py", line 5
    return "hi there fail"
                          ^
SyntaxError: invalid syntax

I was using the Pydev Eclipse Plugin. After a lot of time trying to identify the error I decide to open the file using Notepad ++ http://notepad-plus-plus.org/

clicking in the 'show all characters' button:


I found out that there was missing a (LF) just after the line return "hi there fail":




so I put the cursor just after "hi there fail" and pressed enter:




And finally deleted the line with the single (CR):




Now everything works smoothly.

Python: UnicodeEncodeError: 'ascii' codec can't encode character in position : ordinal not in range(128)

If you get this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

Use the .encode() method instead of using str():


a = u'bats\u00E0'
print a # batsà
print str(a) # UnicodeEncodeError: 'ascii' codec can't encode ...
print a.encode('utf-8') # batsà


Reference:
http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20