Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'completeness': 0.07; 'python': 0.09; 'function:': 0.09; 'issue?': 0.09; 'libraries.': 0.09; 'objects.': 0.09; 'received:155': 0.09; 'typeerror:': 0.09; 'stored': 0.10; 'def': 0.10; 'library': 0.15; 'stack': 0.15; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'file.close()': 0.16; 'folks,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'one)': 0.16; 'overwriting': 0.16; 'received:155.180': 0.16; 'received:155.180.234': 0.16; 'received:155.180.234.122': 0.16; 'received:159.53': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:se3.svr.bankone.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'subject:Writing': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'module': 0.19; 'code.': 0.20; 'to:name:python- list@python.org': 0.20; 'trying': 0.21; 'import': 0.21; 'meant': 0.21; 'assignment': 0.22; 'example': 0.23; 'player': 0.23; 'to:2**1': 0.23; "i've": 0.23; 'seems': 0.23; 'received:169.254': 0.24; 'header:In-Reply-To:1': 0.25; '2.3': 0.27; 'accuracy': 0.27; '(possibly': 0.29; 'pickle': 0.29; 'objects': 0.29; 'received:169': 0.29; '8bit%:5': 0.29; 'class': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'normally': 0.30; 'function': 0.30; 'header:Received:8': 0.30; 'code': 0.31; 'file': 0.32; 'could': 0.32; 'dies': 0.33; 'instead,': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'needed': 0.35; 'built-in': 0.35; 'protocol': 0.35; 'doing': 0.35; 'something': 0.35; 'created': 0.36; 'but': 0.36; 'useful': 0.36; 'anything': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'level': 0.37; 'why': 0.37; 'item': 0.37; 'passed': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'subject:-': 0.40; 'end': 0.40; 'your': 0.60; 'save': 0.61; 'map': 0.61; 'subject:...': 0.63; 'information,': 0.63; 'url:email': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'advantages': 0.71; 'now:': 0.71; 'hey,': 0.72; 'sale': 0.76; 'again!': 0.84; 'received:169.254.8': 0.84; 'avoided.': 0.91 X-DKIM: OpenDKIM Filter v2.1.3 sf3.jpmchase.com qA9HNQQd011371 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1352481806; bh=JhRnTWdXYwyqJXzQqkmv9fON/dxinW0oKll0thTt7QI=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=PgoYi30kU789o6wtzq68beP6T8k/bMg5cnGHDRXGsxCqEidU3hNKcYm5bSRSWZtjF Uzxo2usu8QhPHjSRBhznyaxmKKvaM/npzvBHp20L937ftcTDGaifaWvBvIPRgHOZUd qXj9gRCAMwQEFoQPBvmqHDpkh1y9ymBUNAVUVzoM= From: "Prasad, Ramit" To: Graham Fielding , "python-list@python.org" Subject: RE: Writing game-state data... Thread-Topic: Writing game-state data... Thread-Index: AQHNvkrwi1qddfVO9Ee5Wqj/vuqNq5fhug1Q Date: Fri, 9 Nov 2012 17:23:06 +0000 References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes Content-Type: text/plain; charset="iso-8859-1" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352481815 news.xs4all.nl 6948 [2001:888:2000:d::a6]:51139 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33038 Graham Fielding wrote:=0D=0A> =0D=0A> Hey, folks, me again!=0D=0A> =0D=0A> = I've been puzzling over this for a while now:=0D=0A> =0D=0A> I'm trying to = write data to a file to save the state of my game using the following funct= ion:=0D=0A> =0D=0A> def save_game():=0D=0A> =A0=A0=A0 #open a new empty she= lve (possibly overwriting an old one) to write the game data=0D=0A> =A0=A0= =A0 file_object =3D open('savegame=2Esav', 'wb')=0D=0A> =A0=A0=A0 file['map= '] =3D map=0D=0A> =A0=A0=A0 file['objects'] =3D objects=0D=0A> =A0=A0=A0 fi= le['player_index'] =3D objects=2Eindex(player)=A0 #index of player in objec= ts list=0D=0A> =A0=A0=A0 file['inventory'] =3D inventory=0D=0A> =A0=A0=A0 f= ile['game_msgs'] =3D game_msgs=0D=0A> =A0=A0=A0 file['game_state'] =3D game= _state=0D=0A> =A0=A0=A0 file['stairs_index'] =3D objects=2Eindex(stairs)=0D= =0A> =A0=A0=A0 file['dungeon_level'] =3D dungeon_level=0D=0A> =A0=A0=A0 fil= e=2Eclose()=0D=0A> =0D=0A> However, while 'savegame=2Esav' is created in th= e directory I specify, the function dies on file['map'] =3D map=2E=0D=0A> T= his is the end of the stack trace:=0D=0A> =0D=0A> =0D=0A> =A0 File "C:\Pyth= on Project\Roguelike=2Epy", line 966, in save_game=0D=0A> =A0=A0=A0 file['m= ap'] =3D map=0D=0A> TypeError: 'type' object does not support item assignme= nt=0D=0A> =0D=0A=0D=0A`file` is the built-in for file objects=2E I would sa= y you need=0D=0Ato use file_object[] instead, but it is a file object=0D=0A= and is not meant for this usage=2E You can write directly=0D=0Ato a file bu= t it is easier to use sqllite or shelve/pickle=0D=0Alibraries=2E I will use= shelve in my example since your code=0D=0Ais already doing something simil= ar=2E Do not forget to=0D=0Aimport shelve in your own code=2E=0D=0A=0D=0Ade= f save():=0D=0A shelf =3D shelve=2Eopen('savegame=2Esav', protocol=3D2) = =0D=0A # Change pickle protocol if you use Python < 2=2E3=0D=0A = shelf['map'] =3D map=0D=0A shelf['objects'] =3D objects=0D=0A shel= f['player_index'] =3D objects=2Eindex(player)=0D=0A shelf['inventory'] = =3D inventory=0D=0A shelf['game_msgs'] =3D game_msgs=0D=0A shelf['gam= e_state'] =3D game_state=0D=0A shelf['stairs_index'] =3D objects=2Eindex= (stairs)=0D=0A shelf['dungeon_level'] =3D dungeon_level=0D=0A shelf= =2Eclose()=0D=0A=0D=0A=0D=0A> Now, the map is randomly generated -- could t= hat be an issue?=0D=0A> =0D=0A=0D=0ABoth "file" and "map" are built-in keyw= ords and using those=0D=0Anames for you own variables is called shadowing a= built-in=2E=0D=0AShadowing a built-in can be interesting and useful but sh= ould=0D=0Abe avoided=2E Also, it seems like save() is not in a class nor=0D= =0Ahaving anything passed in; are all the game states variables =0D=0Astore= d at the module level or something?=0D=0A=0D=0A> Should I just scrap the cu= rrent system and use pickle?=0D=0A=0D=0AYou are almost there, so I would no= t bother=2E Normally I would=0D=0Ause pickle over shelve; I have not needed= any of the advantages =0D=0Aof shelve and why use a library on top of pick= le when I only=0D=0Aneed pickle? Of course, YMMV=2E=0D=0A=0D=0A=0D=0A~Ramit= =0D=0A=0D=0A=0D=0AThis email is confidential and subject to important discl= aimers and=0D=0Aconditions including on offers for the purchase or sale of= =0D=0Asecurities, accuracy and completeness of information, viruses,=0D=0Ac= onfidentiality, legal privilege, and legal entity disclaimers,=0D=0Aavailab= le at http://www=2Ejpmorgan=2Ecom/pages/disclosures/email=2E