Posted on Leave a comment

How to Read Specific Columns from CSV File in Python

Rate this post
A Quick Glance at The Solutions [Each solution stays for 5-10 secs.]

Problem: Given a CSV file, how to read only specific column(s) from the csv file? (Reading a specific column from a csv file will yield all the row values pertaining to that column.)

Example: Consier the following csv file (countries.csv):

Country,Capital,Population,Area
Germany,Berlin,"84,267,549","348,560"
France,Paris,"65,534,239","547,557"
Spain,Madrid,"46,787,468","498,800"
Italy,Rome,"60,301,346","294,140"
India,Delhi,"1,404,495,187","2,973,190"
USA,Washington,"334,506,463","9,147,420"
China,Beijing,"1,449,357,022","9,388,211"
Poland,Warsaw,"37,771,789","306,230"
Russia,Moscow,"146,047,418","16,376,870"
England,London,"68,529,747","241,930"

Question: How will you read the above csv file and display the following columns

  1. Country column along with the Capital column?
  2. All values in the population column?

Method 1: Using Pandas

Using the Pandas library is probably the best option if you are dealing with csv files. You can easily read a csv file and store an entire column within a variable.

Code:

import pandas as pd df = pd.read_csv("countries.csv")
country = df['Country']
# or
# country = df.Country
capital = df['Capital']
# or
# capital = df.Capital # displaying selected columns (Country and Capital)
for x, y in zip(country, capital): print(f"{x} {y}") # displaying a single column (Country)
print()
print(df['Population'])

Output:

Germany Berlin
France Paris
Spain Madrid
Italy Rome
India Delhi
USA Washington
China Beijing
Poland Warsaw
Russia Moscow
England London 0 84,267,549
1 65,534,239
2 46,787,468
3 60,301,346
4 1,404,495,187
5 334,506,463
6 1,449,357,022
7 37,771,789
8 146,047,418
9 68,529,747
Name: Population, dtype: object

Explanation:

  • Read the csv file using pd.read_csv() Pandas function.
  • Save all the information of the columns Country and Capital within independent variables using
    • country = df['Country']
      • Alternatively, you can also use country = df.Country
    • capital = df['Capital']
      • Alternatively, you can also use capital = df.Capital
  • To display the country names and their capitals simultaneously, you can bind the two columns, country and capital, using the zip() function and then display each country along with its capital using a for loop upon the zipped object.
  • To display all the values in the population column, you can simply use df['Population'].

TRIVIA
zip() is a built-in function in Python that takes an arbitrary number of iterables and binds them into a single iterable, a zip object. It combines the n-th value of each iterable argument into a tuple.
Read more about zip() here.

List-Based Indexing of a DataFrame

In case you are not comfortable with using zip() to display multiple columns at once, you have another option. You can simply use list-based indexing to accomplish your goal.

List-based indexing is a technique that allows you to pass multiple column names as a list within the square-bracket selector.

Example:

import pandas as pd df = pd.read_csv("countries.csv")
print()
print(df[['Country', 'Capital']])

Output:

 Country Capital
0 Germany Berlin
1 France Paris
2 Spain Madrid
3 Italy Rome
4 India Delhi
5 USA Washington
6 China Beijing
7 Poland Warsaw
8 Russia Moscow
9 England London

Method 2: Integer Based Indexing with iloc

Approach: The idea here is to use the df.iloc[rows, columns].values to access individual columns from the DataFrame using indexing. Note that the first column always has the index 0, while the second column has index 1, and so on.

  • rows is used to select individual rows. Use the slicing colon: to ensure all rows have been selected.
  • columns is used to select individual columns.
    • Use country = data.iloc[:, 0].values to save the values of the Country column.
    • capital = data.iloc[:, 1].values to save the values of the Capital column.
    • population = data.iloc[:, 2].values to save the values of the Population column.
import pandas as pd data = pd.read_csv('countries.csv')
country = data.iloc[:, 0].values
capital = data.iloc[:, 1].values
population = data.iloc[:, 2].values
# displaying selected columns
print(data[['Country', 'Capital']])
print()
# displaying a single column (Population)
print(population)

Output:

Country Capital
0 Germany Berlin
1 France Paris
2 Spain Madrid
3 Italy Rome
4 India Delhi
5 USA Washington
6 China Beijing
7 Poland Warsaw
8 Russia Moscow
9 England London ['84,267,549' '65,534,239' '46,787,468' '60,301,346' '1,404,495,187' '334,506,463' '1,449,357,022' '37,771,789' '146,047,418' '68,529,747']

Method 3: Name-Based Indexing with loc()

Instead of selecting the columns by their index, you can also select them by their name using the df.loc[] selecter.

The following example shows how to select the columns Country and Capital from the given DataFrame.

import pandas as pd data = pd.read_csv('countries.csv')
val = data.loc[:, ['Country', 'Capital']]
print(val)

Output:

Country Capital
0 Germany Berlin
1 France Paris
2 Spain Madrid
3 Italy Rome
4 India Delhi
5 USA Washington
6 China Beijing
7 Poland Warsaw
8 Russia Moscow
9 England London

Related Tutorial: Slicing Data from a Pandas DataFrame using .loc and .iloc

Method 4: Using csv Module

csv module is yet another spectacular option in Python that allows you to play with csv files. Let us have a look at the code that helps us to read the given csv file and then read specific columns from it:

import csv population = []
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile: data = csv.DictReader(csvfile) for r in data: print("Country", ":", "Capital") # append values from population column to population list population.append(r['Population']) # displaying specific columns (Country and Capital) print(r['Country'], ":", r['Capital']) # display the population list print(population)

Output:

Country : Capital
Germany : Berlin
Country : Capital
France : Paris
Country : Capital
Spain : Madrid
Country : Capital
Italy : Rome
Country : Capital
India : Delhi
Country : Capital
USA : Washington
Country : Capital
China : Beijing
Country : Capital
Poland : Warsaw
Country : Capital
Russia : Moscow
Country : Capital
England : London
['84,267,549', '65,534,239', '46,787,468', '60,301,346', '1,404,495,187', '334,506,463', '1,449,357,022', '37,771,789', '146,047,418', '68,529,747']

Explanation:

  • Import the csv module and open up the csv file. Ensure that you feed in the encoding argument as it helps to eliminate any unreadable characters that may occur in the given csv file.
    • with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile
  • Allow Python to read the csv file as a dictionary using csv.Dictreader object.
  • Once the file has been read in the form of a dictionary, you can easily fetch the values from respective columns by using the keys within square bracket notation from the dictionary. Here each column represents the key within the given dictionary.

Bonus: Here’s a quick look at how the DictReader() class looks like:

import csv population = []
with open('countries.csv', newline='', encoding='utf-8-sig') as csvfile: data = csv.DictReader(csvfile) for row in data: print(row)

Output:

{'Country': 'Germany', 'Capital': 'Berlin', 'Population': '84,267,549', 'Area': '348,560'}
{'Country': 'France', 'Capital': 'Paris', 'Population': '65,534,239', 'Area': '547,557'}
{'Country': 'Spain', 'Capital': 'Madrid', 'Population': '46,787,468', 'Area': '498,800'}
{'Country': 'Italy', 'Capital': 'Rome', 'Population': '60,301,346', 'Area': '294,140'}
{'Country': 'India', 'Capital': 'Delhi', 'Population': '1,404,495,187', 'Area': '2,973,190'}
{'Country': 'USA', 'Capital': 'Washington', 'Population': '334,506,463', 'Area': '9,147,420'}
{'Country': 'China', 'Capital': 'Beijing', 'Population': '1,449,357,022', 'Area': '9,388,211'}
{'Country': 'Poland', 'Capital': 'Warsaw', 'Population': '37,771,789', 'Area': '306,230'}
{'Country': 'Russia', 'Capital': 'Moscow', 'Population': '146,047,418', 'Area': '16,376,870'}
{'Country': 'England', 'Capital': 'London', 'Population': '68,529,747', 'Area': '241,930'}

