Dengar's Blog Logo

Dengar's blog...
We will see what I post, probably will be some random tutorials

Monday, 15 October 2012

Python on MS SQL (via pyodbc)

I have been struggling, albeit not very long, to set up a DB connection to a remote MS SQL server through Python. I thought that the process is not as straight forward as I'd like it to be, but I guess that's okay because as far as I can see the best solution is to cobble it together a bit.

Basically it seems that the best solution is not to use a python end to end module as a driver, but to use features that are available otherwise, such as ODBC. Then use a ODBC driver for the MS SQL engine and then use ODBC within Python.

As a Linux person, I first had to install unixodbc:
sudo apt-get install unixodbc unixodbc-dev
This takes care of you being able to use ODBC to connect to ODBC database servers. The other part is having a ODBC driver for the connection to the MS SQL server. There are probably others and I shall list them in due time, but for MS SQL, I found it easiest to use FreeTDS (an implementation of the Tabular Data Stream for Linux).

To install:
sudo apt-get install freedts-dev tdsodbc
With this done, you can preceed to modifying the /etc/odbcinst.ini:
sudo nano /etc/odbcinst,ini
Trace = Yes
TraceFile = /tmp/odbc.log

Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/
Setup = /usr/lib/x86_64-linux-gnu/odbc/
CPTimeout =
CPReuse =
UsageCount = 1
Please note that the paths for and are not necessarily as the ones given in this example. For one, I'm using a 64bit version, so on 32 the path is probably different. Another possible location for the files would be: /usr/lib/odbc/.
Either search for the location or seek Google to find where your distribution saves these files.

You will also need to have the python-dev stuff installed:
sudo apt-get install python-dev 
Now that the system is installed, you will want to install the pydobc module. You can download the latest version of the pyodbc module here. Extract the file where it's convenient for you and 'cd' into the folder.
To install simply do:
sudo python install

Sunday, 16 September 2012

More and more sponsored content on facebook

I logged into facebook today to see how Myko was getting on yesterday (he was on a UK game show).

Unfortunately, facebook is taken over by useless 'like' messages. All I can see is this guy likes Heinz Beans, this girl like Burberry, someone else likes Coca-Cola, Amazon,

Is there something wrong with me that I don't care what sh*t you people like? Or something with other people that need to tell everyone when they like something so trivial and unimportant like a brand? it makes me wonder how much people really like beans? A lot more than me?

Or could it be facebook being incredibly desperate to get some kind of advertising revenue that they swamp us with this nonsense? Unfortunately I seem to be unable to opt-out of these 'This c*nt likes that sh*t' messages.

Am I the only person that is bothered by all the adverts that are being communicated out to me? The way that this is done is also incredibly subliminal - first there were few messages here and there, now it's everywhere and meant to be adapted by us all.

I have never been a 'fan' of facebook, but I'm getting increasingly turned off by the whole ordeal.

Thursday, 13 September 2012

Websites won't work properly without cookies

Yet the European legislative that has passed a few weeks ago now make it mandatory to advise people how cookies are being used.
It's like stopping every customer that enters your high street store and telling him how the CCTV system works, what they are going to do with your purchase and other information and only then letting them through...

Nevermind, protecting yourself from pesky third party advertising cookies is easy. Check this page to opt out of all sorts of networks that use behavioural and remarketing cookies.

This, btw, also works through cookies!

Friday, 24 August 2012

Olympic Glory in Broadcasting

Some post Olympic humour......

Here are the top nine comments made by NBC sports commentators during the London Summer Olympics that they would like to take back:

1. Weightlifting commentator: "This is Gregoriava from Bulgaria. I saw her snatch this morning during her warm up and it was amazing."
2. Dressage commentator: "This is really a lovely horse and I speak from personal experience since I once mounted her mother."
3. Paul Hamm, Gymnast: "I owe a lot to my parents, especially my mother and father."
4. Boxing Analyst: "Sure there have been injuries, and even some deaths in boxing, but none of them really that serious."
5. Softball announcer: "If history repeats itself, I should think we can expect the same thing again."

6. Basketball analyst: "He dribbles a lot and the opposition doesn't like it. In fact you can see it all over their faces."
7. At the rowing medal ceremony: "Ah, isn't that nice, the wife of the IOC president is hugging the cox of the British crew."

8. Soccer commentator: "Julian Dicks is everywhere. It's like they've got eleven Dicks on the field."

9. Tennis commentator: "One of the reasons Andy is playing so well is that, before the final round, his wife takes out his balls and kisses them... Oh my God, what have I just said?"

Sunday, 8 January 2012

Auto Download File From FTP - then rename

A little while ago my friend asked me to help him with some Windows commandline scripting. Basically he was waiting for ages for the development team in his company to sort this simple task out and asked me how he can every day automatically download a file from FTP, then rename it with the current date.

I don't do Windows very much and Linux is just much better for exactly these things, but this seemed like an easy enough challenge.

In the end I didn't manage to do everything in one file, but I think that two should work just as well when you add them to the Windows task scheduler...

Here are the files with some explanation:


@ftp -i -s:"%~f0"&GOTO:EOF
!:--- FTP commands below here ---
mget "*.*"
The first line starts the windows command line FTP client and the parameters essentially say that everything else in the file should be ignored by the command line itself, but be parsed to the FTP client.
Line two opens the connection and line three and four will send the authentication details. Line six gets all files in the current directory. You could really do here anything you want on the remote and local server, eg: change the local directory path (lcd ...), the remote one (cd ...) send files (put) or even delete them (rm). At the end, I simply disconnect.

for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "SomeFileName.txt" SomeFileName_%%f-%%e-%%d.txt
In one line: I'm starting a for-loop and I use a tokeniser to delimit the following string into 5 (1-5) sections. %%d specifies the beginning character used for the token (d) and all that follow will be e,f,g...
then I use the %date% (in ("%date%") function to get the current date. Now, In the do part, I will rename the file from SomeFileName.txt to SomeFileName_yyyy-MM-dd.txt

I hope this makes sense and helps a few people. Best of luck...