Category: Python

Python code examples

Python – Read and Write to Excel ActiveX Textbox

Reading and writing to an Excel ActiveX textbox with Python can be fun. If you really do not have anything better to do. Anyway, if this is your current task, you are on the correct place to see how it

Tagged with: , , , , ,

Python – Docstring – Help and Comments

Docstrings in Python are actually quite simple – the are 3 times this """ and the code between opening and closing them can be formatted in any possible way. The reason I have decided to dedicate a whole article to

Tagged with: , , , , ,

Python – How to fill Excel cell with image, using python

If you have ever dreamed that it will be possible to fill out an Excel cell with a picture, using Python, then you should probably reevaluate your dreams. Seriously, who dreams of something like that? Without further dreaming, the code

Python – Logging in Python. Formatting with Black

Logging in Python is beautifully implemented with the logging library – https://docs.python.org/3/howto/logging.html It simply does anything you need and you do not need to write something yourself, like I did some time ago here in my VBA Boilerplate. So, if

Tagged with: , , ,

Python – Multisets – A class to count the elements of a set

Ok, so you probably have heard about set in Python – it is a collection with non-repeatable elements – as if you are a stamp collector and you need to collect only the unique stamps into your album. However, in

Tagged with: , ,

Python – Writing a Logger in Python with a decorator

Functions in Python are objects. If this is the first time you read this sentence, then you will not get a lot from this article, but it will show you, that there is quite a lot to learn about Python.

Tagged with: , ,

Python – DefaultDict Object. Or how to avoid the Key Error.

Have you seen the error below?

Of course you have. And you know, that it is because you have written something extremely unwise like this:

Yeah. You cannot increment the value of a non-existent key. Unless… Unless you

Tagged with: , ,

VBA and Python – Intersection of dictionaries

Intersection of dictionary (or of any two lists) is a list with elements, that are present in both other lists. Thus, if we have the following 3 dictionaries in Python:

Then, an intersection of their keys will contain only

Tagged with: , , , , , ,

Python – Class Variable vs Instance Variables

Class variables vs instance variables could seem a bit strange, if you have no OOP experience, but after a few thausand lines of code they become logical. So, in around two days of programming you will be getting it intuitively

Tagged with: , , ,

Python – Difference between __str__ and __repr__

__repr__  and __str__  dunders are quite similar in Python. If you need  a quick summary of these 2, then these 3 points will be enough: __repr__ dunder presents the class object, whenever it is called by the terminal __str__ dunder

Tagged with: , , ,

Json.dump vs json.dumps in Python

The difference between json.dump and json.dumps is actually quite visible: dump()  – dumps into a file or StringIO dumps()  – dumps a string, that could be printed Still, I am going to give a few example with these, as the

Tagged with: , , , ,

Python – Iterators

One of the benefits of having your own blog and not following any schedule for subjects to write about is that you may write for whatever you feel like, whenever you feel like. So, Iterations in Python is the subject

Tagged with: ,

Django – CRUD application with Python

Making a CRUD application with Python is actually one of the basic thing a Python Developer should be able to do. Although some may argue it is extremely basic – it is not. Requirements The basic requirements for this one,

Tagged with: , , , , , , ,

Django – Making a simple web app with 4 links

The idea of the article is to show how to make a simple Django App, with 4 links, each of which navigates to a specific page. The whole code of the “app” is in GitHub, here: https://github.com/Vitosh/Python_personal/tree/master/JustProject. In general, these are

Tagged with: , , , , , , ,

Python – Exception handling

Exception handling in python is actually quite an interesting topic. In general, the exceptions in programming could be of three types: syntactic errors – these would be noticed by your IDE or by the interpretor, thus you do not need

Tagged with: , , ,

Using API calls with VBA and Python to Read and Write to a Database

Using API calls with VBA and Python to write a database is actually a pretty decent architecture. Here, I would like to say that “It takes the best of the both worlds”, but the truth is that it takes the

Tagged with: , , , , , , , ,

Python – Virtual Environment

This is a short step-by-step introduction on how to build virtual environment in Python and how to use it. Virtual envirnments keep dependencies, required by the specific project separated. Thus, it is the “clean and clear” place of the project,

Tagged with: , , ,

Python – Django Girls – Introduction to Django – Video

Months ago, I was planning to take part in the #RailGirls event in Sofia, and I was even a partner in the event. Anyway, as it was postponed up to somewhere in the future, I have decided to go through

Tagged with: , , ,

Python – Writing Unit Tests with unittest library – Video

In the previous article for abstraction and inheritance I wrote some classes, showing the basics of these two programming concepts. In this one, I will write unit tests for one of the classes, written before. Software testing is an important

Tagged with: , , , , , ,

Python – Abstraction and Inheritance – Video

Abstract classes in Python are classes that inherit from the ABC (Abstract base class). In the video below, I have solved a problem, showing how 2 classes inherit from an abstract class and implement its methods. The abstract class is

Tagged with: , ,

Python – Closure

In this article I am planning to give an answer of the following two questions: What is a closure? A closure is a function, inheriting variables from enclosing environment. Operationally, a closure is a record storing a function together with

Tagged with: , ,

Python – Abstract Methods and Abstract Class – Live Coding Video

Abstract class and abstract methods are one of the pillars of Object Oriented Programming. In Python, an abstract class is a class, inheriting from Abstract Base Classes (ABC) library. And an abstract method is a method of this class, which

Tagged with: , , ,

Python – Permutations and Combinations Made Easy with Itertools

Long time ago, various people, including me, were trying to make permutations and combinations with nested loops and various other “heavy” structures. Anyway, in Python there is a library, which does this for us and sets the end of the

Tagged with: , , , ,

Python – Defining Classes and Methods with Python – LiveCoding Video

Today I have decided to check whether I can pass the first homework from SoftUni OOP with Python, through a live coding session. I have not read the problem before the video, thus I may say it took me exactly

Tagged with: , , , , ,

Python – Making Maps with Cartopy

Cartopy is a Python package designed for geospatial data processing in order to produce maps and other geospatial data analyses. Making a map with Python and Cartopy is actually easier than you might think, but still it has its own

Tagged with: , , , , , ,

Python – Bayes’ theorem – Drug testing example. 99% is not enough!

The Bayes’ theorem is interesting, as far as it is both quite challenging to understand and shocking once you really realize the simplicity of the numbers behind it. It describes the probability of an event, based on prior knowledge of conditions that

Tagged with: , , , ,

Python – Linear Regression

In 2013 and 2014 (wow, already 7 years ago!) I wrote two articles about linear regression with Excel. Now, I am getting more and more interested in Python, thus I guess it would be interesting to remake the article into

Tagged with: , , , ,

Python – Building New Columns in a Dataframe, Based on Other Columns

Working with python, pandas and dataframes is quite challenging even for experienced Excel & VBA developer, as I love to consider myself for one. Thus, today I was fighting for quite some time to add new columns to a dataframe,

Tagged with: , , , , , , , ,

Python – *args, **kwargs and the normal ones (arg)

The good, the bad and the ugly is a famous western. In Python, “the ugly” is probably the **kwargs, as far as it has two stars before it. Anyhow, the idea of *args, **kwargs and arg is simply to pass arguments.

Tagged with: , , , , , ,

Python – List Comprehension

List comprehension in Python is creation of lists from iterables and other lists, following a condition. It is lightning fast and it looks nice, because it is a 1 liner. In this article, I will show a few examples of

Tagged with: , , , , ,
Top