It is evident from the output that csv.DictReader() returns a dictionary for each row such that the column header is the key while the value in the row is the associated value in the dictionary.

Conclusion

To sum things up, there are majorly four different ways of accessing specific columns from a given csv file:

  • List-Based Indexing.
  • Integer-Based Indexing.
  • Name-Based Indexing.
  • Using csv modules DictReader class.

Feel free to use the one that suits you best. I hope this tutorial helped you. Please subscribe and stay tuned for more interesting tutorials. Happy learning!


Learn Pandas the Fun Way by Solving Code Puzzles

If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

Coffee Break Pandas Book

It contains 74 hand-crafted Pandas puzzles including explanations. By solving each puzzle, you’ll get a score representing your skill level in Pandas. Can you become a Pandas Grandmaster?

Coffee Break Pandas offers a fun-based approach to data science mastery—and a truly gamified learning experience.

Posted on Leave a comment

Computer Science Researcher – Income & Opportunity

5/5 – (2 votes)

Before we learn about the money, let’s get this question out of the way:

What Does a Computer Science Research Scientist Do?

A computer science researcher and scientist identifies and answers open research questions in computer science. They apply scientific reasoning and research techniques to push the state-of-the-art forward in various fields such as machine learning, distributed systems, databases, algorithms, and data science.

These are some of the fields that are relevant for you as a computer science researcher:

Six of the most common daily activities of computer science researchers are:

  • reading research papers,
  • thinking about research questions and problems,
  • identifying research gaps and discussing them with their peers,
  • creating code and software systems for evaluation purposes,
  • writing research papers, and
  • presenting those scientific results at conferences and in journals.

How to Become a Computer Science Researcher?

The following section is based on my own experience and communications with hundreds of computer science researchers at various international conferences and various stages in their careers—from first year PhD students to prestigious computer science professors. My field of study is distributed systems.

A CS research scientist holds an advanced academic degree that’s formally required to work for a research institution such as a University. Thus, you should complete both the bachelor of science (BSc) and the master of science (MSc) in computer science or a related field to become a computer science researcher.

While the previous two steps, i.e., obtaining the BSc and MSc degrees, will be crucial on your path towards a computer science researcher, the following steps are a bit less strict.

First, it doesn’t harm to have good grades in your BSc and MSc degrees. However, you don’t need to have excellent grades for many reasons:

  • Income Potential in the Real World: Due to the high income potential of computer scientists — for example, as freelancers or highly-paid employees in Big Tech corporations — obtaining a research position in computer science is not as competitive as in other fields. When working as a PhD researcher myself a couple of years ago, I have seen many computer science researchers with average grades.
  • Variance of Quality Requirements in Universities: There are many top-notch Universities for computer science research such as Stanford or MIT with strong brands and outstanding researchers. It is very tough and potentially expensive to enter those Universities — even as a student. However, there are many 2nd and 3rd tier Universities that have much more demand for computer science researchers than supply. Getting a research position in a 2nd tier University may be as simple as applying for it after your successful completion of MSc in a computer science related field.
  • MSc Grades may not be available yet. When applying for a CS researching position, you often do so as a master of science student. This means that you don’t even know your final grade for the MSc degree which makes this grade irrelevant for your application as a CS researcher. Even if the grade may be available in your particular case, it is not for many other researchers so it’s not a must-have generally.

Second, any academic project (e.g., a published research paper) or even a writing project (e.g., having written a couple of online articles as a freelancer) can help you land the research position you desire. But again, that’s not a requirement and it will only increase your odds — all things being equal.

You can find many computer science courses on the Finxter Computer Science Academy (flatrate model).

But don’t wait too long to acquire practical experience!

Even if you have little skills, it’s best to get started as a freelance developer and learn as you work on real projects for clients — earning income as you learn and gaining motivation through real-world feedback.

🚀 Tip: An excellent start to turbo-charge your freelancing career (earning more in less time) is our Finxter Freelancer Course. The goal of the course is to pay for itself!

Let’s have a look at the income potential of a computer science researcher next.

Annual Income

How much does a Computer Science Researcher make per year?

The median annual income (=50th percentile) of a computer science researcher was $131,490 in May 2021. The bottom 10% (=10th percentile) of computer science researchers earned less than $74,210 and the top 10% (=90th percentile) earned more than $208,000.

Source: Bureau of Labor Statistics

The source also lists the income for different industries as a computer science researcher:

Computer Science Researcher Industry Annual Income
Private computer systems design and related services $161,870
Corporate software publishers and software houses $152,940
Research and development (R&D) in physical sciences, engineering, and life sciences $132,810
Federal government $112,310
Public and private universities, colleges, and professional schools $79,510
Table: Income of computer science researchers by industry.

Learnings: You make the least amount of money when working as a researcher at University—even though it can open the door to lucrative six-figure opportunities in the private sector (e.g., working in the R&D team for BigTech corporations such as Google, Facebook, and Netflix).

Let’s have a look at the hourly rate of Computer Science Researchers next!

Hourly Rate

Computer Science Researchers are well-paid on freelancing platforms such as Upwork or Fiverr.

If you decide to go the route as a freelance Computer Science Researcher, you can expect to make between $30 and $150 per hour on Upwork (source). Assuming an annual workload of 2000 hours, you can expect to make between $60,000 and $300,000 per year.

This data is consistent with the official Bureau of Labor statistics that lists the median pay of a computer science researcher at $63 per hour (annualized income of $126,000).

⚡ Note: Do you want to create your own thriving coding business online? Feel free to check out our freelance developer course — the world’s #1 best-selling freelance developer course that specifically shows you how to succeed on Upwork and Fiverr!

Industry Demand

But is there enough demand? Let’s have a look at Google trends to find out how interest evolves over time (source):

After an initial peak in 2004, the search trend has remain stable over the last two decades.

You can find more job descriptions for coders, programmers, and computer scientists in our detailed overview guide:

The following statistic shows the self-reported income from 9,649 US-based professional developers (source).

💡 The average annual income of professional developers in the US is between $70,000 and $177,500 for various programming languages.

Question: What is your current total compensation (salary, bonuses, and perks, before taxes and deductions)? Please enter a whole number in the box below, without any punctuation. If you are paid hourly, please estimate an equivalent weekly, monthly, or yearly salary. (source)

The following statistic compares the self-reported income from 46,693 professional programmers as conducted by StackOverflow.

💡 The average annual income of professional developers worldwide (US and non-US) is between $33,000 and $95,000 for various programming languages.

Here’s a screenshot of a more detailed overview of each programming language considered in the report:

Here’s what different database professionals earn:

Here’s an overview of different cloud solutions experts:

Here’s what professionals in web frameworks earn:

There are many other interesting frameworks—that pay well!

Look at those tools:

Okay, but what do you need to do to get there? What are the skill requirements and qualifications to make you become a professional developer in the area you desire?

Let’s find out next!

General Qualifications of Professionals

StackOverflow performs an annual survey asking professionals, coders, developers, researchers, and engineers various questions about their background and job satisfaction on their website.

Interestingly, when aggregating the data of the developers’ educational background, a good three quarters have an academic background.

Here’s the question asked by StackOverflow (source):

Which of the following best describes the highest level of formal education that you’ve completed?

However, if you don’t have a formal degree, don’t fear! Many of the respondents with degrees don’t have a degree in their field—so it may not be of much value for their coding careers anyways.

Also, about one out of four don’t have a formal degree and still succeeds in their field! You certainly don’t need a degree if you’re committed to your own success!

Freelancing vs Employment Status

The percentage of freelance developers increases steadily. The fraction of freelance developers has already reached 11.21%!

This indicates that more and more work will be done in a more flexible work environment—and fewer and fewer companies and clients want to hire inflexible talent.

