Posted on Leave a comment

How to Initialize Multiple Variables to the Same Value in Python?

Rate this post

Summary: To initialize multiple variables to the same value in Python you can use one of the following approaches:

  • Use chained equalities as: var_1 = var_2 = value
  • Use dict.fromkeys

This article will guide you through the ways of assigning multiple variables with the same value in Python. Without further delay, let us dive into the solutions right away.

Method 1: Using Chained Equalities

You can use chained equalities to declare the variables and then assign them the required value.

Syntax: variable_1 = variable_2 = variable_3 = value 

Code:

x = y = z = 100
print(x)
print(y)
print(z)
print("All variables point to the same memory location:")
print(id(x))
print(id(y))
print(id(z))

Output:

100
100
100
All variables point to the same memory location:
3076786312656
3076786312656
3076786312656

It is evident from the above output that each variable has been assigned the same value and each of them point to the same memory location.

Method 2: Using dict.fromkeys

Approach: Use the dict.fromkeys(variable_list, val) method to set a specific value (val) to a list of variables (variable_list).

Code:

variable_list = ["x", "y", "z"]
d = dict.fromkeys(variable_list, 100)
for i in d: print(f'{i} = {d[i]}') print(f'ID of {i} = {id(i)}')

Output:

x = 100
ID of x = 2577372054896
y = 100
ID of y = 2577372693360
z = 100
ID of z = 2577380842864

Discussion: It is evident from the above output that each variable assigned holds the same value. However, each variable occupies a different memory location. This is on account that each variable acts as a key of the dictionary and every key in a dictionary is unique. Thus, changes to a particular variable will not affect another variable as shown below:

variable_list = ["x", "y", "z"]
d = dict.fromkeys(variable_list, 100)
print("Changing one of the variables: ")
d['x'] = 200
print(d)

Output:

{'x': 200, 'y': 100, 'z': 100}

Conceptual Read:

fromkeys() is a dictionary method that returns a dictionary based on specified keys and values passed within it as parameters.

Syntax: dict.fromkeys(keys, value)
➡ keys is a required parameter that represents an iterable containing the keys of the new dictionary.
➡ value is an optional parameter that represents the values for all the keys in the new dictionary. By default, it is None.

Example:

k = ('key_1', 'key_2', 'key_3')
my_dictionary = dict.fromkeys(k, 0)
print(my_dictionary) # OUTPUT: {'key_1': 0, 'key_2': 0, 'key_3': 0}

Related Question

Let’s address a frequently asked question that troubles many coders.

Problem: I tried to use multiple assignment as show below to initialize variables, but I got confused by the behavior, I expect to reassign the values list separately, I mean b[0] and c[0] equal 0 as before.

a=b=c=[0,3,5]
a[0]=1
print(a)
print(b)
print(c)

Output:

a = b = c = [0, 3, 5]
a[0] = 1
print(a)
print(b)
print(c)

But, why does the following assignment lead to a different behaviour?

a = b = c = 5
a = 3
print(a)
print(b)
print(c)

Output:

3
5
5

Question Source: StackOverflow

Solution

Remember that everything in Python is treated as an object. So, when you chain multiple variables as in the above case all of them refer to the same object. This means, a , b and c are not different variables with same values rather they are different names given to the same object.

Thus, in the first case when you make a change at a certain index of variable a, i.e, a[0] = 1. This means you are making the changes to the same object that also has the names b and c. Thus the changes are reflected for b and c both along with a.

Verification:

a = b = c = [1, 2, 3]
print(a[0] is b[0]) # True

To create a new object and assign it, you must use the copy module as shown below:

import copy
a = [1, 2, 3]
b = copy.deepcopy(a)
c = copy.deepcopy(a)
a[0] = 5
print(a)
print(b)
print(c)

Output:

[5, 2, 3]
[1, 2, 3]
[1, 2, 3]

However, in the second case you are rebinding a different value to the variable a. This means, you are changing it in-place and that leads to a now pointing at a completely different value at a different location. Here, the value being changed is an interger and integers are immutable.

Follow the given illustration to visualize what’s happening in this case:

Verification:

a = b = c = 5
a = 3
print(a is b)
print(id(a))
print(id(b))
print(id(c))

Output:

False
2329408334192
2329408334256
2329408334256

It is evident that after rebinding a new value to the variable a, it points to a different memory location, hence it now refers to a different object. Thus, changing the value of a in this case means we are creating a new object without touching the previously created object that was being referred by a, b and c.

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehensionslicinglambda functionsregular expressionsmap and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as arrayshapeaxistypebroadcastingadvanced indexingslicingsortingsearchingaggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groupsnegative lookaheadsescaped characterswhitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagramspalindromessupersetspermutationsfactorialsprime numbersFibonacci numbers, obfuscationsearching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!

Posted on Leave a comment

Polygon for Developers – A Simple Guide with Video

5/5 – (1 vote)

What are Polygon and MATIC all about and why is another blockchain needed? You will find answers to these questions in this article.

The article starts with problems plaguing Ethereum, workable solutions to the problem, and then dives into more details of the Polygon network, its history, tokenomics, and an overview of the Polygon SDK.

Ethereum Scaling Problems

To date, Ethereum remains the most widely adopted and actively used blockchain in the current scenario.

It gained popularity because of its offering of smart contract functionality, variety of tools, ecosystem, and sizable community support.

However, using Ethereum comes at a price because of its high gas fees, network clogging because of many users, and lower transactions per second (approximately ~30 tps).

Ethereum TPS

Why do transactions on Ethereum (or also Bitcoin) take so long?

The answer is in the blockchain‘s structure. In these blockchains, before we approve a transaction, it must pass through various stages – queue in the transaction pools, mining, distribution, and validation.

Ethereum, also called layer-1 or base layer, must handle all the activities before the transaction gets included in the block and the block makes it to the chain.

Inevitably, there are delays in processing which affect the user experience and scalability at Ethereum or layer-1. Compare this with centralized systems such as Paypal or Visa with a TPS value as high as ~24,000.

Solutions to Scaling

You may wonder, “why Ethereum or layer-1 itself can’t be scaled ?”.

First, the problem is that sophistication levels introduced at layer-1 to solve scaling problems mean writing more code at this layer, resulting in more time to bring improvements, new features to Ethereum, and countless discussions.

Second, more code at layer-1 means compromising security for scalability.

Scaling problems such as the one described above have multiple solutions.

As this tutorial focuses mostly on Polygon, the other scaling solutions are mentioned here only for reference. We can divide the scaling solutions mainly as On-Chain and Off-Chain and further classified as in the below fig.

Fig: Scaling Ethereum

Most of the time, the names layer-2 or side chains are applied interchangeably, but there is a difference. While layer-2 inherits the security of the Ethereum network (mainnet), sidechains rely on their own security model.

In the next section, we start with Polygon, a sidechain solution (blockchain that runs parallel to the mainnet Ethereum), and how it solves the Ethereum scaling problem.

Polygon Origins

