Back Home

Setting up Local Environments

pip install virtualenv

to create a virtualenv envrionment

virtualenv -p /c/Python27/python <folder>

to activate it

source <folder>/bin/activate

Language Basics


variables can be appended with comma (plus for string)

print ("file: " + name  + " last modified: ", last_modified, "year ", year)
The basic python **print** command includes a newline 
<code python>
print("hello without newline", end='') 


    open('/path/to/does/not/exist', 'rb')
except (SystemExit, KeyboardInterrupt):
except Exception, e:
    logger.error('Failed to open file', exc_info=True)

File Processing and Samba Shared Folders

Python (on windows) can access a shared folder in the following format:

import os
path = '\\\\seljy1-s0022\\Shared folder\\CCARE PROD\\History\\'
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        print( "file: " + file)


python supports a standard logging framework:

import logging
logger = logging.getLogger(__name__)

Setting the logging configuraiton:

import logging
logger = logging.getLogger(__name__)
# create a file handler
handler = logging.FileHandler('hello.log')
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add the handlers to the logger
logger.addHandler(handler)'Hello baby')

Logging Config via ini File

Logging code to import config:

import logging
# load my module
import my_module
# load the logging configuration
bar = my_module.Bar()

The logging.ini file can look like:

format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

Logging with - Loguru

Xpath with Parcel

- Xpath is supported with libxml2 which is fast but has binary dependencies that can be a problem on windows - A functional alternative is Parsel

from parsel import Selector
sel = Selector(text=u"""<html>
            <h1>Hello, Parsel!</h1>
                <li><a href="">Link 1</a></li>
                <li><a href="">Link 2</a></li>
for item in sel.xpath('.//a/@href'):
    print (item.get())

will print

Static Typing - MyPy

def sum_and_stringify(nums: List[int]) -> str:  
    """Adds up the numbers in a list and returns the result as a string."""
    return str(sum(nums))

Grabbing Data - a better curl - Requests

The Requests library

import requests
r = requests.get('')

a posting example:

import requests, json
url = ''
payload = open("request.json")
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r =, data=payload, headers=headers)4

Python Testing

def inc(x):
    return x + 1
def test_answer():
    assert inc(3) == 5

Python Transpiler

  • RapydScript is a pre-compiler for JavaScript. The syntax is very similar to Python, but allows JavaScript as well. This project was written as a cleaner alternative to CoffeeScript.

Python Logging Libraries

Python Visualization

  • MATLAB like plotting framework
  • Seaborn Built on top of matplotlib

Creates more sophisticated graphs that look more professional

  • Basemap library for plotting 2D data on maps in Python

Had lots of problems with installation

  • Folium Visualize data on a Leaflet map

Built-in tilesets from: OpenStreetMap, MapQuest Open, MapQuest Open Aerial, Mapbox, and Stamen, and supports custom tilesets with Mapbox or Cloudmade API keys.

  • Bokeh A Python interactive visualization library that targets modern web browsers for presentations.
  • Make interactive charts online from Excel or CSV data.
python.txt · Last modified: 2019/05/09 05:49 by root
RSS - 200 © CrosswireDigitialMedia Ltd