Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

[SOLVED] Unexplained TypeError

Holiverh's izHawt Python....

[SOLVED] Unexplained TypeError

Postby Holiverh » Sat Oct 31, 2009 6:11 am

Can someone please run this in their interpreter and tell me if it raises a TypeError.
Code: Select all
#!/usr/bin/env python

import string

class header(object):
   def __init__(self, content_type="text/html", cache_control="no-cache"):
      self.content_type = "Content-type: %s" % content_type
      self.cache_control = "Cache-Control: %s" % cache_control
      return None
   
   def generate(self):
      return "%s\n%s\n\n" % (self.content_type, self.cache_control)
      
class html(object):
   def __init__(self):
      self.code = ["<html>"]
      return None
      
   def __selector(self, selector=("","")):
      valid_types = ["class","id"]
      if selector[0] not in valid_types:
         return ""
      else:
         return " %s='%s'" % (selector[0],selector[1])
      
   def write(self, string=str):
      self.code.append(string)
      return None
      
   def include(self, path=str):
      FILE = open(path, "r")
      
      contents = FILE.readlines()
      FILE.close()

      for item in contents:
         new_item = item.strip()
         self.write(new_item)
      return None
      
   def generate(self):
      if self.code[-1] == "</html>": pass
      else: self.code.append("</html>")
      return string.join(self.code, "")
      
   ## >>> Builtin html generators
   
   def head(self, title=str, description="", keywords=[]):
      self.write("<head>")
      
      self.write("<title>%s</title>" % title)
      
      self.write("<meta name='description' content='%s'" % description)
      self.write("<meta name='keywords' content='")
      counter = 1
      for item in keywords:
         if len(keywords) > 1 and counter < len(keywords):
            self.write("%s%s" % (str(item), ", "))
         else: self.write(str(item))
         counter += 1
      self.write("'>")
      
      self.write("<meta name='GENERATOR' content='HBcgi'")
      self.write("</head>")
      return None
      
   def heading(self, content, weight=6, selector=("","")):
      if weight > 6 or weight < 1: raise ValueError, "Weight not in range(1-6)"
      real_weight = 7 - weight
      
         
      self.write("<h%s%s>%s</h%s>" % (str(real_weight), self.__selector(selector), str(content), str(real_weight)))
      return None
   def introduction(self, content, selector=("","")):
      self.write("<p%sstyle='font-style: italic;'>%s</p>" % (self.__selector(selector), str(content)))
      return None
      
   def paragraph(self, content, selector=("","")):
      self.write("<p%s>%s</p>" % (self.__selector(selector), str(content)))
      return None
      
   def code(self, content, selector=("","")):
      nl_replaced = str(content).replace("\n","<br />")
      self.write("<code%s style='font-family: monospace;'>%s</code>" % (self.__selector(selector), nl_replaced))
      return None
      
   def blockquote(self, content, selector=("","")):
      self.write("<blockquote%s>%s</blockquote>" % (self.__selector(selector), str(content)))
      
print header().generate(),

html = html()
html.code("<body>")
html.code("import foo", ("class","code_block")) #<< WHY WONT THIS WORK?
html.write("</body>")
print html.generate()



I really don't have a clue why it keeps raising:
Code: Select all
TypeError: 'list' object is not callable
I'M NOT EVEN USING LISTS!!!
Any ideas on what could be causing it? Because it seems like the most random-assed error ever... Much appreciated! :mrgreen: :lol:
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: OMFGWTFBBQ!?1/12!/"?!!?!?#'#!?1?2!?

Postby Dink » Sat Oct 31, 2009 2:40 pm

Traceback (most recent call last):
File "C:/Documents and Settings/Obama/Desktop/Development/Python/a", line 93, in <module>
html.code("<body>")
TypeError: 'list' object is not callable

I get the same error. Not sure of the reason why it won't work sorry, your Python has far exceeded what I know of it.

:ugeek:
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia

Re: OMFGWTFBBQ!?1/12!/"?!!?!?#'#!?1?2!?

Postby Holiverh » Sun Nov 01, 2009 3:47 am

Dink wrote:Traceback (most recent call last):
File "C:/Documents and Settings/Obama/Desktop/Development/Python/a", line 93, in <module>
html.code("<body>")
TypeError: 'list' object is not callable

I get the same error. Not sure of the reason why it won't work sorry, your Python has far exceeded what I know of it.

:ugeek:
Okay, thanks for your time anyway. Btw i love your file path('Obama'). :lol:

Just looking at it again i worked out what was going wrong. I already had a attribute named 'code', which is a list. So when i try to call a method by the same name it raises TypeError because you cannot pass a argument to a list. :lol:

Code: Select all
def __init__(self):
      self.code = ["<html>"]
      return None

## And

def code(self, content, selector=("","")):

## And calling html.code obviously tried to call up the attribute instead of the method:

html.code("import foo", ("class","code_block"))
A simple change of the method name will fix it.

your Python has far exceeded what I know of it.
Heh, for a few moments i thought my code had exceeded what i know of Python. :P

How comes all my errors are simple naming ones... I iz n00b!
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK


Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred