Little boy questioned his mother, he asked what he can be in the future..with a sad smile, she tells him he can be anything he wants to be.... Boy said he’d become (an) astronaut and fly out into space crews around the universe he wanted to see the stars and also see other planets in outer space------------- "Why don’t we just keep dreaming, let’s keep our mind with dream and faith, as long as we wish we can make it come true, how old you are never forget your dream and keep dreaming "

Sunday 9 December 2018

Python : Kompilasi source code ke bytecode (.py ke .pyc)



Ketika kita menulis kode program dalam bahasa pemrograman python, script yang kita tulis dalam high level language yang artinya bahasa dan perintah yang dimengerti manusia akan disimpan menjadi source file python berekstensi .py, file tsb dapat dibaca dan diubah oleh python intepreter dengan mengubah setiap baris perintah perintah menjadi instruksi instruksi byte code yang dapat dipahami oleh komputer, file byte code tsb disimpan dalam file berekstensi .pyc dan akhirnya dieksekusi oleh python virtual machine.

Proses mengubah dari high level language ke byte code atau kode instruksi komputer tsb dinamakan dengan proses kompliasi (compile).

Mengubah file souces code ke byte code ini memiliki beberapa kelebihan :

  • File source code dan byte code bisa sama sama langsung dieksekusi oleh python intepreter, akan tetapi byte code akan lebih cepat dieksekusi.
  • Proses eksekusi byte code lebih cepat karena tidak ada kompilasi lagi untuk mengubah source code ke byte code.
  • Dari sisi keamanan jauh lebih aman dibandingkan souce code yang masih berupa kode yang bisa dibaca siapa saja. bayangkan ada orang lain yg memiliki akses ke source code anda yang berisi user name, password dan setingan lainnya.  
Cara mengkompilasi file source code (.py) ke file bytecode (.pyc) bisa dilakukan dengan bantuan module 'py_compile'.

sintaks sederhananya sebagai berikut :
import py_compile

py_compile.compile('nama file.py')

Sintaks tsb berfungsi melakukan kompilasi souce code yang kita inginkan ke file bytecode, jika tidak ditentukan hasil file kompilasi akan berada dalam folder '__pycache__' di direktori aktif anda.

Saya memodifikasi dan menulis script agar lebih mudah dalam melakukan kompilasi :
import os
import time
#menampilkan file file berekstensi .py pada direktori
os.chdir('./')
for file in os.listdir():
    if file.endswith(".py"):
        print(file)
#input nama file source code (.py), bisa multiple file, dipisah dengan tanda koma (,)
a = input("Nama file .py : ")
a = a.split(',')
print(a)
import py_compile
for namefile in a :
    py_compile.compile(namefile+'.py',namefile+'.pyc')
time.sleep(5)

Pertama, tempatkan script ini bersama file file yang akan dikompilasi
Jalankan script, kemudian akan tampil list file berekstensi .py, input nama file tanpa ektensi dan tekan enter, untuk multiple file input nama file dipisahkan dengan tanda koma
 Selesai...

2 comments:

  1. Lucky Club: Get 20 free spins no deposit bonus | Lucky Club Live
    Lucky Club: Get 20 free spins no deposit bonus. Get 20 free spins no deposit bonus. Get 20 free spins no deposit bonus. Get 20 free spins no luckyclub.live deposit bonus. Get 20

    ReplyDelete
  2. BetMGM Casino in Maryland - jtmhub.com
    BetMGM Casino 청주 출장마사지 in Maryland is 충청남도 출장마사지 a trusted online 서귀포 출장샵 casino in Maryland, 포항 출장마사지 USA and offers great promotions. Check out our reviews, reviews, and more at JT 여주 출장샵

    ReplyDelete