Git Product home page Git Product logo

qrf's Introduction

QRF

QRF (Quick Reference File) Handy things that I seem to forget. Will probably need to organize this in a better way later.

Python

File Handling

Picking out components of a file path

from pathlib import Path

path
PosixPath('/home/user/python/test.md')

# .name: the file name without any directory
path.name
'test.md'

# .stem: the file name without the suffix
path.stem
'test'

# .suffix: the file extension
path.suffix
'.md'

# .parent: the directory containing the file, or the parent directory if path is a directory
path.parent
PosixPath('/home/user/python')

path.parent.parent
PosixPath('/home/user')

# .anchor: the part of the path before the directories
path.anchor
'/'

Handling Multiple desired file types

img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.dng']
vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']

p = str(Path(path))  # os-agnostic
p = os.path.abspath(p)  # absolute path
files = []
all_files = []

all_files = sorted(Path(p).rglob('*.*'))
for x in all_files:
    if re.match('Snagged_TO BE CROPPED', x.parts[len(x.parts) - 3], re.IGNORECASE):
        files.append(x)

images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats]
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats]
ni, nv = len(images), len(videos)

Find all the folders/directories with TQDM progress bar.

myList = []
for file in tqdm.tqdm(os.listdir(startPath),"Locating Folders"):
    if os.path.isdir(os.path.join(startPath, file)):
        myList.append(os.path.join(startPath, file))

import logging

import logging

logging.basicConfig(filename='my-log.log', filemode='w', level=logging.DEBUG)

# Message Types
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")

import xlrd

Read in all excel tabs to one list of dictionaries

def readExcelFile(excelFile):
    # Open Excel Workbook
    book = xlrd.open_workbook(excelFile)
    sheets = book.sheets()
    disc_list = []
    # Read Data From Sheets One at a Time
    for sheet in tqdm.tqdm(sheets,'Processing excel sheet Tabs'):
        
        worksheet = book.sheet_by_name(sheet.name)
        header = worksheet.row_values(0) # Select first row as header
        values = [worksheet.row_values(i) for i in range(1, sheet.nrows)] # Skip first row of headers

        for value in values:
            for item in value:
                item = str(item)
            # zip the headings and row values together into a dict
            disc_list.append(dict(zip(header, value)))

    return(disc_list)

Read in one excel tab by name to a list of dictionaries

def readExcelFile(excelFile, sheet):
    # Open Excel Workbook
    book = xlrd.open_workbook(excelFile)
    disc_list = []
    worksheet = book.sheet_by_name(sheet)
    header = worksheet.row_values(0) # Select 2nd row as header
    values = [worksheet.row_values(i) for i in range(1, sheet.nrows)] # Skip first row of headers

    for value in values:
        for item in value:
            item = str(item)
        # zip the headings and row values together into a dict
        disc_list.append(dict(zip(header, value))) 

    return(disc_list)

import pyodbc

Read the tables in a access database.

def printDatabaseTables(db_path):
    cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+str(db_path)+';')
    cursor = cnxn.cursor()
    for row in cursor.tables():
        print(row.table_name)

Open a table and zip headers to content to make a list of dictionaries.

def readDatabase(db_path, table):
    db_list = []
    cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+str(db_path)+';')
    cursor = cnxn.cursor()
    sql = 'select * from ' + str(table)
    cursor.execute(sql)
    dbData = cursor.fetchall()
    desc = cursor.description
    colNames = [col[0] for col in desc]
    for row in dbData:
        db_list.append(dict(zip(colNames, row)))
    return db_list

VBA

Protect All Sheets

Sub Protect()
      'Loop through all sheets in the workbook
      For i = 1 To Sheets.Count
         Sheets(i).Protect
      Next i
End Sub

Unprotect All Sheets

Sub UnProtect()
      'Loop through all sheets in the workbook
      For i = 1 To Sheets.Count
         Sheets(i).UnProtect
      Next i
End Sub

qrf's People

Contributors

ianmhoffman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.