Motivation: I wanted to create a place to contain code snippets that I often look back on when building a new project. Python has excellent online resources, however, sometimes you have to stich experience and resources together to find the solution you are looking for. In this section of the site, I will be posting code that has been successfully used in projects and that covers a wide-range of topics. I will try and categorize topics together in sub-sections and organize them efficiently as the page grows.

Topics: CSVs, Miscellaneous

Notes: This page is under heavy construction! These code snippets are for Python 2.7.x and will assume you have a moderate level of Python experience. If you have any tips or suggestions for me, feel free to send me a message via the contact me page

CSVs

Working with Python's built in csv library

Title: Creating a new CSV

Description: CSVs are a great way to store data. With the CSV package, writing to csvs is made extremely simple. Personally, I like to have the data stored in the form of a list, and each list is representative of a row. However, you can work with dictionaries as well. Therefore, a full csv will be merely a list of lists. There are a ton of things you can do with the CSV package, look here for more information.

Title: Adding data to an existing CSV

Description: In the previous tip, we created a CSV, but what if we want to append new data to a csv? The process is very similiar to creating a new csv, but with a few slight differences.

Title: Reading a CSV

Description: Deciphering data stored in a CSV can be time consuming by hand, but reading one with Python can be very efficient. You can easily find the number of rows and column, perform analysis and a lot more.

Miscellaneous

Useful code galore

Title: Get your computer name and ip

Description: Using python's standard library package, socket, you can easily obtain the computer's name and ip.

Title: Find files in a directory by last modified

Description: Using Python's standard library package, os, find all the files in a specific directory and return them based on what was last modified.

Title: OS path stuff

Description: Using python's standard library package, os, you can manipulate directory and file paths to get information. Extremely useful when dealing with relative paths.

Title: Temporarily append a Python package to your Python path

Description: If you are working with a large Python package project that has modules that need to import modules above it in the directory, you may want to temporarily append the root folder to your Python path. This way you can first import the folder, and then a Python module underneath it. You can easily do this using Python package os, and sys and a one-liner that you want to put in the main file that is ran.

Title: One liners

Description: Sometimes you just want your code more compact, here are a few tips to do just that in the form of function returns. The first function will be a multi-line function and the second one will be the same function result in one-line form.