An undergrad, a drone and a dream: New FAA rules have a UNLV student smelling opportunity


Some people are just born to build stuff.

In his sixth-grade yearbook, Greg Friesmuth answered the question of what he wanted to be when he grew up by saying he wanted to build robots for the military. For Christmas that year, he got a couple Lego Mindstorms robot kits, which he combined into one and used to make his own creations.

Lately, though, Friesmuth has transferred that energy to drones. A couple years ago, he built one in his garage with a couple hundred dollars in mail-order parts. Last year, he built another, bigger one as part of a capstone project at the University of Nevada, Las Vegas.

Today, that project has evolved into a working prototype of a industrial-grade quadcopter — and Friesmuth is trying make drones his career. Working out of a lab in the engineering building at the University of Nevada, Las Vegas, he has created a company…

View original post 775 more words


Django ORM: Optimize your code

In the most code that we write, we use Django queries that guarantee all CRUD operations. However, sometimes you will need to retrieve values that are derived by summarizing or aggregating a collection of objects.

Throughout this post, we’ll refer to the following models.

from django.db import models

class URL(models.Model):
    url = models.URLField()

class Tweet(models.Model):
    tweet_id = models.IntegerField()
    text = models.CharField(max_length=140)
    tweeple = models.CharField(max_length=200)
    urls = models.ManyToManyField(URL)

I want to get most popular URLs (most shared URLs between tweets)

def most_popular_url():
    urls = URL.objects.all()
    res = []
    for url in urls:
        res.append({"url": url.url, "count": url.tweet_set.count()})
    # Let's sort the result
    result = sorted(res, key=lambda x: -x["count"])

Let’s test this: We have two test cases:

  1. [TC1] Small database that contains 2000 tweets
  2. [TC2] Medium database that contains 3M tweets

The function takes 8.281s for TC1, and 175m:361s for TC2. This is not acceptable and should be improved. Fortunately, Django resolve this for us by providing a rich ORM API : look at the solution below using the annotate

def most_popular_url_optimized():
    urls = URL.objects.annotate(rank=Count("tweet")).order_by("-rank")
    res = []
    for url in urls:
        res.append({"url": url.url, "count": url.rank})

The second version of most_popular_url takes only 2.361s for TC1 and 3541.5s (59m & 0.25s) for TC2. This example show for us the interest of optimizing database query. As a general advice, try to do every treatment in models or forms instead of views