Django Form field in Initial Data: requires a FieldFile instance

I just followed the source back from django’s ClearableFileInput all the way to django’s model field magic… all the way to a little thing called FieldFile.

FieldFile can be faked with these minimum attributes:

from django.core.files.storage import default_storage

class FakeField(object):
    storage = default_storage

fieldfile = FieldFile(None, FakeField, file_path)

form = SomeForm(initial={'filefield': fieldfile })

WHEW!

Django DatabaseError invalid byte sequence for encoding “UTF8”

Wow this debugging lead me nowhere.

invalid byte sequence for encoding “UTF8”:
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by “client_encoding”.

If you’re getting a DatabaseError, double check that you are not trying to store pickled data through the ORM. The python docs are wrong about pickle protocol 0 being ASCII only and pickle.dumps(u’©∆∂ƒ©˙∆˚’) will output non ASCII.

bug tracked here: http://bugs.python.org/issue2980

Causing a tough to track error at the DB level which does not consistently appear until a “bad” string is passed in.

Python Profiler on Ubuntu Missing or import hotshots.stats fail

Turns out Ubuntu is missing pythons standard library component profile.py due to licensing issues related to debian.

I don’t really care about that.

What I care about is the solution.

Enable the multiverse repository in your ubuntu.

vim /etc/apt/sources.list

Copy and paste one of the lines (they are version specific) and replace the ending text with “multiverse”. It doesn’t really do any damage to keep other repo names on there.

## Added mutliverse for python-profiler
deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse

Update repo list and install python-profiler.

apt-get update
apt-get install python-profiler

Done.

Finder 100% CPU Usage (or 80%)

Mine was solved by going to Finder, CMD+J, then disabling “Show Icon Preview”.

That let me know some kind of thumbnail generation was failing, and since this was a new problem, I started looking at recent files on my desktop. One of them did not have a preview. I deleted that, and my fans immediately turned off.

I don’t know what file it was, but apparently it was stuck on it…

Find all OSX icons and put them in a folder.

First, set up the locate command. (it will give you instructions).

Wait while the database gets finished (just walk away for 10 mins)

Now type in a terminal:

$> cp $(locate .icns) /path/to/output/folder/
$> open /path/to/output/folder

There are all 858 icons!

Gmail Security Error

If you can’t get to gmail due to HTTPS errors, make sure your systems date/time is correct, as the certificates are validated against your system time.

Python Mailjet API Example

Here’s a way to get all unsubscribed in your account ever;

I’m having trouble with the fact it starts blocking me after a few “bad requests” trying to remove a contact that’s not in the list, but oh well. This is progress.

def get_all_unsubscribed():
	import mailjet

	api = mailjet.Api()
	list_ids = [x['id'] for x in api.lists.all()['lists']]

	unsubscribed = []

	for list_id in list_ids:
		total_accounted_for = 0
		total_count = api.lists.Contacts(id=list_id, status='unsub')['total_cnt']
		while total_accounted_for < total_count:
			if total_accounted_for:
				result = api.lists.Contacts(id=list_id, status='unsub', start=total_accounted_for)
			else:
				result = api.lists.Contacts(id=list_id, status='unsub')
			print 'found ... results ', len(result['result'])
			if not result['result']:
				print 'found end... @ ', total_accounted_for, total_count
				break
			for u in result['result']:
				unsubscribed.append(u)
			total_accounted_for += len(result['result'])

	return unsubscribed