Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Server programming (http://www.velocityreviews.com/forums/t671944-server-programming.html)

koranthala 02-23-2009 12:42 PM

Server programming
 
Hi,
Is server programming in Python procedure oriented or object
oriented?
I have this question because lately I am asked to make a medium
complex web program (extremely database oriented) using Django. When I
used to do application programs earlier (in Python itself), the whole
thing being object oriented came out easily in programming. So, I was
able to use different patterns etc for programming and the whole thing
was - quite fun to design and create.
But when I program in Django, since I just have to work on user
responses - everything else being taken care of by Django - only, the
complete coding has become procedure oriented. It is not kludgy or
anything, but it doesnt have the nice feeling that we get when we code
a proper object oriented program.
Is my coding in error here? This is infact my first web program,
so it might be the reason. What does other people find? Does web
server programming using web frameworks (Django, TurboGears etc) make
it procedure oriented? If I am in the wrong, it might be due to a
wrong design or mindset, and I would like to change it.



Steve Holden 02-23-2009 05:57 PM

Re: Server programming
 
koranthala wrote:
> Hi,
> Is server programming in Python procedure oriented or object
> oriented?
> I have this question because lately I am asked to make a medium
> complex web program (extremely database oriented) using Django. When I
> used to do application programs earlier (in Python itself), the whole
> thing being object oriented came out easily in programming. So, I was
> able to use different patterns etc for programming and the whole thing
> was - quite fun to design and create.
> But when I program in Django, since I just have to work on user
> responses - everything else being taken care of by Django - only, the
> complete coding has become procedure oriented. It is not kludgy or
> anything, but it doesnt have the nice feeling that we get when we code
> a proper object oriented program.
> Is my coding in error here? This is infact my first web program,
> so it might be the reason. What does other people find? Does web
> server programming using web frameworks (Django, TurboGears etc) make
> it procedure oriented? If I am in the wrong, it might be due to a
> wrong design or mindset, and I would like to change it.
>

It's just that Django *seems* procedure-oriented because your views are
written as functions. But they take request objects as arguments and
return response objects as results, so the framework is object-oriented.
Also each model is a class, and querysets eventually yield lists of
instances.

I wouldn't worry too much until Django forces you to do somethign you
really don't want to do ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/


Bruno Desthuilliers 02-23-2009 08:02 PM

Re: Server programming
 
koranthala a écrit :
> Hi,
> Is server programming in Python procedure oriented or object
> oriented?


It's how you want it to be.

> I have this question because lately I am asked to make a medium
> complex web program (extremely database oriented) using Django. When I
> used to do application programs earlier (in Python itself), the whole
> thing being object oriented came out easily in programming. So, I was
> able to use different patterns etc for programming and the whole thing
> was - quite fun to design and create.
> But when I program in Django, since I just have to work on user
> responses - everything else being taken care of by Django - only, the
> complete coding has become procedure oriented. It is not kludgy or
> anything, but it doesnt have the nice feeling that we get when we code
> a proper object oriented program.


So you may want to learn to enjoy the nice feeling we get when we code a
proper procedural program - or a proper functional one FWIW !-)

There's nothing inherently wrong with procedural programming. Nor with a
mix of procedural, OO and functional code - which is usually the case in
Python. It's just a matter of using the right tool for the problem to
solve.

> Is my coding in error here?


Don't know - I don't have access to your code. But my experience with
Django is that I tend to have quite a lot of code in models and
templatetags, and much less in the views themselves. So I wouldn't say
that "Django takes care of everything else". If you really ends up
writing pages upon pages of repeting procedural code in your views and
nothing in the other parts of the app, then yes, there might be
something wrong - probably a case of AnemicDomainModel, and possibly a
lack of knowledge of the whole framework. One of the reasons views are
usually implemented as functions is that in most cases, you shouldn't
have a need for more. FWIW, you sometimes don't even need to write a
specific view - Django's GenericViews can handle quite a lot of cases

Note also that Django doesn't _require_ that you use functions as view
handlers - any callable object will do. But given how the HTTP protocol
works and how Django is designed, there's more often than not just no
*need* for a custom callable object.

