Python: bidimensional dictionary?

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
User avatar
Oberlus
Cosmic Dragon
Posts: 5759
Joined: Mon Apr 10, 2017 4:25 pm

Python: bidimensional dictionary?

#1 Post by Oberlus »

I don't know python, just the obvious stuff for a C programmer.
I'd like to access a nested dictionary, both levels indexed by fo.galaxySetupOptionGeneric (low, medium, high). Like this:

Code: Select all

    age_to_density_factor = {
        #                                            Low density                            Medium density                            High density
        #                Young galaxy:
        fo.galaxySetupOptionGeneric.low:    {fo.galaxySetupOptionGeneric.low: 0.54, fo.galaxySetupOptionGeneric.medium: 0.79, fo.galaxySetupOptionGeneric.high: 1.32},
        #                Mature galaxy:
        fo.galaxySetupOptionGeneric.medium: {fo.galaxySetupOptionGeneric.low: 0.78, fo.galaxySetupOptionGeneric.medium: 1.17, fo.galaxySetupOptionGeneric.high: 1.95},
        #                Ancient galaxy:
        fo.galaxySetupOptionGeneric.high:   {fo.galaxySetupOptionGeneric.low: 0.92, fo.galaxySetupOptionGeneric.medium: 1.46, fo.galaxySetupOptionGeneric.high: 2.42},
    }
    num_planets_in_galaxy = galaxy_size * age_to_density_factor[galaxy_age][planet_density]
The syntax is wrong, and gives varied KeyError, depending on what I try. I also tried using parenthesis in the inner level, which allowed me to hang the server in some kind of infinite loop flooding the command line with what seemed like freeoriond.log contents.

Maybe I should be doing something totally different, like using tuples of galaxySetupOptionGeneric to index a simple dictionary?

Code: Select all

    age_to_density_factor = {
        # Young Low:
        fo.galaxySetupOptionGeneric.low, fo.galaxySetupOptionGeneric.low: 0.54,
        fo.galaxySetupOptionGeneric.low, fo.galaxySetupOptionGeneric.medium: 0.79, 
        fo.galaxySetupOptionGeneric.low, fo.galaxySetupOptionGeneric.high: 1.32,
        # Mature:
        fo.galaxySetupOptionGeneric.medium, fo.galaxySetupOptionGeneric.low: 0.78,
        fo.galaxySetupOptionGeneric.medium, fo.galaxySetupOptionGeneric.medium: 1.17,
        fo.galaxySetupOptionGeneric.medium, fo.galaxySetupOptionGeneric.high: 1.95,
        # Ancient:
        fo.galaxySetupOptionGeneric.high, fo.galaxySetupOptionGeneric.low: 0.92,
        fo.galaxySetupOptionGeneric.high, fo.galaxySetupOptionGeneric.medium: 1.46,
        fo.galaxySetupOptionGeneric.high, fo.galaxySetupOptionGeneric.high: 2.42,
    }
    num_planets_in_galaxy = galaxy_size * age_to_density_factor[galaxy_age, planet_density]
This again allowed me to screw the server and forced me to manually kill zombie processes.

It seems I'm not making the right questions to Google when searching for an answer.

Can anyone help me? Vezzra or o01eg maybe?


I'm tempted to use an ugly if-then block of code...

User avatar
Grummel7
Space Dragon
Posts: 339
Joined: Mon Oct 09, 2017 3:44 pm

Re: Python: bidimensional dictionary?

#2 Post by Grummel7 »

You could use using a single dictionary with tuple-keys:

Code: Select all

    age_density_factor = {
        (fo.galaxySetupOptionGeneric.low, fo.galaxySetupOptionGeneric.low): 0.54,
        (fo.galaxySetupOptionGeneric.low, fo.galaxySetupOptionGeneric.medium): 0.79, 
        ...
    }
    num_planets_in_galaxy = galaxy_size * age_density_factor[(galaxy_age, planet_density)]
Your first implementation looks correct, though.
If you are getting key errors, I guess it means galaxy_age or planet_density have other values then fo.galaxySetupOptionGeneric.low/medium/high

User avatar
Oberlus
Cosmic Dragon
Posts: 5759
Joined: Mon Apr 10, 2017 4:25 pm

Re: Python: bidimensional dictionary?

#3 Post by Oberlus »

Thank you, Grummel7, this worked perfectly.
Now I have to do something (else) with the FOCS part...

Edit: o01eg, once the species focs files are translated to python, would that make it possible to declare data like that? Or will I be forced anyways to use if-else and the such?

o01eg
Programmer
Posts: 2024
Joined: Sat Dec 10, 2011 5:46 am

Re: Python: bidimensional dictionary?

#4 Post by o01eg »

Oberlus wrote: Fri Aug 19, 2022 8:18 pm Thank you, Grummel7, this worked perfectly.
Now I have to do something (else) with the FOCS part...

Edit: o01eg, once the species focs files are translated to python, would that make it possible to declare data like that? Or will I be forced anyways to use if-else and the such?
With Python FOCS it will be available not only if-else control flow but all other.
Gentoo Linux x64, gcc-11.2, boost-1.78.0
Ubuntu Server 22.04 x64, gcc-12, boost-1.74.0
Welcome to the slow multiplayer game at freeorion-lt.dedyn.io.Version 2024-04-07.15cf063.
Donations're welcome:BTC:bc1q007qldm6eppqcukewtfkfcj0naut9njj7audnm

