Python Forum
How to shorten the size of program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to shorten the size of program
#1
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?
Reply
#2
write more efficient code?
Hard to say without seeing your code.
Reply
#3
Maybe the white spaces the reason!!!
Reply
#4
You haven't posted any code.
Reply
#5
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()
Reply
#6
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()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
@wavic: the module is always fully imported, so there is no difference between the two.
https://stackoverflow.com/a/710603/4046632
Reply
#8
Hah! Really?! What I am thinking Huh I had to know that Undecided Thank you! Something new and important I learned today.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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.
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 6,152 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Want to shorten the python code shantanu97 3 1,271 Apr-25-2022, 01:12 PM
Last Post: snippsat
  size of set vs size of dict zweb 0 2,147 Oct-11-2019, 01:32 AM
Last Post: zweb
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,484 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb
  how to add smilar size python interpreter to my program. harun2525 7 5,711 Feb-25-2017, 09:14 PM
Last Post: snippsat
  How do I shorten my input options? - text adventure ShiningKnight 3 4,669 Jan-01-2017, 03:21 AM
Last Post: ichabod801
  Shorten this List Comprehension ATXpython 7 6,997 Oct-10-2016, 07:50 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020