And finally, as Steve already mentioned, OO is first about objects, and
that's what you're dealing with in your views - request objects, session
objects, model objects etc...

> This is infact my first web program,
> so it might be the reason. What does other people find? Does web
> server programming using web frameworks (Django, TurboGears etc) make
> it procedure oriented?


Not necessarily, no. Some frameworks requires your request handlers to
be methods of classes FWIW, and nothing in Django prevents you from
doing so if you want.

> If I am in the wrong, it might be due to a
> wrong design or mindset, and I would like to change it.




koranthala 03-03-2009 08:08 AM

Re: Server programming
 
On Feb 24, 1:02*am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.fr> wrote:
> koranthalaa écrit :
>
> > Hi,
> > * * * Is server programming in Python procedure oriented or object
> > oriented?

>
> It's how you want it to be.
>
> > * * * I have this question because lately I am asked to make a medium
> > complex web program (extremely database oriented) using Django. When I
> > used to do application programs earlier (in Python itself), *the whole
> > thing being object oriented came out easily in programming. So, I was
> > able to use different patterns etc for programming and the whole thing
> > was - quite fun to design and create.
> > * * * But when I program in Django, since I just have to work on user
> > responses - everything else being taken care of by Django - only, the
> > complete coding has become procedure oriented. It is not kludgy or
> > anything, but it doesnt have the nice feeling that we get when we code
> > a proper object oriented program.

>
> So you may want to learn to enjoy the nice feeling we get when we code a
> proper procedural program - or a proper functional one FWIW !-)
>
> There's nothing inherently wrong with procedural programming. Nor with a
> mix of procedural, OO and functional code - which is usually the case in
> Python. It's just a matter of using the right tool for the problem to
> solve.
>
> > * * * Is my coding in error here?

>
> Don't know - I don't have access to your code. But my experience with
> Django is that I tend to have quite a lot of code in models and
> templatetags, and much less in the views themselves. So I wouldn't say
> that "Django takes care of everything else". If you really ends up
> writing pages upon pages of repeting procedural code in your views and
> nothing in the other parts of the app, then yes, there might be
> something wrong - probably a case of AnemicDomainModel, and possibly a
> lack of knowledge of the whole framework. One of the reasons views are
> usually implemented as functions is that in most cases, you shouldn't
> have a need for more. FWIW, you sometimes don't even need to write a
> specific view - Django's GenericViews can handle quite a lot of cases
>
> Note also that Django doesn't _require_ that you use functions as view
> handlers - any callable object will do. But given how the HTTP protocol
> works and how Django is designed, there's more often than not just no
> *need* for a custom callable object.
>
> And finally, as Steve already mentioned, OO is first about objects, and
> that's what you're dealing with in your views - request objects, session
> objects, model objects etc...
>
> > This is infact my first web program,
> > so it might be the reason. What does other people find? Does web
> > server programming using web frameworks (Django, TurboGears etc) make
> > it procedure oriented?

>
> Not necessarily, no. Some frameworks requires your request handlers to
> be methods of classes FWIW, and nothing in Django prevents you from
> doing so if you want.
>
> > If I am in the wrong, it might be due to a
> > wrong design or mindset, and I would like to change it.


Hi Bruno,
After reading your email, I tried reworking my code so that most of
my logic moves to Models.
But, most probably because this is my first application
development, I am unable to do so.
For example:
I have Models A,B, C, D . Now, there is not much model specific
code (preprocessing before updating code inside Models)in it. Rather
most of the code is of the format:
data received and to be added to D. But for adding to D, check
whether it is already in C - if not add to C and B. etc...
Now, I tried putting this code inside Model D,but it does not seem
to belong there - since it modifies other Models.

Is keeping such code inside views against Django's/Application-
Developments philosophy? In that case, where will this go?


Bruno Desthuilliers 03-03-2009 03:09 PM

Re: Server programming
 
koranthala a écrit :
(snip)

> Hi Bruno,
> After reading your email, I tried reworking my code so that most of
> my logic moves to Models.
> But, most probably because this is my first application
> development, I am unable to do so.
> For example:
> I have Models A,B, C, D . Now, there is not much model specific
> code (preprocessing before updating code inside Models)in it. Rather
> most of the code is of the format:
> data received and to be added to D. But for adding to D, check
> whether it is already in C - if not add to C and B. etc...


And you don't find it "model specific" ? Man, this is business rules,
and as such belongs to the models part, not to views. You surely want to
make sure these rules always apply, don't you ?

> Now, I tried putting this code inside Model D,but it does not seem
> to belong there - since it modifies other Models.


And ? Who said a Model (or ModelManager) shouldn't touch other models ?

> Is keeping such code inside views against Django's/Application-
> Developments philosophy?


FWIW, I see this antipattern (AnemicDomainModel) way too often in django
apps. This doesn't make it less of an antipattern.

> In that case, where will this go?


It's impossible to say exactly where in the models module without a
sufficiant knowledge of the domain, rules etc. Sorry, but my crystal
ball is out for repair.

koranthala 03-03-2009 05:29 PM

Re: Server programming
 
On Mar 3, 8:09*pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:
> koranthala a écrit :
> (snip)
>
> > Hi Bruno,
> > * *After reading your email, I tried reworking my code so that most of
> > my logic moves to Models.
> > * *But, most probably because this is my first application
> > development, I am unable to do so.
> > * *For example:
> > * * I have Models A,B, C, D . Now, there is not much model specific
> > code (preprocessing before updating code inside Models)in it. Rather
> > most of the code is of the format:
> > * * *data received and to be added to D. But for adding to D, check
> > whether it is already in C - if not add to C and B. etc...

>
> And you don't find it "model specific" ? Man, this is business rules,
> and as such belongs to the models part, not to views. You surely want to
> make sure these rules always apply, don't you ?
>
> > * *Now, I tried putting this code inside Model D,but it does not seem
> > to belong there - since it modifies other Models.

>
> And ? Who said a Model (or ModelManager) shouldn't touch other models ?
>
> > * *Is keeping such code inside views against Django's/Application-
> > Developments philosophy?

>
> FWIW, I see this antipattern (AnemicDomainModel) way too often in django
> apps. This doesn't make it less of an antipattern.
>
> > In that case, where will this go?

>
> It's impossible to say exactly where in the models module without a
> sufficiant knowledge of the domain, rules etc. Sorry, but my crystal
> ball is out for repair.


Thank you, Bruce.
Sorry for the rather naive questions.

Bruno Desthuilliers 03-04-2009 01:41 PM

Re: Server programming
 
koranthala a écrit :
> On Mar 3, 8:09 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli...@websiteburo.invalid> wrote:
>> koranthala a écrit :
>> (snip)
>>
>>> Hi Bruno,
>>> After reading your email, I tried reworking my code so that most of
>>> my logic moves to Models.
>>> But, most probably because this is my first application
>>> development, I am unable to do so.
>>> For example:
>>> I have Models A,B, C, D . Now, there is not much model specific
>>> code (preprocessing before updating code inside Models)in it. Rather
>>> most of the code is of the format:
>>> data received and to be added to D. But for adding to D, check
>>> whether it is already in C - if not add to C and B. etc...

>> And you don't find it "model specific" ? Man, this is business rules,
>> and as such belongs to the models part, not to views. You surely want to
>> make sure these rules always apply, don't you ?
>>
>>> Now, I tried putting this code inside Model D,but it does not seem
>>> to belong there - since it modifies other Models.

>> And ? Who said a Model (or ModelManager) shouldn't touch other models ?
>>
>>> Is keeping such code inside views against Django's/Application-
>>> Developments philosophy?

>> FWIW, I see this antipattern (AnemicDomainModel) way too often in django
>> apps. This doesn't make it less of an antipattern.
>>
>>> In that case, where will this go?

>> It's impossible to say exactly where in the models module without a
>> sufficiant knowledge of the domain, rules etc. Sorry, but my crystal
>> ball is out for repair.

>
> Thank you, Bruce.


s/ce/no/ !-)

> Sorry for the rather naive questions.


No problem. And these are not "naive" questions FWIW.


All times are GMT. The time now is 08:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.