Originally known as Matic networks, it was started in 2017, in India by Anurag Arjun, Sandeep Nailwal, Jaynti Kanani, and Mihailo Bjelic.

Initially, it offered two solutions to the Ethereum scaling problem.

  • PoS sidechain, a proof of stake Ethereum sidechain
  • Plasma chain

In 2021, it was renamed or rebranded as Polygon.

Polygon’s goal has evolved from offering Ethereum network layer-2 to developing the entire blockchain network architecture for developers, as seen by the name change.

Polygon is presently working on a roadmap for connecting numerous layer-2 solutions forming a multi-layer-2 blockchain network that can coexist with the Ethereum network.

The existing Polygon solutions PoS sidechain and the Plasma chain will continue to exist and will be an important part of the growing Polygon multichain system.

Tokenomics and dApps

MATIC is the token name offered by the Polygon Network. Polygon MATIC began as a testnet in October 2017 and then transitioned to mainnet later that year.

During its maiden offering in April 2019, the company raised $5.6 million in ETH by selling 1.9 billion MATIC tokens over the course of 20 days. They formed the Matic Network in 2020, and Matic changed its name to Polygon Network in February 2021.

AAVE debuted on the platform in April 2021, enhancing the Polygon network’s value. Other famous dApps (Defi, NFTs, and Dex) launched on Polygon include Quickswap, KogeFarm, Aavegotchi, etc.

Polygon SDK Overview

Polygon is more of a protocol than a single solution to scaling, and up to this point, the Polygon framework aims to support two types of Ethereum compatible solutions: Secured chains and Stand-alone chains.

Secured Chains

Secured chains are chains that have the potential to internalize, make use of the existing security of the Ethereum network.

It allows developers to use Polygon’s scalability while still employing the Ethereum network’s security. As secured chains don’t have their own security model, they offer better security and lesser flexibility.

Secured chains are suitable for security focussed projects and startups. An example of a secured chain is Rollups.

Stand-Alone Chains

Stand-alone chains fully define their own security model of consensus mechanisms such as Proof Of Stake (PoS) and Delegated Proof Of Stake(dPoS).

They have their own miner or validator pools.

They offer better flexibility and independence but lesser security because of their own security mechanism in place.

Stand-alone chains are suitable for enterprise blockchains and projects needing more flexibility than security. An example of Stand-alone chains is sidechains.

The Polygon SDK offering can be summarized with the picture below:

Fig: Polygon Multichain support (credit: Polygon)

Dominance Over Other Layer-2 Blockchain Solutions

Polygon offers several advantages over other blockchain ecosystems for layer-2 scaling like Polkadot (DOT), Avalanche (AVAX), and Cosmos (ATOM).

  • Polygon provides developers a fully customizable tech stack, with a user experience akin to Ethereum.
  • Polygon can use the full capacity of the Ethereum network thanks to the multi-chain technology, which allows it to do so without losing throughput or security.
  • Polygon provides blockchain speed-up solutions, allowing for faster transactions and reduced gas rates than the Ethereum mainnet. Polygon also plans to provide developers with tools for creating Ethereum-compatible blockchain networks.

Polygon is thus critical for developers and small-to-medium-sized enterprises concerned with Ethereum’s network congestion. As a long-term scaling solution, Polygon can provide a wide range of utilities for developers. Some people believe that this potential gives the MATIC token a one-of-a-kind value proposition.

Polygon For a User

Users who interact with Polygon frequently only view the blockchain as a whole, and the underlying architecture is unimportant to them.

Polygon allows any Ethereum-compatible web wallet, such as Metamask, to connect, acquire funds (MATIC), and interact with dApps deployed across the network.

For a long time, OpenSea has permitted the minting and trading of NFTs on Polygon.

Conclusion

This article discussed the Ethereum scalability issue and how the Polygon sidechain network can help solve the base-layer or layer-1 issues, as well as an overview of the Polygon SDK framework and how it compares to alternative solutions at this time.

The next posts will explore more on the Polygon networks and hands-on developing dApps on Polygon.

Nerd Humor

Oh yeah, I didn’t even know they renamed it the Willis Tower in 2009, because I know a normal amount about skyscrapers.xkcd (source)
Posted on Leave a comment

Convert CSV to List of Tuples in Python

4.5/5 – (2 votes)
Convert CSV to List of Tuples in Python

Problem Formulation

Given a CSV file (e.g., stored in the file with name 'my_file.csv').

INPUT: file 'my_file.csv'
9,8,7
6,5,4
3,2,1

Challenge: How to convert the CSV file to a list of tuples, i.e., putting the row values into the inner tuples?

OUTPUT: Python list of tuples
[(9, 8, 7), (6, 5, 4), (3, 2, 1)]

Method 1: csv.reader()

Method 1: csv.reader()

To convert a CSV file 'my_file.csv' into a list of tuples in Python, use csv.reader(file_obj) to create a CSV file reader that holds an iterable of lists, one per row. Now, use the list(tuple(line) for line in reader) expression with a generator expression to convert each inner list to a tuple.

Here’s a simple example that converts our CSV file to a nested list using this approach:

import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(tuple(line) for line in reader)

Output:

print(lst)
# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]

Method 2: One-Liner

Method 2: One-Liner

You can also convert a CSV to a list of tuples using the following Python one-liner idea:

Open the file using open(), pass the file object into csv.reader(), and convert the CSV reader object to a list using the list() built-in function in Python with a generator expression to convert each inner list to a tuple.

Here’s how that looks:

import csv; lst=list(tuple(line) for line in csv.reader(open('my_file.csv'))); print(lst)

Concise, isn’t it? 🙂

The output is the same:

