Recursive Model Relationships in Django

The Need for Recursive Relationships There arises many times in the development of modern web applications where the business requirements inherently describe relationships that are recursive. One well known example of such a business rule is in the description of employees and their relationship to their managers, which are also employees. Notice the circular nature of that statement. This is exactly what is meant by a recursive relationship. In this article we will be developing a bare bones demo in […]

Read more

scikit-learn: Save and Restore Models

On many occasions, while working with the scikit-learn library, you’ll need to save your prediction models to file, and then restore them in order to reuse your previous work to: test your model on new data, compare multiple models, or anything else. This saving procedure is also known as object serialization – representing an object with a stream of bytes, in order to store it on disk, send it over a network or save to a database, while the restoring […]

Read more

Python Generators

What is a Generator? A Python generator is a function that produces a sequence of results. It works by maintaining its local state, so that the function can resume again exactly where it left off when called subsequent times. Thus, you can think of a generator as something like a powerful iterator. The state of the function is maintained through the use of the keyword yield, which has the following syntax: yield [expression_list] This Python keyword works much like using […]

Read more

Python: List Files in a Directory

I prefer to work with Python because it is a very flexible programming language, and allows me to interact with the operating system easily. This also includes file system functions. To simply list files in a directory the modules os, subprocess, fnmatch, and pathlib come into play. The following solutions demonstrate how to use these methods effectively. Using os.walk() The os module contains a long list of methods that deal with the filesystem, and the operating system. One of them […]

Read more

TensorFlow: Save and Restore Models

Training a deep neural network model could take quite some time, depending on the complexity of your model, the amount of data you have, the hardware you’re running your models on, etc. On most of the occasions you’ll need to save your progress to a file, so in case of interruption (or a bug), you’ll be able to continue where you left off. Even more, after a successful training you’ll surely need to re-use the model’s learned parameters to make […]

Read more

Python Circular Imports

What is a Circular Dependency? A circular dependency occurs when two or more modules depend on each other. This is due to the fact that each module is defined in terms of the other (See Figure 1). For example: functionA(): functionB() And functionB(): functionA() The code above depicts a fairly obvious circular dependency. functionA() calls functionB(), thus depending on it, and functionB() calls functionA(). This type of circular dependency has some obvious problems, which we’ll describe a bit further in […]

Read more

Python Tutorial for Absolute Beginners

Python is one of the most widely used languages out there. Be it web development, machine learning and AI, or even micro-controller programming, Python has found its place just about everywhere. This article provides a brief introduction to Python for beginners to the language. The article is aimed at absolute beginners with no previous Python experience, although some previous programming knowledge will help, but is not necessarily required. I’ve found that the best way to learn is to try to […]

Read more

Append vs Extend in Python Lists

Adding Elements to a List Lists are one of the most useful data structures available in Python, or really any programming language, since they’re used in so many different algorithms and solutions. Once we have created a list, often times we may need to add new elements to it, whether it be at the end, beginning, or somewhere in between. Python offers us three different methods to do so. In this article I’ll be showing the differences between the append, […]

Read more

How to Copy a File in Python

Introduction When it comes to using Python to copy files, there are two main ways: using the shutil module or the os module. All of the os methods we show here are methods that allow us to execute shell commands from our Python code, which we’ll use to execute the copy command (Windows) or the cp command (Unix). You’ll notice that many of these methods, in both the shutil module and the os module, have very similar functionality (which shouldn’t […]

Read more

Modified Preorder Tree Traversal in Django

Introduction This article is an extension to a previous article titled, Recursive Model Relationships in Django, which demonstrated a way to utilize the bare-bones Django capabilities to define database-backed Classes that model a common use-case for a recursive relationship. The use case I intend to satisfy is the common relationship between employees and managers of employees, which are also employees themselves. Evaluating the Prior Implementation The prior article defined an Employee class that translates into a database table of the […]

Read more
1 852 853 854 855 856 874