Here are the stats from the StackOverflow developer survey (source):

Do you want to become a professional freelance developer and earn some money on the side or as your primary source of income?

Resource: Check out our freelance developer course—it’s the best freelance developer course in the world with the highest student success rate in the industry!

Other Programming Languages Used by Professional Developers

The StackOverflow developer survey collected 58000 responses about the following question (source):

Which programming, scripting, and markup languages have you done extensive development work in over the past year, and which do you want to work in over the next year?

These are the languages you want to focus on when starting out as a coder:

And don’t worry—if you feel stuck or struggle with a nasty bug. We all go through it. Here’s what SO survey respondents and professional developers do when they’re stuck:

What do you do when you get stuck on a problem? Select all that apply. (source)

To get started with some of the fundamentals and industry concepts, feel free to check out these articles:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Resources

Posted on Leave a comment

PHP CURL Post and Get request with example

by Vincy. Last modified on May 6th, 2022.

PHP cURL is a library that allows clients to access a remote server via a URL. It sends HTTP requests to the endpoint from a different application or component.

It allows inter-application hits to get a response over the network. This mechanism is useful to work with PHP RESTful services, API interactions, and etc.

There are many use case scenarios where PHP cURL post is exactly suited. For example,

  1. Extracting content from a webpage.
  2. Preparing feed from external sources.
  3. SDK-free API’s direct access methods.

This quick example gives a straightforward code to implement a PHP cURL post.

Quick example


<?php
$postParameter = array( 'name' => 'Jane', 'dateOfBirth' => '1974-8-17'
); $curlHandle = curl_init('http://domain-name/endpoint-path');
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postParameter);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); $curlResponse = curl_exec($curlHandle);
curl_close($curlHandle); 

Apart from this, we will see more use case examples of PHP cURL post requests in the upcoming sections.

Part 1 – Basics of PHP cURL

The following are the steps to perform a basic PHP cURL request-response cycle.

  • Initialize cURL session.
  • Set cURL options.
  • Execute request.
  • Close session.

How to configure PHP cURL?

PHP contains libcurl library to let the environment work with cURL. This library will be enabled by default.

If not, do the following steps to enable PHP cURL module in your environment.

  1. Open PHP configuration file php.ini
  2. Check for the extension=php_curl.dll initiation.
  3. Remove the semicolon (;) at the beginning of the above line.
  4. Restart the Apache server.

Set PHP cURL POST requests – Alternate methods

There are many ways to send PHP cURL post parameters.

  1. JSON format.
  2. HTTP query string.
  3. POST array format.

JSON format:

 <?php curl_setopt($ch, CURLOPT_POSTFIELDS, "{key1:value1,key2:value2}");
?>

HTTP query string:

 <?php curl_setopt($ch, CURLOPT_POSTFIELDS, "key1=value1&key2=value2"); ?>

PHP cURL POST array format

The CURLOPT_POSTFIELDS may have a PHP array to pass the parameters to the endpoint.

 <?php curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'); curl_setopt($ch, CURLOPT_POSTFIELDS, array("key1"=>"value1", "key2"=>"value2"); ?>

Set cURL header options

To set PHP cURL header, the CURLOPT_HTTPHEADER constant is used. A cURL header can have more information. The following keys are some of the examples to add PHP cURL header options.

  • Accept-Encoding
  • Cache-Control
  • Host
  • Content-Type
  • Accept-Language
  • User-Agent

This program sets the cURL header options to set the content type. There are options to send custom headers also. It is to send non-standard key-value pairs. Use prefix X- to send non-standard headers. Example,


curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-key: value'
));

The CURLOPT_HEADER constant is set with boolean true. It is for allowing the header information attached with the return response.


<?php
$url = "http://domain-name/endpoint-path"; $curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "X-Custom-Header: header-value", "Content-Type: application/json"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
curl_close($curl);
echo $response; 

Part 2 – Example use cases

With some basic knowledge, it will be easy to understand the following examples. It deals with some of the use case scenarios of PHP cURL post or get request methods.

HTTP POST form data

PHP cURL allows posting parameters to the server. It uses any one of the methods we discussed earlier to post parameters via cURL.

The following cURL script shows how to post an array to an endpoint URL. The CURLOPT_POST and the CURLOPT_POSTFIELDS are to send the values via PHP cURL post.


<?php
$url = 'http://domain-name/endpoint-path'; $curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $data = "name=jane&age=23"; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $result = curl_exec($curl);
curl_close($curl);
?>

PHP cURL POST to upload file

It is also possible to upload files to the server via PHP cURL post. The below code shows how to upload an image file to the server.

It prepares the object with the file data. It uses PHP curl_file_create() function to prepare the file post content.

By sending the ‘fileParam’ bundle in this way, the endpoint code can access it via $_FILES[] array.


<?php
$url = 'https://domain-name/path-to-endpoint/php-curl-post-file-endpoint.php'; if (function_exists('curl_file_create')) { $fileContent = curl_file_create("cherry.jpeg", 'image/jpeg');
} else { $fileContent = '@' . realpath("cherry.jpeg", 'image/jpeg');
} $data = array('fileParam'=> $fileContent); $curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST,true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result=curl_exec ($curl);
curl_close ($curl); print $result;
?>

Put the following endpoint code in the server. Then hit the endpoint via the above cURL script. The PHP curl post request sends the file input to this endpoint. This PHP code accesses the uploaded file from the $_FILES array.

php-curl-post-file-endpoint.php


<?php
$targetDir = 'uploads';
if ($_FILES["fileParam"]["tmp_name"] != "") { $tmp_name = $_FILES["fileParam"]["tmp_name"]; // basename() may prevent filesystem traversal attacks; // further validation/sanitation of the filename may be appropriate $name = basename($_FILES["fileParam"]["name"]); if(move_uploaded_file($tmp_name, $targetDir . "/" . $name)) { print "Image uploaded."; } else { print "Image upload failed."; } }
?>

HTTP GET request to grab a webpage

In the cURL request, the default method is GET. This program calls the server via cURL with the default GET request method.

Unlike PHP cURL POST, it sends data as the query string. To pass parameters to a GET request, it should be built as part of the URL.

It grabs the HTML of the website specified as the cURL endpoint. It prints the response and renders the target website’s HTML in the browser.


<?php
$url = 'http://domain-name/endpoint-path'; $curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl);
curl_close($curl);
print $response; 

Grab website HTML via cURL and write to a file

Instead of printing the website layout to the browser, it can also be written into a file.

This code creates a filehandle and writes the cURL HTML response into a file. It uses the file handle as the reference.

It will be useful if you want to download and save the website HTML into the server permanently.


<?php
$url = 'http://domain-name/endpoint-path';
$file = fopen("site-content.html", "w"); $curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $file); curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false); curl_exec($curl);
curl_close($curl);
fclose($file); 

The PHP file_get_contents() function is also used to grab the content of the target URL.

But, the server configuration should allow reading the content by using this method.

PHP CURL post and receive JSON data

This example shows how to send a PHP cURL post in JSON format. It also receives the cURL response in the format of JSON.

This code guides creating API services to get requests and send responses in JSON format.


<?php
$url = 'https://domain-name/path/php-curl-post-endpoint-json.php';
$data = array( "first_name" => "Jane", "last_name" => "Mclane", "email" => "jane_mc@gmail.com", "addresses" => array( "address1" => "21/A", "city" => "Los Angels", "country" => "USA", "phone" => "555-1212", "pincode" => "82312" )
);
$encodedData = json_encode($data);
$curl = curl_init($url);
$data_string = urlencode(json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedData);
$result = curl_exec($curl);
curl_close($curl);
print $result; 

This code prepares the JSON response by setting the content-type using PHP header(). It sets the application/json as the content type.

php-curl-post-endpoint-json.php


