I recently thought that I do not always use beautiful OO concepts when writing Pythonic programs. In particular, I thought it would be interesting for me to see the language in which I could write a typical web script as
HTTPOutput:
HTTPHeaders + Maintext
Flags:
value = URI.split('?').after
refresh = 'r' in value
sort = /sort=([a-z])/.search(value)
HTTPHeaders:
'Content-type:...' + Flags.refresh ? 'Refresh: ...' : ''
Maintext:
Template.replace('$questions', PresentedQuestions [:20] )
Questions:
(Flags.sort = 'r') ? RecentQuestions : TopQuestions
PresentedQuestions:
Questions % '<h4>{title}</h4><p>{body}</p>'
RecentQuestions:
Database.Questions . sort('date')
TopQuestions:
Database.Questions . sort('votes')
See what happens? I try to make as many objects as possible; each paragraph declares what I call a transformation. For example, there is a conversion HTTPHeaders. In an imperative language, which will be a declaration of a class, object, and function in combination:
class HTTPHeaders_class
{
public char* value
HTTPHeaders_class()
{
value = ... + Flags.refresh ? + ...
}
}
class Flags_class
{
public char* flagstring;
public bool refresh;
...
Flags_class()
{
value = ...
refresh = ...
}
}
Flags = new Flags_class (URI)
HTTPHeaders = new HTTPHeaders_class (Flags)
, , , , , , ; . . , , ( " , " ).
, , , Python, M-V-C Django (), , .
value, .HTTPHeader -, , HTTPHeader . HTTPHeader .- ,
URI, . Flags URI HTTPHeaders Flags, . , Database , Questions , , , HTTPOutput . - . , , .
, , -, . , , ?