Python script to count the # of machines in a save

I created a Python script/program to count the number of machines in a saved game. It’s my work around to help in allocating research points. See my post from a while ago http://positech.co.uk/forums/phpBB3/viewtopic.php?f=49&t=14309. Hopefully some of you will find it useful; at least until the function is incorporated into the game.

This is my first attempt of anything in Python so it certainly has room for improvement. I would like to be able to run it from a desktop icon directly or via a Bat file but I wanted to get this posted before I try to add more ‘bells & whistles’. It’s written in version 3.5 and will need to be ran within the IDLE editor included with Python.

On Win32 it’s a right click of the file name (Count_Machines.py) then press F5 (or use the Run Menu). The script will ask you to select the file you what to analyze then print it out in the Python shell that opened when you ran the script.

Below are some sample printouts of some saved games I download from the forms. See this thread: http://positech.co.uk/forums/phpBB3/viewtopic.php?f=49&t=14576. I could have sworn there more than two files on the thread but this was all I saw. Thanks Enzymus.

Enzymus – “Advanced - Even mo’ money 7.sav”
21 Ioniser
12 Dissolver
18 Pill Printer
14 Evaporator
32 Agglometer
10 Multimixer
2 Shaker
7 Creamer
0 Drug Packer
0 Analyzer
14 Autoclave
0 Cryogenic Condensor
3 Sachet Maker
0 Syringe Injector
1 Centrifuge
21 Chromatograph
0 Ultraviolet Curer
0 Atomic Sequencer
0 Hadron Collider

Enzymus – “Quick Start - Quick draw 5.sav”
28 Ioniser
29 Dissolver
18 Pill Printer
16 Evaporator
43 Agglometer
16 Multimixer
12 Shaker
8 Creamer
0 Drug Packer
0 Analyzer
0 Autoclave
0 Cryogenic Condensor
0 Sachet Maker
0 Syringe Injector
0 Centrifuge
3 Chromatograph
0 Ultraviolet Curer
0 Atomic Sequencer
0 Hadron Collider

You should be able to cut and past the below into a text editor and save it as Count_Machines_V1.py

[code]# Count_Machines_V1.py by Acme_Labs

Created September 19, 2015

Written for Python V3.5

Count the occurance of the phrase in varible Machine_Name in the form of (“id”:“pill_maker”)

Display_Name=‘Ioniser,Dissolver,Pill Printer,Evaporator,Agglometer,Multimixer,Shaker,Creamer,Drug Packer,Analyzer,Autoclave,Cryogenic Condensor,Sachet Maker,Syringe Injector,Centrifuge,Chromatograph,Ultraviolet Curer,Atomic Sequencer,Hadron Collider’
Machine_Name=‘ioniser,dissolver,pill_maker,evaporator,agglomerator,multimixer,shaker,cream_maker,drug_packer,analyzer,autoclave,cryogenic_condensor,sachet_maker,syringe_injector,centrifuge,chromatograph,ultraviolet_curer,atomic_sequencer,hadron_collider’
from tkinter import Tk
from tkinter.filedialog import askopenfilename

root=Tk()
root.withdraw()

Ask for the filename to read.

filename = askopenfilename()
fread = open(filename, “r”)
print(filename)

read the whole file into one string

text = fread.read()
fread.close()

Display_List = Display_Name.split(",")
Word_List = Machine_Name.split(",")
M_Cnt=len(Word_List)

for A in range(0,M_Cnt):
Text2Search = ‘“id”:"’+Word_List[A]+’"’
A_Cnt=text.count(Text2Search)
Dis_Name=Display_List[A]
print(A_Cnt, Dis_Name)

[/code]

To download Python go to: https://www.python.org/

Thank You. I had considered doing similar (though it would’ve been simpler and just AWK or GREP in my case).

I know you said this is just start and bells&whistles come later, but note that in some threads asking for this info we, of course, wanted more from Tim/Dev.
Like total machine count and how many are actually “in use” (which has a an unclear definition).

But in any case, I believe a change that shows the split of “state”: “PROCESSING”, “state”: “IDLE”, for each machine might be useful.
The “activeHistory” array also seems to show if machines have been “in use”, but that might be more complicated.

No problem; looks good.
I believe the other thread you were probably thinking about was: viewtopic.php?f=49&t=14641

While there are certainly some changes you could make to make it even better, I think this is a “good-enough”; personally I’d just leave it for Tim to fix in the game itself. Not that I’m trying to discourage you, an interesting problem is always a good way to learn a new language.

Hehe. What about Perl? You can’t do reg-exp without Perl; it’s tradition! :wink:

That said, my serious suggestion would be that doing a text search as done here is the “wrong” way to do it; from a design perspective the “correct” thing to do is use a JSON library to parse the file. i.e. docs.python.org/2/library/json.html , and then just read from the datastructure that comes out. Not trying to be critical of your work Acme; a constructive suggestion instead.

You’re welcome. (I had to Google AWK & GREP to find out what you were referring to; I’m not a Unix user.)

At least I’ve heard of this one. :slight_smile:

That’s the one I was thinking, thanks again.

I’ve gotten the feeling from several comments and tweets he has posted, that with the exception of bug fixes, Tim is done working on this game.

Unfortunately, that’s way beyond my experience.

I went with a quick & easy text search. At first I was trying to do it in a MSWord macro but I wasn’t able to figure out how to get a count of the phrase I was searching for. The built in help is garbage and everything I Googled was focused on word counting.

Doing it in Python was a bit intimidating, as I was using ver 3.4.3 I found a lot of the examples just didn’t work; I think they were for ver2. I finally limited my searching to articles within the last year. I found some of the differences from ver2 to 3 are frustratingly minor; just a change of case or minor syntax changes.

For now, in the words of unofficial motto of GE Medical Systems, Good Enough! lol