User avatar
Oberlus
Cosmic Dragon
Posts: 5759
Joined: Mon Apr 10, 2017 4:25 pm

Re: Python: bidimensional dictionary?

#5 Post by Oberlus »

Awesome!

Then I'll wait for python species files (or I could try to help you with that, if you have any menial tasks to share with me) before continuing with the galaxy-size-bound upkeep formula.
Also, more time to explore alternatives that Geoff could approve.

o01eg
Programmer
Posts: 2024
Joined: Sat Dec 10, 2011 5:46 am

Re: Python: bidimensional dictionary?

#6 Post by o01eg »

Oberlus wrote: Sat Aug 20, 2022 12:37 pm Awesome!

Then I'll wait for python species files (or I could try to help you with that, if you have any menial tasks to share with me) before continuing with the galaxy-size-bound upkeep formula.
Also, more time to explore alternatives that Geoff could approve.
I need re-create species with 98 effect groups for equality test and it's a quite big task before changing parser.
Gentoo Linux x64, gcc-11.2, boost-1.78.0
Ubuntu Server 22.04 x64, gcc-12, boost-1.74.0
Welcome to the slow multiplayer game at freeorion-lt.dedyn.io.Version 2024-04-07.15cf063.
Donations're welcome:BTC:bc1q007qldm6eppqcukewtfkfcj0naut9njj7audnm

Ophiuchus
Programmer
Posts: 3459
Joined: Tue Sep 30, 2014 10:01 am
Location: Wall IV

Re: Python: bidimensional dictionary?

#7 Post by Ophiuchus »

Oberlus wrote: Sat Aug 20, 2022 12:37 pm I'll wait for python species files ... before continuing with the galaxy-size-bound upkeep formula.
out of curiosity - how is that connected?
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.

Look, ma... four combat bouts!

User avatar
Oberlus
Cosmic Dragon
Posts: 5759
Joined: Mon Apr 10, 2017 4:25 pm

Re: Python: bidimensional dictionary?

#8 Post by Oberlus »

The upkeep is calculated in species/common/influence.macros.

Ophiuchus
Programmer
Posts: 3459
Joined: Tue Sep 30, 2014 10:01 am
Location: Wall IV

Re: Python: bidimensional dictionary?

#9 Post by Ophiuchus »

o01eg wrote: Sat Aug 20, 2022 6:33 am With Python FOCS it will be available not only if-else control flow but all other.
maybe I am confused...

what i understand the python code will be evaluated at e.g. game start so we will be able to use python control flow to do focs metaprogramming.

But the influence upkeep oberlus talks about needs to be calculated at runtime, so the formula can not actually use python control flow, is that correct?
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.

Look, ma... four combat bouts!

User avatar
Oberlus
Cosmic Dragon
Posts: 5759
Joined: Mon Apr 10, 2017 4:25 pm

Re: Python: bidimensional dictionary?

#10 Post by Oberlus »

I think it's about the parser.

FOCS parser, being retired, can't do fancy stuff with tables, python FOCS parser will be able.

Ophiuchus
Programmer
Posts: 3459
Joined: Tue Sep 30, 2014 10:01 am
Location: Wall IV

Re: Python: bidimensional dictionary?

#11 Post by Ophiuchus »

Oberlus wrote: Wed Aug 24, 2022 8:47 pm I think it's about the parser.

FOCS parser, being retired, can't do fancy stuff with tables, python FOCS parser will be able.
yes, but not while the game is running (because at runtime only the FOCS c++ data structures exist and if there is no support for fancy tables there.. you wont be able to use it). And invoking a python interpreter per planet just to calculate influence cost would sloooooooow everything, so that is probably not gonna happen.

wait for o01eg to clarify
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.

Look, ma... four combat bouts!

o01eg
Programmer
Posts: 2024
Joined: Sat Dec 10, 2011 5:46 am

Re: Python: bidimensional dictionary?

#12 Post by o01eg »

Ophiuchus wrote: Thu Aug 25, 2022 8:33 am
Oberlus wrote: Wed Aug 24, 2022 8:47 pm I think it's about the parser.

FOCS parser, being retired, can't do fancy stuff with tables, python FOCS parser will be able.
yes, but not while the game is running (because at runtime only the FOCS c++ data structures exist and if there is no support for fancy tables there.. you wont be able to use it). And invoking a python interpreter per planet just to calculate influence cost would sloooooooow everything, so that is probably not gonna happen.

wait for o01eg to clarify
Yes, both FOCS parsers run once at application starts, not each time calculations happened.
Gentoo Linux x64, gcc-11.2, boost-1.78.0
Ubuntu Server 22.04 x64, gcc-12, boost-1.74.0
Welcome to the slow multiplayer game at freeorion-lt.dedyn.io.Version 2024-04-07.15cf063.
Donations're welcome:BTC:bc1q007qldm6eppqcukewtfkfcj0naut9njj7audnm

Post Reply