View Single Post
  #1   Spotlight this post!  
Unread 27-08-2010, 23:09
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Im doing something wrong here (python)

I'm working on a scouting gui and framework for my team. I have the following files (in a spoiler):
[spoiler]
teamLib.py:
Code:
#!/usr/bin/python

import os

toCommit = list()

class team:
    """
    Its a object to hold team data
    """
    def __init__(self, name = "", number = 0, robotName = "", robotType = 0):
        self.name = name
        self.number = number
        self.robotName = robotName
        self.robotType = robotType

def newTeam(name, number, robotName, robotType):
    """
    Wrote this before i found out about constructors, does the same thing as team.__init__()
    """
    temp = team()
    temp.number = number
    temp.name = name
    temp.robotName = robotName
    temp.robotType = robotType

    return temp

def teamFromDict(theDict):
    """
    Returns a team object based on a dictionary argument
    """
    return team(
        theDict["name"], 
        theDict["number"], 
        theDict["robotName"], 
        theDict["robotType"]
    )

def saveTeam(team_obj):
    """
    Appends the team object to the toCommit List
    """
    config = {}
    config["name"] = team_obj.name
    config["teamNumber"] = team_obj.number
    config["robotName"] = team_obj.robotName
    config["robotType"] = team_obj.robotType

    toCommit.append(config)
#    f = open("./Scouting/robot", "w+")
#    f.write(repr(config))
#    f.close()

def read():
    """
    Opens the config file, returns the superDict
    """
    try:
        f = open("./Scouting/robot", 'rt')
        try:
            content = f.read()
            config = eval(content)
            f.close()
            toCommit.insert(999,config)
            
        except:
            print "cannot read file"
    except:
        print "cannot open file"

def commit():
    """
    Writes the team values to the config file in a listed dict
    """
    f = open("./Scouting/robot", "w+")
    f.write(repr(toCommit))
    f.close()
    clearList(toCommit)

def clearList(lists):
    """
    A clever hack to kill a list
    """
    while True:
        try:
            lists.pop()
        except:
            break

def interpretList(lists, value):
    """
    Give it the list you want, and the position of the team, and it will return a team object with the dict data
    """
    return teamFromDict(lists[value])

def countListElements(lists):
    i = 0
    while True:
        try:
            lists[i]
        except:
            break
        i = i + 1
    return i
cmdlinehelpers.py:
Code:
import teamLib

numberOfTeamsInList = teamLib.countListElements(teamLib.toCommit)

def addTeam():
	teamLib.saveTeam(
		teamLib.team(
			raw_input("Team Name: "), 
			input("Team Number: "), 
			raw_input("Robot Name: "), 
			input("Robot Type: ")
		)
	)
	print "Saved to list"
	
def showTeam():
	print numberOfTeamsInList
	temp = numberOfTeamsInList
	request = input("choose a number")
	prettyify(teamLib.interpretList(teamLib.toCommit, request))

def prettyify(teamObject):
	print "Team Name: %d" %teamObject.name
	print "Team Number: %d" %teamObject.number
	print "Robot Name: %d" %teamOcject.robotName
and, finally, cmdLineTest.py:
Code:
#!/usr/bin/python

import teamLib, cmdlinehelpers

teamLib.read()

alive = True

numberOfTeamsInList = teamLib.countListElements(teamLib.toCommit)

print "There are the following number of teams in the list"
print numberOfTeamsInList

print "\nPlease enter your command:"

while alive:
	request = raw_input("$> ")
	if request == "show":
		cmdlinehelpers.showTeam()
	if request == "add":
		cmdlinehelpers.addTeam()
	if request == "help":
		print "Commands are: \n\nshow, add, save, load, help, quit"
	if request == "save":
		teamLib.commit()
		teamLib.read()
		numberOfTeamsInList = teamLib.countListElements(teamLib.toCommit)
		print "Saved :D"
	if request == "load":
		teamLib.read()
		numberOfTeamsInList = teamLib.countListElements(teamLib.toCommit)
		print "Found %d teams" %numberOfTeamsInList
	if request == "quit":
		print "bye"
		alive = False
[/spoiler]
the problem is that when I call teamLib.read(), teamLib.toCommit, which is a list, inserts the contents of ./Scouting/robot (which is a list of dicts) into another list, or (for clarity's sake) I get this:
Code:
[[{"teamName": 'YouBots'}]]
instead of:
Code:
[{"teamName": 'YouBots'}]
any tips?