[('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]

By the way: Do you need all inner elements converted to a certain data type such as integer?

If so, use the following approach on top of the previously shown list lst:

new_lst = [tuple(int(x) for x in inner) for inner in lst]

The converted new_lst now contains a list of tuples of ints:

print(new_lst)
# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]

Method 3: Pandas

Method 3: Pandas

You can convert a CSV to a list of tuples with Pandas by first reading the CSV without header line using pd.read_csv('my_file.csv', header=None) function and second converting the resulting DataFrame to a nested list using df.values.tolist(). Third, convert the nested list to a list of tuples and you’re done.

Here’s an example that converts the CSV to a Pandas DataFrame and then to a nested raw Python list and then to a list of tuples:

import pandas as pd # CSV to DataFrame
df = pd.read_csv('my_file.csv', header=None) # DataFrame to List of Lists
lst = df.values.tolist() # List of Lists to List of Tuples:
new_lst = [tuple(x) for x in lst] print(new_lst)
# [(9, 8, 7), (6, 5, 4), (3, 2, 1)]

This was easy, wasn’t it? 🙂

Of course, you can also one-linerize it by chaining commands like so:

# One-Liner to convert CSV to list of tuples:
lst = [tuple(x) for x in pd.read_csv('my_file.csv', header=None).values.tolist()]

Method 4: Raw Python No Dependency

Method 4: Raw Python No Dependency

If you’re like me, you try to avoid using dependencies if they are not needed. Raw Python is often more efficient and simple enough anyways. Also, you don’t open yourself up to unnecessary risks and complexities.

Question: So, is there a simple way to read a CSV to a list of tuples in raw Python without external dependencies?

Sure!

To read a CSV to a list of tuples in pure Python, open the file using open('my_file.csv'), read all lines into a variable using f.readlines(). Iterate over all lines, strip them from whitespace using strip(), split them on the delimiter ',' using split(','), and pass everything in the tuple() function.

You can accomplish this in a simple list comprehension statement like so:

csv_filename = 'my_file.csv' with open(csv_filename) as f: lines = f.readlines() lst = [tuple(line.strip().split(',')) for line in lines] print(lst)

Feel free to check out my detailed video in case you need a refresher on the powerful Python concept list comprehension:

🌍 Related Tutorial: Understanding List Comprehension in Python.

Become a One-Liner Wizard!

In case you enjoyed the one-liners presented here and you want to improve your Python skills, feel free to get yourself a copy of my best-selling Python book:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!

Posted on Leave a comment

How to Find Number of Digits in an Integer?

Rate this post

To find the number of digits in an integer you can use one of the following methods:
(1) Use Iteration
(2) Use str()+len() functions
(3) Use int(math.log10(x)) +1
(4) Use Recursion


Problem Formulation

  • Given: An integer value.
  • Question: Find the number of digits in the integer/number given.

Test Cases:

Input:
num = 123
Output: 3
=========================================
Input:
num = -123
Output: 3
=========================================
Input: num = 0
Output: 1 

Method 1: Iterative Approach

Approach:

  • Use the built-in abs() method to derive the absolute value of the integer. This is done to take care of negative integer values entered by the user.
  • If the value entered is 0 then return 1 as the output. Otherwise, follow the next steps.
  • Initialize a counter variable that will be used to count the number of digits in the integer.
  • Use a while loop to iterate as long as the number is greater than 0. To control the iteration condition, ensure that the number is stripped of its last digit in each iteration. This can be done by performing a floor division (num//10) in each iteration. This will make more sense when you visualize the tabular dry run of the code given below.
  • Every time the while loop satisfies the condition for iteration, increment the value of the counter variable. This ensures that the count of each digit in the integer gets taken care of with the help of the counter variable.

Code:

num = int(input("Enter an Integer: "))
num = abs(num)
digit_count = 0
if num == 0: print("Number of Digits: ", digit_count)
else: while num != 0: num //= 10 digit_count += 1 print("Number of Digits: ", digit_count)

Output:

Test Case 1:
Enter an Integer: 123
Number of Digits: 3 Test Case 2:
Enter an Integer: -123
Number of Digits: 3 Test Case 3:
Enter an Integer: 0
Number of Digits: 1

Explanation through tabular dry run:

💎Readers Digest:

Python’s built-in abs(x) function returns the absolute value of the argument x that can be an integer, float, or object implementing the __abs__() function. For a complex number, the function returns its magnitude. The absolute value of any numerical input argument -x or +x is the corresponding positive value +x. Read more here.

Method 2: Using str() and len()

Approach: Convert the given integer to a string using Python’s str() function. Then find the length of this string which will return the number of characters present in it. In this case, the number of characters is essentially the number of digits in the given number.

To deal with negative numbers, you can use the abs() function to derive its absolute value before converting it to a string. Another workaround is to check if the number is a negative number or not and return the length accordingly, as shown in the following code snippet.

Code:

num = int(input("Enter an Integer: "))
if num >= 0: digit_count = len(str(num))
else: digit_count = len(str(num)) - 1 # to eliminate the - sign
print("Number of Digits: ", digit_count)

Output:

Test Case 1:
Enter an Integer: 123
Number of Digits: 3

Test Case 2:
Enter an Integer: -123
Number of Digits: 3

Test Case 3:
Enter an Integer: 0
Number of Digits: 1

Alternate Formulation: Instead of using str(num), you can also use string modulo as shown below:

num = abs(int(input("Enter an Integer: ")))
digit_count = len('%s'%num)
print("Number of Digits: ", digit_count)

Method 3: Using math Module

Disclaimer: This approach works if the given number is less than 999999999999998. This happens because the float value returned has too many .9s in it which causes the result to round up.

Prerequisites: To use the following approach to solve this question, it is essential to have a firm grip on a couple of functions:

  1. math.log10(x) – Simply put this function returns a float value representing the base 10 logarithm of a given number.

Example:

2. int(x) – It is a built-in function in Python that converts the passed argument x to an integer value. For example, int('24') converts the passed string value '24' into an integer number and returns 24 as the output. Note that the int() function on a float argument rounds it down to the closest integer.

Example:

Approach:

  • Use the math.log(num) function to derive the base 10 logarithm value of the given integer. This value will be a floating-point number. Hence, convert this to an integer.
  • As a matter of fact, when the result of the base 10 logarithm representation of a value is converted to its integer representation, then the integer value returned will almost most certainly be an integer value that is 1 less than the number of digits in the given number.
  • Thus add 1 to the value returned after converting the base 10 logarithm value to an integer to yield the desired output.
  • To take care of conditions where:
    • Given number = 0 : return 1 as the output.
    • Given number < 0 : negate the given number to ultimately convert it to its positive magnitude as: int(math.log10(-num)).

Code:

import math
num = int(input("Enter an Integer: "))
if num > 0: digit_count = int(math.log10(num))+1
elif num == 0: digit_count = 1
else: digit_count = int(math.log10(-num))+1
print("Number of Digits: ", digit_count)

Output:

Test Case 1:
Enter an Integer: 123
Number of Digits: 3

Test Case 2:
Enter an Integer: -123
Number of Digits: 3

Test Case 3:
Enter an Integer: 0
Number of Digits: 1

Method 4: Using Recursion

Recursion is a powerful coding technique that allows a function or an algorithm to call itself again and again until a base condition is satisfied. Thus, we can use this technique to solve our question.

Code:

def count_digits(n): if n < 10: return 1 return 1 + count_digits(n / 10) num = int(input("Enter an Integer: "))
num = abs(num)
print(count_digits(num))

Output:

Test Case 1:
Enter an Integer: 123
Number of Digits: 3

Test Case 2:
Enter an Integer: -123
Number of Digits: 3

Test Case 3:
Enter an Integer: 0
Number of Digits: 1

Exercise

Question: Given a string. How will you cont the number of digits, letters, spaces and other characters in the string?

Solution:

text = 'Python Version 3.0'
digits = sum(x.isdigit() for x in text)
letters = sum(x.isalpha() for x in text)
spaces = sum(x.isspace() for x in text)
others = len(text) - digits - letters - spaces
print(f'No. of Digits = {digits}')
print(f'No. of Letters = {letters}')
print(f'No. of Spaces = {spaces}')
print(f'No. of Other Characters = {others}')

Output:

No. of Digits = 2
No. of Letters = 13
No. of Spaces = 2
No. of Other Characters = 1

Explanation: Check if each character in the given string is a digit or a letter or a space or any other character or not using built-in Python functions. In each case find the cumulative count of each type with the help of the sum() method. To have a better grip on whats happening in the above code it is essential to understand the different methods that have been used to solve the question.

  • isdigit(): Checks whether all characters in a given are digits, i.e., numbers from 0 to 9 (True or False).
  • isalpha(): Checks whether all charactersof a given string are alphabetic (True or False).
  • isspace(): Checks whether all characters are whitespaces (True or False).
  • sum(): returns the sum of all items in a given iterable.

Conclusion

We have discussed as many as four different ways of finding number of digits in an integer. We also solved a similar exercise to enhance our skills. I hope you enjoyed this question and it helped to sharpen your coding skills. Please stay tuned and subscribe for more interesting coding problems.

Recommended Read: Coding Interview Questions


Recommended: Finxter Computer Science Academy

  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.
Join the Web Scraping with BeautifulSoup Masterclass now, and master it by tomorrow!
Posted on Leave a comment

Finxter Mission – Help Increase Collective Intelligence

5/5 – (1 vote)

It’s great having you here!
The mission of Finxter is to help increase collective intelligence.

What does it mean to increase collective intelligence?

In my view, humanity it’s like a big organism. You and I are two cells of this big organism. There are communication channels between those cells — for example, video, audio, speech, etc.

We’re talking to each other now. This article is a one-way channel. But there are many two ways channels as well.

There are many types of communication and different types of interaction. For example, you may send a Tweet to your followers or a personal message to your friend via WhatsApp. Even if you just look at something with your eyes and another person observes where your attention goes, they gain a valuable piece of information. So your body communicates all the time with other people without you even realizing it.

There are many different levels of interaction, and all of us communicate with each other constantly in a never-ending stream of interaction.

But not only humans talk to other humans but also your body cells like the neurons in your brain talk with each other by means of electrical signals. These neurobiological signals carry information through the meta organism that is your brain, consisting of billions of individual cells (neurons) that exchange information via communication channels (synapses).

Together, these neurons form something bigger, your identity. You are like a meta Organism comprising the smaller cells such as your brain cells and your body cells, bacteria cells, and everything that interacts in your body.

The impulse of energy flowing through the collective organism is life itself.

Another example is the meta organism that is the forest consisting of smaller organisms from which it is built. Trees transform chemical signals in the form of CO2 to O2 which is then transformed back by humans and animals to CO2.

If the trees breathe out, humans breathe in. And as humans breathe out, trees breathe in.

Everything is like one giant organism and we’re all part of it. Together, we form a massive brain from which we are the cells. This is what I mean with collective intelligence.

We need to increase collective intelligence, i.e., our ability to solve problems such as climate change or societal problems. Eventually, the sun will destroy the earth, and we need to become a multi-planetary species in order to survive. We will become more intelligent and more capable of solving bigger and bigger problems – or we die and become extinct.

There are only two possibilities – become more intelligent or die!

An animal brain tends to be more intelligent, with a bigger brain consisting of more neurons and synapses.

Likewise, we need to increase the number of cells in our collective organism and the connection and capabilities of those cells to increase collective intelligence.

Another factor is to add synthetical cells, i.e., computers to integrate the cyber organism with our biological organism in a cyber-biological meta organism.

We are already in the midst of this process – you reading this message means that some algorithms and routing devices have decided to forward this information to you. The fact that I’m communicating with you means that algorithms have enabled us to do so.

Our communication wouldn’t be possible without them!

Consequently, we want to add coding leverage to this cyber-biological collective organism. Every cell in our collective brain should be able to also leverage itself by creating computational intelligence and injecting the computational intelligence into the system.

If we accomplish this, every cell can produce myriads of additional artificial, synthetical cells, i.e., computing devices that now increase collective intelligence even more. These computers can solve problems partially or completely autonomously, they already run without our explicit involvement.

This information was carried through the Web and touched by hundreds of routers to deliver it to your screen. No human was directly involved in carrying this information through time and space. Computing devices did it autonomously and automatically.

The goal of Finxter is to increase the capabilities of an individual cell and add leverage to it through programming.

We want to increase the number of connections between human cells.

We want to strengthen the connection and integrate them with computing intelligence.

We want to help organize them through new ways of synchronization such as Blockchain technology.
This is a long, never-ending push towards higher and higher level of collective intelligence. There’s no short-cut only optimizations.

I will never stop doing it. You and I are in it together. Everything you do impacts me and everything I do impacts you. Let’s work together in making this meta organism more intelligent and more capable. Share this, join our email academy, and become a more successful human being!

Posted on Leave a comment

How to Call an Element from a Numpy Array?

Rate this post

Problem: Given a Numpy array; how will you call an element from the given array?

Example: When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept:

Given:
my_array = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] Question: Retrieve the elements 3 and 8 from the given 2D array. Expected Output: [3 8] - The element 3 has been retrieved from row 0 and column 2.
- The element 8 has been retrieved from row 1 and column 2. 

To master the art of retrieving elements from a Numpy array, you must have a clear picture of two essential concepts –
(1) Indexing Numpy arrays
(2) Slicing Numpy Arrays

In this tutorial, we will dive into numerous examples to conquer the above concepts and thereby learn how to call Numpy array elements in a practical way.

#NOTE: Before we begin, it is extremely important to note that indexing in Python always begins from 0, meaning the first element will have the index 0, the second element will have the index 1 and so on.

Retrieving Elements from a 1D Array

To access an element from a 1D array, you simply have to refer it using its index within square brackets, i.e., arr[i] where arr is the given array and i denotes the index of the element to be accessed.

Example:

import numpy as np arr = np.array([10, 20, 30, 40, 50])
# accessing the first array element at index 0
print(arr[0])
# accessing the middle array element at index 2
print(arr[2])
# accessing the last array element at index 0
print(arr[4])
# accessing and adding first and last element
print(arr[0]+arr[4])

Output:

10
30
50
60

The above examples were a classic case of indexing 1D array elements. But what if we need to access a contiguous group of elements from the given array. This is where slicing comes into the picture.

  • Slicing allows you to access elements starting from a given index until a specified end index.
  • Syntax: arr[start:end:step]
    • If start is not specified, then it is automatically considered as 0.
    • If end is not specified, then it is automatically considered as the length of the array in that dimension.
    • If step is not specified, then it is automatically considered as 1.

Example 1: Accessing the first three elements of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:3])
# or
print(arr[:3]) # OUTPUT: [10 20 30]

