Skip to main content

Posts

Showing posts from July, 2020

Deviants in a normal world

It's definitely been a bit since I've seen this graphy. Anyone who has learnt about standard deviation knows this graph. Standard Deviation Standard deviation shows us how spread out all the values in a set are from the mean. The higher the standard deviation, the more spread out the values are over a wider range and the flatter this curve. In a normal distribution, most values are within 1 standard deviation from the mean(the green part of the graph). Apparently NumPy can calculate standard deviation too! import numpy numSet = [ *lots of numbers* ] numSetStdDev = numpy.std(numSet) Variance The variance also indicates how spread out the values in a set are. It measures the average degree to which each value differs from the mean. variance = standard deviation ^2 import numpy numSet = [ *lots of numbers * ] numSetVar = numpy.var(numSet) Source:  https://www.w3schools.com/python/python_ml_standard_deviation.asp

The 3 Ms

It's been quite a while since I last had to calculate the mean, median or mode of any set of numbers. But here I am, giving myself a refresher on statistical calculations. I'm learning some basics with the help of W3 schools. They always break concepts down so well :)  Calculating Mean We can do this the hard way, which sucks and I'm lazy and is ridiculous when we're looking at incredible large sets of numbers anyway. So instead, we're going to do it the easy way using the NumPy module in Python. import numpy numSet = [ *lots of numbers *] numSetMean = numpy.mean(numSet) Calculating Median This is even more annoying to calculate manually. You have to sort all the values from smallest to largest and search for the value in the middle. No one has time for that. Numpy can do this too. import numpy numSet = [ *lots of numbers* ] numSetMedian = numpy.median(numSet) Mode This is just as troublesome to calculate as the median. You're trying to get the value that appear

Using Modals to render forms

A student wanted to create several tables with foreign keys linking them. He was placing the forms all over the place and that just didn't seem to make sense to me. I suggested that he link all the forms to one main registration page, and while he worked on it, I came up with my own solution that I feel rather proud of. Though I'm well aware it is a very simple solution.  views.py def register (request): # form for team formteam = TeamForm(request.POST or None ) if formteam.is_valid(): formteam.save() return redirect( 'registerForTeam' ) else : print (formteam.errors) formteam = TeamForm() # form for coach formcoaches = CoachesForm(request.POST or None ) if formcoaches.is_valid(): formcoaches.save() return redirect( 'registerForTeam' ) else : print (formcoaches.errors) formcoaches = CoachesForm() # form for player formplayer = PlayersForm(request.POST or None

Passing the parcel(data)

When I first learned how to use Django, passing variables was a constant source of confusion. With some practice, I got a better handle on the basics. You pass variables from the view to the template via context. And you pass variables from the template to the view via : the URL POST(when submitting forms, for instance)  and query parameters.  That's all I needed at that point in time anyway. I've just discovered there are more ways of passing them. One in particular caught my eye, passing it via sessions.  It's so easy to use. Once you've set it in your view, you can access it anywhere in your project. I imagine this is not something you would want to do with sensitive information though? I might be wrong of course. But it's definitely interesting to learn a new way to pass around variables.

A parameter is to argument as magma is to lava?

It's hard enough learning tech jargon but this one, I really struggle with - parameters & arguments "From a function's perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called." - w3schools  The difference is so subtle. Why can't they just use one word and stop confusing us tech newbies 🙈🙉🙊 

Querying tables linked via foreign keys

A student was trying to display data from both her parent and child tables that were linked by foreign key. I played around with it and found 2 ways I can do this.  The models (I took out most of the fields for this post because it was unnecessary for this explanation): class Owner(models.Model): owner_fname = models.CharField( 'First Name' , max_length = 50 , blank = False, null = False ) Owner=models.Manager() class PlayTime(models.Model): dog_name = models.CharField( max_length = 50 , blank = False ) owner = models.ForeignKey(Owner , on_delete =models.CASCADE , blank = False, null = False ) PlayTime=models.Manager() This first method involves multiple queries to the database. The second method involves a single query, and fewer lines of code. It is the equivalent of an inner join. I didn't include the context and the return statement, which are necessary, of course, if you plan to pass these variables on to the template. def details (request , pk):

Querying the database

A student wanted to join 2 tables and display the results but he was not making any headway and couldn't figure out what was wrong with his code. After some debugging via printing to the console, I realised that he was not getting any data and that's why nothing was being displayed on his template. And there was a simple reason for this, his model was not set up to allow such a query. He was joining a table that had no foreign keys to various other tables that were linked to each other rather than to the first table.  class Players(models.Model): PlayerName = models.CharField( max_length = 100 ) PlayerAddress = models.CharField( max_length = 100 ) PlayerEmailAddress = models.CharField( max_length = 100 ) PlayerPhoneNumber = models.IntegerField() TeamID = models.ForeignKey(Team , on_delete =models.CASCADE) Coach = models.ForeignKey(Coaches , on_delete =models.CASCADE) I did what I always do in such situations where I encounter problems I have not seen bef