Python Forum
How to shorten the size of program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to shorten the size of program (/thread-8430.html)

Pages: 1 2


How to shorten the size of program - garikhgh0 - Feb-20-2018

hi there.
I have created a tictactoe game for two players (X and 0) and made it executable, after which it became 15 Mb which is more for such a game. How can I shorten it?

I have not removed the whitespaces maybe they are the reason?


RE: How to shorten the size of program - Larz60+ - Feb-20-2018

write more efficient code?
Hard to say without seeing your code.


RE: How to shorten the size of program - garikhgh0 - Feb-20-2018

Maybe the white spaces the reason!!!


RE: How to shorten the size of program - Larz60+ - Feb-20-2018

You haven't posted any code.


RE: How to shorten the size of program - garikhgh0 - Feb-20-2018

from tkinter import *
def callback(r,c):
  global player
  if player == 'X' and states[r][c] == 0 and stop_game==False:
    b[r][c].configure(text='X', fg='blue', bg='white')
    states[r][c] = 'X'
    player = 'O'
  if player == 'O' and states[r][c] == 0 and stop_game==False:
    b[r][c].configure(text='O', fg='orange', bg='black')
    states[r][c] = 'O'
    player = 'X'
  check_for_winner()
  
def check_for_winner():
  global stop_game
  for i in range(3):
    if states[i][0]==states[i][1]==states[i][2]!=0:
      b[i][0].configure(bg='grey')
      b[i][1].configure(bg='grey')
      b[i][2].configure(bg='grey')
      stop_game = True
  for i in range(3):
    if states[0][i]==states[1][i]==states[2][i]!=0:
      b[0][i].configure(bg='grey')
      b[1][i].configure(bg='grey')
      b[2][i].configure(bg='grey')
      stop_game = True
      
  if states[0][0]==states[1][1]==states[2][2]!=0:
    b[0][0].configure(bg='grey')
    b[1][1].configure(bg='grey')
    b[2][2].configure(bg='grey')
    stop_game = True
    
  if states[2][0]==states[1][1]==states[0][2]!=0:
    b[2][0].configure(bg='grey')
    b[1][1].configure(bg='grey')
    b[0][2].configure(bg='grey')
    stop_game = True
    
root = Tk()
b = [[0,0,0],
     [0,0,0],
     [0,0,0]]
     
states = [[0,0,0],
          [0,0,0],
          [0,0,0]]
          
for i in range(3):
  for j in range(3):
    b[i][j] = Button(font=('Verdana', 56), width=3, bg='yellow',
    command = lambda r=i,c=j: callback(r,c))
    b[i][j].grid(row = i, column = j)

player = 'X'
stop_game = False
mainloop()



RE: How to shorten the size of program - wavic - Feb-20-2018

Import just what you need for the program.
For example:

In [1]: import urllib

In [2]: page = urllib.request.urlopen('http://python-forum.io')

In [3]: html = page.read()
Here I import the whole urllib module. But I am using only urlopen method. I do not need all the rest.
So it becomes:

In [1]: from urllib.request import urlopen

In [2]: page = urlopen('http://python-forum.io')

In [3]: html = page.read()



RE: How to shorten the size of program - buran - Feb-20-2018

@wavic: the module is always fully imported, so there is no difference between the two.
https://stackoverflow.com/a/710603/4046632


RE: How to shorten the size of program - wavic - Feb-20-2018

Hah! Really?! What I am thinking Huh I had to know that Undecided Thank you! Something new and important I learned today.


RE: How to shorten the size of program - abhin - Feb-21-2018

It is highly unlikely that whitespaces contribute anything significant to the program size. Let me turn the question around: why does it matter that your executable is 15MB? That's still a very small executable by modern standards.



If you're still interested in knowing why your executable is 15MB, I want to propose an experiment: strip out all the code, leaving behind something skeletal like:

from tkinter import *

print 'this is a pretty small script'
Now make a standalone executable out of this and see how large that is. I suspect that it'll be pretty close to 15MB (I haven't checked this myself, so I might be wrong).

Another experiment would be to repeat the above process after stripping out the import. I suspect that the answer to those questions would demonstrate that your code contributes little to the size of your standalone executable's size.


RE: How to shorten the size of program - snippsat - Feb-21-2018

(Feb-20-2018, 05:48 AM)garikhgh0 Wrote: and made it executable, after which it became 15 Mb which is more for such a game.
C:\1\exe_env
λ pyinstaller tk_test.py --onefile --windowed --clean
Makes working one file tx_test.exe of size 7.1 MB.