Save figure as a pdf file in Python

This post introduces how to save a figure as a pdf file in Python using Matplotlib.

When using savefig, the file format can be specified by the extension:

savefig('foo.png')
savefig('foo.pdf')

The first line of code above will give a rasterized  output and the second will give a vectorized output.

In addition, you’ll find that pylab leaves a generous, often undesirable, whitespace around the image. You can remove it with:

savefig('foo.png', bbox_inches='tight')

You can also use  figure to set dpi of the figure:

import numpy as np
import matplotlib.pyplot as plt

fig  = plt.figure(figsize=(1,4),facecolor = 'red', dpi=100)
plt.savefig('test.png', dpi=100)

plt.show(fig)

 

 

Make a request to REST API using Python

This post introduces how to make a request to REST API using Python.

requests package is the commonly used one (its GitHub repo).

You can try out requests online here at codecademy, and here at runnable.com

Look at this post for a great tutorial using Requests with Python to make a request to REST API: Python API tutorial – An Introduction to using APIs (pdf) – a very good, comprehensive, and detailed tutorial.

To install Requests, simply:

$ pip install requests

See below for a simple example to make a request to REST API.

#Python 2.7
#RestfulClient.py

import requests
from requests.auth import HTTPDigestAuth
import json

# Replace with the correct URL
url = "http://api_url"

# It is a good practice not to hardcode the credentials. So ask the user to enter credentials at runtime
myResponse = requests.get(url,auth=HTTPDigestAuth(raw_input("username: "), raw_input("Password: ")), verify=True)
#print (myResponse.status_code)

# For successful API call, response code will be 200 (OK)
if(myResponse.ok):

    # Loading the response data into a dictionary variable
    # json.loads takes in only binary or string variables so using content to fetch binary content
    # Loads (Load String) takes a Json file and converts into python data structure (dictionary or list, depending on JSON)
    jData = json.loads(myResponse.content)
    #jData = json.loads(myResponse2.content, 'utf-8') #use this line if your data contains special characters

    print("The response contains {0} properties".format(len(jData)))
    print("\n")
    for key in jData:
        print key + " : " + jData[key]
else:
  # If response code is not ok (200), print the resulting http error code with description
    myResponse.raise_for_status()

 

======working with JSON data

For example, if data.json file looks like this:

{
 "maps":[
         {"id":"blabla","iscategorical":"0"},
         {"id":"blabla","iscategorical":"0"}
        ],
"masks":
         {"id":"mask-value"},
"om_points":"value",
"parameters":
         {"id":"blabla3"}
}

The python code should be something looks like this:

import json

with open('data.json') as data_file:    
    data = json.load(data_file)
print(data)

We can now  access single values in the json file — see below for some examples to get a sense of it:

data["maps"][0]["id"]  # will return 'blabla'
data["masks"]["id"]    # will return 'mask-value'
data["om_points"]      # will return 'value'

References:

Print multiple variables in Python3

This post introduces several ways to print multiple arguments in python 3.

  • Pass it as a tuple:
print("The cost for %s is %s" % (name, cost))
  • Pass it as a dictionary:
print("The cost for %(n)s is %(c)s" % {'n': name, 'c': cost})
  • Use the new-style string formatting:
print("the cost for {} is {}".format(name, cost))
  • Use the new-style string formatting with numbers (useful for reordering or printing the same one multiple times):
print("The cost for {0} is {1}".format(name, cost))
  • Use the new-style string formatting with explicit names:
print("The cost for {n} is {c}".format(n=name, c=cost))
  • Pass the values as parameters and print will do it:
print("The cost for", name, "is", cost)

If you don’t want spaces to be inserted automatically by print in the above example, change the sep parameter:

print("The cost for ", name, " is ", cost, sep='')
  • Use string concatenation
print("The cost for " + name + " is " + cost)

NOTE: If cost  is an int, then, you should convert it to str:

print("The cost for " + name + " is " + str(cost))
  • Note that %s mentioned above can be replace by %d or %f.

If cost is a number, then

print("The cost for %s is %d" % (name, cost))

If cost is a string, then

print("The cost for %s is %s" % (name, cost))

If cost is a number, then it’s %d, if it’s a string, then it’s %s, if cost is a float, then it’s %f

  • Use the new f-string formatting in Python 3.6:
print(f'The cost for {name} is {cost}')

Read file from line 2 or skip header row in Python

This post introduces how to read file from line 2 in Python.

  • method 1:
with open(fname) as f:
  next(f)
  for line in f:
    #do something

Note: If you need the header later, instead of next(f) use f.readline() and store it as a variable.
Or use header_line = next(f).

  • method 2
f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

This will skip 1 line. for example, [‘a’, ‘b’, ‘c’][1:] => [‘b’, ‘c’]

 

Open multiple files using “open ” and “with open” in Python

This post introduces two ways to open multiple files in Python.

  • “with open” # do not need to bother to close the file(s) if use “with open”
with open("datafile.csv" , "r") as f:
  f.read()
with open("datafile2.csv" , "r") as f2:
  f2.read()

Or

try:
  with open('file.csv', 'w') as f, open('file2.csv', 'w') as f2:
    do_something()
except IOError as e:
  print 'Operation failed: %s' % e.strerror
  • “open”  # need to close the file when use open.
f = open("datafile.csv" , "r")
f2 = open("datafile2.csv" , "r")
f.read()
f2.read()
f.close()
f2.close()

 

References:

 

Remove a character from a string using Python

This post introduces how to remove / replace a character from a string using Python.

In Python, strings are immutable, so we need to create a new string. If you want to remove the ‘;’ wherever it appears. See below for an example.

Remove character”;”

>>> original_str = "good; ok"
>>> new_str = original_str.replace(";","")
>>> print(new_str)
good ok
>>>

See the examples below for different ways to check whether a string contains a specific character.

>>> s = "good; ok"
>>> ";" in s
True
>>> ";" not in s
False
>>> s.find(";") == -1
False
>>> s. find(";") != -1
True
>>> chars = set(";o")
>>> if any((c in chars) for c in s):
>>> print("Found")
Found
>>> import re
>>> pattern = re.compile(r";o")
>>> if pattern.findall(s):
>>> print("Found")
Found
>>>

 

References:

How to delete a character from a string using python?

Remove all special characters, punctuation and spaces from string

How to check a string for specific characters? (pdf)

Create a hash table for large data in python

This post introduces how to create a hash table in python.

Text files can have duplicates which will overwrite existing keys in your dictionary (the python name for a hash table). We can create a unique set of the keys, and then use a dictionary comprehension to populate the dictionary.

sample_file.txt:

a
b
c
c

 

Python code:

with open("sample_file.txt") as f:
  keys = set(line.strip() for line in f.readlines())
my_dict = {key: 1 for key in keys if key}

>>> my_dict
{'a': 1, 'b': 1, 'c': 1}