Detailed Notes
We start a new series of bite-sized solutions to common problems with a larger problem where we want to take a URL and return a short form of one as seen on many sites. We will be using Python as our example language, but the application will be described a we build it in a way for you to choose the development language best-suited to your needs.
You can find out more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list and you will periodically receive coupons for courses as well as notifications of the latest releases.
Transcript Text
foreign [Music] well hello and we are focusing we are in like a series of questions here this specific episode we're going to talk about this question how do we allow a user to send us a URL and we're going to give them back a short one now we decided we're going to call this thing shorty so we're just going to go with it's gonna be a new file and we are in pycharm there's some questions around that that we will come back to but we're going to call this uh what's called shorty.py so we're going to have a new script but I do want to do is I want to go back to my application design and I want to do pre-questions because we will want to come back to this or you may want to go look for it and those are going to be how do I actually where do I download and IDE for python or pie chart let's just call it let's do this why do I download the pie charm IDE for python and then probably will get us where we need to we'll see if we need to do extras okay back to the this question problem at hand so shorty what we want to do here is we're going to allow our user send us URL and give a new one so let's come to shorty and we're just going to start with it so our first thing is this is what we're doing so we're going to find a little method and a function and we're going to call it a shortener and as we always do in Python starts with Define we're not going to do a we want to do a class that's a good question we might so let's go ahead and let's do a class and we're going to call it shortener and it's going to be I don't know if it has any properties yet so the first thing we're going to do is let's see retrieve new URL and we're going to get the old URL and what we want to do is so we're just going to have a counter let's do this um and so he's going to have so he's gonna have a counter is going to be equal to one and so this will be something we're going to start very simple so first all we need to do is we're going to say let's say we'll even store this so this will be a source URL let's just call it source so Source equals this and short let's see we'll say new link equals that so that means they're going to give us the older URL which means source is going to be equal to Old URL because we're going to store it and then he may not like that it's like this uh maybe not let's go real quick so real quick we're going to go just because it's one of those things we forget sometimes we talk about our classes so class yeah that's right no it's right the first time so we can do it like this oh I forgot that because this this means it has to be self otherwise it's going to be static oops period comma so that's R2 uh let's do this clean it up so now we're going to do is we're going to store our old URL so so the new let's say what we call it result hour and call return value turn value equals oh here let's do this base equals and we're just going to do we're going to do this really simple right now so it's going to be localhost and that's going to be our base and so return value equals base plus it's got to be a string because it's a number that's going to be self.counter and then self dot counter I'll just say plus equals one so we've got to increment our counter this is a Brute Force way to do it but that's okay and then we're going to do a return return value and we really don't need well we'll keep this for now because we probably will find a useful so self Dot link equals turn value and so for our little test here we'll do it like this Shorty 1.0 [Music] replication just so people know who it is let's do this let's do this in case people need any questions about it info number dot com es whoops and then here I'm going to do input give me a URL sorry uh equals that s is going to be an interest instance of the shortener class and then I'm going to print your new URL is I'll just do it this way up s dot retrieve new URL for source let's see how this runs so now if I do python shorty give me a URL let's take this URL so this URL here I said oh base is not defined uh oops right there oh and let's do this um so I'm also going to need as part of this so let's see let's see we're going to call it navigate and that's going to be self I call it URL and then we're going to return the New Link so we're just going to say if URL equals self dot source nope um let's do this for now we're not going to check that so we're going to do this that's your new URL well let's do that for now and we'll navigate we'll fix that one in a little bit we'll cover that next time because that's going to be coming in so let's go back to this do I have that yes okay so if I give them this big URL it's going to come back and say this is your new URL so technically if this other pieces are worked then I'll be able to do it now that does require us so this is send us a URL we're going to give them a short one now let's do um this will be a URL list and get a little more complicated here so we don't need the New Link doesn't need this but New Link counter equals the return value oh it's going to be self.counter and so navigate here what I'm going to do is I'm going to do url.split on uh let's even do it this way we're going to make it really simple uh yeah let's do it like this and let's do this so short and then that'll make it real easy so this is going to be our new link and then that means that if we split on the question mark this makes it super simple then our value the let's see the code let's call that the index equals that and then we're going to do a URL list idx give me URL list not a new link that's going to be your own list equals that we don't need new Link at all yeah because we don't have it turn value return value okay so now let's do this give me a URL a short URL I'm going to call it short URL and then we're going to do front s dot navigate short URL okay so let's see how this works do I have I don't have that thing doing nope so let's go back to here and let's get our baby thing so let's see if this works so now give me a URL all right now I'm going to give you back whoops um let's do this do it that way I'll just keep it simple oh I need to do this instead of that um that'll actually work I'm just gonna pin this is not the most awesome way to do this but we're going to do it this way anyway so okay so now so it's going to come back and it says hey if you give me zero I send it back list indices must be integers or slices not a string oh this needs to be that's an ant because that was a oh actually let's do it this way idx that needs to be now if I take it and I give it this guy he's going to give me that back he gave me the wrong thing I need to give him uh it's not the return value it's the old URL if we do this and we come in here and now we take that boom it gives us back our address which we can do here and guess what it's the exact same address so we have solved that problem we now have we have a way for a user to give us an a URL and then we're going to send them a short one but we have some more work to do because at this point the next thing we want to do is come in here and say okay they've got this one so this would be like a you know check off of this all right let's do something that's a little more resilient so we're going to create a database and talk about a table and probably within that we're going to connect so we've got a couple of things to do and that will be our next episode thanks a lot for listening in and let us know if you have any questions you can shoot questions as always to info developmentware.com and until next time keep working on becoming a better developer hello this is Rob with developmentor also known as building better developers wanted to announce that we have school.developmentor.com feel free to check it out if you like any of this information any of the content that we've sent and you would like to see more you can come out you can enroll for free we have free courses we've got places for you to get better at just learning a technology our how to's you can work on your business skills we can help you with becoming a better developer as encoding and things like that a lot of the stuff you've seen on YouTube we also have out at school.development or we just have it a little more of a educational format and a way for you to track your progress as you move forward becoming a better developer thank you
Transcript Segments
foreign
[Music]
well hello and we are focusing we are in
like a series of questions here this
specific episode we're going to talk
about this question
how do we allow a user to send us a URL
and we're going to give them back a
short one
now we decided we're going to call this
thing
shorty so we're just going to go with
it's gonna be a new file and we are in
pycharm there's some questions around
that that we will come back to but we're
going to call this uh what's called
shorty.py
so we're going to have a new script but
I do want to do is I want to go back to
my application design and I want to do
pre-questions
because we will want to come back to
this or you may want to go look for it
and those are going to be how do I
actually
where do I download
and IDE for python
or pie chart
let's just call it
let's do this why do I download the pie
charm IDE for python
and then
probably will get us where we need to
we'll see if we need to do extras okay
back to the this question problem at
hand so shorty what we want to do here
is we're going to allow our user send us
URL and give a new one so let's come to
shorty
and we're just going to start with it so
our first thing is
this is what we're doing so we're going
to find a little method and a function
and we're going to call it a shortener
and
as we always do in Python starts with
Define we're not going to do a we want
to do a class that's a good question
we might so let's go ahead and let's do
a class and we're going to call it
shortener
and it's going to be
I don't know if it has any properties
yet so the first thing we're going to do
is let's see
retrieve
new URL
and we're going to get the old URL
and what we want to do is so we're just
going to have a counter let's do this
um
and so he's going to have
so he's gonna have a counter
is going to be equal to one
and so this will be something we're
going to start very simple so first all
we need to do is we're going to say
let's say we'll even store this so this
will be a source URL
let's just call it source so Source
equals this and
short
let's see we'll say new
link
equals that
so that means they're going to give us
the older URL
which means source is going to be equal
to Old URL because we're going to store
it
and then
he may not like that
it's like this
uh maybe not let's go real quick
so real quick we're going to go just
because it's one of those things we
forget sometimes we talk about our
classes so class yeah that's right no
it's right the first time so we can do
it like this
oh I forgot that because this this means
it has to be self otherwise it's going
to be static oops period comma so that's
R2 uh let's do this
clean it up so now we're going to do is
we're going to store
our old URL
so so the new
let's say what we call it result hour
and call return value
turn value
equals
oh here let's do this
base equals and we're just going to do
we're going to do this really simple
right now so it's going to be localhost
and that's going to be our base
and so return value equals
base plus
it's got to be a string because it's a
number
that's going to be self.counter
and then self dot counter
I'll just say plus equals one so we've
got to increment our counter this is a
Brute Force way to do it but that's okay
and then we're going to do a return
return value
and we really don't need well we'll keep
this for now because we probably will
find a useful so self Dot
link
equals
turn value
and so for our little test here
we'll do it like this Shorty 1.0
[Music]
replication
just so people know who it is
let's do this let's do this
in case people need any questions about
it
info number
dot com
es whoops
and then here
I'm going to do input
give me a URL
sorry uh
equals
that
s is going to be an interest instance
of the shortener class
and then I'm going to print
your new URL is
I'll just do it this way up s dot
retrieve new URL for
source
let's see how this runs
so now if I do python shorty
give me a URL let's take this URL
so this URL here
I said oh base is not defined
uh oops right there
oh and let's do this
um
so I'm also going to need
as part of this so let's see
let's see we're going to call it
navigate
and that's going to be self
I call it URL
and then we're going to return
the
New Link
so we're just going to say if
URL
equals
self dot source
nope
um let's do this for now we're not going
to check that so we're going to do this
that's your new URL
well let's do that for now and we'll
navigate we'll fix that one in a little
bit we'll cover that next time because
that's going to be coming in
so let's go back to this
do I have that yes okay so if I give
them this big URL it's going to come
back and say this is your new URL so
technically
if this other pieces are worked then
I'll be able to do it now that does
require us so this is send us a URL
we're going to give them a short one
now let's do
um
this will be a URL list
and
get a little more complicated here so we
don't need the
New Link doesn't need this but New Link
counter
equals the return value oh it's going to
be self.counter
and so navigate
here what I'm going to do is I'm going
to do url.split
on
uh let's even do it this way we're going
to make it really simple
uh
yeah let's do it like this
and let's do this so short and then
that'll make it real easy so this is
going to be our new link and then
that means that
if we split on the question mark this
makes it super simple
then our value the let's see the code
let's call that
the index equals that
and then we're going to do a URL list
idx
give me URL list not a new link that's
going to be your own list equals that
we don't need new Link at all yeah
because we don't have it turn value
return value okay
so now
let's do this
give me a URL a
short URL
I'm going to call it short URL
and then we're going to do front
s dot navigate
short URL okay so let's see how this
works
do I have I don't have that thing doing
nope so let's go back to here
and let's get our
baby thing so let's see if this works
so now give me a URL all right
now I'm going to give you back whoops
um
let's do this
do it that way
I'll just keep it simple
oh I need to do this
instead of that
um that'll actually work
I'm just gonna pin
this is not the most awesome way to do
this but we're going to do it this way
anyway so okay so now so it's going to
come back and it says hey if you give me
zero
I send it back
list indices must be integers or slices
not a string oh this needs to be
that's an ant because that was a oh
actually let's do it this way
idx that needs to be
now if I take it
and I give it this guy
he's going to give me that back
he gave me the wrong thing I need to
give him
uh it's not the return value
it's the old URL
if we do this
and we come in here
and now we take that
boom it gives us back
our address
which we can do here and guess what it's
the exact same address so we have solved
that problem
we now have
we have a way for a user to give us an a
URL and then we're going to send them a
short one but we have some more work to
do because at this point
the next thing we want to do is come in
here and say okay they've got this one
so this would be like a you know check
off of this all right let's do something
that's a little more resilient so we're
going to create a database and talk
about a table and probably within that
we're going to connect so we've got a
couple of things to do and that will be
our next episode
thanks a lot for listening in and let us
know if you have any questions you can
shoot questions as always to info
developmentware.com and until next time
keep working on becoming a better
developer
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology our how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.development or we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
thank you