Example 2: Accessing the last three elements of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[7:])
# or
print(arr[7:]) # OUTPUT: [ 80 90 100]

Example 3: Accessing every other element of a given 1D array.

import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:10:2])
# or
print(arr[::2]) # OUTPUT: [10 30 50 70 90]

Retrieving Elements from a 2D Array

To retrieve elements from a given 2D Numpy array, you must access their row and column indices using the syntax arr[i,j], where arr represents the given array, i represents the row index and j represents the column index.

Examples:

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
# accessing the 3rd element of 1st row
print(arr[0, 2])
# accessing the 1st element of the 2nd row
print(arr[1, 0])
# accessing and adding 1st element of 1st row (1) and last element of second row (10)
print(arr[0, 0] + arr[1, 4])

Output:

3
6
11

Now let us look at how we can slice 2D arrays to access contiguous elements lying within an index range.

Example 1: Accessing the first three elements from the first inner array.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0, 0:3])
# or
print(arr[0, :3]) # OUTPUT: [1 2 3]

Example 2: Accessing the last three elements of the second inner array.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 2:])
# or
print(arr[1, 2:]) # OUTPUT: [ 8 9 10]

Example 3: Access the third element from both the inner arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])
# or
print(arr[:, 2])
# or
print(arr[0:, 2])
# or
print(arr[:2, 2]) # OUTPUT: [3 8]

