On 8/20/07, Stefan Matthias Aust <[EMAIL PROTECTED]> wrote:
> SC keeps all persistent data two global dicts that contain other dicts
> containing more dicts and arrays. I tried to map the data onto
> database tables but doing this, most of the elegance of Python seems
> to vanish. My code is actually worse than the original Pike code.
>
> Here's an example (Pike):
>
>  p[d[series][game]["f"][ixb]["owner"]]["kills"]++;
>
> "p" is the global dict of players (all player data are kept in another
> dict). "d" is the global dict of all other game data. Games are
> instances of series and "f" probably means fleet and stands for a dict
> of all ships. A ship is owned by a player and the server stores how
> many other players a player has beaten. The Django equivalent seems to
> be:
>
>  g = Series.objects.get(name=series).game_set.get(no=game)
>  p = Player.objects.get(name=g.fleet_set.get(no=ixb).owner)
>  p.kills += 1
>  p.save()

Another way to approach this problem is to create your own classes
that behave like the original syntax.  (For example, because you want
to track changes made to the upstream code easily, or make it easier
to notice when you've copied the original code to yours incorrectly.)

class PObject(object):
    def __getitem__(self, username):
        user = getuserfromname(username)
        return PUserObject(user):

p = PObject()

class PUserObject(object):
    def __init__(self, user):
        self.user = user

    def __getitem__(self, fieldname):
        return getattr(self, fieldname)

    def _get_kills(self):
        return self.user.kills

    def _set_kills(self, value):
        self.user.kills = value
        self.user.save()

    kills = property(_get_kills, _set_kills)

Thus, you could use:

p[some['stuff'][that]['returns'][a].username]["kills"]+=1

But, it might be that it is good enough to just have a copy of the
line you're trying to copy in a comment above the multi-line version
using the Django ORM (if that's the ORM you're most familiar with).
That way, if a particular line changes in your upstream, then you only
need to find the original line in a comment in your code, and update
the comment and the code.

Neil
-- 
Neil Blakey-Milner
http://nxsy.org/
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to