<?php
header("Content-Type:application/json");
$data = file_get_contents('php://input');
print $data; 

Handle redirects (HTTP 301,302)

The CURLOPT_FOLLOWLOCATION is set to true to perform the 3XX redirect via PHP cURL.

During the redirect, the cURL will send a GET request on successive redirects. To change this, the CURLOPT_POSTREDIR has to be set.

This program sets CURL_REDIR_POST_ALL to send PHP cURL POST requests on successive attempts.

It limits the number of redirects by using the CURLOPT_MAXREDIRS constant.


<?php
$url = 'http://domain/path';
$data = array( "first_name" => "Jane", "last_name" => "Mclane"
);
$encodedData = json_encode($data);
$curl = curl_init($url);
$data_string = urlencode(json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedData); curl_setopt($curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl,CURLOPT_MAXREDIRS, 3);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
print "<PRE>";
print_r($info);
print_r($result); 

This program will return more information as shown below.

  • Redirects count.
  • Time to redirect.
  • A header with the 3XX status.

php curl post 3xx redirect

Writing cURL error logs into a file

Keeping logs is a best practice for audit purposes. When the site is live, sometimes the logs are very useful for debugging also.

Since it is a remote call, logging cURL errors into a file will help to analyze and fix the issue.

This code guides how to log the error that occurred during the PHP cURL post. It uses PHP curl_error() function to


<?php
$logFileHandle = fopen("log/curl-error-log.txt", 'a+');
$curl = curl_init("http://domain_name/path");
if(curl_exec($curl) === false)
{ $date = date("m/d/Y"); $errorMessage = curl_error($curl); $curlError = $date . ' Error: ' . $errorMessage . "\n\n";
}
curl_close($curl);
fwrite($logFileHandle, $curlError);
fclose($logFileHandle);

Write cURL log using CURLOPT_STDERR constant

There is an alternate method to log the PHP cURL error into a file. The CURLOPT_STDERR writes the error message with the reference of the file handle.


<?php
$logFileHandle = fopen("log/curl-error-log.txt", 'a+');
$curl = curl_init("http://domain_name/path");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_FILE, $logFileHandle);
curl_setopt($curl, CURLOPT_STDERR, $logFileHandle);
curl_exec($curl);
curl_close($curl);
fclose($logFileHandle); 

This program will return the following output.
php curl error log

Part 3 – Creating PHP cURL script to extract images from a website

In this part of the article, we are going to create a end-to-end cURL component. It will do the following to achieve grabbing images from a webpage.

  1. Create API service to instantiate DOM to load response.
  2. Create cURL service to instantiate, configure and execute requests.
  3. Read cURL response and load it into the DOM object.
  4. Get the image source URL by accessing the DOM object.
  5. Create a photo gallery by using the PHP cURL response array.

API service class to initiate cURL and create DOM object

This GrabImageAPI class creates PHP DOMDocument instants to load the site HTML.

The constructor initiates cURL and grabs the complete HTML response of the URL. Then, it loads this response into the DOM object.

With the reference of the object, the getWebsiteImage() gets the image source URLs.

This function reads all images by using getElementsByTagName(). By iterating the image data array, it prepares the JSON bundle of image URLs.

Service/GrabImageAPI.php


<?php
namespace Phppot\Service; use \Phppot\Service\CurlService;
use DOMDocument; class GrabImageAPI
{ private $dom; public function __construct($url) { require_once __DIR__ . '/CurlService.php'; $curlService = new CurlService($url); $siteHTML = $curlService->executeCurl(); $this->dom = new DOMDocument(); @$this->dom->loadHTML($siteHTML); } function getWebsiteImage() { // Parse DOM to get Images $images = $this->dom->getElementsByTagName('img'); $imageSourceURL = array(); for ($i = 0; $i < $images->length; $i ++) { $image = $images->item($i); $src = $image->getAttribute('src'); if(filter_var($src, FILTER_VALIDATE_URL)) { $imageSourceURL[] = $src; } } $imageSourceJSON = json_encode($imageSourceURL); return $imageSourceJSON; }
} 

Create cURL service to perform routine life cycle operations

This class is nothing but for performing basic curl operations we have seen at the beginning.

The GrabImageAPI constructor includes this service and creates the cURL instance.

Service/CurlService.php


<?php
namespace Phppot\Service; class CurlService
{ private $curl; private $endpoint; private $response; function __construct($url) { $this->endpoint = $url; $this->curl = curl_init($this->endpoint); } function setCurlOption() { curl_setopt($this->curl, CURLOPT_HEADER, 0); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); } function executeCurl() { $this->setCurlOption(); $this->response = curl_exec($this->curl); curl_close($this->curl); return $this->response; }
}

Trigger API to grab images via PHP cURL post

This code will hit the API to grab images via PHP cURL post. It requires the API class reference on which it creates the dynamic image gallery using cURL.

This code is useful to create a gallery widget for your external shop independently.

php-curl-grab-image.php


<?php
namespace Phppot; use \Phppot\Service\GrabImageAPI; require_once __DIR__ . '/Service/GrabImageAPI.php';
$url = "https://domain-name-here/";
$imageSourceArray = array();
try { // Call image grab PHP cURL script $grabImageAPI = new GrabImageAPI($url); // Get image source array in JSON format via PHP cURL $imageSource = $grabImageAPI->getWebsiteImage(); $imageSourceArray = json_decode($imageSource, true);
} catch (Exception $e) { // Handle API request failure $statusMsg = $e->getMessage(); print $statusMsg; exit;
} // Iterate response and form image gallery in UI
foreach($imageSourceArray as $imageSource) { ?> <img src="<?php echo $imageSource; ?>" style="width: 300px; margin: 20px;" /> <?php } 

Conclusion

Hope this article helps you to know a deep knowledge about PHP cURL post and other request methods.

The short and end-to-end examples might be useful to create a cURL component for your application.

I welcome your comments to continue giving more value-adds to the learners.
Download

↑ Back to Top

Posted on Leave a comment

Pandas DataFrame Methods [Cheat Sheet]

Rate this post

The following table provides you with an overview of Pandas DataFrame methods — and where you can learn more about the specific method.

ALL LINKS OPEN IN A NEW TAB!

