Python Modules --> by myCODEnotein

MySQL with Python

Installing the module

pip install mysql-connector

Importing the module

import mysql.connector as m

Connecting with the database

connection = m.connect(**options)
connection = m.connect(host='localhost',user='myCODEnotein',password='myCODEnotein',database='myCODEnotein')

Creating a cursor object -- to execute queries

cursor = connection.cursor()

Executing queries

cursor.execute(query)
# Note: if a command like "SELECT" is already used then another select command can only be executed after you have fetched the retrieved records

Fetching records

records = cursor.fetchall()

# To Get One Record
onlyOneRecord = cursor.fetchone() 
                    
# This function works like read function in file handle. After fecthing one record if the function is called again then next record will be returned

rowcount function

rowsAffected = cursor.rowcount()
# Returns the rows affected by a query , usually after a dml command

OS module in python

Importing os module

import os

Check if file/dir exists

os.path.isfile(Path) 
os.path.isdir(Path)

Directory Functions

os.mkdir(name_of_dir)
os.rmdir(path_of_dir)
os.listdir(list_dirs_in) # list_dirs_in by default = current working directory
os.getcwd() # returns current working directory
os.chdir(path_of_dir) # changes current working dir to path_of_dir

Start A file with os module

os.startfile(filePath)

Running a terminal command through os module

syntax: os.system(command_name) # command_name is strings
#For example:
os.system('cls') # clears the command prompt screen in windows

Tkinter ---> GUI dev in python

Click the link for Detailed Notes

Importing tkinter

from tkinter import *

Required Code

root = Tk()
# Note this is valid only if 2nd import statement is written
root.geometry("widthxheight")
root.title("myCODEnotein -- python modules")
root.mainloop()

Basic widget forming syntax

widgetHolder = Widget(master,**options)
# To know **options run widgetHolder.keys()

Displaying the widgets on screen

To display widgets on screen you can use pack,grid or place.
widgetHolder.pack(**options)
# pack and grid can't be used in widgets with same parent
widgetHolder.grid(row=rowNo,column=columnNo,**options)
# pack and grid can't be used in widgets with same parent
widgetHolder.place(x=x_coordinate,y=y_coordinate,**options)
To know **options you can run 
help(widgetHolder.pack/grid/place)

Basic widgets

  • Label
  • Frame
  • Text
  • Entry
  • Button
  • Canvas
and a lot more

Some New widgets

# To get the new widgets importing ttk module in tkinter is required
from tkinter import ttk
New Widgets:
  • Combobox --> # Entry + MenuButton
  • Notebook --> # Used to give tabs
  • Progressbar
  • Separator
  • Sizegrip
  • Treeview
Click the link for More info : Official python tkinter docs

Datetime module in python

Importing the module

import datetime
# Importing date class only
from datetime import date

Knowing today's date

d = date.today()
#Knowing the month,year,day
d.month   # Returns integer type value
d.year   # Returns integer type value
d.day   # Returns integer type value