Stay Mindful


A few weeks ago at the CMU iOS App Challenge, following the release of Swift and Xcode 6, Ally and I created a Mindfulness Journaling app, which you can view via the GitHub and ChallengePost links above.

Mindfulness journaling is all about non-judgmental observations of the internal experience of thoughts and emotions. To help with this aspect of journaling, the New Entry view has, along with an empty text box, the option to create a list of observations and their intensities. For example, along with my journal entry, I could add an observation of Anxiety with the rating 7/10. As we further develop the app, this type of data can be used to track anxiety levels over time, or to track the frequency of various observations.

Stay Mindful

Web apps!

Over the last couple months I’ve been working on web applications, since I want more of my work to be accessible to more people.

Grid is a game my father, Alec Barker, originally wrote for Windows. I adapted it for web, and also added an AI API. There are three builtin AIs, and you can also upload your own JavaScript file to include your own AI. The hard AI is literally nearly impossible to beat*, partly due to the way the game is designed: the AI just follows its opponent, and neither player (usually) makes any progress. If you can think of a modification to the game that would solve this issue, let me know!

*My sister Emily managed to beat the hard AI

*My sister Emily managed to beat the hard AI

Cubr is an application I originally wrote in Python using OpenCV for a programming course (read about that here.) As of now, Web Cubr can solve the cube, but this is only the beginning. I’m working on a more user-friendly tutorial mode, as well as webcam integration. A goal I have for this project is to make color detection adaptive. That is, every time the program detects the wrong color and the user corrects it, it will learn and become more accurate for future users.

cubr

Reddit Scraper: Scraping top images from your favorite subreddits (with Python)

This post is a bit rushed, but if I don’t post it now, I know I won’t have a chance for another few weeks. I leave for the airport to go back to CMU tomorrow. My bags are packed, everything’s all squared away… except I want to write about a little project I’ve been working on this summer. So, without further ado…


The problem

If you don’t already know, Reddit is a hub of internet links and associated comment sections. The top links shown are the ones that receive the most upvotes (user-submitted clicks of approval.) Reddit is divided into “subreddits”, each with its own community theme. For example, reddit.com/r/dexter is a community where people share links about the tv show Dexter. /r/wallpapers is a subreddit where people post links to their favorite desktop wallpaper images. I like to look at /r/aww when I need some cheering up. People post links to pictures of adorable kittens and puppies and all sorts of cute things.

But I’ve been thinking, wouldn’t it be great if, instead of navigating to Reddit to see those pictures, I just had to look at a folder on my computer? Or even just at my desktop wallpaper? And what if there were a different adorable kitten on my desktop every time I looked?

The solution

I created an app that can grab the top pictures from any subreddits and save them to any directories on your computer. Mac OS X has the option to change the desktop wallpaper, at a set interval, to a random image from a given directory. So, I configured Reddit Scraper to grab the top 5 pictures from /r/aww every day at 8:00 PM (using cron) and put them in my wallpapers directory.

The app is very configurable. You can take images from any number of subreddits and save them in whichever directories you choose. If you want to, you can even take non-image files, like HTML files, and save them for offline viewing. See the “How to use it” section below for details on how to start getting daily (or even hourly, or minutely, or weekly…) updated wallpapers or archival folders from /r/wallpapers, /r/earthporn, you name it!

The results

RedditScraper
The configuration GUI

Finder
A folder I’ve scraped photos into

Wallpaper
My (current) desktop, thanks to /r/wallpapers

How to use it:

1. Fork the source on GitHub (link at the top of this post)
2. Make sure you have Python 2.7 and other dependencies installed (notably PRAW)
3. Run __main__.py and use the GUI to configure your settings (make sure to press Save)
4. Use your favorite process scheduler to run scrape.py at whatever interval you choose
5. Sit back and let reddit_scrape do all the work.


That’s it! If just one person gets use out of this, I’ll be happy. Please comment below with any feedback (suggestions, bugs, etc.)

Connecting iCloud PhotoStream to Dropbox

If you live in the world of Apple, you probably know what the Photo Stream is. You take a picture on your iOS device, and it automatically goes into your private photo stream. You can also choose to share it with friends or family in a photo stream to which they’ve subscribed. In my family, each of the four of us has our own photo stream, and we use this to share photos with each other. It’s been a really nice way to stay in touch with my family in Seattle when I’m in Pittsburgh. Yes, of course we talk on the phone too, but the photo stream allows us to share small things right then and there.

ANYWAY, this isn’t an advertisement for the photo stream.

If you have a Mac with iPhoto, you may have noticed that your streams, both private and shared, are synced with iPhoto as well. Cool! But what if you like keeping your photos somewhere else, like Dropbox or Google+? It turns out, iLife stores your photos in a really strange way. Each photo is in its own directory, and if you subscribe to multiple photo streams, your photos will have duplicate names. So, getting photos from your stream to another place on your system isn’t as easy as dragging and dropping.

