31fps by Sam Purtill A blog about business, technology, and life

22Jul/072

One of the many reasons why I still hate Java

Ok here is the Python that I wrote:

word_list = words.split(', ')
word_list.sort(lambda x, y: len(y)-len(x))
for word in word_list: print word

And here is the Java:


import java.util.Comparator;
import java.util.Arrays;
class LengthComparer implements Comparator {
public int compare(Object a, Object b) {
return ((String)b).length() - ((String)a).length();
}
}
public class JavaTest {
public static void main(String[] args) {
String words = "java, python";
String[] word_list = words.split(", ");
Arrays.sort((Object[])word_list, new LengthComparer());
for(int i = 0; i < word_list.length; i++)
System.out.println(word_list[i]);
}
}

Indentations will probably be lost in the HTML, oh well. Anyways.

Oh someone just told me a way to make the Python even shorter:

print "\n".join(sorted(words.split(", "), lambda x, y: len(y) - len(x)))

And I couldn't resist putting this picture up from ICANHASCHEEZBURGER, my favorite place on the internet... This made me think of Java (it also made me laugh hysterically)

NO PAIN NO GAIN

Filed under: Python 2 Comments
28Jun/071

Wow, I finally learned recursion.

One of the things that bothers me the most about programming is all the terms that everyone throws around. Sometimes I hear terms that I have no idea what they mean, and they sound really complicated, but in reality they are just big words and could be implemented by a 3 year old. So tonight I had to write this stupidly easy script.

It was like this: create a random 10 character string, and if that string exists in the database, create another random one until you have a unique string. So I wrote a function that was very practical; it did the job and made sense. I asked a question in the Django IRC to see if what I was doing was 1) right and 2) efficient, and somehow someone started talking about a recursive function. WTF is recursion? I hear this word all the time, I thought, but I never know what people are talking about. So I did what everyone with an IQ greater than 40 would do -- I Googled it. Turns out I've been writing recursive functions since I was 12.

Just because you know all the terms doesn't mean you're a great hacker. This post goes out to all you CS majors that know all the theory but can't code for sh......

Filed under: Django, Python 1 Comment
7May/077

Django and S3 – integrating

This was so much easier than I had anticipated. There are three things you need to do to get S3 up and running.

#1) Sign up on Amazon S3 to get the secret keys that you need (there are two of them). Don't want to spend time talking about this because it's a Google search away.

#2) Download the S3 library that Amazon gives out. Beautiful code in there.

#3) Set up your models so that you can store keys in them. I store some extra data just to make retrieval faster, content-type and name (original filename) because I plan on making it look like they are downloading it from my site in the future. Here is my exact code that took me about an hour to come up with (super super bare right now, will be writing more later on tonight/in the coming days)

site_s3.py:

import S3
import time
import sys
from django.conf import settings
AWS_ACCESS_KEY_ID = 'YOUR-KEY-HERE'
AWS_SECRET_ACCESS_KEY = 'YOUR-KEY-HERE'
# I declare this in my settings file so I can have a local bucket for testing and one for the server
BUCKET_NAME = settings.BUCKET_NAME
conn = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
generator = S3.QueryStringAuthGenerator(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
def save_s3_data(key, data, content_type):
  conn.put(
    BUCKET_NAME,
    key,
    S3.S3Object(data),
    { 'x-amz-acl': 'public-read' , 'Content-Type': content_type }
  )
  return key
def delete_s3_data(key):
  return conn.delete(BUCKET_NAME, key)
def get_s3_url(key):
  return generator.make_bare_url(BUCKET_NAME, key)

MODEL:

from mysite.amazon import site_s3
class MediaFile(models.Model):
  name = models.CharField(maxlength=200, blank=False)
  key = models.CharField(maxlength=100, blank=False)
  content_type = models.CharField(maxlength=100, blank=False)
  upload_date = models.DateTimeField(auto_now_add=True)
  active = models.BooleanField(default=True)
  def _get_file_url(self):
    return site_s3.get_s3_url(self.key)
  url = property(_get_file_url)

VIEW:

# this is extremely simple for example, I've cut out all validation (I do it in my view, too much for this post though)
copied_data = request.POST.copy()
if 'upload' in request.FILES:
  copied_data.update(request.FILES)
if 'upload' in copied_data:
  content_type = copied_data['upload'].get('content-type')
  file = copied_data['upload']['content']
  key = '%s-%s' % (request.user.id, ''.join(copied_data['upload']['filename'].split(' ')))
  copied_data['key'] = key
# save it to S3
  site_s3.save_s3_data(key, file, content_type)
if data['key']:
  d = MediaFile()
  d.name = data['upload']['filename']
  d.content_type = data['upload'].get('content-type')
  d.key = data['key']
  d.save()

and finally...

TEMPLATE:

{% if assignment.attachment %} <a href="{{ assignment.attachment.url }}">Grab It</a> {% endif %}

Whew! So let's review that third step real quick.

In the file site_s3.py, I defined three methods. The first one saves the data to the S3 servers, and I invoke it in my view when uploading a document and makes it publicly viewable. The second one deletes an object if you tell it the key. I haven't used this yet, don't know why I included it haha, but it works. The third one retrieves a bare URL so that users can easily download the file.

In my model, I define a class MediaFile, so I can use a ForeignKey from any other model to this one so it can just store and retrieve all the URLs that I could ever want.

In my view, basically what I do is check if I have the file named 'upload' in the request, and if it exists, copy it into the request.POST data, which is copied_data. I also put the key in copied_data so I can retrieve it anywhere using copied_data['key']. This is probably stupid, but whatever, I'm just getting this going right now. I then save all the data to the database and right there is where I'd link 'm' to some other model as a ForeignKey. In this instance, I saved it to the FK of the model Assignment.

In my template, I just did a quick check to see if the attachment (MediaFile FK) existed, and if it did, render a link for someone to download the file. The variable {{ assignment.attachment.url }} gets the URL because url is a property in the MediaFile model.

And it all works PERFECTLY.

Thank God for Amazon. I love you guys ;).