Example 4: Accessing middle elements from both the arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
# or
print(arr[:, 1:4])
# or
print(arr[0:, 1:4])
# or
print(arr[:2, 1:4]) # OUTPUT: [[2 3 4]
[7 8 9]]

There’s one more way to select multiple array elements from a given 2D array. Considering that you want to retrieve elements from the i-th row and j-th column, you can pack them in a tuple to specify the indexes of each element you want to retrieve.

Example:

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[(0, 1), (2, 2)]) # OUTPUT: [3 8]

Explanation: The first tuple contains the indices of the rows and the second tuple contains the indices of the columns.

Retrieving Elements from a Multi-Dimensional Array

To retrieve elements of multi-dimensional arrays, you can access the index of individual elements with the help of square bracket notation and comma-separated index values, one per axis.

As a rule of thumb: the first element in the comma-separated square bracket notation identifies the outermost axis, the second element the second-outermost axis, and so on.

Example: In the following code we will access the third element from the second array of the second dimension.

import numpy as np arr = np.array([[[100, 200, 300], [400, 500, 600]], [[700, 800, 900], [1, 2, 3]]])
print(arr[1, 1, 2]) # OUTPUT: 3

Graphical Visualization:

🖊Note: You must remember that each axis can be sliced separately. In case the slice notation is not specified for a particular axis, then the interpreter will automatically apply the default slicing (i.e., the colon :).

Accessing Elements Using Negative Indexing

You can also access elements of arrays using negative indices, starting from the end element and then moving towards the left.

Negative Indexing with 1D Arrays

Example 1: Accessing last element of a given array.

import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # OUTPUT: 50

Example 2: Accessing the last three elements of a given array.

import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-3:]) # OUTPUT: [30 40 50]

Negative Indexing with 2D Arrays

Example 1: Accessing last elements of both inner arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -1]) # OUTPUT: [ 5 10]

Example 2: Accessing last three elements of both arrays.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -3:])

Output:

[[ 3 4 5] [ 8 9 10]]

Example 3: Access all columns except the last one.

import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[:, :-1])

Output:

[[1 2 3 4] [6 7 8 9]]

💎ADVANCED READ: Learn how to conditionally select elements in a NumPy Array here:
Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?

Conclusion

Congratulations! You have successfully mastered the art of retrieving elements from arrays. We have seen numerous examples and demonstrations of selecting elements from 1D, 2D and other multi-dimensional arrays. I hope this tutorial helped you. Here’s a list of highly recommended tutorials that will further enhance your Numpy skills:


Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

Coffee Break NumPy
Posted on Leave a comment

Top 6 Developer Jobs for White-Hat Hackers in 2023

Rate this post

Hackers have a wide variety of specific skills that are super valuable in a “white hat” environment.

If you’re interested in leaving the “dark side” or you simply want to reduce your risk profile as a hacker—these could be some interesting career paths you could pursue easily without needing to learn a whole new stack of skills.

Security Engineer

A security engineer is a “white-hat hacker”, i.e., an IT professional who analyzes computer systems and computer networks to ensure they are running securely. This involves proactive analysis and understanding of possible security threats and attack vectors and designing the system to minimize the exposure to these threats.

Question: How much does a Security Engineer in the US make per year?

Figure: Average Income of a Security Engineer in the US by Source. [1]

The average annual income of a Security Engineer in the United States is between $75,732 and $144,874, with an average of $108,851 and a statistical median of $105,928 per year.

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

Network Engineer

A network engineer plans, supervises, debugs, installs, and implements computer networks. The required skill set of a network engineer includes a profound understanding of basic networking protocols such as TCP/IP and UDP, as well as basic hardware configurations such as Ethernet switches and routers.

The average annual income of a Network Engineer in the United States is between $68,000 and $104,000 with a median income of $87,000 per year according to multiple sources including Indeed, Glassdoor, and Talent.com.

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

Distributed Systems Developer

A distributed systems engineer designs, implements, and debugs distributed systems for data storage, crypto & web3, or analytics. The idea is to design a distributed system that can provide a service to users that couldn’t be provided by a centralized system (e.g., providing a decentralized, censorship-free monetary network).

How much does a Distributed Systems Engineer in the US make per year?

Average Income of a Distributed Systems Engineer in the US by Source.
Figure: Average Income of a Distributed Systems Engineer in the US by Source.

The average annual income of a Distributed Systems Engineer in the United States is between $97,000 and $169,656, with an average of $126,894 and a statistical median of $130,000 per year.

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

Bash Shell Developer

A bash developer creates scripts and software in the bash shell language. Bash is a Unix command processor that runs in a text window where the user types commands that cause actions (source).

Bash developers work in a wide range of fields such as web development, server architecture, security, databases, distributed systems, and administration.

The average annual income of a Bash Unix Shell Script Developer is $74,000 according to Ziprecruiter (source). PayScale lists an even higher annual income for a bash developer of $80,000

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

System Administrator

A system administrator (SysAdmin) is responsible for keeping a system, server, or server application running smoothly and without unexpected negative system behavior.

This involves tasks such as management, debugging, troubleshooting, licensing, and updating hardware and software systems.

A SysAdmin makes sure to proactively avoid negative system behavior such as security exploits, data loss, or system downtime.

How much does a System Administrator make per year?

Figure: Average Income of a System Administrator in the US by Source.
Figure: Average Income of a System Administrator in the US by Source. [1]

The average annual income of a System Administrator in the United States is between $75,207 and $92,500, with an average of $83,088 and a statistical median of $84,031 per year.

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

Test Automation Engineer

A Test Automation Engineer is a software developer who creates automated software tests for existing or new applications. Testing is a crucial phase in the software development cycle to learn about bugs, usability, and security issues and fix them before deploying an application in the real world.

How much does a Test Automation Engineer in the US make per year?

Average Income of a Test Automation Engineer in the US by Source.
Figure: Average Income of a Test Automation Engineer in the US by Source. [1]

The expected annual income of a Test Automation Engineer in the United States is between $74,821 and $120,000 per year, with an average annual income of $95,285 per year and a median income of $93,657 per year.

🌍 Learn More: I’ve compiled more information about this career path — income, industry trends, learning paths — in an in-depth guide on the Finxter blog here.

Conclusion

These are only some possible career paths as a white hat hacker. I’ve focused on the ones that require minimal learning time and switching costs.

But as hackers are often very proficient in programming languages and systems thinking, the opportunities are endless.

Also, feel free to check out our full guide on Python career opportunities for coders—I’m sure there are many opportunities that look exciting to you!

Posted on Leave a comment

jsPDF AutoTable example – Table to PDF

by Vincy. Last modified on June 17th, 2022.

The jsPDF AutoTables plugin is for converting a table to a PDF. This is a dependable client-side library to generate reports online in a table format.

It has many features around the PDF generation process using this library. It supports customizing the appearance by defining table column structure and styles.

In this article, we will see the capability of this plugin and its usage with examples.

The below code shows a quick example to learn how to use the jsPDF autoTable library to convert tabular data into a PDF.

It builds the options array to specify a body, start position and more to create a PDF document. It outputs a PDF document as a result and prompts to download it to the browser.

Quick example


window.onload = function() { var doc = new jsPDF('p', 'pt', 'letter') // Supply data via script var body = [ ['SL.No', 'Product Name', 'Price', 'Model'], [1, 'I-phone', 75000, '2021'], [2, 'Realme', 25000, '2022'], [3, 'Oneplus', 30000, '2021'], ] // generate auto table with body var y = 10; doc.setLineWidth(2); doc.text(200, y = y + 30, "Product detailed report"); doc.autoTable({ body: body, startY: 70, theme: 'grid', }) // save the data to this file doc.save('auto_table_with_javascript_data');
}

custom styles pdf tables

Basics of jsPDF Autotable

This plugin can receive two types of source formats for generating a standard PDF.

  1. An array of table row data to supply via script.
  2. HTML table element object to parse for converting a table to PDF.

How to integrate?

There are many ways to integrate this library into an application. Those are listed below.

  • Using npm command as like npm install jspdf jspdf-autotable
  • Download the plugin library from Github.
  • Include jsPDF and jsPDF Autotable via CDN URL.
https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.25/jspdf.plugin.autotable.min.js

Features

This list shows some of the widely used features of the jsPDF AutoTables plugin. Other than that, it has more features that can be found in the official documentation page.

  • It provides options to set table content with htmlheadbodyfoot, columns. The html and body are required among them.
  • It provides more control over table cells and columns using CellDef and ColumnDef properties.
  • It has built-in themes for tables and also allows adding custom styles.
  • It provides hooks to callback on an event basis. This callback passes the hookData reference.

Database script

The below SQL script is used for having the backend data for the PDF table source. Import this SQL before running the AutoTable examples using database data to Generate PDF.

It helps to load dynamic data into the HTML table.

  • This HTML source will be used as a reference with the Autotable.html property.
  • This data can also be converted as a JSON object and mentioned in the Autotable.body property to Generate PDF.

structure.sql


