a strange SyntaxError

C

CoolGenie

Hi!
I'm trying to write a small adesklet that will read newsfeeds. Here's
the code:

#
#####################################################################
# fparser.py
#
# P. Kaminski <[email protected]>
# Time-stamp: <>
######################################################################
import feedparser
import adesklets
from os import getenv, spawnlp, P_NOWAIT
from os.path import join, dirname

class Config(adesklets.ConfigFile):
cfg_default = { 'feedsrc' : 'http://slashdot.org/index.rss',
'numfeeds' : 5,
'numlines' : 4
}

def __init__(self, id, filename):
adesklets.ConfigFile.__init__(self, id, filename)

class Events(adesklets.Events_handler):
def __init__(self, basedir):
if len(basedir)==0:
self.basedir='.'
else:
self.basedir=basedir
adesklets.Events_handler.__init__(self)

def __del__(self):
adesklets.Events_handler.__del__(self)

def ready(self):
self.config = Config(adesklets.get_id(),
join(self.basedir, 'config.txt'))
self.feed = self.config['feedsrc']
self.numfeeds = self.config['numfeeds']
self.numlines = self.config['numlines']

self.w = 520
self.h = 12*self.numfeeds*(self.numlines+1)
adesklets.window_resize(self.w, self.h)
adesklets.window_reset(adesklets.WINDOW_UNMANAGED)
adesklets.window_set_transparency(True)
adesklets.window_show()

def quit(self):
print 'Quitting...'

def alarm(self):
print 'Alarm. Next in 360 seconds.'
self._display()
return 360

def _display(self):
print "Getting feed..."
y = 0
x = 0
d = feedparser.parse(self.feed)
print d.channel.title
print d.channel.description

# clear the buffer
buffer = adesklets.create_image(self.w, self.h)
adesklets.context_set_image(buffer)
adesklets.context_set_blend(False)
adesklets.context_set_color(0,0,0,0)
adesklets.image_fill_rectangle(0,0,self.w,self.h)
adesklets.context_set_blend(True)

adesklets.context_set_font(adesklets.load_font('Vera/7'))
adesklets.context_set_color(255, 0, 0, 255)
adesklets.text_draw(0, y, str(d.channel.title))
y+=12
l = len(d.entries)
l = min(l, self.numfeeds)
for i in range(l):
ent=d.entries
adesklets.context_set_color(255, 255, 0, 255)
adesklets.text_draw(0, y, str(ent.title))
print ent.title
y+=12
adesklets.context_set_color(255, 255, 255, 255)
for k in range(0, min(len(ent.summary)/100,
self.numlines)):
print str(ent.summary)[k*100:(k+1)*100]
adesklets.text_draw(0, y, str(ent.summary)[k*100:(k
+1)*100])
y+=12
adesklets.free_font(0)
adesklets.free_image(buffer)

Events(dirname(__file__)).pause()

Unfortunately, I'm getting this bug for some time now and I just don't
know what's going on:

File "./fparser.py", line 40
self.w = 520
^
SyntaxError: invalid syntax

Regards,
P.K.
 
J

John Machin

Hi!
I'm trying to write a small adesklet that will read newsfeeds. Here's
the code:

#
#####################################################################
# fparser.py
#
# P. Kaminski <[email protected]>
# Time-stamp: <>
######################################################################
import feedparser
import adesklets
from os import getenv, spawnlp, P_NOWAIT
from os.path import join, dirname

class Config(adesklets.ConfigFile):
cfg_default = { 'feedsrc' : 'http://slashdot.org/index.rss',
'numfeeds' : 5,
'numlines' : 4
}

def __init__(self, id, filename):
adesklets.ConfigFile.__init__(self, id, filename)

class Events(adesklets.Events_handler):
def __init__(self, basedir):
if len(basedir)==0:
self.basedir='.'
else:
self.basedir=basedir
adesklets.Events_handler.__init__(self)

def __del__(self):
adesklets.Events_handler.__del__(self)