Filed under: Django, Python 7 Comments
6May/070

Django – Find list of emails from a comma separated string

I need to start posting more code. Here's something I just did real quick.


from django.utils.html import simple_email_re
if request.POST['invite_list']:
  post_list = request.POST['invite_list']
  email_list = post_list.split(',')
  list = []
  for e in email_list:
    email = e.strip()
    if simple_email_re.match(email):
      list.append(simple_email_re.match(email).group(0))
  return list

Where post_list is something like "foo@foobar.com, somebody@whatever.com, jimmy@django.com" etc.

This will clean out that list just in case your user put in bad emails.

Okthanksbye

Filed under: Django, Python No Comments
29Mar/070

YouTube

I am generating random codes for a certain part of Classowl, and I decided to do the little algorithm that YouTube does to generate their random codes. According to the compiler, there are:

5.20365606838e+019

Different possibilities. Chances of a duplication, even if there are billions of random codes in the database, are next to nothing.

Down at Point Loma right now, this school is gorgeous. Too bad it's not in Silicon Valley.

Filed under: College, Python No Comments
8Mar/071

There’s always an easier way

I am no beginner in Python, but I'm definitely no guru. So I still have problems expressing some things. It's like learning Spanish -- you know enough of the language for others to understand you, but there is always an easier way to convey what you want to say. I know enough Python for the computer to understand me, but here is a situation that just came up that shows there's always an easier, shorter way to do things.

def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  str = ''
  i = 1
  total_cli = self.custodian_total_clients
  for cli in self.clients.all():
    str += cli.name
    i += 1
    if i <= total_cli:
      str += ' | '
  return str

I knew there had to be an easier way to do it, but I just couldn't figure it out. So I asked people that knew more than me, i.e., the guys on the #django IRC Channel. Two minutes later, and I have a nice, elegant, even readable solution.


def _get_custodian_clients(self):
  "Returns all the clients of the custodian"
  return ' | '.join([client.name for x in self.clients.all()])
custodian_clients = property(_get_custodian_clients)

Sometimes I am amazed by my stupidity.

Filed under: Python 1 Comment
27Feb/070

FizzBuzz :D

In a recent blog post on Coding Horror entitled Why Can't Programmers... Program?, they talked about the fact that the majority of so-called programmers are useless when it comes to solving problems. Apparently they give people 10-15 minutes in an interview to solve this simple problem.

After a fair bit of trial and error I've discovered that people who struggle to code don't just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

So I set out to develop questions that can identify this kind of developer and came up with a class of questions I call "FizzBuzz Questions" named after a game children often play (or are made to play) in schools in the UK. An example of a Fizz-Buzz question is the following:

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I suppose I could have written it in Ruby, ActionScript, PHP, ColdFusion, and JavaScript. But I just learned Python, so I figured I might as well do it in that.

for i in range(1,101):
  if i%3 == 0:
    if i%5 == 0: print 'FizzBuzz'
    else: print 'Fizz'
  elif i%5 == 0: print 'Buzz'
  else: print str(i)

Count it. 6 lines. Took 5 minutes. Gotta love Python. I don't know why I'm bragging, that was a joke.

Filed under: Python No Comments
   

Blogroll

Archive

Meta