Senin, 09 Desember 2013

Tips for Python


  1. Use [Ctrl+Alt+Enter] to configure interactive console in Eclipse-PyDev (Console for currently active editor)
  2. Use print('x',...) to print without newline concate with list
    print('x', os.listdir(self.right))
    
  3. Enable PyDev Interactive Console while Debugging Session
    To enable, go to Window | Preferences | PyDev | Interactive Console and check 'Connect console to Debug Session?
  4. If you get an error message like this
    Assignment to reserved built-in symbos: abs
    , then then you can add comments
    # @ReservedAssignment
     at the end of the line so that it is no longer regarded as an error
  5. ...

Get Modification Time of File in Python

'''
Created on Dec 9, 2013

@author: BalonCoding
'''
import os
  
fileName = 'c:\\fm\\fmb\\MTR00035.fmb'
#using os.stat.st_mtime
st=os.stat(fileName)    
mtime=st.st_mtime
print("mtime (using os.stat.st_mtime) = " + str(mtime))
#mtime (using os.stat.st_mtime) = 1141933677.28125

#using os.path.getmtime
print("mtime (using os.path.getmtime) = " + str(os.path.getmtime(fileName)))
#mtime (using os.path.getmtime) = 1141933677.28125


############################################################################################
import datetime

#using datetime.datetime
print("mtime (using datetime.datetime) = {}".format(datetime.datetime.fromtimestamp(mtime)))
#mtime (using datetime.datetime) = 2006-03-10 02:47:57.281250


############################################################################################
import stat, time
print("mtime (using time.ctime ) = {}".format(time.ctime(st[stat.ST_MTIME])))
#mtime (using time.ctime ) = Fri Mar 10 02:47:57 2006

Jumat, 29 November 2013

Disconnect and Map Network Drive on Windows with Python

I'm using Python version 3.3.2
  1. method using subprocess.Popen

  2. # Balon Coding, 29 November 2013
    # Map network drive using subprocess.Popen
    # Drive letter: Q
    # Shared drive path: \\192.168.51.1\balon$
    # Username: balon
    # Password: coding
    
    import subprocess 
    
    # initialize
    driveLetter = 'Q:' 
    networkPath = '\\\\192.168.51.1\balon$' 
    user = 'balon' 
    password = 'coding'
    
    print('map network drive using subprocess.Popen')
    
    # Disconnect anything on drive letter Q
    winCMD = 'NET USE ' + driveLetter + ' /delete'
    print(winCMD)
    subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
     
    # Connect to map network drive to letter Q
    winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
    print(winCMD)
    subprocess.Popen(winCMD, stdout=subprocess.PIPE, shell=True)
    

  3. method using subprocess.call
  4. # Balon Coding, 29 November 2013
    # Map network drive using subprocess.call
    # Drive letter: Q
    # Shared drive path: \\192.168.51.1\balon$
    # Username: balon
    # Password: coding
    
    import subprocess 
    
    # initialize
    driveLetter = 'Q:' 
    networkPath = '\\\\192.168.51.1\balon$' 
    user = 'balon' 
    password = 'coding'
    
    print('map network drive using subprocess.call')
    
    # Disconnect anything on drive letter Q
    winCMD = 'NET USE ' + driveLetter + ' /delete'
    print(winCMD)
    subprocess.call(winCMD, shell=True)
    
    # Connect to map network drive to letter Q
    winCMD = 'NET USE ' + driveLetter + ' ' + networkPath + ' /User:' + user + ' ' + password
    print(winCMD)
    subprocess.call(winCMD, shell=True)
    

Kamis, 28 November 2013

List Available Drive Letters In Python

I'm using Python version 3.3.2
import string
from ctypes import windll

def get_drives():
    drives = []
    bitmask = windll.kernel32.GetLogicalDrives()
    for letter in string.ascii_uppercase:
        if bitmask & 1:
            drives.append(letter)
        bitmask >>= 1

    return drives

if __name__ == '__main__':
    print (get_drives())     # On my PC, this prints ['C', 'D', 'W', 'X', 'Y', 'Z']

Minggu, 17 November 2013

Print 'Hello World' di Python

Pertama kali belajar di Python sudah menghadapi tantangan pertama, yakni hanya untuk memunculkan "Hello World".
>>> print "Hello World!"
     File "", line 1
       print "Hello World!"
                                 ^
SyntaxError: invalid syntax
>>>

Ternyata setelah gugel, ada yang menyatakan ini merupakan syntax yang berlaku di Python 2.x, sementara yang BalonCoding gunakan adalah Python 3.3 yang sudah menggunakan syntax print (). Sehingga untuk menjalankan print "Hello World", maka lakukan seperti berikut ini,
text = "Hello World!"
print (text)

Jumat, 01 November 2013

Starting emulator for AVD - Failed to allocate memory: 8

The solution is edit file config.ini [C:\Users\acer\.android\avd\.avd\ 

from

hw.ramSize=1024

to 

hw.ramSize=1024MB

First Android Tutorial Project Error

I got error messages like this on DisplayMessageActivity.java
Error:(17, 6) Gradle: error: cannot find symbol class SuppressLint
Error:(37, 9) Gradle: error: cannot find symbol class Intent
Error:(41, 9) Gradle: error: cannot find symbol class TextView
Error:(41, 33) Gradle: error: cannot find symbol class TextView
import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.TextView;