That’s why I wrote this script, which recursively moves all the photos from the (hidden) photo stream storage location into a specified folder and guarantees unique filenames. On my computer, I set up a cron job so all the photos from my stream are synced with Dropbox every evening.

This has several advantages: one, since it’s a shared Dropbox folder, my family members who use Windows (all of them) can access the stream photos from their computers, without relying on a fast connection from icloud.com. Two, it means I have all my photos in one place, in Dropbox, rather than scattered between iPhoto and my file system. iPhoto is great if it’s all you use, but if you need integration between your photos and the rest of your OS, iPhoto doesn’t like to play nice.

So, now that we’ve covered the rationale, here’s the script:

Exploring Python’s new Enum type

The first alpha for Python 3.4 was released yesterday [link]. Two of the major changes to 3.4 were new additions to the standard library: functools.singledispatch and enum.Enum.

Enumerations (Enums) are a way to define a finite set of cases or states with no programmatic ordering. For example, we could have an Enum class with members ["apple", "banana", and "orange"], but it wouldn’t make sense to have an Enum class with members [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] because these have a programatic ordering and are better expressed as integers. Enums have been around in other languages (like C#) for years. There are many times when it’s useful to have a variable representing a particular state, and Python developers have had to find ways around it. Tkinter, for example, has N, S, W, E as cardinal directions used for anchoring widgets. These are stored as integers.

Up until Python 3.4, there were two prominent ways of implementing Enums: global constants, and class variables. Module-level enumeration states are almost always stored as globals. For example:

>>> RED, GREEN, BLUE = range(3)
>>> ORANGE, BANANA, APPLE = range(3)

This works well enough, although I’ve intentionally provided an example where it causes problems. Is ORANGE a color or a fruit?

>>> color = RED
>>> color == RED
True
>>> color == ORANGE
True
>>> color + GREEN == BANANA
True
>>> # Completely nonsensical

Another option is to use classes to get around this:

>>> class Color(object):
        red = 1
        green = 2
        blue = 3

>>> class Fruit(object):
        orange = 1
        banana = 2
        apple = 3

This sort of deal with the ambiguity of the word orange:

>>> color = Color.red
>>> color == Fruit.orange # no one would make this comparison
True

So at least now the members are assigned to a class rather than just global integers. But comparisons between makeshift enum classes still give undesirable results, and we can still perform integer operations on them.

Enter the enum module.

>>> # The new way of doing things:
>>> from enum import Enum
>>> Color = Enum("Color", "red green blue orange black")
>>> Color.red

>>> Color.red + Color.green
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in 
    Color.red + Color.green
TypeError: unsupported operand type(s) for +: 'Color' and 'Color'
>>> # That makes a lot more sense!

This is a huge improvement over the old way of implementing Enums. We can’t add enum members – not without defining a special __add__ method for our class, anyway! However, this is certainly possible, if you want to define a custom __add__ so that Color.red plus Color.blue is Color.purple. Also, Color.red won’t evaluate as equal to anything but itself, regardless of what its internal integer value is.

Wait, its what? Now that we have a dedicated Enum class, why do we need integer values associated with enumeration members? The main reason is for backwards compatibility. Some code relies on modules’ integer values their enum members. In fact, the enum module also provides an IntEnum class, where members can be treated as ints, specifically to support backwards compatibility. Another benefit of having integer values associated with enum members is the ability to include aliases, such as aubergine and eggplant in this example:

>>> class Food(Enum):
        apple = 1
        banana = 2
        orange = 3
        aubergine = 4
        eggplant = 4  # Synonyms can be aliases
        def eat(self):
            print "nom!"

>>> Color.orange == Food.orange
False
>>> Color.orange.name == Food.orange.name
True
>>> Color.red == Food.apple
False
>>> Color.red.value == Food.apple.value
True
>>> for food in Food:
        print(food)
Food.apple
Food.banana
Food.orange
Food.aubergine
Food.eggplant
>>> type(Food.eggplant)
<enum 'Food'>
>>> isinstance(Food.eggplant, int)
False
>>> Food.banana.eat()
nom!

This offers a much more intuitive way to deal with Enums than we have in python 2.7.

Takeaways

Python 3.4′s Enum implementation is definitely better than what we have been using. However, it still relies on externally associating enumeration members with integers. This is mainly because the Enum class is long overdue, and there have been so many workarounds implemented. Also, there’s no reason the enum module can’t be ported to Python 2.7, other than that the core developers really want us to migrate to 3K. I don’t blame them – it has a lot to offer. But there just isn’t enough traction yet, and it may be worth it to include Enum in the Python 2.7 standard library. Or perhaps as a 3rd party module… Thoughts?

Update: Redditor eliben commented that there is an official backport for 2.4-3.3.