--
-- Table structure for table `tbl_product`
-- CREATE TABLE `tbl_product` ( `id` int(11) NOT NULL, `product_name` varchar(255) NOT NULL, `price` varchar(255) NOT NULL, `model` varchar(255) NOT NULL
); --
-- Dumping data for table `tbl_product`
-- INSERT INTO `tbl_product` (`id`, `product_name`, `price`, `model`) VALUES
(1, 'GIZMORE Multimedia Speaker with Remote Control, Black', '2300', '2020'),
(2, 'Black Google Nest Mini', '3400', '2021'),
(3, 'Black Digital Hand Band, Packaging Type: Box', '1800', '2019'),
(4, 'Lenovo IdeaPad 3 Intel Celeron N4020 14\'\' HD ', '29490', '2021'),
(5, 'JBL Airpods', '2300', '2020'),
(6, 'Black Google Nest Mini', '3400', '2021'),
(7, 'Black Digital Hand Band, Packaging Type: Box', '1800', '2019'),
(8, 'Lenovo IdeaPad 3 Intel Celeron N4020 14\'\' HD ', '29490', '2021'); ALTER TABLE `tbl_product` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_product` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;

Simple HTML Table to PDF generation using jsPDF AutoTables

The PDF table data is dynamic from the database. The below code fetches the database records and display them in the UI with a HTML table.

It also shows a button to trigger the PDF generation on clicking it.

The on click event calls a JavaScript function named generateTable(). It refers to HTML table in the UI in the AutoTable.html option.

html-to-pdf-using-jspdf.php


<?php
namespace Phppot; require_once __DIR__ . '/lib/DataSource.php';
$conn = new DataSource();
$sql = "SELECT * FROM tbl_product";
$result = $conn->select($sql);
?>
<html>
<title>Product</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script>
<script src="assets/js/jspdf-autotable-custom.js"></script>
<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body> <div class="container"> <h2 class="text-center heading">Product detailed list</h2> <table class="table" id="product-table"> <tr class="border"> <th class="text-align">SL.No</th> <th>Product Name</th> <th class="text-align">Price</th> <th class="text-align">Model</th> </tr> <?php if (! empty($result)) {?> <?php foreach ($result as $k=>$val) {?> <tr class="content border"> <td class="text-align"><?php echo $result[$k]["id"];?></td> <td class="border-dark"><?php echo $result[$k]["product_name"];?></td> <td class="text-align"><?php echo $result[$k]["price"];?></td> <td class="text-align"><?php echo $result[$k]["model"];?></td> </tr> <?php }}?> </table> <input type="button" class="export-button" onclick="generateTable()" value="Generate PDF" /> </div>
</body>
</html>

Generate PDF from HTML table using AutoTable function

This script shows the generateTable() function and prepares the title and the table data to display in the PDF.

The autoTable() function uses the HTML table’s id selector reference to get the table data for the PDF.

Also, it fixes the PDF starting coordinates, themes and styles with the reference of the jsPDF instance.

By parsing the HTML table source, it outputs a PDF document that can be downloaded and saved.


function generateTable() { var doc = new jsPDF('p', 'pt', 'letter'); var y = 20; doc.setLineWidth(2); doc.text(200, y = y + 30, "Product detailed report"); doc.autoTable({ html: '#product-table', startY: 70, theme: 'grid', columnStyles: { 0: { halign: 'right', tableWidth: 100, }, 1: { tableWidth: 100, }, 2: { halign: 'right', tableWidth: 100, }, 3: { halign: 'right', tableWidth: 100, } }, }) doc.save('auto_table_pdf');
}

basic autotable example

Set default theme and custom styles for PDF tables

The jsPDF AutoTable plugin provides built-in themes. The possible values are ‘stripped’, ‘grid’, ‘plain’ and ‘css’.

It also supports adding custom styles by changing the plugin’s default options.

This example uses this plugin to customize the output PDF appearance by applying exclusive styles. These styles are applied on top of the configured theme appearance.

pdf-with-plugin-theme-and-custom-styles.php


<html>
<title>Product</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script>
<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body> <div class="container"> <input type="button" class="export-button" onclick="setThemeCustomStyle();" value="Generate PDF" /> </div>
</body>
</html>

The autoTable sets PDF table theme as grid and applies custom styles using headStyles and columnStyles options.

The custom styles override the cell background color, height and other default settings.


function setThemeCustomStyle() { var doc = new jsPDF('p', 'pt', 'letter') // generate the above data table var body = [ [1, 'GIZMORE Multimedia Speaker with Remote Control, Black,Lorem Ipsum is simply dummy...', 75000, '2021'], [2, 'Realme', 25000, '2022'], [3, 'Oneplus', 30000, '2021'], ] // New Header and Footer Data Include the table var y = 10; doc.setLineWidth(2); doc.text(200, y = y + 30, "Product detailed report"); doc.autoTable({ body: body, startY: 70, head:[['SL.No', 'Product Name', 'Price', 'Model']], headStyles :{lineWidth: 1,fillColor: [30, 212, 145],textColor: [255,255,255], }, theme: 'grid', columnStyles: { 0: { halign: 'right', cellWidth: 50, fillColor: [232, 252, 245], }, 1: { halign: 'left', cellWidth: 380, fillColor: [232, 252, 245], }, 2: { halign: 'right', cellWidth: 50, fillColor: [232, 252, 245], }, 3: { halign: 'right', cellWidth: 50, fillColor: [232, 252, 245], } }, }) // save the data to this file doc.save('auto_table_theme_custom_styles');
}

Create a PDF table with a header and footer

When preparing reports in a tabular format, the header, and footer components are most probably required. The header is for classifying the column data with keys or categories.

The footer components’ purpose commonly depends on the type of tabular report. If the table is huge in length, the footer may duplicate the header columns. If the table contains statistical data, the footer may reflect the consolidated figures.

In this way, the header and footer give value-add to the tabular report.

This plugin gives various methods to add header and footer parts along with the table body. Two of them are taken here for implementation in the below examples.

  • Using head and foot properties
  • Using column properties

Using head and foot properties

The page will contain a “Generate” button to call the below JavaScript function. This function initiates autoTable and specify the following properties.

  • body
  • head
  • foot
  • headStyles
  • footStyles
  • columnStyles

Then, To press the export button it not only downloads the table but also shows the table along with the header footer data value that we have added.

jspdf-long-text-header-footer.php


function generateHeaderFooterTable() { var doc = new jsPDF('p', 'pt', 'letter') // generate the above data table var body = [ [1, 'GIZMORE Multimedia Speaker with Remote Control...', 75000, '2021'], [2, 'Realme', 25000, '2022'], [3, 'Oneplus', 30000, '2021'], ] // New Header and Footer Data Include the table var y = 10; doc.setLineWidth(2); doc.text(200, y = y + 30, "Product detailed report"); doc.autoTable({ body: body, startY: 70, head:[['SL.No', 'Product Name', 'Price', 'Model']], foot:[[' ', 'Price total', '130000', ' ']], headStyles :{textColor: [255, 255, 255],}, footStyles :{textColor: [255, 255, 255],}, theme: 'grid', columnStyles: { 0: {halign: 'right', cellWidth: 50,}, 1: {halign: 'left', cellWidth: 380,}, 2: {halign: 'right', cellWidth: 50,}, 3: {halign: 'right', cellWidth: 50,} }, }) ...
... // save the data to this file doc.save('auto_table_header_footer');
}

Using column properties

Using this method, it maps the data key-value pair in the body of the autoTable specification.

Then, it specifies the column name in the header with the corresponding key reference used in the body.

It allows adding styles to the table columns with the columnStyles property. In the below example, it aligns the price column data to the right by setting halign in the columnStyles.


doc.autoTable({ columnStyles: { price: { halign: 'right' } }, body: [ { s_no: '1', product_name: 'GIZMORE Multimedia Speaker with Remote Control, Black', price: '75000' }, { s_no: '2', product_name: 'Realme', price: '25000' }, { s_no: '3', product_name: 'Oneplus', price: '30000' }, ], columns: [ { header: 'SL.No', dataKey: 's_no' }, { header: 'Product Name', dataKey: 'product_name' }, { header: 'Price', dataKey: 'price' }, ],
})

table with header footer

Create PDF with nested data table

First, the UI will show a parent table using the below HTML code. This example will describe how to add a nested table inside this parent while generating a PDF.

The AutoTable plugin provides various callback functions. This example script uses drawCell function to insert the nested table in the callback.

It helps to add more data via JavaScript to the preliminary level of information displayed on load.

jspdf-nested-autotable.php


<html>
<title>Product</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.6/jspdf.plugin.autotable.min.js"></script>
<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
<script src="assets/js/jspdf-autotable-custom.js"></script>
</head>
<body> <div class="container"> <h2 class="text-center heading">Product detailed list</h2> <table class="table" id="product-table"> <tr class="content border border-dark"> <td>iPhone</td> <td>Version: 13</td> </tr> </table> <input type="button" class="export-button" onclick="generateNestedPdf()" value="Generate PDF" /> </div>
</body>
</html>

The below script refers to the HTML table object to generate the parent table in the PDF. Then, it defines a callback to insert sub information in a child table.

This child table is inserted into a cell on a conditional basis. This condition checks the dataKey and the section attribute of the document instance. The custom styles define the cell dimension.


function generateNestedPdf() { var doc = new jsPDF(); doc.autoTable({ html: '#product-table', head: [["Product", "Specification"]], didDrawCell: function (data) { if (data.column.dataKey === 1 && data.cell.section === 'body') { doc.autoTable({ body: [ ["Model: ", "Mini"], ["Size: ", "6.2 inches"] ], startY: data.cell.y + 10, margin: { left: data.cell.x + data.cell.padding('left') }, tableWidth: 'wrap', theme: 'grid', }); } }, columnStyles: {5: {cellWidth: 40}}, bodyStyles: {minCellHeight: 30} }); doc.save('nested_table_pdf');
};

nested data table

PDF tables with a horizontal page break

This example is very important and useful in a report generation application.

When the loaded table data exceeds the target PDF layer, the table has to be wrapped. It is to prevent the data from being cut off from the PDF boundary.

By enabling the horizontalPageBreak it wraps the table and displays the wrapped content on the next page.

This example shows a 9-column table that wraps the lost column on exceeding the boundary.

It also enables the horizontalPageBreakRepeat to show the mapping for wrapped content with unique column data.

jspdf-horizontal-page-break.php


<html>
<title>Product</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.js"></script>
<link href="assets/css/style.css" rel="stylesheet" type="text/css" />
<body> <div class="container"> <input type="button" class="export-button" onclick="generateHorizontalPageBreak();" value="Export PDF" /> </div>
</body>
</html>

The autoTable sets horizontalPageBreak: true to switch on this feature.


function generateHorizontalPageBreak() { var doc = new jspdf.jsPDF('l') var head = [['Product ID', 'Product Name', 'Price in USD', 'Version', 'Model', 'Last updated date', 'Number of active installations', 'Reviews', 'Long text']] var body = [['2022#3v5', 'Mailer Adapter Component', '300', 'v5', '2022.3.3', 'JUN 2022', '100000+', '3245', 'Sed a risus porta est consectetur mollis eu quis dui. Phasellus in neque sagittis, placerat massa faucibus, commodo quam.']] doc.autoTable({ head: head, body: body, startY: 25, // split overflowing columns into pages horizontalPageBreak: true, // repeat this column in split pages horizontalPageBreakRepeat: 0, })
doc.save('table.pdf');
}

Table with Horizontal Page Break

Conclusion

Thus, we have seen various tools in jsPDF AutoTable to create PDF tables. By learning how to convert HTML table to PDF, it will be helpful in a report generation utility of an application.

PDF tables always useful to export statistics or other tabular data from HTML. The source of data can be a database or excel which loaded into HTML tables or converted into an JSON array to parse.

Download

↑ Back to Top

Posted on Leave a comment

Convert CSV to Dictionary in Python

5/5 – (1 vote)

The best way to convert a CSV file to a Python dictionary is to create a CSV file object f using open("my_file.csv") and pass it in the csv.DictReader(f) method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the first row to the specific row value.

Let’s have a look at a simple example to demonstrate this solution next!

Basic Solution: CSV to Dict Example

Here’s the content of an example CSV file "my_file.csv" used in our code snippet below:

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

If you visualize this CSV in table form, it looks like this:

Name Job Age Income
Alice Programmer 23 110000
Bob Executive 34 90000
Carl Sales 45 50000

Here’s the code to convert that CSV file to multiple dictionaries, one dictionary per row by using the csv.DictReader(file) function:

import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.DictReader(f) for row in reader: print(row)

A dictionary is a data structure that maps keys to values.

The output of the previous code snippet shows how the first row of the CSV is used as a header to determine the keys of the dictionary that are mapped to the values defined in the individual rows of the CSV file:

{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}
{'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}
{'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}

The csv.DictReader(f) method takes a file object f as an input argument. So, you first need to open the file using the built-in Python open() function.

🪲 Note: A common error is to pass the filename as a string—but this doesn’t work! The csv.DictReader(f) method expects a file object as a required argument.

One-Liner Solution: CSV to Dict

I love Python one-liners. That’s why I have written a book on those after all. 🙂

So, can we convert a CSV to a list of dictionaries in a single line of Python?

Of course, we can!

Here’s the one-liner that accomplishes the same as the code discussed before:

import csv; print(*csv.DictReader(open('my_file.csv')), sep='\n')

💡 Explanation: We import the csv module, use the semicolon ; to package two statements in one line, unpack * all rows from the csv.DictReader() output in a print statement, and use the newline character '\n' as a separator between two dictionary rows.

The output is the same as before:

{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}
{'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}
{'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}

If you just want to store the CSV contents in a list of dictionaries rather than printing them, you can use the following technique:

import csv; lst=[*csv.DictReader(open('my_file.csv'))]; print(lst)

The output is a list of dictionaries, one per (non-header) row of the original CSV:

[{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}, {'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}, {'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}]

If you’re interested in learning one-liners as well, feel free to check out my book:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!

Posted on Leave a comment

50 Signs You’re a Programmer Geek

5/5 – (1 vote)
  1. I consider 256 to be a nice, round number.
  2. I become annoyed when 10K means 10,000.
  3. I’ve written a useless program just for the “fun” of it.
  4. In fact, I prefer writing useless programs.
  5. I start counting from 0 and end up with one less than everyone else.
  6. I write equals as == and not equals as !=.
  7. I understand (0x2b||!0x2b) and find it funny.
  8. I know where to find the {braces} keys without looking.
  9. I call text phrases “strings.”
  10. I dream in code.
  11. I refer to eating and drinking as uploading.
  12. I refer to using the bathroom as downloading.
  13. I frequently use words like iteration, implementation, contiguous, trivial, version, array, polymorphic, parse, and WTF in casual conversations.
  14. When someone asks me what languages I speak, I reply: “Python, C++, JavaScript, and Solidity.”
  15. I hear the word “Scuzzy” and don’t think it’s a bad thing.
  16. My favorite f-word is fdisk.
  17. <rant>I include XML in regular correspondence.</rant>
  18. I use camelCase for names.
  19. I take things too literally.  For example, my wife gets upset when she asks “Do you want to take out the garbage?” (no) instead of “Will you take out the garbage?” (yes).
  20. I respond to questions too logically.  For example, when a waitress asks me, “Would you like coffee or tea?”  I respond, “Yes.”
  21. I answer negative questions in the technically-correct but awkward way.  When my mom asks me, “Wouldn’t you like a glass of milk?”  I respond, “Yes, I wouldn’t like a glass of milk.”
  22. I refer to having sex as setting up a LAN!
  23. When I make a mistake or say something I shouldn’t have, I wish I could press Ctrl+Z.
  24. When searching a paper book, I get frustrated that I cannot simply press Ctrl+F to find the text I’m looking for.
  25. When a store cashier asks me for my zip code, I demand to see the store’s privacy policy.
  26. I get sudden attacks of bittersweet nostalgia when thinking about my long-lost Commodore 64, Sinclair ZX-81, TRS-80, or Amiga 1000.
  27. It’s hard for me to make an absolute statement because I always consider there may be an edge case.
  28. I unit-test my spouse, expecting deterministic, solid outputs for a certain input with boundary conditions.
  29. I tell my spouse to “stop throwing exceptions that I’m not willing to catch.”
  30. I email myself notes rather than writing them.
  31. I hold a mouse more than my wife’s hand.
  32. I end my sentences with a semicolon…
  33. … actually not anymore since I discovered Python.
  34. I assume that most people love their jobs like I do.
  35. I’d rather text the guy in the next cubicle than talk to him.
  36. Nighttime and sleep are no longer irrevocably linked.
  37. I think these programming jokes are hilarious.
  38. I think xkcd is the funniest webcomic ever.
  39. I think the three primary colors are red, green and blue.
  40. I have more than one monitor.
  41. I have more email addresses than pairs of shoes.
  42. The number of computers in my house exceeds the number of romantic relationships I’ve had in my lifetime.
  43. I run a Web server at home.
  44. Instead of playing games on my Xbox, I set up a Bitcoin node.
  45. I carry a USB flash drive in my pocket wherever I go.
  46. I know what a router is, and I know what a bit is, but I don’t know what’s a router bit.
  47. I helped my grandma create her own blog.
  48. I talk to my computer the way most people talk to their significant other.
  49. I email myself to remind me to do something.
  50. I rig up elaborate mechanisms to perform basic tasks.
  51. When I’m reading a magazine and I see an underlined passage, I feel compelled to click on it.

Shout Out

This compilation is from multiple awesome ❤ online sources such as the following: