A new blog

January 30, 2009 by squarewheel

The “Square Wheel blog”, previously available at WordPress has moved to a new site – http://squarewheel.pl

Taxpayer data collection

December 12, 2008 by squarewheel

I got a new identity card recently (I moved), so now I need to inform some institutions of the change. Not really unreasonable, eg. my bank knows this number and would check the ID card if I tried to close the account.

One of the institutions that requires being informed of such change is Urząd Skarbowy (Polish equivalent of IRS). So I filled a NIP-3 form and started wondering: does Urząd Skarbowy really need to know where I live (address – required field), what’s my phone number, e-mail address, bank account number? Most of those fields are optional, but anyway, why are they collected at all?

While I’m at it — I must admit Urząd Skarbowy is probably one of the most well-organized government offices

Travel plan

September 1, 2008 by squarewheel

02. IX 2008, 10:12–17:45 Train to Toruń

04. IX 2008, 10:12–13:17 Train to Malbork

04. IX 2008, 20:16–06:56 Train to Kraków.

new_record?

June 11, 2008 by squarewheel

Just in case anybody is interested – when a transaction fails (is rolled back), all the objects created in that transaction still respond false to new_record?, even though they are not saved to the db. One more reason to go for identity-mapping ORMs.

Reinventing the square wheel again

June 2, 2008 by squarewheel

There was a recent post on pylons-discuss about creating a python web framework not based on WSGI, but on higher-level abstraction – WebOb.

Well, I’m doing some research on web applications for my MSc thesis, where I’ll need a framework that is simple and stays off my way – in other words does as little to possible. I’d have used CherryPy, but I found that even it is doing too much. So, with such a suggestion on discussion list I’m trying to build a framework around WebOb.

It’s simple with a layered architecture – each layer accepts an dictionary with a few objects in it (especially webob.Request object) and returns webob.Response. The topmost layer performs application-specific tasks, while lower layers add functionality – much like WSGI middlewares. And because WebOb is a helper for WSGI, I can use any WSGI middleware.

Because of that I’m not wasting time writing things like session support – I just put beaker into the stack and wrote a simple wrapper (7 lines of code).

Perhaps I’ll release the code when it’s usable. Maybe it’ll just serve as a proof of concept – but at least I’ll have some environment for my thesis-related research.

Random late-night thoughts

April 25, 2008 by squarewheel

What kind of trouble-minded teenager would IM a randomly choosen strange person at 00:39 AM to talk about how stressed she is about high school final exams? What happened to good old face-to-face discussions with friends or parents?

I guess there are a few people I could phone late at night if I were in a dire need to talk to somebody – but these are people whom I know for a long time in real life and spent hours talking and listening to.

To all my friends, if you happen to stumble upon this – thank you for all the time, all the words and all the understanding.

PS. 19-yr old girls should really watch out whom they meet on the internet. Lying over the wire is so easy…

PPS. Sorry for a non-technical post.

Some rails plugins

March 10, 2008 by squarewheel

The company I work for – Code Sprinters – released three rails plugins I wrote under the MIT licence.

OutputStream and FlashMessages

Those two plugins allow safe mixing of unescaped text and html content. Strings are marked as being in either format and then are escaped appropriately in the view layer. This allows the developer to embed eg. links to other pages in flash messages without worrying about having some other piece of data unescaped.

Strings not marked explicitly as safe for html are escaped with default rails h() helper.

Although currently the plugin only knows how to escape text output to HTML, it can be easily extended to support other formats.

Expose

This plugin is inspired by CherryPy (http://www.cherrypy.org) – a simple yet powerful HTTP application server (and microframework).

This plugin changes the default policy of exporting all public methods of controllers via HTTP protocol to only exporting explicitly stated methods – and only to specified HTTP verbs. This helps prevent mistakingly exposing methods that should only be filters – or exposing via GET methods that should only accept POSTs.

Also, this is generally good practice to deny access by default – and allow access only when explicitly stated.

For actual downloads, go to the plugin page.

Why I hate ActiveRecord – part II (validation)

January 25, 2008 by squarewheel

Everybody using Rails probably knows that ActiveRecord provides many methods for declaring validation of records – the validate method as well as validates_xxx methods which generate code for most common validation tasks. There is a problem though…

The validation can be easily skipped. Just use the update_attribute method and you can set any values without validation. Kinda scary if you’re used to encapsulating consistency checks in model – some other coder writing controllers for your model can just break your rules…

Just one more reason to ignore DHH’s words on moving validation to application layer and putting it back in database (where it should always be, even if you have the rules in your application layer too; skip consistency checks provided by the RDBMS only if they are too complicated to be effectively expressed with SQL or you’re using RDBMS with limited capabilites in this regard, like MySQL or SQLite)

Threading in RoR 2.0.2

January 12, 2008 by squarewheel

We’re working on a project using Ruby on Rails 2.0.2 currently.

As you probably know, RoR is not thread-safe. But even if you are not using ActiveRecord, ActionPack nor any other Rails part you still might run into some problems if you thread.

We use the probably most common setup – small loop launching threads to query some web services in parallel. This seemed safe, as it would not use any of RoR classes/modules.

Now here’s the gotcha – automatic constant resolver (part of ActiveSupport) that loads classes from properly named files. It’s a part of RoR too…. and has threading problems, just like the rest of RoR. If a constant is not known before the threads launched, every thread would try to load that constant on its own; only the first thread would succeeded, all the other would raise “constant X is not unknown” exception.

Currently we worked this around by requiring files defining all dependencies in the file that launches the thread before going parallel. It’s ugly and not easily maintainable, as threads use different functions, each with it’s own dependencies – but it works, for now.

Perhaps it’s time to port that project to python – it has proper thread support (not to mention the fact that I wouldn’t have to deal with RoR worst part, that is ActiveRecord – SqlAlchemy is way better)

Unicode “support”

December 6, 2007 by squarewheel

Just a simple console session:

>> "Zażółć"[0,3]
=> "Za\305"
>> "Zażółć"[0,4]
=> "Zaż"
>> "Zażółć".length
=> 10

OK, it quite works with Chars class:

>> "Zażółć".chars[0,3].to_s
=> "Zaż"
>> "Zażółć".chars[0,4].to_s
=> "Zażó"
>> "Zażółć".chars.length
=> 6

Not the most elegant solution, but works.