Category: python

How Do You Repeat Code In Python?

One of my first tasks as a new QA engineer / Python dev back in the day was to figure out how do you repeat code in Python, how to iterate from 0 to 10 in Python or how do I repeat code in Python forever. Let’s look into possible solutions in this post.

How Do You Repeat Code In Python?

There are a few ways to repeat code in Python, but for loops are a very common way to repeat code in Python, especially when you need to iterate over a sequence of items (such as a list, tuple, or string).

They are easy to use and can be more efficient than using a while loop, especially when you know exactly how many times you need to repeat the code.

To repeat a block of code a specific number of times use:

# Repeat 5 times
for index in range(5):
    print(f'iteration index: {index}')
Continue reading

How Do I Open A Log File In Python?

In my early days as a QA engineer / Python developer, I was working on test-related projects a lot. For such projects, the focus is on logging and the question: “How do I open a log file in Python?” comes up on a daily basis. Let’s look into possible solutions in this post.

How Do I Open A Log File In Python?

In Python, you can use the open() function to open a log file.

This function needs to know the file path and the mode (like ‘read’ or ‘write’) that you want to use.

In Python, the r mode is used to open a file in read mode.

with open('log.txt', 'r') as log_file:
    content = log_file.read()

print('content: ', content)

This means that when you open a file with this mode, you can read the data from the file, but you cannot write to it.

Continue reading

What Is dict() In Python? (5 Examples)

Back when I used to work as a QA engineer and when I first got into Python (around 7 years ago), one of the key concepts I learned to use was dict() in Python. You might wonder what is dict()in Python and what are the main features of a dictionary in Python? Let’s discuss the answer to these questions in great detail below. 

What Is dict() In Python?

In Python, the dict() function is used to create a dictionary.

A dictionary is a collection of key-value pairs, where each key is associated with a value.

This function takes in a sequence of items and returns a dictionary with the items in the sequence as keys and values.

For example, you could use the dict() function to create a dictionary like this:

my_dict = dict(
    [
        ('key1', 'value1'),
        ('key2', 'value2')
    ]
)
Continue reading

How Do You Change A Specific Line In A Text File In Python?

I used to be a QA engineer and when I first got into Python, one of the first questions that came to my mind while working on Python scripts was: “How Do You Change A Specific Line In A Text File In Python?” We will discuss the answer to this question in much detail below. 

How Do You Change A Specific Line In A Text File In Python?

To change a specific line in a text file in Python, you can use the replace() method on strings, which allows you to specify the text you want to replace and the text you want to replace it with.

Here is a quick example:

# Open the file in read mode
with open('your_text_file_path.txt', 'r') as f:
    # Read the contents of the file into a list of lines
    lines = f.readlines()

# Use for loop to go through each line in a "your_text_file_path.txt"
for line_index, line in enumerate(lines):

    # Check each line for "old_text" and replace it
    # with "new_text" if it is found
    lines[line_index] = (
        lines[line_index]
        .replace('old_text', 'new_text')
    )

# Open a new file in writing mode (this will create a new file)
with open('your_new_text_file_path.txt', 'w') as f:
    # Write the modified lines to the new "your_new_text_file_path.txt"
    f.writelines(lines)
Continue reading

Can You Parse A String In Python?

If you’re an engineer (I used to be a QA engineer) you’ll have to extract information from a string and it’s likely you’re going to use Python for it. So, can you parse a string in Python? We will discuss the answer to this question in much detail below. 

Can You Parse A String In Python?

Yes, you can parse a string in Python. Parsing a string in Python is a common task that is performed when working with data. It involves taking a string and breaking it down into smaller pieces, or tokens so that it can be more easily manipulated and processed.

Python has a number of built-in functions and methods that can be used to build your own Python parser script. 

Continue reading

Class Python Example: String Validation

Hello! It’s Roberts Greibers here. 

Having worked in IT industry as a QA (software engineer) and later as a Python developer for more than 7 years, I’ve gathered numerous Python examples of classes.

I’ll tell you more about my experience as a Software Engineer/Python developer later in the post..  ⚑️

But now, let me list what Python classes examples you’re about learn in this post..

Read and pay ATTENTION!

You’re about to explore the MOST important parts of what is a class in Python! πŸš€

One of the CORE understandings for objects in Python (Object Oriented Programming with Python)

In this post, you’re going to find out:

  • What is a class in Python?
  • How to define a Python class blueprint?
  • How to call Python class method?
  • How to execute Python class as a callable function?

I’ll do my best to make you understand classes in Python from this one blog post!

Also, if you’re coming from Javascript world, a keyword this might be familiar..

.. well in Python there isn’t “Python class this“, but there’s a similar concept we use.

And at the end, I’ll also share more about how my students are using classes in Python training – this might be an OPPORTUNITY FOR YOU to follow their steps!

More on that at the end of the post! πŸ‘‡πŸ»

Continue reading

Python Requests POST GET (HTTP Request Explained)

Welcome back or nice to see you for the first time!

