Trying to convert 2.7 script to 3.3.2 - Need guidance

This is the place for queries that don't fit in any of the other categories.

Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby psb1967 » Sat Jun 29, 2013 6:28 pm

Hi,

I am a total beginner to programming and learning Python 3.3.2. The script I am working with, originally written in Python 2.7 by Mr. Mark, I am trying to convert it to 3.3.2. Getting the following error:

Traceback (most recent call last):
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 65, in <module>
q = GoogleIntradayQuote('AAPL',300,30)
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 53, in __init__
if csv[bar].count(',')!=5: continue
TypeError: Type str doesn't support the buffer API


There is absolutely no issue with the original 2.7 script, runs nicely in python 2.7, installed in my another laptop, but I want to make it run in 3.3.2.
Your guidance will help improve my learning.
Thanks in advance.

Suresh

Code: Select all
import urllib.request
import datetime
import time

class Quote(object):
    DATE_FMT = '%Y-%m-%d'
    TIME_FMT = '%H:%M:%S'
   
    def __init__(self):
        self.symbol = ''
        self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))
       
    def append(self,dt,open_,high,low,close,volume):
        self.data.append(dt.date())
        self.time.append(dt.time())
        self.open_.append(float(open_))
        self.high.append(float(high))
        self.low.append(float(low))
        self.close.append(float(close))
        self.volume.append(int(volume))
       
    def to_csv(self):
        return ''.join(["{0},{1},{2},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7}\n".format(self.symbol,
              self.date[bar].strftime('%Y-%m-%d'),self.time[bar].strftime('%H:%M:%S'),
              self.open_[bar],self.high[bar],self.low[bar],self.close[bar],self.volume[bar])
                        for bar in range(len(self.close))])
       
    def write_csv(self,filename):
        with open(filename, 'w') as f:
            f.write(self.to_csv())
           
    def read_csv(self,filename):
        self.symbol = ''
        self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))
        for line in open(filename, 'r'):
            symbol,ds,ts,open_,high,low,close,volume = line.rstrip().split(',')
            self.symbol = symbol
            dt = datetime.datetime.strptime(ds+' '+ts,self.DATE_FMT+''+self.TIME_FMT)
            self.append(dt, open_, high, low, close, volume)
        return True
       
    def __repr__(self):
        return self.to_csv()
   
class GoogleIntradayQuote(Quote):
    ''' Intraday quotes from Google. Specify interval seconds and number of days '''
    def __init__(self,symbol,interval_seconds=300,num_days=5):
        super(GoogleIntradayQuote,self).__init__()
        self.symbol = symbol.upper()
        url_string = "http://www.google.com/finance/getprices?q={0}".format(self.symbol)
        url_string += "&i={0}&p={1}d&f=d,o,h,l,c,v".format(interval_seconds,num_days)
        csv = urllib.request.urlopen(url_string).readlines()
        for bar in range(7,len(csv)):
            if csv[bar].count(',')!=5: continue
            offset,close,high,low,open_,volume = csv[bar].split(',')
            if offset[0]=='a':
                day = float(offset[1:])
                offset = 0
            else:
                offset = float(offset)
            open_,high,low,close = [float(x) for x in [open_,high,low,close]]
            dt = datetime.datetime.fromtimestamp(day+(interval_seconds*offset))
            self.append(dt, open_, high, low, close, volume)
           
if __name__ == '__main__':
    q = GoogleIntradayQuote('AAPL',300,30)
    print(q)
           
Last edited by psb1967 on Sat Jun 29, 2013 9:15 pm, edited 1 time in total.
psb1967
 
Posts: 4
Joined: Sun Jun 23, 2013 5:06 am
Location: Chennai, India

Re: Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby metulburr » Sat Jun 29, 2013 6:41 pm

Code: Select all
if csv[bar].decode().count(',')!=5: continue


http://blog.ludovf.net/python-str-unicode/
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby psb1967 » Sat Jun 29, 2013 6:56 pm

metulburr wrote:
Code: Select all
if csv[bar].decode().count(',')!=5: continue


http://blog.ludovf.net/python-str-unicode/


Hello Metulburr,

Thanks for your instant response. After replacing the above code, getting a new error:

Traceback (most recent call last):
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 66, in <module>
q = GoogleIntradayQuote('AAPL',300,30)
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 55, in __init__
offset,close,high,low,open_,volume = csv[bar].split(',')
TypeError: Type str doesn't support the buffer API
psb1967
 
Posts: 4
Joined: Sun Jun 23, 2013 5:06 am
Location: Chennai, India

Re: Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby Crimson King » Sat Jun 29, 2013 7:07 pm

psb1967 wrote:
metulburr wrote:
Code: Select all
if csv[bar].decode().count(',')!=5: continue


http://blog.ludovf.net/python-str-unicode/


Hello Metulburr,

Thanks for your instant response. After replacing the above code, getting a new error:

Traceback (most recent call last):
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 66, in <module>
q = GoogleIntradayQuote('AAPL',300,30)
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 55, in __init__
offset,close,high,low,open_,volume = csv[bar].split(',')
TypeError: Type str doesn't support the buffer API



You got the same typeError as before, i'd assume that changing
Code: Select all
offset,close,high,low,open_,volume = csv[bar].split(',')
to
Code: Select all
offset,close,high,low,open_,volume = csv[bar].decode().split(',')
would do the trick.
User avatar
Crimson King
 
Posts: 118
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby psb1967 » Sat Jun 29, 2013 7:30 pm

Crimson King wrote:
psb1967 wrote:
metulburr wrote:
Code: Select all
if csv[bar].decode().count(',')!=5: continue


http://blog.ludovf.net/python-str-unicode/


Hello Metulburr,

Thanks for your instant response. After replacing the above code, getting a new error:

Traceback (most recent call last):
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 66, in <module>
q = GoogleIntradayQuote('AAPL',300,30)
File "C:\Users\sureshdell\workspace\TestPyProject\src\googletest.py", line 55, in __init__
offset,close,high,low,open_,volume = csv[bar].split(',')
TypeError: Type str doesn't support the buffer API



You got the same typeError as before, i'd assume that changing
Code: Select all
offset,close,high,low,open_,volume = csv[bar].split(',')
to
Code: Select all
offset,close,high,low,open_,volume = csv[bar].decode().split(',')
would do the trick.


Hello King,

Thanks for yet another super response. I was just trying the same with less confidence but your reply did do to trick. Now, there are no errors, but when I run the script, the console is totally blank.
psb1967
 
Posts: 4
Joined: Sun Jun 23, 2013 5:06 am
Location: Chennai, India

Re: Trying to convert 2.7 script to 3.3.2 - Need guidance

Postby psb1967 » Sat Jun 29, 2013 8:46 pm

Hello Metulburr, King,

Problem SOLVED.

There was a typographic error; didn't enter "self.close.append(float(close))" in my script (corrected it in my first post); due to that error I was getting blank screen when the script was executed. Now it is solved.

Thanks once again to both of you for your immense patience and cooperation.

Pleasant weekend.

Suresh
psb1967
 
Posts: 4
Joined: Sun Jun 23, 2013 5:06 am
Location: Chennai, India


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 6 guests