Shorter code with functional-style programming in PHP.

So, I'm doing wordpress programming, and one of the headaches is that they store serialized objects in the database with the update_option() and related functions.

This makes querying some data difficult. Here's an example of the data structure I was dealing with (it stores sidebar settings).

Array
(
    [wp_inactive_widgets] => Array
        (
        )

    [sidebar-1] => Array
        (
            [0] => search-2
            [1] => recent-posts-2
            [2] => recent-comments-2

PulseAudio Microphone Not Working in Skype or Gnome PulseAudio

This took forever to fix. I upgraded the computer two versions of Ubuntu ago, and ever since then, the front mic stopped working.

It could be the mic's wire or the mic, but even a standalone mic wasn't working. It worked OK with Audacity, which uses Alsa.

It turned out the problem was that the Sound control panel in Gnome didn't work. Somehow, the system decided to change the input from Analog to Digital SPDIF. I don't use SPDIF.

WordPress Unit Tests in the Main Directory

I'm hacking some of the original WP files. Not the smartest thing to do, but the options are to replace the functions, or alter them, because a plugin isn't going to work.

So, I needed to run tests in the main program. Here's the quck howto.

Install wp-cli and get it working.

Then, go into your wp root and type:

wp scaffold plugin-tests --dir=.

Then run the installer script:

bin/install-wp-tests.sh wordpress_test root '--password--'

WordPress wp-signup headaches, and refactoring.

I've been modifying the wp-signup script a lot, and figured out a better pattern for doing these screens. This technique works more like "views and controllers", so it's one step closer to MVC.

WP doesn't have routes, but the wp-signup script has a paramter named "stage" that specifies the next state. It's effectively a route.

There are two types of stages: showing a form, or validating a form.

When you validate a form, you see if there are errors. If there are, you show the form, with error messages. If there are no errors, you commit the changes, and then show the next form.

Deleting Many Files (Somewhat) Quickly

I had to delete a bunch of files across multiple directories. What works quickly?

The first option I found online was:

find . -type f -exec rm {} \;

There's a faster way that deletes directly.

find . -type f -delete

That works well, unless there are a lot of files in each directory. Then, you might want to go into each directory and run the command. You don't want to run too many at once, though.

There's another way that turns out to be faster:

ls | xargs -L1 rm 

What is Dependency Injection?

I've answered this a couple times so I'm writing it down :)

DI is a way to separate services from the code that uses those services.

DI makes testing easier. It also allows the services to be replaced with alternatives - but this is less important than the testing.

DI is used in Angular, Zend Framework 2, Symfony and more frameworks.

DI is implemented as an array or dictionary of services with generic names. Services are created during startup, and registered.

My Google calendar shows up on iPhone under iCloud, but it is not in iCloud...

This is freaking weird. My personal calendar, kept on Google, showed up in an iPhone I set up, right after the phone was set up. It showed up as an iCloud calendar.

Too weird. So I have to find out what's going on - and how to stop it.

I looked at iCloud.com - there were only two calendars there, Home and Work, neither of them the Google calendar. I don't use iCloud, really.

I put a new event in my calendar. This was on my Linux desktop. It didn't sync to the iPhone!

The Mac was off! So it's probably syncing via the Mac. I turn it on. Nothing.

Django 1.8 Tutorial - 5.2 Adding a User Profile

In the previous section, I noted that I needed to learn how to make my own authenticator. I also needed to learn to add fields to the User. There are two ways to do it, and the Django docs don't clearly state which is better, for what situations.

I think the old way of adding fields, called User Profiles, is the way to go. It's more code, but I think the code ends up being a little more explicit and easier to read. It also allows you to make multiple different profiles for different things.

Django 1.8 Tutorial - 5.1 Alternative Authentication (Make Your Own)

I was hoping to get into Allauth, or maybe that and some more UX tweaks, but ended up hitting some walls. We're trying to implement an authenticator that works with an external service, but it's more complex than expected. (I won't be posting it here.)

To get to the point where I could even write parts of the authenticator, I needed to teach myself how to write authenticators. So, here are two authenticators. Both are a little odd, but I think the code is short enough that it won't be confusing.

Django Edge

Edge 2 looks nice. I'm kind of surprised I haven't heard of it before.

Also, while I'm keying up this tutorial series, it's dawning on me that it would be good to state that I'm aware that the "in" thing is moving away from apps with templates, and toward REST and real-time. I am learning Django because I started out with Django REST Framework - and the DRF material didn't really cover the various authentication, authorization, and general programming issues.

Using List Comprehensions in Python to Look at Lists of Objects

List comprehensions are a difficult-to-learn feature of Python that seem unnecessarily terse, but that's because the examples aren't that practical. It's really a powerful tool.

I was using Tweepy to get some tweets from the Twitter API, and it was a LOT of data. You'd think a 140 character string isn't that big, but Twitter delivers a lot of metadata. It's around 7K of metadata per tweet.

So, I had 15 tweets in a list, or around 100K worth of __str__ representing dozens of objects. How do you slice that up?

You use dir() to explore the properties of the different objects.

Django 1.8 Tutorial - 5. Django Registration Redux

This text is a work in progress. I'm not even done with this part myself.

In doing some digging, I found out that the leading registration app, django-registration, was abandoned. Some time later, django-registration-redux picked up the ball and has maintained it. There's also another alternative django-allauth, which does registration and integrates with social sites.

The instructions for django-registration-redux are pretty good. Just read through them, and consider this tutorial just a slight gloss of what's covered, plus some specifics about our demo app.

Django 1.8 Tutorial - 4. Integrating the Default Login Screens, Adding HTML Email

So, I started implementing the Django provided user login screens yesterday and it was requiring a ton of reading to get the different parts working. It seems so simple, from the outside, but all the configuration options made it seem more difficult than it really is.

In the attached file, a few of the old configs and views have been deleted, but I'm not going to cover that here. Just do diffs between the contents of the attached tgz files to see the differences.

Create a file in your global templates, templates/registration/login.html:

{% extends "base.html" %}

Django 1.8 Tutorial - 3. Adding Account Logins

The previous article cleaned up the UI and made the comment system work more like a comment system, but it has a glaring flaw: you could choose to post as any user. LOLz.

This small modification adds login and logout features. It does it the raw way rather than use the built-in classes, or the django-registration-redux library. This is just a temporary feature, an example to learn authentication.

That said, it does something a little different from what seems to be provided by Django: the login is embedded right in the page, where the form would have been.

Django 1.8 Tutorial - 2. Polishing the App with Static Files, CSS, JS, etc.

The last article took several hours to write, so I'm going to take a break from writing and editing for a while. These tutorial posts will still happen, but they'll be harder to read.

The previous tutorial created a "comment system", and while it was a reasonable example of using ModelForms and generic View classes, it didn't look like a real comment system. This tutorial polishes the original and makes it more like a real web app.

Here's what it looks like:

It's still not "nice", but it's getting there.

Syndicate content