Wei's Blog

🐒 Software engineer | 📷 Photographer | 👹 Urban explorer

[Python]Source Code Encodings

Defining the Encoding

Python will default to ASCII as standard encoding if no other encoding hints are given.

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as:

# coding=<encoding name>

or (using formats recognized by popular editors):

#!/usr/bin/python
# -*- coding: <encoding name> -*-

or:

#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
  ...


[Go] Basics

Imports

import (
	"fmt"
	"math"
)

Or

import "fmt"
import "math"
  ...


[Go] Setup Go Environment

Install the Go tools

If you are upgrading from an older version of Go you must first remove the existing version.

  ...


[Python] Multithreading

Start a Thread

import logging
import threading
import time

def thread_function(name):
    logging.info("Thread %s: starting", name)
    time.sleep(2)
    logging.info("Thread %s: finishing", name)

if __name__ == "__main__":
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO,
                        datefmt="%H:%M:%S")

    logging.info("Main    : before creating thread")
    x = threading.Thread(target=thread_function, args=(1,))
    logging.info("Main    : before running thread")
    x.start()
    logging.info("Main    : wait for the thread to finish")
    x.join()
    logging.info("Main    : all done")
  ...


[Python] Basics - Nameing Conventions

PEP 8 specifies the following naming conventions rules:

  • use lower letters at the beginning of each word;
  • use an underscore ("_") to concatenate multiple words;
  • use a single underscore for protected attributes;
  • use double underscores for private attributes.
  ...