hi serdar here for infoworld at idg today in smart python
we have the first in a multi-part series where we're going to create a simple
very simple social network using python we're going to call it talkster and with this project
we'll be exploring a slew of different things how to set up and maintain a small python project how
to work with databases and data schemas and how to create a website that displays and modifies data
using a web framework this will also be a way to demonstrate how a project can evolve and change
over its lifetime for instance i'm not beginning this project with anything too sophisticated in
terms of python's own project management tools i'm just going to create a virtual environment
and set up some requirements files but over time as things get more complex we may need
to swap in some of the more powerful project management tools available in python's arsenal
for now we're just going to start with an empty directory the first thing we need to do is create
our virtual environment and this we can do from the command line using the venv command
we're going to call our virtual environment dot end because that keeps it visually discrete
from the rest of the code and also keeps it from showing up at random when we look in the directory
next we activate the python extension and visual studio code so that we can use the
venn and visual studio codes terminal one of the first things that you want to do with any freshly
minted venn by the way is update its copies of pip and setup tools pip we need to have freshly
updated so that we can install packages into our virtual environment without issues but setup tools
should also be updated whenever you update pip as well because i found that the two tend to
work hand in hand quite often so it's a good idea to keep them both updated simultaneously
next up we're going to create the requirements for our projects this is a list of the packages
that are used by the python virtual environment that need to be installed into it in order to
work with the project in this under in this other window here i have a full version of the project
that i've been saving incrementally in a series of git commits so to save ourselves
time during these demos i'm just going to copy files from the other repository into this one
now most projects are going to use a single file to specify projects requirements it's called
requirements.txt and in this case i'm using two requirements files one is requirements.txt and
the other is requirements.dev.txt the first one lists the requirements we need to run the project
the second is the requirements we need to work on the project in this case all we
need right now is the black formatting tool we'll need to install requirements for each
of these so i'm going to get them set up with our virtual environment and finally i've also
got to create an empty folder that's going to hold our project zone data just called data
now we're ready to actually start creating code in our project so to start with a social
network needs to store data in the form of user accounts and messages among other things and
for that we need a database and while we can work directly with databases by using raw sql commands
it's often better for the sake of a project's maintainability to use a framework that allows us
to handle the database and its behaviors as python objects essentially as abstractions projects like
this are called an orm and i've talked briefly about orms in previous videos i have a link down
in the description where you can learn more in this project i'm going to use the same orm that
i did in a previous video an orm called peewee which is very small and very lightweight and
very easy to work with i'm also going to be using the same database as before sql lite and since we
don't plan on deploying this project at scale at least not yet we can get away with using
sqlite since it's good for small or single user applications it's perfect for a demo like this too
now our project has two main kinds of data structures first is a user account and the second
is a message posted by a user each of these we're going to store it in a different database table
and we're going to use the code in this file here models.pi to describe those tables for the user
all we have right now is just the user's name for the message we'll hold the user
that created the message a reference to the user that created the message the text of the message
and the date the message was posted the last one is auto-generated and to do this we use a
function that just gives us the current time to supply default now note that we haven't
actually created any data yet all we've done is describe how the data is going to be stored to
actually create the data we want to set up a data initialization script which i have here init db.pi
what this script does is it deletes and recreates the existing database entirely if there is one
it creates the tables inside the database and then populates them with some placeholder data
right now it's only going to insert one user me and one message from that user but by itself that
isn't very interesting i mean the whole point of a social network is to talk to other people right
okay so let's revise this and add some more users to our sample data and also some more
posts to go with them and then when we do that we can rerun the script to regenerate the data
and there's still one more thing missing though the whole point of having other people on a social
network is to be able to talk to them to reply directly to their messages and we don't have a way
to do that yet we don't have anything in the database that reflects who is responding to what
message so to do that we need to alter the schema or the the database models that we've created just
a little bit we need to add a new field to the message class called reply to which indicates
which message this is a reply to now i want to point out something interesting here
because tables are classes in our orm it can be tricky to have a table refer
to itself without running into a little limitation of how classes work in python
a class cannot contain a definition for itself at the top level
so to get around this our orm has a field type named deferred foreign key where we can supply the
textual name of a table instead of an object that hasn't actually been finished being defined yet
now let's upgrade our data setup script to match all of this
for the messages that are replies we can just list them as part of the
message they're replying to and we can rewrite our setup script a little bit
to insert those references recursively and obviously this doesn't cover all the possible ways
that we could list messages and replies but it's enough to get our sample data set up and loaded
now when we rerun the script setup script you're going to see an indication after each message as
to which message that one replies to in our little info dump here in the in the terminal that should
be enough to get us started just a few messages but before we go any further with this we should
probably save our work now yes we've been saving these files as we go along but for a project this
potentially complex we want to checkpoint our progress at every step so that if we make a
mistake or that if we find ourselves in a dead end or a false start we can roll back and what better
way to do that than with a version control system such as get and fortunately for us visual studio
code has built-in integration with git so we can initialize the repository right in our editor
as you can see though git is trying to add a whole mess of stuff to the repository
and that's not what we want so we'll want to set up a get a git ignore file to prune all that down
now for python project we can exclude everything in the virtual environment and we can also exclude
every file that has a dot pi c extension this is the compiled byte code generated from python files
you do not want to commit that to a repository as it just bloats up the repository pointlessly and
generates a lot of commit noise now for views of visual studio code we can exclude the dot vs code
directory which contains all the configuration information for the the current project directory
and finally we can exclude the project's own data directory since we're just going to be recreating
that every single time with our own scripts there's no reason to commit it so now we can
make the first full commit for our project in its early early stages we've got many more stages to
go after this because we haven't even created the web portion of the project let alone any
interactivity but that's all going to come in future installments so stay tuned that's it for
this video if you liked it please leave a comment below don't forget to subscribe to the idg tech
talk channel on youtube and for more smart python be sure to follow us on facebook youtube and
infoworld.com