nthexwn / factorio-calculator

A simple Python script thrown together to perform ingredient calculations for the game Factorio
0 stars 0 forks source link

Use json to store recipes #1

Open FooSoft opened 5 years ago

FooSoft commented 5 years ago
import json

with open(path) as fp:
    data = json.load(fp)

# data now contains dictionaries/lists/values of json file

Awesome that you are using python now :)

nthexwn commented 5 years ago

I dunno.

I've done that kind of thing at work a lot to make programs more easily configurable for users. Sometimes even going so far as to read YAML files into memory, use a tool to parse them into JSON strings on the first pass, and parse the JSON strings into usable data structures on the second pass.

For a program which I'm just going to use myself it seems kind of silly to slow it down by maintaining the data separately when I can just define the data structures directly.

FooSoft commented 5 years ago

IMHO it's good to separate data and logic, and maybe it's even more efficient since parsing JSON is going to be faster than parsing python. You will have to build up the data structures, sure, but you will have to do that in either case.

nthexwn commented 5 years ago

I just played around a bit with a JSON solution. It's slow and ugly for two reasons:

  1. The regular json library only provides support for serializing primitives. Anything more complicated requires extending JSONEncoder and JSONDecoder and defining your own serialization scheme. I was too lazy to do that right away so I used the jsonpickle library instead. This adds an extra layer of indirection which nukes performance. It also splits apart all of the tuples and turns each individual recipe entry into 30 lines of decidedly less maintainable JSON.
  2. The default CPython implementation caches bytecode for the existing data structures in src/v0_17.py into a .pyc file which loads almost instantly on the 2nd+ execution. Unfortunately there's no equivalent for parsed JSON files so they have to be re-parsed every time. This makes the JSON implementation much slower in practice.

I believe I can solve problems with the first point by not being lazy and implementing JSON configuration the "right" way. I believe the second point would be solvable with additions to the interpreter, which is way out of scope for this little project. ;)

FooSoft commented 5 years ago

Meh, creating actual classes just to store data in Python is overrated :grin: