Python Programming

Class 7

INI File Structure [Configparser]

A configuration file consists of sections, each led by a [section] header, followed by key/value entries separated by a specific string.

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

Load Credentials with Configparser

import configparser

#Load Credentials
config = configparser.ConfigParser()
config.sections()
config.read('Credentials.ini')
config.sections()


Connecting to MySQL Using Connector/Python

import mysql.connector

# Database
dbconfig = {
    'database': database,
    'user': user,
    'password': password,
    'host': host
}

# Conect to database
connection = mysql.connector.connect(**dbconfig)
  • Keep connection arguments them in a dictionary and use the ** operator

Read SQL query  into a DataFrame

#Consult query
df = pd.read_sql(sql, con=connection)
  • sql: SQL query to be executed.

Pandas Arithmetic Functions

See Example

  • sum()
  • max()
  • min()
  • idxmax

Parse JSON into Python dict

import json

file_name = 'json_example.txt'
myfile = open(file_name, 'r')
json_example = myfile.read()
my_dict = json.loads(json_example)

Challenge

See on Evernote

Resources

Python-Programming [Class 7]

By Jose Arrieta

Python-Programming [Class 7]

INI File, Load Credentials, Connecting to MySQL, Read SQL query into a DataFrame, Parse JSON into Python dict,

  • 1,579