Posts Tagged ‘Languages’

21
Jul

Django protip #1: A better App structure

   Posted by: Swizec    in Coding, Django Protip

Crow's nest of SMS Derfflinger
Image via Wikipedia

When I started coding for our latest project at Preona a bit of an epiphany happened. I suddenly got django. Every pattern that used to feel a bit strange and I may have fought a little, suddenly became obvious and simple. Suddenly out of the blue my whole codebase is so marvelously organised and separated into files it brings a tear to my eye.

In the old days the average Django App in my project had a structure a file structure a bit like so:

  • models.py
  • views.py
  • tests.py
  • RandomActualWorkLogic.py
  • AnotherRandomOfWork.py

Sure this works quite alright, but it feels a little weird and after way too much work you suddenly realise that instead of the clean design you envisioned as you started out, you’re left with a crow’s nest of interdependancy, circular crappery and your project is in pretty miserable shape.

This point was really driven home when I figured two different django projects should share some code. Extrapolating a clean interface and making stuff work … was a nightmare. I actually gave up after a week and decided to start afresh.

And boy, was that the best decision ever!

Better application structure

The first step was to take up a better way to organise my codebase inside applications. It goes something like this:

  • models.py
  • managers.py
  • views.py
  • tests.py
  • forms.py
  • decorators.py
  • processing.py — (this is for certain special views)
  • urls.py
  • settings.py

The first result of this organisation is that now everything has its own place. There are no StrangeWorker.py crappy things anymore. All of those reside in a special ‘application’ just for those called a lib. Whenever you are making something and are looking for a place to put it -> the file is an obvious pick!

Another great result is that it’s now much easier to keep applications contained within themselves. Because both its specific url config and its specific settings config are contained within the application you can easily switch it around between projects or even make it a standalone thingy that projects can simply include.

Also because the file structure makes sense now and all those WeirdoWorker.py are separated elsewhere as proper libraries, there is less of a temptation to create mixed and confusing dependancies all over the place.

Conclusion

That’s it for django application structure. If you’ve got any better ideas do mention them on twitter or via the comments below. If not, that’s alright :P

Either way, join me in the next installment of Django Protip when I’ll be talking about using “Forms, forms for everything”

Enhanced by Zemanta

Tags: , , , , ,

12
Apr

Processing as-you-read in clojure

   Posted by: Swizec    in Insanity

Six hard disk drives with cases opened showing...
Image via Wikipedia

Sometimes we’re given a stupid algorithm or two to implement at this or that course at uni. This time ’round they wanted us to implement something called external matrix transposition … the idea is that you have to transpose a matrix live from the hard drive because it is potentially so bloody huge you can’t store a single row in memory.

Which is a bit silly, who’s got 60+ gig huge matrixes these days? You’re doing it wrong if you need that many dimensions! VERY wrong!

But anyway, what really struck me as a bit silly is that they were big nazis when it comes to how we’re supposed to do this. Needs to compile with their specific command, can only be done in Java or C/C++ … yeah fuck you and fuck your grade, let’s learn something.

So I went and did it in Clojure … well not really since I didn’t finish, but I came close.

Surprisingly, it is in fact very very hard to process data live as it’s being read in a functional environment. Every library that’s already out there open-read-closes the file, at best you get something that can read lines and allows you to work on every line as it’s being read, but oh no, not characters. Nope, what sort of bloody idiot would want that?

Well … me.

So here is my implementation, probably not the most elegant thing out there since I’m new to this, of what Java people call “nextInt”. It returns the next integer in a file as a proper integer and so on.

(ns io (:require [clojure.contrib.io :as io]))
 
(defn read-char [rdr]
   (char (.read rdr)))
 
(defn delimiter? [c]
  (if (or (< (int c) (int '\0)) (> (int c) (int '\9)) (= c nil)) true false))
 
(defn digit [c place]
  (* (- (int c) 48) (Math/pow 10 (- place 1))))
 
(defn numify [seq acc]
  (if (= seq nil)
    (int acc)
    (numify (next seq)
	    (+ acc (digit (first seq) (count seq))))))
 
(defn read-number [rdr acc]
  (let [c (read-char rdr)]
    (if (delimiter? c)
      (numify acc 0)
      (read-number rdr (concat acc [c])))))

You call it with an open reader and an empty [] thingy because, the latter because I couldn’t figure out optional function arguments.

And if anyone’s interested, here’s the rest of the code that opens a file where the first number denotes the number of columns in the matrix and then contains the rest of it delimited with spaces or tabs or anything non-numerical, and copies every row into a separate file so a transposition can then be done (didn’t quite get to this part)

(defn copy-number [rdr writer]
  (.write writer (str (read-number rdr []) " ")))
 
(defn copy-row [rdr writer i]
  (copy-number rdr writer)
  (if (> i 1) (copy-row rdr writer (dec i))))
 
(defn break-row [rdr i columns]
  (try
   (with-open [writer (io/writer (str "podatki-0-" i))]
     (copy-row rdr writer columns))
   (break-row rdr (inc i) columns)
  (catch Exception e
    nil)))
 
(defn transpose-matrix [file-name]
  (with-open [rdr (io/reader file-name)]
    (let [columns (read-number rdr [])]
      (break-row rdr 0 columns))))
 
(transpose-matrix "/home/swizec/Documents/APS2/input.txt")

PS: that try/catch in there is fugly, but I didn’t know how else to check for EOF *blush*

Reblog this post [with Zemanta]

Tags: , , , , ,

11
Jan

How Lisp changed my style

   Posted by: Swizec    in Inspiration

IBM 402 Accounting Machine plug-board wiring. ...
Image via Wikipedia

This is the first post in the series of How <x> changed my style where I shall talk about tools and events that had a significant impact on my style of doing things. If you happen to like this idea, I would be very happy if you could help it spread like wildfire, because it’s a form of pay-it-forward where we say Thanks for cool stuff.

Some months ago, fuck has it been two? three? four?,

Tags: , , , , ,