Roberts Greibers here with another experience-based post about Python requests.

I’ve been working professionally as a Python developer for well over 7 years, I’ll share more details about my story later in the post.. (just scroll down the page to find out more about me). 

But before we talk about how qualified I am to write posts about Python requests, I’ll just give you all the answers you’ve been Googling for. πŸš€

WARNING: You’re about to see the most important Python requests library use cases with clearly defined examples. 

This is the BIGGEST breakthrough you’ll ever achieve as a Python developer.

Learning and implementing this way of calling API with a Python request is a major achievement in your career that you’ll use on a daily basis for years to come. 

In this post, you’re going to explore:

🚨 I’ll explain in detail everything you should know about Python3 requests (why Python2 requests are NOT used anymore)

And I’ll show you how API calls in Python are done in a real project (from Django projects I’ve worked on)

The intention here is to give you a real Python HTTP request example you could use in your own projects. 

Just a quick note before we start: At the end of the post I’ll share details about what helped my students to get hired as Python developers, read till the end and see if that helps you too!πŸ‘‡πŸ»

Continue reading

Python Read CSV File (Python Excel Reader)

Hey! It’s Roberts Greibers here.

I’ll share details about my 7+ years of experience as a Python developer with you a bit later in the post (you’ll have to scroll down to find them).

But now, let’s talk about how I came to write my “read from CSV Python example” script.. πŸš€

Firstly, understand that CSV is a file format supported by nearly all platforms..

That’s why for Python programming purposes I’d suggest you use CSV format instead of any other excel format. 

It’s possible to find a Python excel library and use Python for excel files in general, but in this post, we’re going to strictly focus on how to read CSV file in Python. 

Also, we’re going to check:

  • Should you learn Excel or Python? (using Python in Excel?)
  • Are there any ways you can use Python for excel? (e.g. Parsing CSV Python)
  • Any Python excel library you could find and use? (e.g. Python CSV file reader)
  • How to import CSV file in Python? (the process of importing CSV into Python)

If you’ve been Googling for “Python Read CSV” or the typical “Reading CSV file in Python” you’ve found the RIGHT place! πŸŽ‰

I’m about to explain the details of how I optimize my time by parsing spreadsheets in Python (e.g. CSV in Python) and how I avoid manual CSV reading tasks using Python. 

Building a quick CSV file reader in Python can take you up to 30 minutes and save you hours later assuming you’re using the script on a daily basis. 

Let’s get into it and I’ll also share a way that helped me and might even help you to become a professional Python developer at the end of the post πŸ‘‡πŸ»

Continue reading

Subprocess In Python (Run Command Line)

Good evening, Roberts Greibers here.

We will get into my Python subprocess story and how I even got the point of writing blog posts about Python a bit later. 

But at this moment, I want you to pay attention and read very carefully!

What I’m about share with you is a real subprocess Python example! 🚨

How to use subprocess in Python to run command line commands!

The difference between running a shell command in Python with:

Also, a real example of me actually executing a shell command in Python.

Most “Python subprocess example” tutorials you’ll find online won’t give you a REAL project example..

And they won’t tell you what steps you’ll need to take in your specific situation. 

In this post, I’ll share how you should THINK in order to be able to come up with a solution for your own “how to run shell command in Python?” problem.. πŸš€

If this sounds interesting to you, keep on reading, I’ll also give you an opportunity to become a Python developer at the end of the post πŸ‘‡πŸ»

Continue reading

Python XML Parse – ElementTree Reader & Parser

In this Python XML tutorial, you will learn how to use xml.etree.ElementTree package (which is one of the many Python XML parsers) to process XML response.

For this tutorial, you can use your own XML response or follow the steps below and use the one I have provided.

The Python XML parsing steps I’m about to explain and guide you through were developed alongside a refund payment integration I was working on in Django. 

We’re going to explore the parsing end result for the SOAP XML response I wrote about in the previous post. The SOAP XML parsing post was written with a focus on an actual regex parsing in Python and all the steps before you get to the actual XML scheme style response. 

In this post, I’m about to give you a Python XML tutorial for parsing XML schemes when you already have a decoded version of an XML response and you see the information.

The post here is about extracting specific values from the whole XML tree without using regex or any other typical Python parsing tools, but the actual xml.etree.ElementTree package (the right way)

By the way, Roberts Greibers here – in my day job, I’m working as a Python Django backend developer for a local company in Riga, Latvia, and building a white label payment gateway platform. 

Over the past couple of years, I’ve gathered a lot of experience in the FinTech industry, practical knowledge of how payment systems work, how to develop, test and deploy them with minimal headache. 

Of course, I can’t go into too much detail here in a single blog post, but if you want to build your own portfolio project and become a Python and Django developer, I’d suggest checking out client testimonials here and reaching out to me.

I go very deep and always explain all the steps in detail with specific videos and documents for my clients. So the post here is just a small example of how I could help you out.

Also, I always answer all client questions along the way.. but enough of talking, let’s get into the code. 

Continue reading