Caso di studio: accesso a dati remoti

Transcript

Caso di studio: accesso a dati remoti
Caso di studio:
accesso a dati remoti
Prof. Mauro Gaspari: [email protected]
Mauro Gaspari - University of Bologna - [email protected]
DB Locali vs DB Remoti
●
●
Accesso locale: si scarica una copia del DB nel
proprio PC e poi si accede direttamente ai dati
che contiene.
Accesso on line: utilizzando un DB su un server
remoto.
Mauro Gaspari - University of Bologna - [email protected]
Access to remote data
●
Utilizzando un programma (uno script Python):
–
pyODBC (Open Database Connectivity) supporto per l'accesso
a DB locali e romoti (utilizza SQL).
–
Web services (chiamate di funzione basate su web):
●
●
xignite (http://www.xignite.com/)
yahoo finance (http://finance.yahoo.com/)
Mauro Gaspari - University of Bologna - [email protected]
Web vs Web Services
Ordinary Web
HTTP Request
Browser
HTTP Response
Webserver
Application
Webserver
Client PC
HTML or Other Document
Ordinary
Ordinary web
web was
was created
created to
to download
download documents
documents
Mauro Gaspari - University of Bologna - [email protected]
Web Services
HTTP Request
Python
Program
HTTP Response
Service
Object
Client PC
Message
Web
Web services,
services, in
in contrast
contrast to
to web,
web,
provide
provide program-to-program
program-to-program communication.
communication.
Programs
Programs on
on the
the webserver
webserver are
are called
called service
service objects.
objects.
Messages
Messages follow
follow ad-hoc
ad-hoc formats
formats (HTTP,
(HTTP, XML
XML –– SOAP).
SOAP).
Mauro Gaspari - University of Bologna - [email protected]
Webserver
Web Services
Parameters
Python
program
Result of Calculation
Service
Object
Client PC
Message
The
The sending
sending program
program sends
sends parameters
parameters for
for the
the calculation.
calculation.
The
The service
service object
object does
does the
the calculations
calculations and
and sends
sends back
back results.
results.
Mauro Gaspari - University of Bologna - [email protected]
Webserver
ystockquote
●
Python API per Yahoo Finance.
●
Basata su HTTP (non usa XML).
●
Getting started:
–
Scaricare e salvare ystockquote.py da
http://goldb.org/ystockquote.html nella cartella dove
ci sono i programmi Python.
–
Connettersi a Internet.
–
Eseguire python ed importare il modulo ystockquote:
>>> import ystockquote
Mauro Gaspari - University of Bologna - [email protected]
Funzioni di ystockquote
●
Recuperare il prezzo di un azione: ystockquote.get_price('symbol')
I simboli delle azioni sono disponibili su yahoo finance
restituisce un valore floating point.
●
Altre caratteristiche come: cambio valute, volume ...
●
E' possibile recuperare tutti I dati di un certo simbolo:
ystockquote.get_all('symbol')
restituisce un dizionario.
●
Dati storici: get_historical_prices('symbol','start_date', 'end_date')
una data e' una stringa con la forma: 'yyyymmdd'
restituisce una lista di liste.
Mauro Gaspari - University of Bologna - [email protected]
Esempi cambio valuta
●
Currencies:
get_price('usdeur=x')
– get_price('eurusd=x')
–
Mauro Gaspari - University of Bologna - [email protected]