I’m working on a scouting gui and framework for my team. I have the following files (in a spoiler):
[spoiler]
teamLib.py:
#!/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*
except:
break
i = i + 1
return i
cmdlinehelpers.py:
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:
#!/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 "
Please enter your command:"
while alive:
request = raw_input("$> ")
if request == "show":
cmdlinehelpers.showTeam()
if request == "add":
cmdlinehelpers.addTeam()
if request == "help":
print "Commands are:
show, 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:
{"teamName": 'YouBots'}]]
instead of:
{"teamName": 'YouBots'}]
any tips?*