3 Replies - 884 Views - Last Post: 03 April 2009 - 10:00 AM Rate Topic: -----

#1 retroscience  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 08

syntax error

Posted 03 April 2009 - 05:35 AM

syntax error line 52 "w = ncols or len(lst)/nrows + (1 if len(lst)%nrows else 0)" .. new to python.

thanks in advance.

import traceback
import sys
import os
def write_log(odir,log_str,outfile='simulation.log'):
	fname = os.path.join(odir,outfile)
	fh = open( fname,'w' )
	fh.write(log_str)
	fh.close()
	print "Simulation log written to ", fname
#********************************
def formatExceptionInfo(maxTBlevel=5):
	cla, exc, trbk = sys.exc_info()
	excName = cla.__name__
	try:
		excArgs = exc.__dict__["args"]
	except KeyError:
		excArgs = "<no args>"
	excTb = traceback.format_tb(trbk, maxTBlevel)
	return (excName, excArgs, excTb)
#********************************
# can return unique lists using the set function, but I want to preserve the
# order of appearance. This function is from ASPN
def unique_list(seq, keepstr=True):
	t = type(seq)
	if t in (str,unicode):
		t = (list, ''.join)[bool(keepstr)]
	seen = []
	return t(c for c in seq if not (c in seen or seen.append(c)))
#********************************
def findall(L, value, start=0):
 	""" The indicies of all occurences of 'value' in 'L'. 
 	Generator version 
 	"""
 	i = start - 1
 	try:
 		while 1:
 			i = L.index(value, i+1)
 			yield i
 	except ValueError:
 		pass
#********************************
def reshape_list( lst, ncols, nrows, horiz=True ):
	"""Change shape of list lst (like APL, J, K, NumPy).
	Return list of lists: ncols x nrows. horiz - is the
	table direction (horizontal or vertical if horiz is
	False). Empty cells will be with ''
	"""
	## source: http://www.djangosnippets.org/snippets/613/
	
	if not ncols and not nrows: return lst

	w = ncols or len(lst)/nrows + (1 if len(lst)%nrows else 0)
	h = nrows or len(lst)/ncols + (1 if len(lst)%ncols else 0)
	if horiz:
		flatten = lambda irow, icol: icol + irow*w
	else:
		flatten = lambda irow, icol: irow + icol*h

	rows = []
	for irow in range( h ):
		col = []
		for icol in range( w ):
			iflat = flatten( irow, icol )
			el = '' if iflat >= len(lst) else lst[iflat]
			col.append( el )
		rows.append( col )

	return rows


This post has been edited by retroscience: 03 April 2009 - 08:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: syntax error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: syntax error

Posted 03 April 2009 - 07:43 AM

You're going to have to point out what line it is, I'm not seeing anything highlighted.
Was This Post Helpful? 0
  • +
  • -

#3 retroscience  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 08

Re: syntax error

Posted 03 April 2009 - 08:26 AM

View PostJackOfAllTrades, on 3 Apr, 2009 - 06:43 AM, said:

You're going to have to point out what line it is, I'm not seeing anything highlighted.


this section right here
  w = ncols or len(lst)/nrows + (1 if len(lst)%nrows else 0)
	h = nrows or len(lst)/ncols + (1 if len(lst)%ncols else 0)


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: syntax error

Posted 03 April 2009 - 10:00 AM

That's what I figured.

Looks like that's the ternary operator, which is in version 2.5. Are you perhaps using an older version of Python?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1