I am newbie in object-oriented design.
I try to incorporate a python function into my init . (Function works, as I asked earlier).
Here is my code:
import struct
import urllib2
import StringIO
class wallpaper:
def __init__(self, url):
self.url = url
self.content_type = ''
self.height = 0
self.width = 0
image = urllib2.urlopen(self.url)
data = str(image.read(2))
if data.startswith('\377\330'):
self.content_type = 'image/jpeg'
jpeg = StringIO.StringIO(data)
jpeg.read(2)
b = jpeg.read(1)
try:
while (b and ord(b) != 0xDA):
while (ord(b) != 0xFF): b = jpeg.read(1)
while (ord(b) == 0xFF): b = jpeg.read(1)
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
jpeg.read(3)
h, w = struct.unpack(">HH", jpeg.read(4))
break
else:
jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
b = jpeg.read(1)
self.width = int(w)
self.height = int(h)
except struct.error:
pass
except ValueError:
pass
x = wallpaper('http://i.imgur.com/rapwX.jpg')
print x.url, "\t", x.content_type,"\t", x.height,"\t", x.width
The problem is that I cannot initialize height and width properties (they are equal 0).
Where is the problem?
Edit:
I've found problem, here is solution. Thanks for your hints.
One more question: is my approach proper as far as OOP rules are concerned? Should I compute width/height in init or create other method (eg. size, or so)?