abs()
all()
any()
clip() https://blog.finxter.com/pandas-dataframe-clip-method/
corr() https://blog.finxter.com/pandas-dataframe-corr-method/
corrwith() https://blog.finxter.com/a-simple-recommendation-system-with-pandas/
count() https://blog.finxter.com/pandas-dataframe-count-method/
cov() https://blog.finxter.com/pandas-dataframe-cov-method/
cummax() https://blog.finxter.com/pandas-dataframe-cummax-method/
cummin() https://blog.finxter.com/pandas-dataframe-cummin-method/
cumprod() https://blog.finxter.com/pandas-dataframe-cumprod-method/
cumsum() https://blog.finxter.com/pandas-dataframe-cumsum-method/
describe() https://blog.finxter.com/pandas-dataframe-describe-method/
diff() https://blog.finxter.com/pandas-dataframe-diff-method/
eval() https://blog.finxter.com/pandas-dataframe-eval-method/
kurtosis() https://blog.finxter.com/pandas-dataframe-kurtosis-method/
mad() https://blog.finxter.com/pandas-dataframe-mad-method/
min() https://blog.finxter.com/pandas-dataframe-min-method/
max() https://blog.finxter.com/pandas-dataframe-max-method/
mean() https://blog.finxter.com/pandas-dataframe-mean-method/
median() https://blog.finxter.com/pandas-dataframe-median-method/
mode() https://blog.finxter.com/pandas-dataframe-mode-method/
pct_change() https://blog.finxter.com/pandas-dataframe-pct_change-method/
quantile() https://blog.finxter.com/pandas-dataframe-quantile-method/
rank() https://blog.finxter.com/pandas-dataframe-rank-method/
round() https://blog.finxter.com/pandas-dataframe-round-method/
prod() and product() https://blog.finxter.com/pandas-dataframe-prod-and-product-method/
add_prefix() https://blog.finxter.com/pandas-dataframe-add_prefix-method/
add_suffix() https://blog.finxter.com/pandas-dataframe-add_suffix-method/
align() https://blog.finxter.com/pandas-dataframe-align-method/
at_time() https://blog.finxter.com/pandas-dataframe-at_time-method/
between_time() https://blog.finxter.com/pandas-dataframe-between_time-method/
drop() https://blog.finxter.com/pandas-dataframe-drop-method/
drop_duplicates() https://blog.finxter.com/pandas-dataframe-drop_duplicates-method/
duplicated() https://blog.finxter.com/pandas-dataframe-duplicated-method/
equals() https://blog.finxter.com/pandas-dataframe-equals-method/
filter() https://blog.finxter.com/pandas-dataframe-filter-method/
first() https://blog.finxter.com/pandas-dataframe-first-method/
last() https://blog.finxter.com/pandas-dataframe-last-method/
head() and tail() https://blog.finxter.com/pandas-dataframe-head-method/
reset_index() https://blog.finxter.com/pandas-dataframe-reset_index-method/
sample() https://blog.finxter.com/pandas-dataframe-sample-method/
set_axis() https://blog.finxter.com/pandas-dataframe-set_axis-method/
set_index() https://blog.finxter.com/pandas-dataframe-set_index-method/
take() https://blog.finxter.com/pandas-dataframe-take-method/
truncate() https://blog.finxter.com/pandas-dataframe-truncate-method/
backfill() and bfill() https://blog.finxter.com/pandas-dataframe-backfill-and-bfill-method/
fillna() https://blog.finxter.com/pandas-dataframe-fillna-method/
dropna() https://blog.finxter.com/pandas-dataframe-dropna-method/
interpolate() https://blog.finxter.com/pandas-dataframe-interpolate-method/
isna() and isnull() https://blog.finxter.com/pandas-dataframe-isna-and-isnull-method/
notna() and notnull() https://blog.finxter.com/pandas-dataframe-notna-and-notnull-method/
pad() https://blog.finxter.com/pandas-dataframe-pad-method/
replace() https://blog.finxter.com/pandas-dataframe-replace-method/
drop_level() https://blog.finxter.com/pandas-dataframe-drop_level-method/
pivot() https://blog.finxter.com/pandas-dataframe-pivot-method/
pivot_table() https://blog.finxter.com/pandas-dataframe-pivot_table-method/
reorder_levels() https://blog.finxter.com/pandas-dataframe-reorder_levels-method/
sort_values() https://blog.finxter.com/pandas-dataframe-sort_values-method/
sort_index() https://blog.finxter.com/pandas-dataframe-sort_index-method/
nlargest() https://blog.finxter.com/pandas-dataframe-nlargest-method/
nsmallest() https://blog.finxter.com/pandas-dataframe-nsmallest-method/
swap_level() https://blog.finxter.com/pandas-dataframe-swap_level-method/
stack() https://blog.finxter.com/pandas-dataframe-stack-method/
unstack() https://blog.finxter.com/pandas-dataframe-unstack-method/
swap_axes() https://blog.finxter.com/pandas-dataframe-swap_axes-method/
melt() https://blog.finxter.com/pandas-dataframe-melt-method/
explode() https://blog.finxter.com/pandas-dataframe-explode-method/
squeeze() https://blog.finxter.com/pandas-dataframe-squeeze-method/
to_xarray() https://blog.finxter.com/pandas-dataframe-to_xarray-method/
t() and transpose() https://blog.finxter.com/pandas-dataframe-t-and-transpose-method/
append() https://blog.finxter.com/pandas-dataframe-append-method/
assign() https://blog.finxter.com/pandas-dataframe-assign-method/
compare() https://blog.finxter.com/pandas-dataframe-compare-method/
join() https://blog.finxter.com/pandas-dataframe-join-method/
merge() https://blog.finxter.com/pandas-dataframe-merge-method/
update() https://blog.finxter.com/pandas-dataframe-update-method/
asfreq() https://blog.finxter.com/pandas-dataframe-asfreq-method/
asof() https://blog.finxter.com/pandas-dataframe-asof-method/
shift() https://blog.finxter.com/pandas-dataframe-shift-method/
slice_shift() and tshift() https://blog.finxter.com/pandas-dataframe-slice_shift-and-tshift-method/
first_valid_index() https://blog.finxter.com/pandas-dataframe-first_valid_index-method/
last_valid_index() https://blog.finxter.com/pandas-dataframe-last_valid_index-method/
resample() https://blog.finxter.com/pandas-dataframe-resample-method/
to_period() https://blog.finxter.com/pandas-dataframe-to_period-method/
to_timestamp() https://blog.finxter.com/pandas-dataframe-to_timestamp-method/
tz_localize() https://blog.finxter.com/pandas-dataframe-tz_localize-method/
tz_convert() https://blog.finxter.com/pandas-dataframe-tz_convert-method/
plot() https://blog.finxter.com/pandas-dataframe-plot-method/
plot.area() https://blog.finxter.com/pandas-dataframe-plot-area-method/
plot.bar() https://blog.finxter.com/pandas-dataframe-plot-bar-method/
plot.barh() https://blog.finxter.com/pandas-dataframe-plot-barh-method/
plot.box() https://blog.finxter.com/pandas-dataframe-plot-box-method/
plot.density() https://blog.finxter.com/pandas-dataframe-plot-density-method/
plot.hexbin() https://blog.finxter.com/pandas-dataframe-plot-hexbin-method/
plot.hist() https://blog.finxter.com/pandas-dataframe-plot-hist-method/
plot.pie() https://blog.finxter.com/pandas-dataframe-plot-pie-method/
from_dict() https://blog.finxter.com/pandas-dataframe-from_dict-method/
to_dict() https://blog.finxter.com/pandas-dataframe-to_dict-method/
from_records() https://blog.finxter.com/pandas-dataframe-from_records-method/
to_records() https://blog.finxter.com/pandas-dataframe-to_records-method/
to_json() https://blog.finxter.com/pandas-dataframe-to_json-method/
to_pickles() https://blog.finxter.com/pandas-dataframe-to_pickle-method/
to_clipboard() https://blog.finxter.com/pandas-dataframe-to_clipboard-method/
to_html() https://blog.finxter.com/pandas-dataframe-to_html-method/
to_sql() https://blog.finxter.com/pandas-dataframe-to_sql-method/
to_csv() https://blog.finxter.com/pandas-dataframe-to_csv-method/
to_excel() https://blog.finxter.com/pandas-dataframe-to_excel-method/
to_markdown() https://blog.finxter.com/python-dataframe-to_markdown-method/
to_stata() https://blog.finxter.com/pandas-dataframe-to_stata-method/
to_hdf() https://blog.finxter.com/pandas-dataframe-to_hdf-method/
to_latex() https://blog.finxter.com/pandas-dataframe-to_latex-method/
to_xml() https://blog.finxter.com/pandas-dataframe-to_xml-method/
to_parquet() https://blog.finxter.com/pandas-dataframe-to_parquet-method/
to_feather() https://blog.finxter.com/pandas-dataframe-to_feather-method/
to_string() https://blog.finxter.com/pandas-dataframe-to_string-method/
to_bgq() https://blog.finxter.com/pandas-dataframe-to_gbq-method/
to_coo() https://blog.finxter.com/pandas-dataframe-to_coo-method/
Posted on Leave a comment

How to Check Your Internet Connection in Python?