def ready(self):
self.config = Config(adesklets.get_id(),
join(self.basedir, 'config.txt'))
self.feed = self.config['feedsrc']
self.numfeeds = self.config['numfeeds']
self.numlines = self.config['numlines']

self.w = 520
self.h = 12*self.numfeeds*(self.numlines+1)
adesklets.window_resize(self.w, self.h)
adesklets.window_reset(adesklets.WINDOW_UNMANAGED)
adesklets.window_set_transparency(True)
adesklets.window_show()

def quit(self):
print 'Quitting...'

def alarm(self):
print 'Alarm. Next in 360 seconds.'
self._display()
return 360

def _display(self):
print "Getting feed..."
y = 0
x = 0
d = feedparser.parse(self.feed)
print d.channel.title
print d.channel.description

# clear the buffer
buffer = adesklets.create_image(self.w, self.h)
adesklets.context_set_image(buffer)
adesklets.context_set_blend(False)
adesklets.context_set_color(0,0,0,0)
adesklets.image_fill_rectangle(0,0,self.w,self.h)
adesklets.context_set_blend(True)

adesklets.context_set_font(adesklets.load_font('Vera/7'))
adesklets.context_set_color(255, 0, 0, 255)
adesklets.text_draw(0, y, str(d.channel.title))
y+=12
l = len(d.entries)
l = min(l, self.numfeeds)
for i in range(l):
ent=d.entries
adesklets.context_set_color(255, 255, 0, 255)
adesklets.text_draw(0, y, str(ent.title))
print ent.title
y+=12
adesklets.context_set_color(255, 255, 255, 255)
for k in range(0, min(len(ent.summary)/100,
self.numlines)):
print str(ent.summary)[k*100:(k+1)*100]
adesklets.text_draw(0, y, str(ent.summary)[k*100:(k
+1)*100])
y+=12
adesklets.free_font(0)
adesklets.free_image(buffer)

Events(dirname(__file__)).pause()

Unfortunately, I'm getting this bug for some time now and I just don't
know what's going on:

File "./fparser.py", line 40
self.w = 520
^
SyntaxError: invalid syntax


As viewed with Google Groups, lines 40/41, 63/69, and 89 are indented
8 spaces more than they should be.

When I save your file and try to run it, I get this:
C:\junk>coolgenie.py
File "C:\junk\coolgenie.py", line 40
self.w = 520
^
IndentationError: unexpected indent

Possibilities:
(1) Your file contains junk like tabs or no-break spaces. Use a text
editor that will not mask what is really there to find the junk and
edit it out.
(2) You don't understand that:
if some_condition:
blah1
blah2
blah3
is not valid. Blah2 has to be at the same level as blah1 and blah3.

HTH,
John
 
S

Samuel

OK, sorry, this was about indents. Stupid VIM!

$ mkdir -p $HOME/.vim/ftplugin/

$ echo "setlocal sw=4
setlocal ts=4
noremap <buffer> <LocalLeader>py o/**************<CR><CR>/<Esc>
" >> ~/.vim/ftplugin/python.vim

$ echo "syntax on
set sw=2
set ts=2
set nu
set nuw=3
set autoindent
set expandtab" >> $HOME/.vimrc
---------------

Problem solved, never to be found again.

Bye,
-Sam
 
P

Peter Otten

John said:
As viewed with Google Groups, lines 40/41, 63/69, and 89 are indented 8
spaces more than they should be.

When I save your file and try to run it, I get this: C:\junk>coolgenie.py
File "C:\junk\coolgenie.py", line 40
self.w = 520
^
IndentationError: unexpected indent

Possibilities:
(1) Your file contains junk like tabs or no-break spaces. Use a text
editor that will not mask what is really there to find the junk and edit
it out.

As a first step towards sanity, make sure that your current editor is
configured to use a tabwidth of 8 spaces (it is probably set to 4
at the moment). You should then be able to at least see the offending
lines.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top