Python Logo

Converting The Current Time Into A Sentence In Python

3rd October 2021 - 16 minutes read time

Changing time into different formats is quite a common thing to do in programming. I have seen examples that change times into Roman numerals and other formats, but I realised I hadn't seen any code that changed the current time into a sentence. This means converting a numeric time value into a sentence that can be read. For example, the time 9:05 can be read as "it is five past nine", or if the time is 9:00 then it would read "it is nine o'clock". At it turns out, there are only a few rules that govern doing this.

Python Logo

SOLID Principles In Python

26th September 2021 - 18 minutes read time

SOLID is a set of object oriented design principles aimed at making code more maintainable and flexible. They were coined by Robert "Uncle Bob" Martin in the year 2000 in his paper Design Principles and Design Patterns. The SOLID principles apply to any object oriented language, but I'm going to concentrate on what they mean in a Python application in this post.

I originally wrote about SOLID principles with PHP as the basis of the article, but as the lessons here can be easily applied to any object oriented language I thought that I would re-write it with Python in mind. If you are familiar with only PHP or Python then this will be a good learning resource on learning the other side.

Python Logo

The Basics Of Object Oriented Programming In Python

19th September 2021 - 25 minutes read time

I have been learning Python recently, and whilst I'm familiar with PHP and other C-like languages, Python has been a bit of a challenge to get used to due to the different syntax. This is especially the case with objects as although Python is object oriented there are a couple of gotchas when creating custom objects. This article sets out how to use classes and objects, but I will assume that you have a basic understanding of object oriented programming in other languages.

Python Logo

A Guide To Using Different Tkinter Widgets In Python

12th September 2021 - 14 minutes read time

The Python Tkinter module has a lot of widgets built in that provide different parts of the graphical user interface. Everything from labels to buttons are all widgets and they extend from the base Widget class.  Some Widgets are easy to put together, but others require a little bit of configuration to get working. This article will show each Tkinter widget available and how to add each one to an application. There are two sorts of Widgets available. Simple widgets exist in their own right, but some widgets are made to contain other widgets.

Python Logo

Slicing Arrays and Strings Using The Colon Operator In Python

5th September 2021 - 7 minutes read time

Strings and arrays can be manipulated in a number of ways in python. Splitting a string or an array into sections can be done using the section slicing that is built into the core python syntax and uses the colon (:) symbol.

Generating a simple array in python is straightforward, the following creates an array of 9 elements.


an_array = [1,2,3,4,5,6,7,8,9]

To grab a particular element of the array you can use the simplest section slicing syntax. The number in the square brackets represents the position of the item to be returned. Remember that the counting starts at 0, so using 1 will return the item from the "second" position.

Python Logo

Getting Started Creating Graphical User Interfaces With Python And Tkinter

29th August 2021 - 12 minutes read time

When starting out learning Python you will probably start by looking at the command line a lot. This is understandable as it's important to learn about variables, conditionals and loops and the command line is the best way to start out with the language.

I have always found it useful to quickly introduce graphical user interfaces when learning programming as it allows people learning programming to have something they can engage with. Being able to see the output of their program in real, tangible things that they can interact with.

There are a couple of different libraries in Python that can be used to create user interfaces, and perhaps the first one Python developers tend to learn is Tkinter or the "Tk interface" module. This is a Python binding to the Tcl/Tk toolkit to create graphical user interface.

Drupal Logo

Drupal 9: Stubbing API Modules For Fun And Profit

22nd August 2021 - 21 minutes read time

If you've been building websites sites for a while you will realise that no site lives in isolation. Almost every site you build integrates with some form of API, and this is especially the case for the more enterprise sites where data is often synchronised back to a CRM system or similar. Drupal's hook and service architecture means that you can easily build integration points to that API to pull in data.

Pulling in data from an API into a Drupal site means installing an off the shelf module or creating a custom module to provide the integration. What route you go for depends on the integration, but for enterprise sites the API is quite often very custom to the business. I have even seen APIs being built at the same time as the site that it needs to integrate with, which is especially the case for startups and new businesses.

Drupal Logo

Drupal 9: An Introduction To Services And Dependency Injection

15th August 2021 - 23 minutes read time

Drupal 8 and 9 are built upon services, with many parts of the system available through dependency injection, so it's important to understand the concepts. Services are a way to wrap objects and use dependency injection to produce a common interface. They are powerful and are used all over Drupal to do pretty much everything.

They can, however, be a little difficult for newcomers to the system to understand, especially if they are coming from Drupal 7 or other non-object oriented systems. When you look at some Drupal source code you are likely to see objects being created out of apparent thin air. It's a little hard to know where they come from if you aren't used to the how they work.

PHP Logo

Debugging And Testing PHP With assert()

8th August 2021 - 18 minutes read time

PHP has a built in debugging and testing tool called assertions, which is essentially the assert() function and a few configuration options.

With this feature you can add additional checks to your application whilst you are developing it so that when you deploy to production you can be sure that things will run correctly. It is simple to run and allows you to embed testing code within your production code without having an adverse effect on performance.

I've seen the use of assert() in a couple of open source PHP projects, but had not really dug into how it works or what it does. As it turns out, assert() is actually very useful and can also be found in other languages like C/C++, Python and JavaScript.

Drupal Logo

Drupal 9: Selecting Machine Names For Content Entities And Fields

1st August 2021 - 26 minutes read time

Naming things is hard[citation needed] and there are a lot of things that you can name when configuring a Drupal site. Picking the right machine names for the different parts of Drupal can make your life easy in the long run. Changing labels is simply a case of tweaking the label in the interface, or through configuration updates. The issue is that once you decide on a machine name for something in Drupal it's pretty much set in stone forever.

The machine names you pick are often used in database tables, paths, interface elements and pretty much anywhere it is used. Changing entity or field machine names at a later date is difficult and can mean writing complex code or using migrations to achieve.