5/5 – (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to check an Internet Connection in Python.

Problem: Given a Python program. You want to check if your computer currently has access to the Internet so you can do some follow-up work.

Example:

  • If your computer has access, you want to print "Success".
  • Otherwise, you want to print "Failure".

Specifically, how to implement the function has_connection() in the following sample code snippet?

if check_connection(): print('Success!')
else: print('Failure!')

To make it more fun, we have the following running scenario:

Let’s assume you are a Python Coder working for AllTech. Lately, they have been having issues with their internet connections. You are tasked with writing code to check the connection and return a status/error message.

💬 Question: How would we write Python code to check to see if an internet connection has been established?

We can accomplish this task by one of the following options:


Preparation

Before any data manipulation can occur, one (1) new library will require installation.

  • The Requests library allows access to its many methods and makes data manipulation a breeze!

To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.


$ pip install requests

Hit the <Enter> key on the keyboard to start the installation process.

If the installation was successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required library.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

from urllib.request import urlopen as url
import requests
import socket 

Method 1: Use urlopen()

This example uses urlopen() to establish a connection to the URL shown below. In addition, two (2) parameters are passed: a valid URL and a timeout.

try: url('https://finxter.com/', timeout=3) print('Success')
except ConnectionError as e: print(f'Failure - {e}')

This code is wrapped inside a try/except statement. When run, the code drops inside the try statement and checks to see if a connection can be established to the indicated URL. This attempt waits three (3) seconds before timing out.

Depending on the connection status, a message indicating the same is output to the terminal.

Output

Success

Method 2: Use requests.get()

This example requires the use of the requests library and uses requests.get() to establish a connection to the URL shown below. A status code returns indicating Success or Failure.

res = requests.get('https://finxter.com/')
print(res) if (res.status_code): print('Success')
else: print('f'Failure')

This code accepts a URL and attempts to establish a connection to the same. The results of this connection save to res as an object.

<Response [200]>

This object must be referenced as indicated above to retrieve the status code. Then, the appropriate message is output to the terminal depending on this code.

Output

Success

Method 3: Use a Lambda

In the methods above, we used a few lines of code to establish a connection and display the appropriate result. This one-liner accomplishes the same task in one line!

# One-Liner to Check Internet Connection:
print((lambda a: 'Success' if 0 == a.system('ping finxter.com -w 4 > clear') else 'Failure')(__import__('os')))

This code pings the shown URL and, depending on the results, outputs the appropriate message to the terminal. The remarkable thing is how you can import a library on-the-fly!

Output

Success

Method 4: Use socket

This example requires the socket library and creates a function to establish a connection to the URL shown below. A Boolean value returns indicating True/False.

def check_connection(): try: host = socket.gethostbyname('www.google.com') s = socket.create_connection((host, 80), 2) return True except: return False res = check_connection()
print(res)

This code defines a new function, check_connection. Using a try/except statement attempts to connect to the indicated URL. Depending on the result, the function returns either True or False.

Finally, the function is called, the code runs, and the result outputs to the terminal.

Output

True

Summary

These four (4) methods to check the internet connection should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Posted on Leave a comment

5 Easy Ways to List Imported Modules in Python

5/5 – (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to display the imported modules in Python.

As a Python Coder, you will encounter times when you need to view a list of all imported modules possessing a global or local scope. This article answers the question below.

💬 Question: How would we write Python code to display the imported modules?

We can accomplish this task by one of the following options:


Method 1: Use pip freeze

This method displays a list of all imported global module names and versions sorted, by default, in alphabetical order.

pip freeze

Navigate to the terminal window from an IDE and enter the above command. Then, hit the <Enter> key to execute. The output is sent to the terminal.

💡 Note: Your prompt may be different from the example shown above.

Output (snippet)

Your imported global module names and versions may differ from that shown below.

absl-py==1.0.0
anyio==3.5.0
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.2
asttokens==2.0.5
astunparse==1.6.3
attrs==18.2.0
Babel==2.10.1
backcall==0.2.0
beautifulsoup4==4.10.0
...
zope.interface==5.4.0]

Method 2: Use List Comprehension

This example uses the sys library with List Comprenehsion to return all imported local module names, by default, in an unsorted list.

import sys
results = [m.__name__ for m in sys.modules.values() if m]
results = sorted(results)
print(results)

This code loops through sys.modules.values() using __name__ (aka a dunder) and determines if the item is a locally scoped module. If so, the module name saves to results.

This code sorts the results variable and saves it back to itself for readability. These results are output to the terminal in list format.

Output (snippet)

Your imported local module names may differ from that shown below.

['main', '_abc', '_codecs', '_collections', '_distutils_hack', '_functools', '_imp', '_operator', '_signal', '_sitebuiltins', '_stat', '_thread', '_warnings', '_weakref', 'abc',...'zope']

Method 3: Use dir()

This example uses the dir() function to return all local module names in a sorted list format.

modules = dir()
print(modules)

The output below confirms this script displays only the names that apply to our local scope.

Output (snippet)

Your imported local module names may differ from that shown below.

['annotations', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec']

Method 4: Use inspect.getmember() and a Lambda

This example uses inspect.getmember() and a Lambda to return the imported local modules in a sorted format.

import inspect
import os
m = inspect.getmembers(os)
res = filter(lambda x: inspect.ismodule(x[1]), m) for r in res: print(r)

This code returns the names of the imported local modules and their location on the system as an iterable object. A for the loop is used to iterate through this and output one/line.

Output

('abc', <module 'abc' from 'C:\\mypythoninstall\\lib\\abc.py'>)
('path', <module 'ntpath' from 'C:\\mypythoninstall\\lib\\ntpath.py'>)
('st', <module 'stat' from 'C:\\mypythoninstall\\lib\\stat.py'>)
('sys', <module 'sys' from 'C:\\mypythoninstall\\lib\\sys.py'>)

Bonus: Count Modules

If you want to determine the total number of imported modules, use the dir() and len() functions.

count = dir()
print(len(count))

This code references the imported local modules and uses len() to determine how many are imported. The output is sent to the terminal.

Output

Your count may differ from the output below.

11

Summary

These four (4) methods to list imported modules should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Posted on Leave a comment

Can a Miner Change a Bitcoin Transaction?

Rate this post

You should always apply your own critical thinking when it comes to the crypto space. One question asked by many critical thinkers who know the overall idea of the Bitcoin protocol but not yet its technicalities is:

💬 Question: What if a miner is not trustworthy and tries to change my transaction?

  • Can the miner replace the receiver address with its own?
  • Can the miner change the transaction amount?
  • Can the miner change the sender address?

The answer to all those questions is: No. Because if you want to issue a transaction, you need to broadcast the information

(sender_public_key, receiver_public_key, amount)

But here’s the trick: you sign the transaction using the private key of the sender:

sender_private_key --> sign(sender_public_key, receiver_public_key, amount)

Everybody knows the public key of the sender because it’s included in the transaction and therefore in the block.

Knowing the public key of the sender, anybody can verify that the whole transaction was signed by the owner of the private key.

If you changed one thing in the transaction (even by 1 SAT), the signature would not fit the transaction anymore and everybody would be able to know it!

💡 Info: With public-key cryptography, robust authentication is possible. A sender can combine a message with a private key to create a short digital signature on the message. Anyone with the sender’s corresponding public key can combine that message with a claimed digital signature. If the signature matches the message, the origin of the message is verified because it must have been made by the owner of the corresponding private key. (Modified from Wikipedia)

Now, what would happen if the miner would change any of the following information?

  • sender_public_key,
  • receiver_public_key,
  • amount,

Well, the signature would not match the changed transaction, so there are two possibilities for a malicious miner:

  • The miner would now have to include the original signature in the block which would not match the changed data. Any other miner would quickly see that the transaction is invalid and reject the block from the malicious miner. Remember: the assumption is that a majority of the mining power behaves properly in the Bitcoin protocol!
  • The miner would have to calculate a new signature that fits to the changed transaction data. However, this is not possible as they don’t know the private key of the sender!

The following video does a great job explaining these details in Bitcoin:

There are some details to it that I abstracted away. For example, miners do not actually check if a transaction is valid—that’s what full nodes are here for:


ALL full nodes verify all transactions in all blocks that they receive (as well as transactions received outside of blocks). Just because a block has a valid proof of work does not mean that the block is valid. It must still build upon a valid block and must only contain valid transactions. Full nodes still verify that transactions contained within a block are valid.

Contrary to popular belief, miners do not say what transactions are valid. Their job is to determine the order of transactions, within certain constraints. It is the job of full nodes to verify transactions, and all miners (or the mining pools) should be running full nodes.

(StackOverflow)


Posted on Leave a comment

Extract File Name From the Path, No Matter What the os/path Format

Summary: os.path.basename(path) enables us to get the file name from the path, no matter what the os/path format. Another workaround is to use the ntpath module, which is equivalent to os.path.


✨Problem: How to extract the filename from a path, no matter what the operating system or path format is?

For example, let’s suppose that you want all the following paths to return demo.py:

➤ C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py
➤ /home/username/Desktop/codes/demo.py
➤ /home/username/Desktop/../demo.py

Expected Output in each case:

demo.py

Recommended: How To Get The Filename Without The Extension From A Path In Python?

Let us dive into the solutions without further delay.

Method 1: Using os.path.basename

os.path.basename is a built-in method of the os module in Python that is used to derive the basename of a file from its path. It accepts the path as an input and then returns the basename of the file. Thus, to get the filename from its path, this is exactly the function that you would want to use.

Example 1: In Windows

import os
file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py'
print(os.path.basename(file_path)) # OUTPUT: demo.py

Example 2: In Linux

Caution: If you use the os.path.basename() function on a POSIX system in order to get the basename from a Windows-styled path, for example: “C:\\my\\file.txt“, the entire path will be returned.

Tidbit: os.path.basename() method actually uses the os.path.split() method internally and splits the specified path into a head and tail pair and finally returns the tail part. 

Method 2: Using the ntpath Module

The ntpath module can be used to handle Windows paths efficiently on other platforms. os.path.basename function does not work in all the cases, like when we are running the script on a Linux host, and you attempt to process a Windows-style path, the process will fail.

This is where the ntpath module proves to be useful. Generally, the Windows path uses either the backslash or the forward-slash as a path separator. Therefore, the ntpath module, equivalent to the os.path while running on Windows, will work for all the paths on all platforms.

In case the file ends with a slash, then the basename will be empty, so you can make your own function and deal with it:

import ntpath def path_foo(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) paths = [r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py', r'/home/username/Desktop/codes/demo.py', r'/home/username/Desktop/../demo.py']
print([path_foo(path) for path in paths]) # ['demo.py', 'demo.py', 'demo.py']

Method 3: Using pathlib.Path()

If you are using Python 3.4 or above, then the pathlib.Path() function of the pathlib module is another option that can be used to extract the file name from the path, no matter what the path format. The method takes the whole path as an input and extracts the file name from the path and returns the file name.

from pathlib import Path
file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py'
file_name = Path(file_path).name
print(file_name) # demo.py

Note: The .name property followed by the pathname is used to return the full name of the final child element in the path, regardless of whatever the path format is and regardless of whether it is a file or a folder.

💡Bonus Tip: You can also use Path("File Path").stem to get the file name without the file extension.

Example:

from pathlib import Path
file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py'
file_name = Path(file_path).stem
print(file_name) # demo

Method 4: Using split()

If you do not intend to use any built-in module to extract the filename irrespective of the OS/platform in use, then you can simply use the split() method.

Example:

import os
file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py'
head, tail = os.path.split(file_path)
print(tail) # demo.py

Explanation: In the above example os.path.split() method is used to split the entire path string into head and tail pairs. Here, tail represents/stores the ending path name component, which is the base filename, and head represents everything that leads up to that. Therefore, the tail variable stores the name of the file that we need.

A Quick Recap to split():
split() is a built-in method in Python that splits a string into a list based on the separator provided as an argument to it. If no argument is provided, then by default, the separator is any whitespace.

Learn more about the split() method here.

Alternatively, for more accurate results you can also use a combination of the strip() and split() methods as shown below.

file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py'
f_name = file_path.strip('/').strip('\\').split('/')[-1].split('\\')[-1]
print(f_name)
# demo.py

Explanation: The strip method takes care of the forward and backward slashes, which makes the path string fullproof against any OS or path format, and then the split method ensures that the entire path string is split into numerous strings within a list. Lastly, we will just return the last element from this list to get the filename.

Method 5: Using Regex

If you have a good grip on regular expressions then here’s a regex specific solution for you that will most probably work on any OS.

import re
file_path = r'C:\Users\SHUBHAM SAYON\Desktop\codes\\'
def base_name(path): basename = re.search(r'[^\\/]+(?=[\\/]?$)', path) if basename: return basename.group(0) paths = [r'C:\Users\SHUBHAM SAYON\Desktop\codes\demo.py', r'/home/username/Desktop/codes/demo.py', r'/home/username/Desktop/../demo.py']
print([base_name(path) for path in paths]) # ['demo.py', 'demo.py', 'demo.py']

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Conclusion

To sum thungs up, you can use one of the following methods to extract the filename from a given path irrespective of the OS/path format:

  • os.path.basename('path')
  • ntpath.basename()
  • pathlib.Path('path').name
  • os.path.split('path')
  • using regex

Please stay tuned and subscribe for more interesting articles!


To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members:

Posted on Leave a comment

How to Measure Elapsed Time in Python?

Summary: You can evaluate the execution time of your code by saving the timestamps using time.time() at the beginning and the end of your code. Then, you can find the difference between the start and the end timestamps that results in the total execution time.


Problem: Given a Python program; how will you measure the elapsed time ( the time taken by the code to complete execution)?

Consider the following snippet:

import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x p = perimeter(8)
print("Perimeter: ", p)
a = area(8)
print("Area: ", a)
  • Challenges:
    • How will you find the time taken by each function in the above program to execute?
    • How will you compute the total time elapsed by the entire code?

Tidbit: sleep() is a built-in method of the time module in Python that is used to delay the execution of your code by the number of seconds specified by you.

Now, let us conquer the given problem and dive into the solutions.

Method 1: Using time.time()

time.time() is a function of the time module in Python that is used to get the time in seconds since the epoch. It returns the output, i.e., the time elapsed, as a floating-point value.

The code:

import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.time() start = time.time()
p = perimeter(8)
end = time.time()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.time()
a = area(8)
end = time.time()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.time()
print("Total time elapsed: ", end - begin)

Output:

Perimeter: 32
Time Taken by Perimeter(): 5.0040647983551025
Area: 64
Time Taken by area(): 2.0023691654205322
Total time elapsed: 7.006433963775635

Approach:
➤ Keep track of the time taken by each function by saving the time stamp at the beginning of each function with the help of a start variable and using the time() method.
➤ Similarly, the end time, i.e., the timestamp at which a function completes its execution, is also tracked with the help of the time() function at the end of each function.
➤ Finally, the difference between the end and the start time gives the total time taken by a particular function to execute.
➤ To find the total time taken by the entire program to complete its execution, you can follow a similar approach by saving the time stamp at the beginning of the program and the time stamp at the end of the program and then find their difference.

Discussion: If you are working on Python 3.3 or above, then another option to measure the elapsed time is perf_counter or process_time, depending on the requirements. Prior to Python 3.3, you could have used time.clock, however, it has been currently deprecated and is not recommended.

Method 2: Using time.perf_counter()

In Python, the perf_counter() function from the time module is used to calculate the execution time of a function and gives the most accurate time measure of the system. The function returns the system-wide time and also takes the sleep time into account.

import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.perf_counter() start = time.perf_counter()
p = perimeter(8)
end = time.perf_counter()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.perf_counter()
a = area(8)
end = time.perf_counter()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.perf_counter()
print("Total time elapsed: ", end - begin)

Output:

Perimeter: 32
Time Taken by perimeter(): 5.0133558
Area: 64
Time Taken by are(): 2.0052768
Total time elapsed: 7.0189293

Caution: The perf_counter() function not only counts the time elapsed along with the sleep time, but it is also affected by other programs running in the background on the system. Hence, you must keep this in mind while using perf_counter for performance measurement. It is recommended that if you utilize the perf_counter() function, ensure that you run it several times so that the average time would give an accurate estimate of the execution time.

Method 3: Using time.process_time()

Another method from the time module used to estimate the execution time of the program is process_time(). The function returns a float value containing the sum of the system and the user CPU time of the program. The major advantage of the process_time() function is that it does not get affected by the other programs running in the background on the machine, and it does not count the sleep time.

import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = time.process_time() start = time.process_time()
p = perimeter(8)
end = time.process_time()
print("Perimeter: ", p)
print("Time Taken by perimeter(): ", end - start) start = time.process_time()
a = area(8)
end = time.process_time()
print("Area: ", a)
print("Time Taken by area(): ", end - start) end = time.process_time()
print("Total time elapsed: ", end - begin)

Output:

Perimeter: 32
Time Taken by perimeter(): 5.141000000000173e-05
Area: 64
Time Taken by area(): 4.1780000000005146e-05
Total time elapsed: 0.00029919000000000473

Method 4: Using Timeit Module

timeit is a very handy module that allows you to measure the elapsed time of your code. A major advantage of using the timeit module is its ability to measure and execute lambda functions by specifying the number of executions.

Note: The timeit module turns off the garbage collection process temporarily while calculating the execution time.

Let us dive into the different methods of this module to understand how you can use it to measure execution time within your code.

4.1 Using timeit.timeit()

Example 1: In the following example, we will have a look at a lambda function being executed with the help of the timeit module such that we will be specifying the number of times this anonymous function will be executed and then calculate the time taken to execute it.

import timeit count = 1 def foo(x): global count print(f'Output for call{count} = {x * 3}') count += 1 a = timeit.timeit(lambda: foo(8), number=3)
print("Time Elapsed: ", a)

Output:

Output for call1 = 24
Output for call2 = 24
Output for call3 = 24
Time Elapsed: 6.140000000000312e-05

Explanation: After importing the timeit module, you can call the lambda function within the timeit.timeit() function as a parameter and also specify the number of times the function will be called with the help of the second parameter, i.e., number. In this case, we are calling the lambda function three times and printing the output generated by the function every time. Finally, we displayed the total time elapsed by the function.

4.2 Using timeit.repeat

Even though the above method allowed us to calculate the execution time of a lambda function, it is not safe to say that the value evaluated by the timeit() function was accurate. To get a more accurate result, you can record multiple values of execution time and then find their mean to get the best possible outcome. This is what timeit.repeat() function allows you to do.

Example:

import timeit count = 1 def foo(x): global count print(f'Output for call{count} = {x * 3}') count += 1 a = timeit.repeat(lambda: foo(8), number=1, repeat=3)
print(a)
s = 0
for i in a: s = s + i
print("Best Outcome: ", s)

Output:

Output for call1 = 24
Output for call2 = 24
Output for call3 = 24
[5.160000000001275e-05, 1.3399999999996748e-05, 1.0399999999993748e-05]
Best Outcome: 7.540000000000324e-05

4.3 Using timeit.default_timer()

Instead of using timeit.timeit() function, we can also use the timeit.default_timer(), which is a better option as it provides the best clock available based on the platform and Python version you are using, thereby generating more accurate results. Using timeit.default_timer() is quite similar to using time.time().

Example:

import timeit
import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = timeit.default_timer() start = timeit.default_timer()
p = perimeter(8)
end = timeit.default_timer()
print("Perimeter: ", p)
print("Time Taken by Perimeter(): ", end - start) start = timeit.default_timer()
a = area(8)
end = timeit.default_timer()
print("Area: ", a)
print("Time Taken by Perimeter(): ", end - start) end = timeit.default_timer()
print("Total time elapsed: ", end - begin)

Output:

Perimeter: 32
Time Taken by Perimeter(): 5.0143883
Area: 64
Time Taken by Perimeter(): 2.0116591
Total time elapsed: 7.0264410999999996

Methdo 5: Using datetime.datetime.now()

The elapsed time can also be calculated using the DateTime.datetime.now() function from the datetime module in Python. The output of the method is represented as days, hours, and minutes. However, the disadvantage of this method is that it is slower than the timeit() module since calculating the difference in time is also included in the execution time.

Example:

import datetime
import time def perimeter(x): time.sleep(5) return 4 * x def area(x): time.sleep(2) return x * x begin = datetime.datetime.now() start = datetime.datetime.now()
p = perimeter(8)
end = datetime.datetime.now()
print("Perimeter: ", p)
print("Time Taken by Perimeter(): ", end - start) start = datetime.datetime.now()
a = area(8)
end = datetime.datetime.now()
print("Area: ", a)
print("Time Taken by Perimeter(): ", end - start) end = datetime.datetime.now()
print("Total time elapsed: ", end - begin)

Output:

Perimeter: 32
Time Taken by Perimeter(): 0:00:05.003221
Area: 64
Time Taken by Perimeter(): 0:00:02.011262
Total time elapsed: 0:00:07.014483

Conclusion

Thus to sum things up, you can use one of the following modules in Python to calculate the elapsed time of your code:

  • The time module
  • The timeit module
  • The datetime module

With that, we come to the end of this tutorial, and I hope you found it helpful. Please subscribe and stay tuned for more interesting articles.

Here’s a list of highly recommended tutorials if you want to dive deep into the execution time of your code and much more:

Posted on Leave a comment

How to Check if a List Has an Even Number of Elements?

Problem Formulation

Given a list in Python. How to check if the list has an even number of elements?

Examples:

  • [] --> True
  • [1] --> False
  • [1, 2] --> True
  • [1, 2, 3] --> False

Related Article:

Method 1: len() and Modulo

The most Pythonic way to check if a list has an even number of elements is to use the modulo expression len(my_list)%2 that returns 1 if the list length is odd and 0 if the list length is even. So to check if a list has an even number of elements use the expression len(my_list)%2==0.

Here’s a simple code example:

def check_even(my_list): return len(my_list)%2==0 print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False

As background, feel free to watch the following video on the modulo operator:

The length function is explained in this video and blog article:

A slight variant of this method is the following.

Method 2: len() and Modulo and bool()

To check if a list has an even number of elements, you can use the modulo expression len(my_list)%2 that returns 1 if the list length is odd and 0 if the list length is even. So to convert the even value 0 to a boolean, use the built-in bool() function around the result and invert the result, i.e., not bool(len(my_list)%2).

Here’s a simple code example:

def check_even(my_list): return not bool(len(my_list)%2) print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False

As background, you may want to look at this explainer video:

Method 3: Bitwise AND

You can use the expression len(my_list)&1 that uses the Bitwise AND operator to return 1 if the list has an even number of elements and 0 otherwise. Now, you simply convert it to a Boolean if needed using the bool() function and invert it using the not operator: not bool(len(my_list)&1).

Python’s bitwise AND operator x & y performs logical AND on each bit position on the binary representations of integers x and y. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0.

If you run x & 1, Python performs logical and with the bit sequence y=0000...001. For the result, all positions will be 0 and the last position will be 1 only if x‘s last position is already 1 which means it is odd.

After converting it using bool(), you still need to invert it using the not operator so that it returns True if the list has an even number of elements.

Here’s an example:

def check_even(my_list): return not bool(len(my_list)&1) print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False

Bitwise AND is more efficient than the modulo operator so if performance is an issue for you, you may want to use this third approach.

You may want to watch this video on the Bitwise AND operator:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!