Go to Post Stop making me think so hard! :D - Chris is me [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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?
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What am I doing wrong? grrrrrrr techplex CAN 5 05-08-2010 19:40
Something Wrong with usfirst! Eric W. Jones General Forum 5 09-01-2007 02:01
Something wrong with this picture sanddrag Chit-Chat 2 30-04-2006 12:41


All times are GMT -5. The time now is 00:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi