Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79184 > unrolled thread

Question About Running Python code

Started byryguy7272 <ryanshuell@gmail.com>
First post2014-10-15 15:50 -0700
Last post2014-10-16 09:11 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Question About Running Python code ryguy7272 <ryanshuell@gmail.com> - 2014-10-15 15:50 -0700
    Re: Question About Running Python code Dan Stromberg <drsalists@gmail.com> - 2014-10-15 16:09 -0700
    Re: Question About Running Python code Andrew Jaffe <a.h.jaffe@gmail.com> - 2014-10-16 09:11 +0100

#79184 — Question About Running Python code

Fromryguy7272 <ryanshuell@gmail.com>
Date2014-10-15 15:50 -0700
SubjectQuestion About Running Python code
Message-ID<f5931a20-dfc0-4ebd-b205-b3e0e966b634@googlegroups.com>
I'm trying to run this script (using IDLE 3.4)

#!/usr/bin/env python

import urllib2
import pytz
import pandas as pd

from bs4 import BeautifulSoup
from datetime import datetime
from pandas.io.data import DataReader


SITE = "http://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
START = datetime(1900, 1, 1, 0, 0, 0, 0, pytz.utc)
END = datetime.today().utcnow()


def scrape_list(site):
    hdr = {'User-Agent': 'Mozilla/5.0'}
    req = urllib2.Request(site, headers=hdr)
    page = urllib2.urlopen(req)
    soup = BeautifulSoup(page)

    table = soup.find('table', {'class': 'wikitable sortable'})
    sector_tickers = dict()
    for row in table.findAll('tr'):
        col = row.findAll('td')
        if len(col) > 0:
            sector = str(col[3].string.strip()).lower().replace(' ', '_')
            ticker = str(col[0].string.strip())
            if sector not in sector_tickers:
                sector_tickers[sector] = list()
            sector_tickers[sector].append(ticker)
    return sector_tickers


def download_ohlc(sector_tickers, start, end):
    sector_ohlc = {}
    for sector, tickers in sector_tickers.iteritems():
        print 'Downloading data from Yahoo for %s sector' % sector
        data = DataReader(tickers, 'yahoo', start, end)
        for item in ['Open', 'High', 'Low']:
            data[item] = data[item] * data['Adj Close'] / data['Close']
        data.rename(items={'Open': 'open', 'High': 'high', 'Low': 'low',
                           'Adj Close': 'close', 'Volume': 'volume'},
                    inplace=True)
        data.drop(['Close'], inplace=True)
        sector_ohlc[sector] = data
    print 'Finished downloading data'
    return sector_ohlc


def store_HDF5(sector_ohlc, path):
    with pd.get_store(path) as store:
        for sector, ohlc in sector_ohlc.iteritems():
            store[sector] = ohlc


def get_snp500():
    sector_tickers = scrape_list(SITE)
    sector_ohlc = download_ohlc(sector_tickers, START, END)
    store_HDF5(sector_ohlc, 'snp500.h5')


if __name__ == '__main__':
    get_snp500()


I got it from this link.
http://www.thealgoengineer.com/2014/download_sp500_data/

I'm just about going insane here.  I've been doing all kinds of computer programming for 11 years, and I know 10 languages.  I'm trying to learn Python now, but this makes no sense to me.  

I would be most appreciative if someone could respond to a few questions.

The error that I get is this.
'invalid syntax'

The second single quote in this line is highlighted pink.
print 'Downloading data from Yahoo for %s sector' % sector

#1)  That's very bizarre to mix single quotes and double quotes in a single language.  Does Python actually mix single quotes and double quotes?  

#2)  In the Python 3.4 Shell window, I turn the debugger on by clicking 'Debugger'.  Then I run the file I just created; it's called 'stock.py'.  I get the error immediately, and I can't debug anything so I can't tell what's going on.  This is very frustrating.  All the controls in the debugger are greyed out.  What's up with the debugger?

#3)  My final question is this?  How do I get this code running?  It seems like there is a problem with a single quote, which is just silly.  I can't get the debugger working, so I can't tell what's going on.  The only thins I know, or I think I know, is that the proper libraries seem to be installed, so that's one thing that's working.


I'd really appreciate it if someone could please answer my questions and help me get this straightened out, so I can have some fun with Python.  So far, I've spent 2 months reading 4 books, and trying all kinds of sample code...and almost every single thing fails and I have no idea why.

[toc] | [next] | [standalone]


#79185

FromDan Stromberg <drsalists@gmail.com>
Date2014-10-15 16:09 -0700
Message-ID<mailman.14901.1413414563.18130.python-list@python.org>
In reply to#79184
On Wed, Oct 15, 2014 at 3:50 PM, ryguy7272 <ryanshuell@gmail.com> wrote:
> I'm trying to run this script (using IDLE 3.4)

> I would be most appreciative if someone could respond to a few questions.
>
> The error that I get is this.
> 'invalid syntax'

You may get better help if you give the context of this message.

> The second single quote in this line is highlighted pink.
> print 'Downloading data from Yahoo for %s sector' % sector
>
> #1)  That's very bizarre to mix single quotes and double quotes in a single language.  Does Python actually mix single quotes and double quotes?

I'm not sure what you mean by "mix".  C uses single quotes and double
quotes, right?

Python treats single quotes and double quotes pretty much
interchangeably, except if you start a string with a single quote (for
example), you can easily put a double quote inside it, and you must
terminate the string with another single quote.  And vice versa.

> #2)  In the Python 3.4 Shell window, I turn the debugger on by clicking 'Debugger'.  Then I run the file I just created; it's called 'stock.py'.  I get the error immediately, and I can't debug anything so I can't tell what's going on.  This is very frustrating.  All the controls in the debugger are greyed out.  What's up with the debugger?

You have Python 2.x code there.  The differences between 2.x and 3.x
are far from insurmountable, but it does require a little code
adjustment.  If you're a python novice, you might be better off
running this under 2.x.

I believe your debugger won't help until your code compiles.

> #3)  My final question is this?  How do I get this code running?  It seems like there is a problem with a single quote, which is just silly.  I can't get the debugger working, so I can't tell what's going on.  The only thins I know, or I think I know, is that the proper libraries seem to be installed, so that's one thing that's working.

The print statement in 2.x has been made a print function in 3.x;
that's likely necessary to fix, though not necessarily sufficient.

EG in 2.x:
print 'hello word', 6

while in 3.x that would be:
print('hello world', 6)

Interestingly, you can write a single command that works in both with:
print('hello world {}'.format(6))

To 2.x, it's printing the result of a parenthesized expression.  To
3.x, it's a function call with one argument.


> I'd really appreciate it if someone could please answer my questions and help me get this straightened out, so I can have some fun with Python.  So far, I've spent 2 months reading 4 books, and trying all kinds of sample code...and almost every single thing fails and I have no idea why.

You may also need to install pytz, pandas and BeautifulSoup.  I favor
pip or pip3 for such things.

[toc] | [prev] | [next] | [standalone]


#79201

FromAndrew Jaffe <a.h.jaffe@gmail.com>
Date2014-10-16 09:11 +0100
Message-ID<mailman.14915.1413447085.18130.python-list@python.org>
In reply to#79184
On 15/10/2014 23:50, ryguy7272 wrote:
> The error that I get is this.
> 'invalid syntax'
>
> The second single quote in this line is highlighted pink.
> print 'Downloading data from Yahoo for %s sector' % sector

This is a script written for Python 2.*, but you say you are using 
Python 3.4. In Python 3, "print" is a function, not a statement, so you 
need to translate this to

   print('Downloading data from Yahoo for %s sector' % sector)


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web