Re: [pygame] stupidity, and a lesson learned
Im glad I could help. I use it that way and it works fast and reliable. Am 19.12.2006 um 20:44 schrieb spotter .: Very nice implementation and code, Farai, this will be a great help. Thanks! -spot On 12/18/06, Farai Aschwanden <[EMAIL PROTECTED]> wrote: Maybe this helps: you can read the whole map directly into a dictionary/list. Lets say the map is divided into two parts: 1. line: meta data 2. - n. line: map data The whole map file could look like this: ['Dark Forrest', 'Donald Duck', timestamp, 20, 50, 'weather_sun', ...] # Line 1(meta data) {'map_pos1': ['stone.png', passable_yes, trap_no, ...], # must be in structure of a dictionary while map_pos1 is the key 'map_pos2: ['grass.png', passable_yes, trap_yes, ...],# ... 'map_posn: [...] } That way the position inside the lists defines what it represents, like 1. item of meta data is the map name. The file will be smaller than having it in the file like: map_name = 'Dark Forrest' author = 'Donald Duck' etc. Having the first line as list and the further lines as dict you can now read it w/o care of items inside: map_meta_dict = [] # List to store metadata of map map_area_dict = {} # Dict to store whole map map_merge = '' # Temporarily needed to read/add all map lines # Read head of map map_head = eval(map_read.readline()) # Read first line of file and set file read pointer to line 2 (eval is important) # Read map structure for map_line in map_read:# Iterate now from line 2 to to end of the file map_merge += (line.rstrip ()) # Read line by line of map and add it to to the previously read lines map_area_dict = eval(map_merge) # Evaluate file structure after completly reading it and assign it to the dict file.close (map_read) # Tnx and goodbye file So you dont have to care every single item when reading in the file and you can easy access it when the map is into dictionary/list format. Its not even optimized yet but quite fast. Another advantage is that the map lines can have different lengths and must not be square like. There is also an option to read in the whole file what would make it even faster but then you cant initially split meta and map not that easy. File handling is easier if you have meta data and map in one file. No 'if's reading the file and string functionality needed. ;) Just my 2 cents Farai Am 18.12.2006 um 06:26 schrieb spotter .: > Hi everybody, > > I just got hit by the silliest, stupidest error on my part and I > decided to share it, since it was kinda funny after the fact and so > that no one else would make my mistake. > > This was the data I was trying to parse (a map format I was making): > > mapname=DeathMap > mapauthor=pumaninja > mapdate=12172006 > mapversion=0.0.1 > mapwidth=10 > mapheight=5 > > And this was the code I was using to get the info out of the file: > > elif info.find("mapauthor=") == 0: # 0 means that the string has > been found >temp = "" >temp = info.strip("mapauthor=") >map_author.append(temp) > > I was thinking that the .strip() simply took out the specified > absolute phrase. I was wrong. The .strip() looks for all the > characters specified and takes them out. For the other tags, this did > not get me since none of the other names matched any where close to > data type or the combination of letters. Only for the author part > because I had put in pumaninja which if you see has p, u, m, a > contained in mapauthor. > > Ah, well, at least this didnt come back and get me way after the fact. > Although, this code was based on a part of a game I had already > written, that one missed the bullet because the info was numbers and > not characters. > > Moral of the story : Clearly read the docs, and test the function with > various data types. > > Happy coding, > -spot
Re: [pygame] stupidity, and a lesson learned
Very nice implementation and code, Farai, this will be a great help. Thanks! -spot On 12/18/06, Farai Aschwanden <[EMAIL PROTECTED]> wrote: Maybe this helps: you can read the whole map directly into a dictionary/list. Lets say the map is divided into two parts: 1. line: meta data 2. - n. line: map data The whole map file could look like this: ['Dark Forrest', 'Donald Duck', timestamp, 20, 50, 'weather_sun', ...] # Line 1(meta data) {'map_pos1': ['stone.png', passable_yes, trap_no, ...], # must be in structure of a dictionary while map_pos1 is the key 'map_pos2: ['grass.png', passable_yes, trap_yes, ...],# ... 'map_posn: [...] } That way the position inside the lists defines what it represents, like 1. item of meta data is the map name. The file will be smaller than having it in the file like: map_name = 'Dark Forrest' author = 'Donald Duck' etc. Having the first line as list and the further lines as dict you can now read it w/o care of items inside: map_meta_dict = [] # List to store metadata of map map_area_dict = {} # Dict to store whole map map_merge = '' # Temporarily needed to read/add all map lines # Read head of map map_head = eval(map_read.readline()) # Read first line of file and set file read pointer to line 2 (eval is important) # Read map structure for map_line in map_read:# Iterate now from line 2 to to end of the file map_merge += (line.rstrip()) # Read line by line of map and add it to to the previously read lines map_area_dict = eval(map_merge) # Evaluate file structure after completly reading it and assign it to the dict file.close(map_read) # Tnx and goodbye file So you dont have to care every single item when reading in the file and you can easy access it when the map is into dictionary/list format. Its not even optimized yet but quite fast. Another advantage is that the map lines can have different lengths and must not be square like. There is also an option to read in the whole file what would make it even faster but then you cant initially split meta and map not that easy. File handling is easier if you have meta data and map in one file. No 'if's reading the file and string functionality needed. ;) Just my 2 cents Farai Am 18.12.2006 um 06:26 schrieb spotter .: > Hi everybody, > > I just got hit by the silliest, stupidest error on my part and I > decided to share it, since it was kinda funny after the fact and so > that no one else would make my mistake. > > This was the data I was trying to parse (a map format I was making): > > mapname=DeathMap > mapauthor=pumaninja > mapdate=12172006 > mapversion=0.0.1 > mapwidth=10 > mapheight=5 > > And this was the code I was using to get the info out of the file: > > elif info.find("mapauthor=") == 0: # 0 means that the string has > been found >temp = "" >temp = info.strip("mapauthor=") >map_author.append(temp) > > I was thinking that the .strip() simply took out the specified > absolute phrase. I was wrong. The .strip() looks for all the > characters specified and takes them out. For the other tags, this did > not get me since none of the other names matched any where close to > data type or the combination of letters. Only for the author part > because I had put in pumaninja which if you see has p, u, m, a > contained in mapauthor. > > Ah, well, at least this didnt come back and get me way after the fact. > Although, this code was based on a part of a game I had already > written, that one missed the bullet because the info was numbers and > not characters. > > Moral of the story : Clearly read the docs, and test the function with > various data types. > > Happy coding, > -spot
Re: [pygame] stupidity, and a lesson learned
Maybe this helps: you can read the whole map directly into a dictionary/list. Lets say the map is divided into two parts: 1. line: meta data 2. - n. line: map data The whole map file could look like this: ['Dark Forrest', 'Donald Duck', timestamp, 20, 50, 'weather_sun', ...] # Line 1(meta data) {'map_pos1': ['stone.png', passable_yes, trap_no, ...], # must be in structure of a dictionary while map_pos1 is the key 'map_pos2: ['grass.png', passable_yes, trap_yes, ...], # ... 'map_posn: [...] } That way the position inside the lists defines what it represents, like 1. item of meta data is the map name. The file will be smaller than having it in the file like: map_name = 'Dark Forrest' author = 'Donald Duck' etc. Having the first line as list and the further lines as dict you can now read it w/o care of items inside: map_meta_dict = [] # List to store metadata of map map_area_dict = {} # Dict to store whole map map_merge = '' # Temporarily needed to read/add all map lines # Read head of map map_head = eval(map_read.readline()) # Read first line of file and set file read pointer to line 2 (eval is important) # Read map structure for map_line in map_read: # Iterate now from line 2 to to end of the file map_merge += (line.rstrip()) # Read line by line of map and add it to to the previously read lines map_area_dict = eval(map_merge) # Evaluate file structure after completly reading it and assign it to the dict file.close(map_read) # Tnx and goodbye file So you dont have to care every single item when reading in the file and you can easy access it when the map is into dictionary/list format. Its not even optimized yet but quite fast. Another advantage is that the map lines can have different lengths and must not be square like. There is also an option to read in the whole file what would make it even faster but then you cant initially split meta and map not that easy. File handling is easier if you have meta data and map in one file. No 'if's reading the file and string functionality needed. ;) Just my 2 cents Farai Am 18.12.2006 um 06:26 schrieb spotter .: Hi everybody, I just got hit by the silliest, stupidest error on my part and I decided to share it, since it was kinda funny after the fact and so that no one else would make my mistake. This was the data I was trying to parse (a map format I was making): mapname=DeathMap mapauthor=pumaninja mapdate=12172006 mapversion=0.0.1 mapwidth=10 mapheight=5 And this was the code I was using to get the info out of the file: elif info.find("mapauthor=") == 0: # 0 means that the string has been found temp = "" temp = info.strip("mapauthor=") map_author.append(temp) I was thinking that the .strip() simply took out the specified absolute phrase. I was wrong. The .strip() looks for all the characters specified and takes them out. For the other tags, this did not get me since none of the other names matched any where close to data type or the combination of letters. Only for the author part because I had put in pumaninja which if you see has p, u, m, a contained in mapauthor. Ah, well, at least this didnt come back and get me way after the fact. Although, this code was based on a part of a game I had already written, that one missed the bullet because the info was numbers and not characters. Moral of the story : Clearly read the docs, and test the function with various data types. Happy coding, -spot
Re: [pygame] stupidity, and a lesson learned
On 12/18/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: > and then use split(";") to move them into a list and assign it to > variables then. Just thought I'd mention, since the split method returns a list, you could do: name,author,date,version,width,height = astr.split(";") > Does this seem alright, no glaring things I have missed? It does seem > to work from > my small amount of testing that I have put it through. Yeah, as long as you don't expect any of your values to have semicolons in them. You could always use something strange like chr(245) as the separator, and it'd be less likely to occur in your values. But if you're the one defining these map files anyway, just make sure you don't use semicolons. And make sure you make a note of that if you give people the ability to make custom maps. Ah, thanks Luke, it would be much easier if I used a nonstandard character. Thanks for the name, author, date = data.split(";"). I keep forgetting how easy it is to do stuff in python. -spot
Re: [pygame] stupidity, and a lesson learned
spotter . wrote: name;author;date;version;width;height and then use split(";") to move them into a list and assign it to variables then. Does this seem alright, no glaring things I have missed? It'll work, as long as none of the data fields need to contain a ";" (probably true in this case). Perhaps not the most user-friendly or extensible, since you just have to remember what field goes where, and the more fields you add, the harder it will get. -- Greg
Re: [pygame] stupidity, and a lesson learned
spotter . wrote: Very interesting ideas. The link will be handy for later use. For now, I have simply used this format : name;author;date;version;width;height and then use split(";") to move them into a list and assign it to variables then. Just thought I'd mention, since the split method returns a list, you could do: name,author,date,version,width,height = astr.split(";") Does this seem alright, no glaring things I have missed? It does seem to work from my small amount of testing that I have put it through. Yeah, as long as you don't expect any of your values to have semicolons in them. You could always use something strange like chr(245) as the separator, and it'd be less likely to occur in your values. But if you're the one defining these map files anyway, just make sure you don't use semicolons. And make sure you make a note of that if you give people the ability to make custom maps. Another alternative you could use is defining a Map class and pickling the instances. -Luke and Richard - you're right, of course. I didn't notice there weren't quotes.
Re: [pygame] stupidity, and a lesson learned
Very interesting ideas. The link will be handy for later use. For now, I have simply used this format : name;author;date;version;width;height and then use split(";") to move them into a list and assign it to variables then. Does this seem alright, no glaring things I have missed? It does seem to work from my small amount of testing that I have put it through. -spot On 12/18/06, Richard Jones <[EMAIL PROTECTED]> wrote: On Monday 18 December 2006 16:31, Luke Paireepinart wrote: > Note that the way your mapfile is formatted, you could just import it as > a python module, and the variables would be declared automatically. Not quite. The strings aren't quoted. He could however use the standard library ConfigParser which is designed for this very thing. http://docs.python.org/lib/module-ConfigParser.html Richard
Re: [pygame] stupidity, and a lesson learned
On Monday 18 December 2006 16:31, Luke Paireepinart wrote: > Note that the way your mapfile is formatted, you could just import it as > a python module, and the variables would be declared automatically. Not quite. The strings aren't quoted. He could however use the standard library ConfigParser which is designed for this very thing. http://docs.python.org/lib/module-ConfigParser.html Richard
Re: [pygame] stupidity, and a lesson learned
spotter . wrote: Hi everybody, I just got hit by the silliest, stupidest error on my part and I decided to share it, since it was kinda funny after the fact and so that no one else would make my mistake. [snip] I was thinking that the .strip() simply took out the specified absolute phrase. I was wrong. The .strip() looks for all the characters specified and takes them out. For the other tags, this did not get me since none of the other names matched any where close to data type or the combination of letters. Only for the author part because I had put in pumaninja which if you see has p, u, m, a contained in mapauthor. Happened to me too. Was once trying to change an extension from .rbs to .mp3, and just did 'filename.strip('.rbs')'. Yeah, it sucks. Note that the way your mapfile is formatted, you could just import it as a python module, and the variables would be declared automatically. As long as you can trust people not to mess with your mapfiles. Considering they could just change the source of your actual program if they wanted to be malicious, I don't really see this as a security vulnerability. But... to each his own, I guess. -Luke
[pygame] stupidity, and a lesson learned
Hi everybody, I just got hit by the silliest, stupidest error on my part and I decided to share it, since it was kinda funny after the fact and so that no one else would make my mistake. This was the data I was trying to parse (a map format I was making): mapname=DeathMap mapauthor=pumaninja mapdate=12172006 mapversion=0.0.1 mapwidth=10 mapheight=5 And this was the code I was using to get the info out of the file: elif info.find("mapauthor=") == 0: # 0 means that the string has been found temp = "" temp = info.strip("mapauthor=") map_author.append(temp) I was thinking that the .strip() simply took out the specified absolute phrase. I was wrong. The .strip() looks for all the characters specified and takes them out. For the other tags, this did not get me since none of the other names matched any where close to data type or the combination of letters. Only for the author part because I had put in pumaninja which if you see has p, u, m, a contained in mapauthor. Ah, well, at least this didnt come back and get me way after the fact. Although, this code was based on a part of a game I had already written, that one missed the bullet because the info was numbers and not characters. Moral of the story : Clearly read the docs, and test the function with various data types. Happy coding, -spot