Raymond Klucik - March 16th 2016
Sometimes you need to spin off tasks to be done in the background of your program so your main task continues to run. If you were to have a music application, such as Spotify, playing your music you'd want it to be seamless and play your next track immediately after the current one should you press 'skip'. This is where multithreading is useful; while the main program processes the current file it sends another thread to fetch the next song. Doing this is a more efficient way to distribute CPU.
Setting up a multithreaded application can be done using the threading module. We need to import Thread from the threading class in addition to the time class so that we can print the duration of each timer. We create our 2 threads in main, t1 and t2, and pass a target function to each of them. Our target function, timer requires 3 arguments; (name, delay, repeat) which we set to the values seen below so that we have an overlap in our threads making it a true multithreaded example. Define the timer function and pass the parameters. We use ctime to print the time in c-format which is easier to work with in Python code.
After we have defined the target function and set up the timers we need to tell them to start in main() and this is where the two threads will actually start and loop through the printing commands. The threads will run in the background as two separate processes and will finish after the main program.
TRAVERSING DIRECTORIES USING PYTHON
RAYMOND KLUCIK - MARCH 8TH 2016
I took a look into traversing directories with Python to search files for a particular string. Considering what I would have to do in C/C++ it gave me a headache but after throwing google search after google search of doing this by use of Python it became clear that Python has a simple means of accomplishing this. There seems to be a library and method for almost everything.
The task was to parse many files line by line and search for an absurdly unique string;
Pretty scary, right? Not until you realize that to Python, that's just a string of characters and to look through files checking if that string exists on each line is actually pretty easy by use of the 'os module'. The hardest part was getting the framework of how I would approach this problem down and spoofing data to actually test how to use it.
Traversing any directory is as simple as a call to os.walk and a couple nested for loops. Doing so allowed me to traverse the entire directory and pull data from each sub directory and then the .txt files from there. The problem was I didn't have any data to run this program under...nothing wget couldn't fix. First you need to install wget and if you have Brew as your package handler it's as easy as;
brew install wget
and once its finished installing, go ahead and run this command into the terminal to get some files to test your program with;
wget -r -A=.txt http://www.textfiles.com/etext/AUTHORS/SHAKESPEARE/
Huzzah! Now you have a ton of Shakespeare transcripts to test your program with. I created folders A-D and moved several files into each and embedded them all under my development directory. From there I was able to run my program and a message prompted me with what word I wanted to search the directories for and it printed out how many times that word was mentioned in each sub directory. The trick is to use os.walk so you can list out every file in each sub directory. From there you are able to specify the type of file you want to to search within, (.txt for our Shakespeare files), and the Python program goes to each file and searches each line for the unique string. If the string is found in the .txt file, it immediately tallies in which sub directory it was found in and stops searching that particular file and moves on to the next.
I was able to successfully traverse the sub directories for the unique strings with my Python code. And for the record, 'faith' was mentioned 32 times in the Shakespeare plays:)
I took this a step further allowing us to visualize how many times the searched string appears in each subdirectory using matplotlib.
PYTHON + Accelerometer Data
Raymond Klucik - March 6th 2016
Using Python to plot various activities in my Sunday routine.
An app named SensorLog in the App store reads out accelerometer data from an iOS device. I would expect that someone who analyzes motion data would work with .csv files so, I exported the data collected into this format. I placed the phone in my pocket and collected accelerometer data from 7 'Everyday Activities' listed here;
- Working at a computer while sitting
- Working at a computer while standing
- Going up and down stairs quickly
- Standing and talking on the phone
- Walking to the bus stop
- Pacing back and fourth while talking on a different phone
- Eating while standing up
The data was formatted as seen in the Xcel and rtf files below. The numbers indicate; sequence, x-acceleration, y-acceleration, z-acceleration followed by an activity label, respectively.
Over 100k data points were collected which spanned about 4 hours of the above activities.
In one file I wrote a Python script called 'accelerometer.py' which parsed through the 8_data.csv file from SensorLog and then plotted that data using two common Python libraries; numpy and matplotlib.pyplot. The resulting plot can be seen below;