Syntax personal notes (Python)

Content

NoteCode Purpose Syntax Additional Information
?AN001 Dealing with Japanese character /format [String encoding] QtCore.QString.fromUtf8
  • Library: PySide
  • Keyword: try, except, AttributeError, def, UTF8 Encoding, string encoding
AN002 Using Japanese character inside the source file (.py) [File encoding] # -*- coding: utf-8 -*- Keyword: file encoding, UTF8
AN002 Print without newline  from __future__ import print_function Keyword: __future__ , print statement, print() function, Python 3, Python 2.6, end

 

AN001
Purpose: Dealing with Japanese character /format [String encoding]
Library: PySide (using QtCore)
Keyword: try, except, AttributeError, def, UTF8 Endoding, string encoding

What is
UTF8 Encoding: Unicode Transformation Format, that uses 8-bit blocks to represent characters

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

Code location: at the beginning (header)

AN002
Purpose: Using Japanese character inside the source file (.py) [File encoding]
Keyword: file encoding, UTF8

# -*- coding: utf-8 -*-

Code location: at the VERY beginning (header)

AN003
Purpose: Print without newline
Keyword: __future__ , print statement, print() function, Python 3, Python 2.6, end

  • Currently &  previously (before Python 3):
    print is a statement, and the resulted printed text always end with newline (or whitespace, if print command is ended with comma ‘,’)
  • After Python 3:
    print becomes function (instead of statement) print() and argument “end” can be used to specify the end of printed text
  • How to use print() if installed python version is still Python 2.6 (and above):
    using code
from __future__ import print_function
print("\t", end="")

_

Advertisements

Libraries in Python

Useful modules, packages, and libraries in Python. This is a list of personal preferences (previously/currently used).
Some definitions are taken from here and here.

Purpose Modules name Additional information
GUI TkInter Traditional python UI toolkit
PyQt Qt framework
PySide Qt framework

  • QtGui
  • QtCore
Foreign function interface CTypes Package for calling functions of dlls/shared libraries
[now is included w/ Py2.5 as Standard Library ]
 XML processing ElementTree [now is included w/ Py2.5 as Standard Library]
 Database sqlite3
 Operating System services os miscellaneous OS interfaces
[Standard Library]
time time access and conversions
[Standard Library]
 Text Processing services re Regular expression
[Standard Library]
Numeric & Mathematical modules  math mathematical functions
[Standard Library]
 Data types copy shallow & deep copy operations
[Standard Library]
 Structured Markup processing tools xml.dom XML processing, Document Object Model (DOM) API
[Standard Library]
xml.dom.minidom minimal DOM implementation
[Standard Library]
 Binary Data Services codecs Codec registry and base classes
[Standard Library]
 3D-rendering related pivy python binding library for Coin3D (3D-rendering library)
Read/write office files openpyxl to read/write Ecxel 2010 files
[stored in PyPackageIndex]

CMD command, python related

Berikut ini daftar perintah cmd yang berhubungan dengan python

Purpose Command Additional Information
Installing pip (python install manager) python get-pip.py From cmd & directory where you save get-pip.py
pip install
where pip if “pip install” results error, check whether pip command refer to other program, e.g., strawberry perl (if yes, then uninstall strawberry perl)
Checking which python is associated with (if accidentally installed 2 version of python) assoc .py
  • probable output:
    .py=Python.File
ftype Python.File
  • probable output:
    Python.File=”C:\…\python.exe” “%1” %*
Installing in specific target (e.g., other directory inside specific package) pip install –target=”/Applications/FreeCAD.app/Contents/lib/python2.6/site-packages” openpyxl  *belum berhasil nyoba di FCAD
 Installing pyside pip install pyside
 To convert .ui (pyside) into .py, using pyside-uic (converter provided by pyside) pyside-uic NAMEOFUIFILE -o NAMEOFPYFILE -x
  •  pyside-uic is located in folder: python Lib\site-packages\PySide
  • switch -x: if you want to double-click the resulted .py
Help python –help help: list of options/switches/parameters
 Python version python –version
python -V