![]() |
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/pythonCode:
import teamLibCode:
#!/usr/bin/pythonthe 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'}]]Code:
[{"teamName": 'YouBots'}] |
Re: Im doing something wrong here (python)
If that's consistently the problem, try doing a simple
Code:
teamLib.toCommit = teamLib.toCommit[0] |
Re: Im doing something wrong here (python)
What does your "./Scouting/Robot" file look like normally? It's hard to just read your code and provide feedback without seeing the data format.
Also, have you looked at "pickling" your data instead of using eval and repr? Pickled data should be faster to load and more secure than eval (which is a giant security hole...). And easier to get working. |
Re: Im doing something wrong here (python)
I wasn't able to spot the problem exactly, but try to check your objects' values (print them to the interactive shell) just before/after you repr/eval them, and try to see if there was some sort of mistake in there.
Aside from that, here are a couple of tips you might wanna' use to better you program's performance a bit, and make it easier to use: Enums: You have yet to represent the type of robot in your outputs, such as prettyfiy(), but I already see that you defined the member robotType as a numeric one, so just in case you'll want to represnt the robot's type in text and not in numbers, you could use enums for that: Taken from http://stackoverflow.com/questions/3...enum-in-python Quote:
Len(object): The Len method is used to get the length of an object holding a collection. e.g, a string can be a collection (a collection of single strings or "chars" appended). You could use it to return the count of items in your list, like this: Code:
def countListElements(lists): |
Re: Im doing something wrong here (python)
here is ./Scouting/robot: (whitespaced in json-like format for your readability)
Code:
{ |
Re: Im doing something wrong here (python)
That's your issue. (assuming I'm internally evaluating that correctly...) Python is going to interpret that as an array, and then you are inserting that array into a list. Hence your double packed list.
I'd highly recommend learning how pickling works. It's very easy and better than using eval in that location. Code:
#!/usr/bin/python |
Re: Im doing something wrong here (python)
how would you reccomend i proceed with as little security vulnerabilities as possible?
|
Re: Im doing something wrong here (python)
Quote:
|
Re: Im doing something wrong here (python)
I am intending this to be a library that i can implement in many different ui's (symbian was the origional target, but I am going to write a gtk+ ui for linux and a tkinter one if you really want me to), so i need a 100% python solution. I am most likely going to go with http://pypi.python.org/pypi/simplejson/ as the json parser.
|
Re: Im doing something wrong here (python)
Sounds reasonable.
|
| All times are GMT -5. The time now is 00:06. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi