---
title: 'General Architecture Framework for a Python App'
source: 'https://youtube.com/watch?v=dlCcnJdh4c4'
video_id: 'dlCcnJdh4c4'
date: 2026-06-15
duration_sec: 0
---

# General Architecture Framework for a Python App

> Source: [General Architecture Framework for a Python App](https://youtube.com/watch?v=dlCcnJdh4c4)

## Summary

This video concludes a series on Python project structure by introducing a general architecture framework inspired by the book 'Architecture Patterns with Python'. The framework organizes code into three layers: domain (business logic), services (orchestration), and adapters (external integrations). The presenter demonstrates refactoring a shopping cart app to use this structure and adds a scoring adapter as an example.

### Key Points

- **Framework Overview** [00:00] — The framework includes a domain folder for business logic, a services folder for application logic, and an adapters folder for external integrations.
- **Inspiration from Book** [00:37] — The idea comes from the book 'Architecture Patterns with Python', which covers advanced topics like unit of work but also provides basic structuring ideas.
- **Domain Layer** [01:10] — The domain layer defines business logic. In the shopping cart example, fruits and shopping carts are domain entities.
- **Service Layer** [01:22] — The service layer defines jobs the system performs and orchestrates components. For the shopping cart, adding and removing items are services.
- **Adapters Layer** [01:39] — Adapters provide concrete implementations of interfaces that connect the system to the outside world, such as databases or external APIs.
- **Restructuring Project** [02:04] — The presenter creates folders for domain, services, and adapters, moves files accordingly, and updates imports to make the app work.
- **Adapter Example: Scoring** [04:10] — An adapter is created to score a shopping cart based on item prices, demonstrating how to integrate external logic.
- **Multiple Adapters** [08:47] — The presenter shows how to support multiple scoring strategies (e.g., base and Apex) using a factory function in the adapters layer.

### Conclusion

The framework helps structure Python apps into domain, services, and adapters, making code more maintainable and scalable. The presenter encourages viewers to apply these ideas to their own projects.

## Transcript

hey everybody welcome back in this final
part of my python project structure
video series I'm going to look at
extending the app we build just a little
bit but using a general framework that
you can use for all of your applications
or scripts that you write in Python what
we're going to be doing is adding a
domain folder to group all of the domain
Logic for our app uh Services folder
which is going to have all of the sort
of logic of our app and then an adapters
folder which is going to include things
that external to our app that we want to
bring in and use in here this is a
general purpose framework that you can
use to structure your projects I got
this idea from this architectures book
for python the ideas discussed in this
book apply to Big monolithic systems
that are really complicated there's
Advanced topics like a unit of work they
have a lot of abstractions that make
these really big systems work nicely so
if this is so complicated and advanced
why am I talking about it well there's a
few basic ideas that I gleamed from here
which I apply to my projects and
basically just help me structure things
so I've already talked about them in my
own words but I'm going to use the
author's words there's a domain layer
this defines the business logic in our
case with our example of a shopping cart
with fruits in it the business logic is
this concept of a fruit and a shopping
cart these are our domain entities next
we'll have a service later this defines
the jobs the system should perform and
orchestrates different components in our
case we want want to add items to the
shopping cart and remove them it's
pretty much it so that's our service
layer although the authors have more in
here the last one that I use regularly
is an adapter's layer concrete
implementations of an interface that
goes from our system to the outside
world all right so the authors are
describing this as a way of of of their
system talking to the outside world this
could be like a database integration
where you want your system to write data
to a database
but for me I also use adapters to pull
data in from the outside world so let's
go ahead and restructure our project to
work like this it's really easy first
I'm going to hop into our shopping cart
and make these folders that I've been
describing I'll have one for domain
Services adapters typing inside of each
of these I'm going to put an init
file and now the fruit file we just want
to put this directly into adapters into
here okay let's move my shopping cart
now into Services let's check out fruit
oops I accidentally put that in adapters
okay that's in domain this is clearly
like a domain entity and shopping cart
is like a service right so I put that in
here but I've got this domain object
inside of here let's move that over
shoppingcart
py let's open that and paste and this is
good because fruit is local now right to
this domain so we we've got all our
domain stuff together in one place and
and now I I just want to make sure we
get rid of it here get rid of that
probably don't need data classes now I
don't have my shopping cart I'm going to
go to my domain shopping import shopping
cart so here's my project so if I run my
app so it's not finding a module named
shopping cart. shopping cart anymore and
maybe this is my init files I never
updated that so let's go back in this
init file I'm grabbing some stuff from
shopping cart now in my services like so
I have to update that and for fruit
that's in my domain now see let's try it
again fruit is not defined I suppose
what I did do inside of my shopping cart
service I probably have a fruit object
here just instead of shopping cart I
want fruit right and uh I'll do it this
way just grab it like that still feeling
pretty good U I'm not really going to
think too much about it let's try and
run it again great it worked so my app
is still working after refactoring it
well let's throw together a quick
example on the Fly of how we might want
to have an adapter for this application
let's imagine we're like an e-commerce
website where users will have a shopping
cart what you might want to do is
classify those users for like a
propensity of how valuable they are
maybe all the items in the shopping cart
are worth like a lot of money so we
should kind of identify that let me go
ahead and build this starting in the
services layer score shopping cart and
let's add some documentation assign a
score to the shopping cart based on the
contents like the other functions here
we want to add a shopping cart we're
passing in like what our current cart is
now what I want to do is score it here's
where our adapters layer is going to
come in so I'll say from adapters score
and I'm going to say import shopping
card scorer so no I'm using camel case
or rather like python convention for an
object goe and initialize it in here
score do score and I'm going to pass in
my cart this will provide me score I'll
call it cart
score I'm going to assign this to the
shoing cart object let's remind oursel
of what that looks like so now I could
have a score down here and I'm going to
call that int it's going to be an
integer score and it's going to default
To None because we don't need to have a
score when we're initializing shopping
cart let's go back to our services cart
doore equals
this assigning score cart
score okay so what do we have to do now
well this shopping cart score doesn't
exist we need to create it and we need
to make sure that it has this right
method I'm going to create a score file
and we know we need these things so
class shoing card scorer is going to
come in here and this is going to have
to do some stuff let is going to have to
to have this
method okay it's going to pick itself
and a cart when we initialize
this I'm not going to actually do
anything when we initialize this and
then for this guy here um we need to
make sure he's returning an integer I
could just say that score is going to
equal and I could say item. price for
item in cart.
items and I could say or zero because
the price might be none and then I can
just sum those up and I could return
that as a score one last thing I want to
do is pass that as an integer or I guess
to this would be more in line with the
Zen of python to do things this way
pretty happy with with everything here
even though I'm not really using my
initialization method in general I might
want to do that now one other thing I
might want to do here is say from domain
do shopping cart import go here and
explicitly say what we're passing in
here um let's see if everything uh works
if I run my app that seems to be working
the thing is my app's not actually doing
anything we're going to need to import
that so if I go back to my services here
it is score shopping cart is what I want
put that here okay so I'm just going to
import it along with everything else and
then if I go to the again and grab the
names now where am I doing okay so now I
want to score this and I want to score
my card and that was it I didn't have
any other arguments so after I do that
if I print my cart
again and I'll just say
print and we'll be able to see
that see if it'll work it can't import
that I believe it's because I didn't add
it to my
init okay so score shopping cart cool
let's see so here it's assigning a score
zero because um I have no prices when I
add my fruits I'll just assign some
arbitrary prices say $99 because these
are the most fantastic fruits in the
world and there's my score of 198 you
could stop watching here and I wouldn't
blame you and if you do I thank you for
going this far with me I would really
appreciate it if you consider giving
these videos a like or maybe subscribing
to my channel if you like this content
as a new channel this really helps me
get visibility if you liked my layout
with the terminal I have another video
on using vim and t-o and how I've
configured these things it will send
your productivity into an absolute
spiraling death I mean um I mean uh you
should check that out a little bonus the
last thing I'm going to do is make this
adapter better we might want to have
different ways of scoring things what
I've created here is more of like a base
score it's almost more like a unit test
really I make a new folder and I'm going
to call it Gore
plural and then inside of here I'm going
to I'm going to copy this guy and I'm
going to put it in here delete I want to
call this a base score rename it and I'm
going to rename it base I guess py
called this base shopping cart score I'm
going to have to go down an additional
level so I'll add another dot there
where am I going with this well I might
want to have different scores I'm going
to copy this guy and I'm going to paste
it what do we want to call another score
Apex I don't know what that means but
let's say we have some model we've
called it Apex okay so let's call that
Apex shopping cart score so I could
imagine one situation where I have a
different projects set up for this Apex
and I can say from Apex scorer import
shopping cart
score and then I could go down here and
where I have score shopping cart I just
want to return the Apex call to shopping
cart score we're assuming that we have
this somewhere else on our system and
all we want to do is call that here and
so this adapter lets us do that in a
nice structured way so we have our Apex
shopping card score object and we also
have a base scoring object which does
the same thing it has a scoring shopping
cart function so these are implementing
the same thing which is going to be
important for what we do next so I'll
have scorer. py so in here I'm going to
create a new function score shop cing
card I want a function that's going to
bring in my objects from my subfolder in
this case from do
scores and then I'll do the same thing
for
base now in here I want a score type gu
string I'm going to return a score a
union of one of these so B basically it
means it's one of these two things this
is not really super clean I wouldn't
actually want to do it this way but I'll
say if the score type
is equal to Apex bring that into our
application and else if this is B like
that okay good everybody's happy so I'm
typing have Union if we haven't returned
anything I'm going to raise a value
error and a
type and just a little exclamation mark
and actually what I really want to do is
I want to get the scar this is going to
return an object and then I'm going to
have to call that object still so by
doing this the last thing I want to do
is fetch that here and I can say in my
init file for adapters I'll say from
scorer I'll import um get scorer and uh
let's go down and Implement that in our
services now in my adapter's file I just
put that in my init file is get score I
can just fetch that directly out of my
adapters now here is the shopping cart
score no longer like that it's get score
and I need a score type so score type
and we're going to use um let's surface
this type up to here and score type and
it's going to be like a string we said
so now that just kind of is going to get
passed in from here
oops okay happy with that and we're
going to score our cart base on this
score which is returned from here now we
need to add the scor type to our app
wherever I score my shopping cart here
um I need my score type to be defined
what I could do is make this a global
variable in in my in my app here and I
I'm going to say we're going to use the
base one we know what to expect if I've
done everything right this should just
work okay it's saying no module named
Apex scorer
oh of course there's no module named
Apex score because we just created that
right it it it doesn't actually exist
for the purposes of getting this working
let's just like I don't know why I
didn't see that coming leave that and
say return zero and Implement score see
if that does the trick uh score shopping
cart is missing a cart argument okay
yeah this is a weird one this is an
object or this is a class you could call
it score class just so you understand
what's happening right and I need to
initialize that I was not doing that
that's what I want to do
okay we're working here this is
fantastic and one last thing I could do
just for my own edification is in my app
I want to try the other scor type our
awesome Apex model which
returns
zero like we see here oh yeah okay this
was a lot of fun as you can see it's got
quite dark outside I have another video
that talks about my t-u and my Vim
config which has eaten up massive hours
of my evenings this week and I think vim
and t-o configs could eat up massive
hours of your evening as well it has
been fantastic to go through this demo
and I hope you take these ideas and use
them in your coding and build better
python applications thanks so much and
see you in the next one
