python - Where is the circular reference? -


i'm writing python classes want encode json. when try jsonify objects, error mentioning 'circular reference'. think understand circular reference means, can't find examples in code.

the relationship (has a/is a) between objects

  • signup has a
  • registrant has a
  • address

code (python):

class address:     def __init__(self, address1, address2, city, state, zip):         self.address1 = address1         self.address2 = address2         self.city = city         self.state = state         self.zip = zip  class signup:     def __init__(self, registrant, classid, date, time, paid, seatcost, notes, classname, seats, groupid, agentname, agentcompany):         self.registrant = registrant         self.classid = classid         self.date = date         self.time = time         self.paid = paid         self.seatcost = seatcost         self.notes = notes         self.classname = classname         self.seats = seats         self.groupid = groupid         self.agentname = agentname         self.agentcompany = agentcompany  class registrant:     def __init__(self, firstname, lastname, address, phone, email):         self.firstname = firstname         self.lastname = lastname         self.address = address         self.phone = phone         self.email = email  def scrape(br):     signups = []      soup = libstuff.getsoup(br, 'http://thepaintmixer.com/admin/viewdailysignups.php')      table = soup.find(id='calendar')     rows = table.find_all('tr')     rownumber = 0     row in rows:         if rownumber == 0:             rownumber = rownumber + 1             continue         cells = row.find_all('td')         cellnumber = 0         cell in cells:             if cellnumber == 0:                 try:                     firstname = cell.contents[0]                 except indexerror:                     firstname = none             elif cellnumber == 1:                 try:                     lastname = cell.contents[0]                 except indexerror:                     lastname = none             elif cellnumber == 2:                 try:                     address1 = cell.contents[0]                 except indexerror:                     address1 = none             elif cellnumber == 3:                 try:                     address2 = cell.contents[0]                 except indexerror:                     address2 = none             elif cellnumber == 4:                 try:                     city = cell.contents[0]                 except indexerror:                     city = none             elif cellnumber == 5:                 try:                     state = cell.contents[0]                 except indexerror:                     state = none             elif cellnumber == 6:                 try:                     zip = cell.contents[0]                 except indexerror:                     zip = none             elif cellnumber == 7:                 try:                     phone = cell.contents[0]                 except indexerror:                     phone = none             elif cellnumber == 8:                 try:                     email = cell.contents[0]                 except indexerror:                     email = none             elif cellnumber == 9:                 try:                     classid = cell.contents[0]                 except indexerror:                     classid = none             elif cellnumber == 10:                 try:                     date = cell.contents[0]                 except indexerror:                     date = none             elif cellnumber == 11:                 try:                     time = cell.contents[0]                 except indexerror:                     time = none             elif cellnumber == 12:                 try:                     paid = cell.contents[0]                 except indexerror:                     paid = none             elif cellnumber == 13:                 try:                     seatcost = cell.contents[0]                 except indexerror:                     seatcost = none             elif cellnumber == 14:                 try:                     notes = cell.contents[0]                 except indexerror:                     notes = none             elif cellnumber == 15:                 try:                     classname = cell.contents[0]                 except indexerror:                     classname = none             elif cellnumber == 16:                 try:                     seats = cell.contents[0]                 except indexerror:                     seats = none             elif cellnumber == 17:                 try:                     groupid = cell.contents[0]                 except indexerror:                     groupid = none             elif cellnumber == 18:                 try:                     agentname = cell.contents[0]                 except indexerror:                     agentname = none             elif cellnumber == 19:                 try:                     agentcompany = cell.contents[0]                 except indexerror:                     agentcompany = none             cellnumber = cellnumber + 1          address = address(address1, address2, city, state, zip)         registrant = registrant(firstname, lastname, address, phone, email)         signup = signup(registrant, classid, date, time, paid, seatcost, notes, classname, seats, groupid, agentname, agentcompany)         signups.append(signup)     return signups #i call json.dumps() on returned list json.dumps(scrape(br), default=lambda o: o.__dict__) 

are constructors messed up? passing shouldn't?

the cause cell.contents[0] returning complex beautifulsoup object rather straight text. beautifulsoup objects know parents, siblings, parser class, attribute, , other objects might shared or circular.

the situation arises when <td> element contains inner html. common in tables (perhaps table entry bolded or italicized example).

a solution problem make sure use beautifulsoup's .text make sure you're getting text , not inner beautifulsoup elements:

columns = [col.text col in row.findall('td')]  

fwiw, here easy diagnostic technique see going on. modify default function in json.dumps() make output visible:

def view_dict(obj):     print '--------------'     print 'type:', obj.__class__     d = obj.__dict__     pprint.pprint(d)     return d  json.dumps(scrape(br), default=view_dict) 

the circular reference should pop right out. hope clears mystery (because otherwise code looks fine , not explicitly creating circular references).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -