Normal view

Received before yesterday HolyPython.com

History of Speech-to-Text AI models

15 March 2023 at 21:41

History of Speech-to-Text Models​ & Current Infrastructure

As speech technology continues to advance, so too do the models that power it. From early traditional algorithms to modern machine learning models, the field has come a long way in a relatively short amount of time. Especially today, we are experiencing breakthroughs and paradigm shifts with the rise of neural networks and applications based on the Transformer architecture.

In this article, we will take a deep dive into the history of AI models in speech-to-text and explore how these models have evolved over time, bringing us closer to achieving human-level accuracy in this critical technology. Additionally we will briefly cover the current infrastructures that redefine almost all industries through innovative implementation of AI models on computational cloud services.

History of Speech-to-Text Models

We can see several scientific and technological milestones as well as accelerated innovation in the short history of speech-to-text models since 1950s. We’ll take a closer look at the exciting developments that occurred in this field throughout the decades.

Pre-2000

Audio waves

The history of speech-to-text (STT) dates back to the early 1950s, when researchers first began to explore the use of computers to recognize speech. Here is a brief timeline of some of the key developments in the history of STT:

1952:

Researchers at Bell Laboratories developed the “Audrey” system, which could recognize single-digit spoken numbers.

1960s:

The advent of digital signal processing (DSP) and the development of algorithms for pattern recognition led to significant progress in speech recognition research.

1970s:

The “Hidden Markov Model” (HMM) was developed, which became a popular approach for speech recognition. HMMs use statistical models to represent the probability of speech sounds and transitions between them. “Markov Models for Pattern Recognition: From Theory to Applications” by Lawrence R. Rabiner, published in 1989 was particularly influential. In this paper, Rabiner described the use of Hidden Markov Models (HMMs) for automatic speech recognition (ASR), which revolutionized the field of speech recognition and has remained a cornerstone approach ever since. His work contributed significantly to the development of practical ASR systems that were accurate and efficient, and paved the way for future research in this area. Here is a technical tutorial on HMM and Selected Applications in Speech Recognition.

1980s: The “Dynamic Time Warping” algorithm was developed, which could recognize speech in noisy environments. Professor Romain Tavenard from Université Rennes 2 has a great article regarding Dynamic Time Warping and its algorithmic foundation. Professor Tavenard also has an open source Dynamic Time Warping repo where source code for applications of tslearn (time series machine learning) with Python can be found along with many other interesting repositories such as K-means clustering demonstrations. Tslearn is a go-to Python library for implementing DTW with time series.

1990s: The development of neural networks and machine learning algorithms led to significant progress in speech recognition. Large vocabulary speech recognition systems also became more widely available.

Early Millenia

2000s:

The development of deep learning techniques in general, such as convolutional neural networks (CNNs) and recurrent neural networks (RNNs), led to significant improvements in speech recognition accuracy.

2010s:

The advent of big data and cloud computing led to the development of more powerful speech recognition systems that could process vast amounts of data. Consumer applications of speech recognition, such as virtual assistants and voice-controlled devices, also became more widely available.

Following research papers have been highly influential in advancing the field of speech-to-text technology using RNN and CNN.

  1. Convolutional Neural Networks for Speech Recognition” by O. Abdel-Hamid et al. published in 2012.
  2. Improving neural networks by preventing co-adaptation of feature detectors” by G. Hinton et al. published in 2012.
  3. Speech Recognition with Deep Recurrent Neural Networks” by A. Graves et al. published in 2013.
  4. Deep Speech: Scaling up end-to-end speech recognition” by A. Hannun et al. published in 2014.
  5. Very Deep Convolutional Networks for Large-Scale Speech Recognition” by A. Senior et al. published in 2015.

Post 2015

By the mass adoption of mobile devices and smart home assistants combined with advancements in neural network and deep learning technologies, scientists had the perfect recipe for speech-to-tech innovation.

2015:

Listen, Attend and Spell (LAS): Developed by Carnegie Mellon University in 2015, LAS is a neural network model for speech recognition that uses an encoder-decoder architecture with an attention mechanism. It is designed to be highly accurate and can handle noisy speech data.

Deep Speech 2: Developed by Baidu in 2015, Deep Speech 2 is a neural network model for speech recognition that uses a deep learning architecture based on convolutional and recurrent neural networks. It achieves state-of-the-art performance on several speech recognition benchmarks.

2017:

MobileNet: Developed by Google in 2017, MobileNet is a neural network model for speech recognition that uses a lightweight architecture optimized for mobile devices. It is designed to be fast and efficient, making it well-suited for real-time speech recognition applications on mobile devices.

2019:

Speech Recognition Transformer (SRT): Developed by Microsoft in 2019, SRT is a neural network model for speech recognition that uses a transformer architecture with a hybrid CTC/attention decoding mechanism. It achieves state-of-the-art performance on several speech recognition benchmarks.

RNN-T: Developed by Google in 2019, RNN-T is a neural network model for speech recognition that uses a recurrent neural network (RNN) transducer architecture. It is designed to be highly efficient and can handle long sequences of speech data.

Large Scale Multilingual Speech Recognition with Streaming E2E: Developed by Google in 2019, the Streaming End to End Models are neural networks for real time speech recognition that use end to end architecture with a streaming attention mechanism. It is designed to be highly efficient and can handle long sequences of speech data in real-time.

2020:

wav2vec 2.0: Developed by Facebook AI Research in 2020, wav2vec 2.0 is a self-supervised learning approach for speech recognition that uses a convolutional neural network (CNN) architecture. It achieves state-of-the-art performance on several speech recognition benchmarks.

2020: Speech-XLNet: Developed by the University of Science and Technology of China in 2020, Speech-XLNet is a neural network model for speech recognition that uses a transformer architecture with a novel permutation-based pre-training approach. It achieves state-of-the-art performance on several speech recognition benchmarks.

2021:

Streaming Conformer: Developed by Google in 2021, the Streaming Conformer is a neural network model for speech recognition that uses a conformer architecture with a streaming attention mechanism. It is designed to be highly efficient and can handle long sequences of speech data in real-time.

 

Speech Recognition with Augmented Contextual Representation (SCAR): Developed by Google in 2021, SCAR is a neural network model for speech recognition that uses a transformer architecture with an augmented contextual representation mechanism. It achieves state-of-the-art performance on several speech recognition benchmarks.

2022:

OpanAI published and open sourced Whisper speech-to-text models. Open source whisper models; tiny, small, medium, large, large-v2, tiny.en, base.en, small.en and medium.en are available via models open source Whisper Github Repository.

Open Source Whisper Models

Open AI Whisper

Multimodal AI models will enable humans and IT systems to interact via multiple modes such as speech, text, code, image and video

2023:

Whisper API: Whisper-1 is available via OpenAI’s API on its developer platform along with ChatGPT API. You can see this tutorial of detailed explanation and developer guide for Whisper API services. Whisper is based on the AI architecture introduced by Google named transformer.

The architecture’s main strength is its ability to take into account the dependencies among different parts of the speech input when generating the text output. This approach is more effective than traditional methods for processing such as convolutional neural networks, as it is less dependent on the order in which the input is sequenced. 

2024 and beyond:

With the recent advancements in AI technologies, we can expect to use speech to interact with multimodal AI models which have the potential to become precursors of Artificial General Intelligence.

Today, speech recognition technology is mostly a solved problem with AI models accurately being able to transcribe and translate almost 100 languages of our world.

Speech-to-text is used in a wide variety of applications, from virtual assistants and auto-translators to automated transcription services. Ongoing research in machine learning and artificial intelligence is likely to continue to drive progress in the field of speech-to-text in the coming years but we also expect to see many developments in multimodal AI applications and possibly, eventually AGI.

OpenAI models are quite the hot topic right now. OpenAI has a very useful API structure which you can access with Python and leverage this edge-cutting modern technology.

In this tutorial, we will demonstrate a few examples of OpenAI’s API using Python and the openai library.

Speech-to-Text on Cloud

“Speech-To-Text on Cloud” refers to the process of generating text from spoken words using cloud computing services. In other words, users can speak into a microphone or phone, and their words are automatically transcribed into text format by leveraging cloud-based tools and resources. This approach is becoming increasingly popular due to its convenience, scalability, and affordability, as it eliminates the need for on-premise hardware and software installations. Cloud providers offer Speech-To-Text services that use algorithms derived from machine learning and natural language processing to convert audio recordings to text accurately, quickly, and with high fidelity.

OpenAI Whisper API

Whisper API has recently been made available and it might provide an ideal infrastructure to interact with OpenAI’s popular AI service ChatGPT. Whisper speech-to-text API costs $0.006 per 1 minute audio so it is quite affordable and given the experiences OpenAI recently had with scaling and optimizing their systems, Whisper should provide a flawless experience. AI developers can sign up and get FREE credits to test their API services and the free credits go a very long way in terms of testing and demonstrations.

Google Cloud Speech-to-Text:

Google Cloud Speech-to-Text is a powerful cloud-based speech recognition service that can transcribe audio into text with high accuracy. It can recognize speech in over 120 languages and variants, and supports various audio formats such as FLAC, WAV, and MP3. It also supports various features such as real-time streaming and automatic punctuation, and can handle noisy backgrounds and different speaking styles. However, Google’s premium services can be a bit expensive especially if you process a very large volume of data.

Google Speech-to-Text uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, meeting recordings and telephone calls.

Microsoft Azure Cognitive Services Speech Services

Microsoft Azure Speech Services is a cloud-based speech recognition service that offers both speech-to-text and text-to-speech capabilities. It supports various languages and accents, and offers features such as real-time streaming, custom models, and automatic language detection. It also integrates with other Azure services such as Azure Cognitive Services and Azure Machine Learning. However, it can be a bit complicated to set up and configure compared to other services.

Amazon Transcribe

Amazon Transcribe is a cloud-based speech recognition service that can convert speech to text in real-time. It supports various audio and video formats, and can recognize speech in multiple languages and accents. It also offers features such as automatic punctuation, speaker identification, and custom vocabularies. However, it doesn’t support streaming transcription, and the accuracy can be affected by background noise or poor audio quality.

IBM Watson Speech to Text

IBM Watson Speech to Text is a cloud-based speech recognition service that supports various languages and dialects. It can convert speech to text with high  accuracy and offers features such as speaker diarization, real-time streaming, and custom  models. It also integrates with other IBM Watson services such as Watson Studio and Watson Assistant. However, it can be expensive to use if you process a large volume of data.

Oracle Cloud Infrastructure Speech-to-Text

OCI Speech is a cloud-based speech recognition service that uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, and telephone calls. It also offers real-time transcription and keyword spotting capabilities.

Speechmatics

Speechmatics is a cloud-based speech recognition service headquartered in Cambridge, England. The company uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, and telephone calls. They are highly specialized in speech technologies.

Otter.ai:

Otter.ai is a cloud-based speech recognition web app that uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, and telephone calls. They have a free tier for individual to try users as well as pro and business tiers. A very modern user interface and with free tier option you can’t really lose much by trying if it seems like an interesting service to you. They provide services for Business and Education applications. Otter.ai is well positioned for the current AI transformation that’s happening in the world right now and their integrated voice assistants can help you save time and get more done.

Other Speech-to-Text Alternatives:

Rev.ai: Rev.ai is a cloud-based speech recognition service that uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, and telephone calls. Rev.ai has a free trial tier for individual users and also great use case scenarios so you can have a pleasant experience and learn about speech-to-text without even paying anything. That being said their processing cost per minute for audio might need reconsideration after the recent developments such as Whisper API they might have to reconsider their pricing for transcription services.

Speech Recognition Anywhere: Speech Recognition Anywhere is an interesting Chrome extension which can be used for filling forms and other documents and even Google Docs and Spreadsheets. Mostly suitable for individual use cases.

Voicebase: Voicebase is a cloud-based speech recognition service that uses deep learning algorithms to convert audio to text. It supports multiple languages and can transcribe audio from various sources, including video files, streaming audio, and telephone calls. It also offers keyword spotting, entity extraction, and sentiment analysis capabilities. They have specialized services for telecommunication, financial services, healthcare and other industries suitable for enterprise solutions.

Kaldi Speech Recognition Toolkit: Kaldi is an open-source speech recognition toolkit that can be deployed on the cloud. It uses deep learning algorithms to convert audio to text and supports multiple languages. It also offers speaker recognition and language understanding capabilities.

Pros and Cons of Speech-to-text Cloud Services

 
While there are several benefits to using these services, there are also some drawbacks to consider. In this section, we will explore the pros and cons of speech-to-text cloud services to help you make an informed decision about whether they are right for your needs.
 
Pros:

All of these speech-to-text cloud services offer high accuracy and support various languages and accents. They also offer various features such as real-time streaming, speaker identification, and custom vocabularies. Additionally, they can easily integrate with other cloud services and offer developer-friendly APIs and SDKs that can be used with Python and other modern programming languages.

Cons:

However, some of these services can be expensive, especially if you process a large volume of data. Also, the accuracy can be affected by background noise or poor audio quality, and some services such as Amazon Transcribe don’t support streaming transcription. Finally, some services such as Microsoft Azure Speech Services can be a bit complicated to set up and configure compared to other options.

Summary

We have provided a comprehensive overview of the evolution of speech recognition technology in our article titled “History of Speech-to-Text AI Models”. We trace the early beginnings of speech recognition research to the 1950s and the development of the first commercial speech recognition systems in the 1980s.

Our article then delved into the major milestones of the last two decades and development of more sophisticated speech-to-text models, from early statistical models to the latest deep learning architectures. We highlighted some of the key advancements that have enabled such progress, such as the availability of large speech corpora and advances in graphics processing units (GPUs).

We emphasize the importance of responsible AI as the significant impact of major breakthroughs in machine learning start to have visible effects in our society. Introduction of the transformer architecture started a new paradigm in AI since early 2020. If there is any message we can conclude from the history of speech-to-text AI models and the accelerated growth in innovative applications in this domain, it is that there will be a lot of impactful development in near term and long term.

The post History of Speech-to-Text AI models appeared first on HolyPython.com.

Advanced Level Python Tips & Tricks

26 February 2023 at 21:40

Python Tips & Tricks (Part III)

Advanced Level

HolyPython.com

Here are more Python tips about relatively more complex Python concepts.

71) Annotated Assignment Statement: (:)

This might not seem as impressive as some other tricks but it’s a new syntax that was introduced to Python in recent years and just good to be aware of.

Annotated assignments allow the coder to leave type hints in the code. These don’t have any enforcing power at least not yet. It’s still nice to be able to imply some type hints and definitely offers more options than only being able to comment regarding expected types of variables.

Check out some of these examples:

day: str = 'Saturday'
print(day)

lst: list = [1,2,3]
print(lst)
'Saturday'
[1,2,3]

The slightly less cool and older implementation would be like this:

day= 'Saturday' #str
print(day)

lst= [1,2,3] # list
print(lst)

You can read Python Official Documentation about Annotated Assignments here.

72) Yield Statement: ()

You can think of yield statement in the same category as the return statement. The difference is, while return statement returns a value and the function ends, yield statement can return a sequence of values, it sort of yields, hence the name.

If you’re interested in algorithms, here is a nice demonstration of Bubble Sort Algorithm Visualization where you can see how yield is needed and used.

Also, here are a couple of examples with yield statement:

def f1(a):
    yield a
    yield a**2
    yield a**3
    
print (f1(5))

for i in f1(5):
    print (i)    
<generator object f1 at 0x00000275EF339AC8>
5
25
125
def f2(b):
    for j in range(b):
        yield j

for i in f2(5):
    print(i)
0
1
2
3
4

73) News brought to you by: (Python Newspaper)

Another fabulous Python library newspaper3k gives a whole another meaning to newspaper access.

Sleek businessperson reading business newspaper

You can install newspaper3k via pip from your Anaconda Command Prompt. If you need more instructions you can check out this article regarding how to install packages using pip with Anaconda.

pip install newspaper

After installing newspaper3k you can start using it. Here are some examples:

import newspaper

paper = newspaper.build('http://theatlantic.com')

for article in paper.articles:
    print(article.url)

Code above will list all the articles found in the entire newspaper.

Check out the code below to see all the other attributes you can conveniently extract from a single article or a whole newspaper.

Information is power, when managed well it can offer great contributions to specific tasks, projects and objectives.

 

from newspaper import Article

url = 'https://www.theatlantic.com/ideas/archive/2020/06/daca-activism/613213/'
article = Article(url)

article.download()
article.parse()


print(article.text)
print(article.authors)
print(article.publish_date)
print(article.images)
print(article.title)
print(article.tags)
print(article.summary)

You can see the Github repository of this cool Python library here: Newspaper 3k Github

74) Geopy: (Work on Open File)

Geopy is another great library that can be used for geocoding services. Converting open addresses to longitude and latitude values or converting coordinates to addresses is called geocoding. It’s a common application with almost any location related service such as media sharing, social network, navigation, transportation, streaming, gaming, websites, communication, identification, security etc.

Here are some simple examples to demonstrate what you can quickly do with geopy library using the address of New York rooftop bar 230 Fifth.

Please note that you might need to change the user_agent to avoid application errors. One common error related to user_agent is InsufficientPrivilege Error.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="HiApp")
location = geolocator.geocode(" 230 Fifth Ave, New York")

print(location.address)
230, Fifth Avenue, Sheridan Meadows, Amherst Town, Erie County, New York, 14221, United States of America
print((location.latitude, location.longitude))

(42.983481438214326, -78.70683044449504)
print(location.raw)


{'place_id': 259174015, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. 
https://osm.org/copyright', 'osm_type': 'way', 'osm_id': 12316939, 'boundingbox': 
['42.983431438214', '42.983531438214', '-78.706880444495', '-78.706780444495'], 
'lat': '42.983481438214326', 'lon': '-78.70683044449504', 'display_name': '230, 
Fifth Avenue, Sheridan Meadows, Amherst Town, Erie County, New York, 14221, 
United States of America', 'class': 'place', 'type': 'house', 'importance': 0.511}

Open Street Map’s Nominatim is a very convenient geocoding service but you should check their official documentation for any serious use case.

Some other similar web services are Geonames, Yandex Map API, Bing Map API, Google Maps API V3 and Yahoo BOSS Finder. They all offer different usage benefits.

Big Apple and Lake States Satellite View at Night

75) sh: (Shell scripts and bash commands)

sh is a subprocess wrapper that can be used to dynamically map system functions to Python. It facilitates writing shell scripts with bash features.

Unfortunately latest version of sh only works for linux and Mac. If you’re using Windows you’ll need to install an old version that can be found here.

Let’s check out some examples of what can be done with sh:

import sh
sh.google_chrome("http://google.com”)

Here are some other ideas:

import sh

sh.whoami()
sh.pwd()
sh.mkdir('foldername')
sh.touch('filename.txt')
sh.echo('Hello World')

76) Decorators: (Augmenting functions without modifications)

Decorators are another elegant representative of Python’s expressive and minimalistic syntax.

By using decorators you can change a function’s behavior or outcome without actually modifying it.

In other words decorators decorate functions to make them fancier in some way.

A decorator starts with @ sign in Python syntax and is placed just before the function.

Here are some simple examples:

Let’s say we have a simple function that adds 2 numbers.

def adder(a, b): 
    return a+b
    
print(adder(2, 3))
5

Now, without touching the original function, let’s decorate it so that it multiplies the result by 100.

def decoten(func): 
    def times10(x, y): 
        return func(x*10, y*10) 
    return times10

@decoten
def adder(a, b): 
    return a+b 

print(adder(2,3))
50

77: Memory Optimization: (__slots__)

At the advanced level Python can have

When you start editing default Python implementations for speed and efficiency reasons you know you’re starting to get into the expert territory.

Python classes utilize dictionaries for instant attributes by default which can take quite a space even when you’re constructing a class object.

When this is the case you can use __slots__ magic to force Python not to have a big chunks default instance attribute dictionary and instead have a small custom list. This can save lots of memory in suitable applications.

On top of that, another benefit of __slots__ is faster access to class attributes.

Real Madrid : 5
class Team:
    __slots__ = ["name", "score"]
    def __init__(self, name, score):
        self.name = name
        self.score = score
        

a=Team("Real Madrid", 5)
print(a.name, a.score, sep=" : ")

78) Time it: (Time Processes)

timeit library is great for timing Python execution times. Just pass a function in string format to timeit.timeit() method and it will carry out 1 million executions to report the minimum time the code takes.

Its very useful to compare small code pieces and different functions but can be sluggish with big code.

Check out the example below demonstrating the execution time difference between 2 very similar list comprehension methods in Python:

import timeit

lst1='''list(range(1000))'''
lst2='''[i for i in range(1000)]'''

x=timeit.timeit(lst1)
y=timeit.timeit(lst2)

print(x, y, sep="------")
11.646945999999843 ------- 27.643676500000083

timeit library is great for timing Python execution times. Just pass a function in string format to timeit.timeit() method and it will carry out 1 million executions to report the minimum time the code takes.

Its very useful to compare small code pieces and different functions but can be sluggish with big code.

Check out the example below demonstrating the execution time difference between 2 very similar list comprehension methods in Python:

79) Virtual env: (Managing Dependencies)

Virtual Environment lets programmer create a specific environment with different versions of libraries when needed.

When the developer needs only certain versions of modules and libraries because development requires it virtual environment can be a practical solution.

It’s also quite straightforward with Python.

Make sure virtualenv module is installed (Installed by default in Anaconda3)

pip install virtualenv

To create a virtual environment type virtualenv followed by an environment name you’d like:

virtualenv environment1

you can also activate and deactivate a virtual environment simply by typing:

activate environment1

deactivate

You’ll need to activate your specific virtual environment before installing a specific module with a specific version. Once you are done working in that environment you can deactivate it and go back to base.

80) Groupby from itertools: (Grouping Python Data)

If you have iterables with elements belonging to different groups, you can use itertools’ groupby method to group them and process items in groups.

Here is an example:

from itertools import groupby

lst = [("car", "Ferrari"), ("car", "Renault"), ("country", "France"), ("country", "New Zealand"), ("fruit", "Kiwi")]

for i, group in groupby(lst, lambda x: x[0]):
    for j in group:
        print ("{} is a {}.".format(j[1], val))
    print()
Ferrari is a fruit.
Renault is a fruit.

France is a fruit.
New Zealand is a fruit.

Kiwi is a fruit.

81) Transposing Data: (zip method)

This trick is simple yet genius and can be very useful sometimes.

You can combine zip function and * unpacking notation for *args appropriate objects.

mat = [[1, 2, 3], [1000, 2000, 3000]]
zip(*mat)
[(1, 1000), (2, 2000), (3, 3000)]

82) Working with Zipped files: (ZipFile Library)

As you get more and more proficient with Python, you start experimenting more and more cases. One of these cases is encountering zip files whether it’s data science, game development, finance or scripting, zip files are a very common part of the digital world.

Let’s check out how zipfile library can be a super convenient and quick solution for zip related tasks.

First you can import zipfile as below:

from zipfile import ZipFile

There are many useful methods to be used with zipfile, here are some common ones:

  • Open: Opening with zipfile is more like creating a handle to work with. You can open a zip file in 2 ways: read; “r” and write; “w” depending on your use case.
from zipfile import ZipFile

f = ZipFile("myarchive.zip", "r")
f = ZipFile("myarchive.zip", "w")
  • Get file names: You can have a list of all the files’ names returned using namelist() method
from zipfile import ZipFile

f = ZipFile("myarchive.zip", "r")
print(f.namelist())
['app.html', 'credits.html', 'app.css', 'auth.css', 'bridge.css', 'history.css', 'd3dcompiler.dll']
  • Extract files:
from zipfile import ZipFile

f = ZipFile("myarchive.zip", "r")
f.extractall("directory")
  • Read & Write:
from zipfile import ZipFile

f = ZipFile("myarchive.zip", "r")
a=f.read(r"FF/app.html")
print(a)
  • Please note that during the write operation zipfile is specifically opened with “w” parameter.
from zipfile import ZipFile

with ZipFile("myarchive.zip", "w") as f:
    f.write("pricelist.txt")
    f.write("deliveries.xls")

83) Time library: (More Time Functions)

Time library provides lots of time related functions and methods and is good to know whether you’re developing a website or apps and games or working with data science or trading financial markets. Time is essential in most development pursuits and Python’s standard time library comes very handy for that.

Let’s check out a few simple examples:

moment=time.strftime(“%Y-%b-%d__%H_%M_%S”,time.localtime())

import time

time_now=time.strftime("%H:%M:%S",time.localtime())
print(time_now)

date_now=time.strftime("%Y-%b-%d",time.localtime())
print(date_now)
21:47:43
2020-Jun-24
Cool tube clock showing current time

84) getsizeof: (Sys Library)

getsizeof() method from Python’s sys library will tell you the size of a Python object in the memory. Let’s see an example:

import sys
a="Paris"
b=sys.getsizeof(a)
print(b)
54

The result is in bytes.

Getsizeof() will give the size of any Python object whatsoever. Range object, byte object, reversed object, list object, dictionary object, list goes on.

Let’s create a huge list and see its size in bytes:

import sys
a=range(1000000)
b=sys.getsizeof(list(a))
print(b)
9000112

Wow, 9MB in memory. Talk about a big list!

85) Named Tuples: (From Collections Library)

Named Tuple is such a sophisticated data type and it lets you create exactly what the name says and more.

Namedtuple is a:

  • Light object
  • Readable
  • Allows creating attributes
  • Callable by names
  • Comes with object notation
from collections import namedtuple

flights = namedtuple("flight", "price, distance")
US = flights(2000,5000)
Iceland = flights(500,500)
France = flights(1000,1000)

print(France.distance)

86) Animated Charts: (Save as Gif or Mp4)

Visualization is big in Python. Matplotlib is a well-known library for charts but something super fancy and useful for visualization purposes that goes unnoticed is animated charts possibility in Matplotlib Animations.

Although the function itself, which is FuncAnimation module from matplotlib.animation library, is straightforward itself, there are some parameters that can get confusing such as figure itself, update function for animation, saving options such as codecs, fps settings, FFMpegWriter, PillowWriter, saving as gif, saving as mp4 or avi etc.

We have 2 great tutorials that you can check out explaining:

How to create a Matplotlib Animation

How to save a Matplotlib Animation

In short a matplotlib animation is created with 2 major pieces:

  • Figure –fig here: a figure window (can be initialized as plt.figure)
  • Animation function –animate here below: an animation function that continuously updates the figure

For a more detailed walkthrough please visit the individual Python animation tutorials mentioned above.

import random
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure(figsize=(15,15))

x,y = [], []
index= count()
def animate(i):
    x.append(next(index))
    y.append(random.randint(2,20))
    plt.style.use("ggplot")    
    plt.plot(x,y)

ani = FuncAnimation(fig, animate, interval=300)
plt.show()

87) All or any: (or & and Alternatives)

Any & all are similar to or & and.

  • Any returns True when at least 1 of the statements is True
  • All returns True when all statements are True

Here are some examples:

any([False, False, False])
all([False, True, True])
any([True, False, False])
any([False, False, True])
all([True, True, True])
False
False
True
True
True

88) Recursive Functions: (Functions calling themselves)

Recursion in programming is a very powerful algorithmic phenomenon that helps question to be solved by solving mini pieces of the problem and continuously feeding the answer or solution back to the problem.

Because of this nature, it goes hand in hand with concepts such as iteration (think for loop or while loop), yield statement (in Python), user-defined functions, algorithms.

Recursive programming is a broad subject and it can go well beyond these Python concepts but solving a problem within Python’s syntax and structure can also be infinitely interesting at the micro level.

Let’s look at a commonly known number sequence to demonstrate a simple recursion idea in Python:

Fibonacci number is calculated by adding 2 numbers preceding the current Fibonacci number. When it’s continuously done it gives Fibonacci Sequence:

Fib(n-2) + Fib(n-1) = Fib(n)

So it goes,

0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 ….

Let’s put it in code:

def fibo(x):
    
    if x <= 1:
        return x
    else:
        return fibo(x-1) + fibo(x-2)
    
for i in range(40):
    print(fibo(i), end=" || ")

This code will return 1 for fibo(1) and 0 for fibo(0). After that it starts an infinite recursion. fibo(2) is fibo(1)+fibo(0) and fibo(3) is fibo(2)+fibo(1).

0 || 1 || 1 || 2 || 3 || 5 || 8 || 13 || 21 || 34 || 55 || 89 || 144 || 233 || 377 || 610 || 
987 || 1597 || 2584 || 4181 || 6765 || 10946 || 17711 || 28657 || 46368 || 75025 || 121393 ||
196418 || 317811 || 514229 || 832040 || 1346269 || 2178309 || 3524578 || 5702887 || 9227465 ||
14930352 || 24157817 || 39088169 || 63245986 ||

89) Cache results with decorators: (Efficient Functions)

There is a great way to cache functions with decorators in Python. Caching will help save time and precious resources when there is an expensive function at hand.

Implementation is easy, just import lru_cache from functools library and decorate your function using @lru_cache.

Here is an example:

from functools import lru_cache

@lru_cache(maxsize=None)
def fibo(x):
    if x <= 1:
        return x
    else:
        return fibo(x-1) + fibo(x-2)

for i in range(50):
    print(fibo(i), end="|")

print("\n\n", fibo.cache_info())

Furthermore, you can also ask for a cache status report using cache_info().

0|1|1|2|3|5|8|13|21|34|55|89|144|233|377|610|987|1597|2584|4181|6765|
10946|17711|28657|46368|75025|121393|196418|317811|514229|832040|
1346269|2178309|3524578|5702887|9227465|14930352|24157817|39088169|
63245986|102334155|165580141|267914296|433494437|701408733|
1134903170|1836311903|2971215073|4807526976|7778742049|
CacheInfo(hits=96, misses=50, maxsize=None, currsize=50)

Hits and misses show cache hits and cache misses that occurred during operation. 

Cache hit roughly denotes when a value is looked up in the cache and is found in the cache and cache miss is when a value is looked up but can’t be found in the cache.

Currsize signifies the current size of cache or in other words how many items have been cached, in this case 50, basically all of them, and maxsize is the maximum allowed items allowed to be cached, in this case None because we specified maxsize to be None in the beginning.

90) Textblob: (Sentiment Analysis)

Sentiment analysis is a term used to refer to the techniques that aims to extract sentiments, emotions or tone from a text. With the advancements in computation power and technology and accumulation of text (social media, online books, news, translation of ancient scripts etc.) Natural language processing (NLP) has become a very interesting and significant field in the last decade.

Textblob is a fantastic library that can be used for sentiment analysis and more. Sentiment can be used to tell the features of a text such as tone positivity, subjectivity, objectivity, hopefulness, kindness, explicit language, grammar analysis, translation,  spelling check, etc etc.

This method is currently being used by multiple hedge funds to analyze social media and online news to identify trading opportunities and analyze companies, markets, indexes, economies, stocks and commodities.

It can also be used to analyze political stability, global events, crisis management, books, articles, social media profiles and more.

Here is a simple demonstration from a random book:

from textblob import TextBlob

f = r"c://Users/USA/Desktop/asd.txt"
file = open(f, "r", encoding="utf8")
data= (file.read())

blob = TextBlob(data)

b=blob.sentences
print(b[0])
Spending time together is like playing in the anteroom of heaven.
  • First TextBlob module needs to be imported
  • Then a blob object needs to be created with the text being analyzed
  • And you can start sentiment analysis right away after that
  • There are already lots of convenient methods and attributes included such as:
    • .tags
    • .noun_phrases
    • .sentiment
    • .sentiment.polarity
    • .words
    • .sentences
    • .lemmatize (grouping similar words for analysis based on context)
    • .definition
    • .correct()
    • .spellcheck()
    • .words.count()
    • .ngrams()
  •  
Note: If you don’t have textblob library installed, you can refer to this installation tutorial.

.sentences returns a list of sentences from the text and here we’re printing only the 1st sentence (index 0). Here is what .tags will return:

b=blob.tags
print(b[:5])
[('Spending', 'NN'), ('time', 'NN'), ('together', 'RB'), ('is', 'VBZ'), ('like', 'IN')]
b=blob.sentiment
print(b[:5])
Sentiment(polarity=0.0059523809523809494, subjectivity=0.5603174603174603)

Polarity here refers to a sentiment range between -1 and +1, from most negative to the most positive. Subjectivity can take a value between 0 and +1, 1 being the most subjective text possible while 0 signifies maximum objectivity.

Let’s see the last 5 words this time:

b=blob.words[-1:-6:-1]
print(b)
['mountains', 'huge', 'become', 'now', 'love']

And definition of the last word:

b=blob.words[-1].definitions
print(b)
['a land mass that projects well above its surroundings; higher than a hill', 
"(often followed by `of') a large number or amount or extent"]

You can also check out this sentiment analysis tutorial with textblob for more examples.

91) Kwargs: (Arguments for named sequences)

**kwargs and *args are function arguments that can be very useful.

They are quite underused and often under-understood as well. (Sorry couldn’t resist that)

Let’s try to explain what kwargs are and how to use them.

  • While *args are used to pass arguments at an unknown amount to functions, **kwargs are used to do the same but with named arguments.
  • So, if *args is a list being passed as an argument, you can think of **kwargs as a dictionary that’s being passed as an argument to a function.
  • You can use arguments as you wish as long as you follow the correct order which is: arg1, arg2, *args, **kwargs. It’s okay to use only one of those but you can’t mix the order, for instance, you can’t have: function(**kwargs, arg1), that’d be a major faux pas in Python.
  • Another example: You can do function(*args,**kwargs) since it follows the correct order.
  •  

Here is an example. Let’s say satelites are given with their names and weight in tons in dictionary format. Code prints their weight as kilograms along with their names.

def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*1000)

payloads(NavSat1 = '2.8', BaysatG2 = '5')
NavSat1 ||| 2800.0
BaysatG2 ||| 5000.0

Since the function above would work for any number of dictionary keys, **kwargs makes perfect sense rather than passing arguments with a fixed amount.

Also ** syntax can be used to unpack while calling a function. Check out this example:

def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*1000)

sats={"Tx211":"2", "V1":"0.65"}
payloads(**sats)
Tx211 ||| 2000.0
V1 ||| 650.0

92) Compile a regex formula: (Advanced Regex)

You can also compile regular expressions in Python.

Regex has a massive place in working with all kinds of text, database queries, strings, search engine queries, tables, web data etc.

Python comes with a standard regular expression library called re. If you’re new to regex and would like to learn more you can check out this extensive:

Python Regular Expression Tutorial

and

Python Regular Expression Exercises (Online & Interactive)

So in this tip we will check out how regex can be compiled and the benefits of doing so.

Normally, when you use a regex expression Python will compile it during the execution of your program and it won’t really make a difference if regex is compiled before hand or not. Especially since Python also caches regular expressions the performance benefits of separately compiling regex is not expected to be significant.

However, some main benefits of compiling regex are reusability and readability.

On top of that it can make sense to compile some regex expressions ahead of execution so that compilation is not done at a random or even worse critical moment when running the program.

Check out this simple example to understand how regex can be compiled in Python:

Regular Expression without compiling:

import re

str='''Chuck Norris can divide by zero.
When Chuck Norris falls in water, Chuck Norris doesn't get wet. Water gets Chuck Norris.
Chuck Norris once visited the Virgin Islands. They are now The Islands.'''

result2 = (re.match('[A-Z][a-z]*\s\S*', str))
print((result2))

Regular Expression Compiled Example:

import re

str='''Chuck Norris can divide by zero.
When Chuck Norris falls in water, Chuck Norris doesn't get wet. Water gets Chuck Norris.
Chuck Norris once visited the Virgin Islands. They are now The Islands.'''

query = re.compile('[A-Z][a-z]*\s\S*')
result1 = (query.match(str))

print((result1))

93) Create a File Server: (A Surprising One Liner)

This one is kind of well-known. Still very impressive nevertheless.

At the comfort of your command prompt (we recommend Anaconda Prompt), you can create an HTTP server with a tiny one line Python command like below:

python -m http.server 8000

Number in the end here is the Port Number.

python https server

Server will start immediately. If you’d like to kill the server just pres Ctrl+C and server will be interrupted as above or you can just close the Prompt Window.

If you’d like to make sure you can type ps from the Command Prompt to see all the processes. Scroll up to and you will see a Python process when the server is running which will be shut down after you stop the server.

Please note that if you have other instances of Python running such as Spyder or any Python IDE or Python itself, these processes will also appear under Python in the list.

python https server process list
  • You can access you Python file server by navigating to localhost:8000 (Or whatever port you chose) from your browser’s address bar.
  • It’d be wise to open the server on a dedicated not-so-important folder somewhere in your computer. i.e.: You can create a new folder on your desktop, navigate to that directory using cd command and start the server within there.
Python file server directory

You can get Spyder inside Anaconda Individual Package for free here.

Or if you wish you can also get Spyder IDE individually from this link.

94) Enum Class: (Members with unique ID values)

Here is another classy approach from Python, no pun intended. Python’s enum class will let you create enum object members with constant and unique values which then can be listed, compared or used for identification. 

Enum classes are also iterable so they can be iterated as well:

Here is a simple example: 

from enum import Enum

class sports(Enum):
    skiing = 0
    soccer = 1
    running = 2
    football = 3
    golf = 4
    swimming = 5
    
    
print(sports.golf)
print(repr(sports.golf))
sports.golf
<sports.golf: 4>

Python enum class iteration example:

for i in sports():
    print(i)
<sports.skiing: 0>
<sports.soccer: 1>
<sports.running: 2>
<sports.football: 3>
<sports.golf: 4>
<sports.swimming: 5>

95) Connect to Database: (SQLite)

Database is such a powerful concept to store data. Apps use it, software uses it, websites use it. It’s not that hard and you can also use it.

But then when you combine Database programming with Python, that’s real power.

SQlite is a nice open-source application that works well with Python.

All you need to do is type a few Python codes and implement your SQL code inside. It will be something like this:

import sqlite3

q = sqlite3.connect('Mydatabase.db')
cursor = q.cursor()

cursor.execute('SELECT id, name FROM Prices')
data = cursor.fetchall()
for i in data:
    print(i)

cursor.close()

q.close()
  1. First connect to database via sqlite3 (sqlite3.connect(‘Mydatabase.db’))
  2. Create a cursor (q.cursor())
  3. Then, cursor.execute for executing Database Commands.
  4. .fetchall() to get data from the Database Rows 
  5. Finally close the cursor and terminate the connection (cursor.close() and q.close())

96) StringIO, cStringIO and BytesIO: (Python Streams)

These very potent, practical but unused methods can mean the difference between hardcore performance and inefficient operations.

Generally Python stream is a file-like object that exists in the Ram of the computer instead of the Disk IO. Ram is much faster than Disk and can be perfect for parts of the program that’s running.

That’s why lots of software has some kind of Ram usage and you can implement it too.

  • StringIO is used with unicode objects (like strings) and it will accept any string with Unicode encoding
  • BytesIO is used with byte data (binary input)
  • cStringIO is like StringIO since it’s a C implementation but there is a catch, cString won’t accept any string that can not be encoded as plain Ascii, which can be limiting depending on the situation.
All of these methods will provide a file like object to write data.
import StringIO

str = "Hello World"
file = StringIO.StringIO(str)

print (file.read())
Hello World

97) Work with gzip files in Python: (gzip library)

Gzip or gnu is a compression algorithm that can provide lots of compression benefits with large files. You might have seen it before as a file which uses .gz extension. Since gzip only works for a single file you might also have seen the tar archive solution which combines multiple files before creating gzip compression resulting in a tar.gz (or tgz) file.

Python has a great gzip library that makes working with gzip files a breeze.

Here is a super simple demonstration of compressing a Python list: 

import gzip

str = b"Hello World"
c_str = gzip.compress(str)

That was too easy because our string was in byte format already b””.

Because compress() method takes byte data only. So, sometimes you might need to make that conversion but it’s not that complicated once you know it.

Here is another example where a regular string is “prepared” for compression:

  1. First, string is going to be converted to bytes using bytes() function 
  2. And second, “utf8” is passed as encoding parameter in the bytes function.
import gzip
str = "Hello World"
str = bytes(str, "utf8")
c = gzip.compress(str)
print(c)

So, what if you want to compress Python objects such as lists, dictionaries, tuples or files and images. In each case you will need to do appropriate conversions so that data is ready for compression. Here is an idea to compress a list: First it’s converted to string and then compressed. Similar methods can be used with JSON method.

import gzip

lst = ["kiwi","apple","banana"]
lst = str(lst)
lst = bytes(lst, "utf8")
c=gzip.compress(lst)
print(c)

Decompression part is the reverse of a compression process and somewhat more straightforward:

import gzip
dec_lst = gzip.decompress(c_lst)

Finally, you can also use open() method to open gzip files and work with them.

open with structure can also be useful in this case:

with gzip.open("textfile.txt.gz", "rb") as file:
	data = file.read()

98) Memory Management: (getrefcount method)

getrefcount will show how many times an object is used in the memory. It’s a fantastic tool that can be used for memory management in any program and it’s very convenient too. 

Getrefcount will calculate the object usage at a low level ByteCode so it can tend to be higher than expected. For instance when you print a value that value is actually processed multiple times in the background inside the print function itself and getrefcount also counts the instance when the value is called with getrefcount method itself. So, it’s safe to say that the count will actually always be at least 1 time higher than expected.

Here is a code to show how many times alphanumeric characters are referenced in a random code:

It makes sense to see that x, i and y are so frequently utilized.

import sys
import matplotlib.pyplot as plt
 
#string with all character letters
str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
x_ax = [sys.getrefcount(i) for i in str]
y_ax = range(len(str))
Graph = plt.figure(figsize=(250,150))

plt.bar(y_ax, x_ax, align='center', color="orange")
plt.xticks(y_ax, str)

plt.xlabel("Character")
plt.ylabel('sys.getrefcount(str)')
 
Graph.show()

This code will show reference counts for 2 random variables: First an integer that gets increased by 1, second, a random word that’s not likely to be used anywhere else in the backend of the functions (Heritage).

Interestingly first variable (integer 10) is referenced 309 (minus one) times in the kitchen of this little program. Second variable is never used except our assignment and reference count attempt which adds up to 3 times. Cool trick?

a_val = 10
a_val += 1

b_val = "Heritage"

a = sys.getrefcount(a_val)
b = sys.getrefcount(b_val)

print(a, b, sep="\n")
309
3

99) os.environ: (environment variables)

environ is a very cool module from Python’s default os library.

You can access all environment variables in your operating system using os.environ.

It works in a dictionary format and lists all the variables. Let’s see:

import os
print(os.environ)

Above code will give you a raw and messy output to the human eye. But, you can specify which data you’d like to extract with os.environ as well.

Since it’s in dictionary we can tweak the code a little bit to make everything more readable, also it’s an opportunity to use the pprint library:

{‘ALLUSERSPROFILE’: ‘C:\\ProgramData’,
‘APPDATA’: ‘C:\\Users\\ABC\\AppData\\Roaming’,
‘CLICOLOR’: ‘1’,
‘COMMONPROGRAMFILES’: ‘C:\\Program Files\\Common Files’,
‘COMMONPROGRAMFILES(X86)’: ‘C:\\Program Files (x86)\\Common Files’,
‘COMMONPROGRAMW6432’: ‘C:\\Program Files\\Common Files’,
‘COMPUTERNAME’: ‘DESKTOP-465GJRJ’,
‘CONDA_PREFIX’: ‘C:\\Anaconda3’,
‘DRIVERDATA’: ‘C:\\Windows\\System32\\Drivers\\DriverData’,
‘GIT_PAGER’: ‘cat’,
‘HOMEDRIVE’: ‘C:’,
‘HOMEPATH’: ‘\\Users\\ABC’,

‘PROCESSOR_ARCHITECTURE’: ‘AMD64’,
‘PROCESSOR_IDENTIFIER’: ‘Intel64 Family 6 Model 158 Stepping 10, GenuineIntel’,
‘PROCESSOR_LEVEL’: ‘6’,
‘PROCESSOR_REVISION’: ‘gxxa’,
‘PROGRAMDATA’: ‘C:\\ProgramData’,
‘PROGRAMFILES’: ‘C:\\Program Files’,
‘PROGRAMFILES(X86)’: ‘C:\\Program Files (x86)’,
‘Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules’,

‘SYSTEMDRIVE’: ‘C:’,
‘SYSTEMROOT’: ‘C:\\WINDOWS’,
‘TMP’: ‘C:\\Users\\ABC\\AppData\\Local\\Temp’,
‘USERDOMAIN’: ‘DESKTOP-465GJRJ’,
‘USERDOMAIN_ROAMINGPROFILE’: ‘DESKTOP-465GJRJ’,
‘USERNAME’: ‘ABC’,
‘USERPROFILE’: ‘C:\\Users\\ABC’,
‘WINDIR’: ‘C:\\WINDOWS’}

import os
import pprint
print(os.environ)

100) Serialization: (pickle & Json)

Sometimes you may need to save your Python object locally for later use or Network transfers. Python has fantastic libraries for serialization such as Json and Pickle.

Serialization is storing data structures in the program so they don’t just disappear after the program is terminated.

There is also marshal library but it’s more primitive and can’t handle certain techniques such as class instances and recursion. Marshal is closer to json in its scope of serialization.

So, when would you use pickle, cpickle, json or ujson?

cpickle and ujson are faster versions of respective libraries that take advantage of C implementations in Python. So they’d always be favorable for speed reasons.

Apart from that, json is a more secure and readable version of serialization than pickle which comes at a cost of speed.

While you can take care of almost any data structure in Python with Json it gets inefficient with large files or uncommon objects. Pickle on the other hand operates in a sweet spot where you’d like to work with large files (multiple GBs) and still don’t want to be bothered with database solutions.

The thing is depending on your application, you may have to watch out for security vulnerabilities pickle introduces to the system so it’s usually wise to seek out json or database solutions before resorting to pickle when possible.

You can check out our JSON Tutorial explaining JSON solutions in Python.

Here is a quick pickle serialization and deserialization:

import pickle

lst = ["LAX", "LGA", "DEN", "SFO", "JFK", "MIA"]
a = pickle.dumps(lst)

print(a)
b'\x80\x03]q\x00(X\x03\x00\x00\x00LAXq\x01X\x03\x00\x00\x00LGAq\x02X\x03\x00\x00\x00DENq\
x03X\x03\x00\x00\x00SFOq\x04X\x03\x00\x00\x00JFKq\x05X\x03\x00\x00\x00MIAq\x06e.'
print(pickle.loads(a))
['LAX', 'LGA', 'DEN', 'SFO', 'JFK', 'MIA']

Python Tips & Tricks (Bonus Parts)

Various Levels

HolyPython.com

Bonus) Abstract Base Classes: (ABC)

Abstract Classes are nice alternatives to concrete classes since they conveniently allow multiple inheritence.

They work well with unfinished, concept-like classes hence the name abstract. Abstract classes become functional with methods written in subclasses. When a developer is working with subclasses of an abstract class they know that the class is either incomplete or empty so this gives an opportunity to work with incomplete classes in a structured way. @abstractmethod is a useful decorator for abstract base classes in Python.

Here is an example with an @abstractmethod :

from abc import ABC, abstractmethod
 
class ClassAbstract(ABC):
 
    def __init__(self, val1):
        self.value1 = val1
    
    @abstractmethod
    def under_construction(self):
        pass

class DDD(ClassAbstract):

    def under_construction(self):
        return self.value1
    
data1 = DDD(10)

print(data1.under_construction())
10

While the example demonstrates abstract classes perfectly here are 2 “bad” examples to further elaborate abstract classes:

from abc import ABC, abstractmethod
 
class ClassAbstract(ABC):
 
    def __init__(self, val1):
        self.value1 = val1
    
    @abstractmethod
    def under_construction(self):
        pass

class DDD(ClassAbstract):

    def multiplier(self):
        return self.value1*10
    
data1 = DDD(10)

print(data1.multiplier())
TypeError: Can't instantiate abstract class DDD with abstract methods under_construction

You get the above error because you didn’t modify and complete the incomplete abstractmethod inherited from the abstractclass.

Let’s look at another troubled example:

In this example, abstractmethod is not enforced. This is only good old inheritence and not an abstractclass. It still works even though under_construction method is left untouched.

from abc import ABC, abstractmethod
 
class ClassAbstract:
 
    def __init__(self, val1):
        self.value1 = val1
    
    def under_construction(self):
        pass

class DDD(ClassAbstract):

    def multiplier(self):
        return self.value1*10
    
data1 = DDD(10)

print(data1.multiplier())
100

Bonus) c classes: (cPickle, uJson and others)

If you are serious about execution speed then you should definitely consider mingling with C and C++ in relation with Python.

With the help of C extensions some Python operations can be sped up to 100x, 500x or 1000x.

The details of these concepts is beyond the scope of this Python tutorial but if you’re interested you should definitely check out Cython, distutils and setuptools.

Distutils and setuptools make it possible to write extension modules for Python written in either C or Python or even a mix code.

Cython is a Python-like languages that compiles like C catching up with C speeds in many cases.

CPython, often confused with Cython is a Python implementation in C and it requires writing C code for the most part but both will allow you to write libraries in C that can be implemented in Python such as Python’s standard library numpy.

You can also check out alternative libraries that are already written in C by the community. One example to that is uJSON, an ultra-fast alternative to Python’s standard JSON library, or cPickle as an alternative to Pickle.

The post Advanced Level Python Tips & Tricks appeared first on HolyPython.com.

How to Create an app with a 3D model using VIKTOR

14 September 2022 at 09:25

Introduction

We are going to need smart engineering solutions to solve our planet’s problems (and soon in other celestial bodies in our solar system) in a smart and scalable manner. Parametric programming is a fantastic approach to optimize efficient engineering methods and achieve those goals.

We recently came across a very interesting platform named VIKTOR which introduces smart app development for engineering with our favorite programming language: Python. The Python-based software is applicable to so many disciplines in the engineering and applied science fields. From material science to organic chemistry, civil engineering to mechanical engineering, Viktor offers an impressive toolbox and takes care of tedious processes such as app hosting, app deployment, intellectual property management and server administration.

In this mini-tutorial we will demonstrate how to create parametric design elements using VIKTOR and Python. VIKTRO also helps package, save, deploy and distribute your engineering app with your colleagues or clients and it can be useful for both engineers and engineering-related business teams as well.

Tutorial: Creating Web Apps with VIKTOR (powered by Python)

Creating a web app is an awesome way to let people interact with your Python code because people everywhere in the world can access it; even the ones without any Python skills can use a graphical interface. It brings a whole another level of visualization and interactivity to your engineering project. First, we will ask VIKTOR to create a folder with an empty app containing all files you need, and then install it. 

Some years ago, you would be right, but nowadays, the low-code app development platform VIKTOR makes it so easy that virtually anyone with Python knowledge can do it.

In this tutorial, we will guide you to build your very first app with VIKTOR. We will start with a blank app and create an interactive 3D model of a radio tower step by step. So, let’s get started!

Note: In case you need it, we included a complete web app code at the end of this tutorial.

Open Viktor.AI in Terminal
Full app showing parametric design of a radio tower with spherical, conical and cylindrical elements created by Python & VIKTOR.

Contents:

  1. Creating and starting an empty app
  2. Creating a 3D model
  3. Adding input fields
  4. Connecting the input fields to the 3D model
  5. Adding a Sphere
  6. Adding the mast
  7. Adding Materials
  8. What is next?
  9. All code together

Prerequisites

  • A free VIKTOR account
  • Installing the free VIKTOR library based on the instructions you receive by mail after making an account
  • Some Python programming experience
  • Ability to write and use functions
    (You are always welcome to refer to our Python User Functions Exercises and the relevant lesson here as well as any other Python lessons, tutorials and exercises we have to reinforce your programming proficiency.)

Installing VIKTOR

Before we get started, we need to download and install VIKTOR. VIKTOR is the platform that will help us create and host the web app using nothing but Python.

Here, you can create your VIKTOR account for free. Just follow the installation instructions which are pretty straightforward. This will take approximately 10 mins.

Creating, installing and starting an empty app

First, we will first ask VIKTOR to create a folder with an empty app containing all files
you need, and then install it. We will use this app as a basis to make your app. So, let’s get started!

  1. In the file explorer, go to the location where you like to store this and other future apps. Let’s assume the folder is called ‘viktor-apps’.
  2. Right-click in this folder and click on ‘Open in Terminal’ (or similar, e.g. ‘Open PowerShell window here’) to open the command-line shell, as in the image below. Note that in older versions of Windows, you may need to Shift + Right-click on the folder.

 

Open Viktor.AI in Terminal

 

  1. On the command line, write the following command:
viktor-cli create-app tower-app

This command creates an app with the name ‘tower-app’. After executing the command, you will find a folder called ‘tower-app’ inside the ‘viktor-apps’ folder.

1. In the PowerShell navigate to the ‘tower-app’ folder:

  cd tower-app

2. Before installing a new app, you need to clear the database by running the following command:

viktor-cli clear

3. Now you can install your app using the command below. Installation can take up to 3 minutes, so just have a little patience and read the information below.

viktor-cli install

The command tells VIKTOR to install the app stored inside the folder where you run the command. That is why we ask you to navigate to the app folder first.

4. Start the app with:

viktor-cli start

Congratulations, your app is up and running! However, do not close your command-line shell; otherwise, your app will stop.

5. Open your app in your browser by visiting VIKTOR cloud as explained in the command-line shell

6. After logging in, open the “Development” card. Great, your app is up and running. Now let’s go to the fun part: start building the 3D model!

Creating a 3D model

In this part of the tutorial, we will create a 3D model of the radio tower using the geometry module. Before we start building the 3D model, let’s check out what the current application looks like.

In your browser,

  1. Click on “Create”.
  2. Fill in a name in the pop-up
  3. Click “Create and open”

You should now see an empty page. So let’s start creating our 3D model.

Add a 3D model to your app

First, we will visualize the base of the radio tower. The base will just be a cylinder, which will have a height and a diameter. The first part is a bit slow, but I promise that we will draw the rest of the tower at lighting speed.

  1. Go to the folder tower-app and open app.py in your favourite code editors.
  2. Check app.py so that it looks like this:
from viktor.core import ViktorController
from viktor.parametrization import ViktorParametrization

class Parametrization(ViktorParametrization):
     pass

class Controller(ViktorController):
    label = 'VIKTOR Tutorial'
    parametrization = Parametrization

3. On the top, import some additional functions we will need to create the app:

from viktor.geometry import Point,Line,CircularExtrusion,Material,Color
from viktor.views import GeometryView,GeometryResult

4. First we will create a geometry view to show your 3D model and define a function to draw everything. We do this inside your controller like this.

...
class Controller(ViktorController):
  label = 'VIKTOR Tutorial'
  parametrization = Parametrization

  @GeometryView("3D", duration_guess=1)
  def visualize_tower(self, params, **kwargs):
    """Creates the 3D view and visualizes the Radio Tower"""

    return GeometryResult()
  • Now we will start drawing. First, let’s define 2 reference points we need for our design:
...
  @GeometryView("3D", duration_guess=1)
  def visualize_tower(self, params, **kwargs):
    """Creates the 3D view and visualizes the Radio Tower"""

    # Reference points

    base_start = Point(0, 0, 0)         #<--- Add this lines
    base_end = Point(0, 0, 50)          #<--- Add this lines

    return GeometryResult()
  • To create your base of the tower, we will use CirculerExtrusion, which draws a cylinder following a line between the points base_start and base_end. Under the two point we just created add:
    # Create tower base

    line = Line(base_start, base_end)
    base = CircularExtrusion(diameter=5, line=line)

    return GeometryResult(base)     # <--- don't forget to change this line
  • If everything went right, you should be able to see the cylinder in your app. Save the app.py file, go to your app and refresh the page.

Python code up to cylinder creation

In case something went wrong, here you can see the complete code so far. Just copy/paste it in app.py and everything should work.

from viktor.core import ViktorController
from viktor.parametrization import ViktorParametrization
from viktor.geometry import Point, Line, CircularExtrusion, Cone, Sphere, Material, Color
from viktor.views import GeometryResult, GeometryView


class Parametrization(ViktorParametrization):
    pass


class Controller(ViktorController):
    label = 'VIKTOR Tutorial'
    parametrization = Parametrization

    @GeometryView("3D", duration_guess=1)
    def visualize_tower(self, params, **kwargs):
        """Creates the 3D view and visualizes the Radio Tower"""

        # Reference points

        base_start = Point(0, 0, 0)
        base_end = Point(0, 0, 50)

        # Create tower base

        line = Line(base_start, base_end)
        base = CircularExtrusion(diameter=5, line=line)

        return GeometryResult(base)

Adding input fields

We created a 3D model in the last section, but what if you want a user to change the geometry dynamically? Using VIKTOR’s input fields you can. Let’s add a few input fields to change the length, width and height of your cylinder.

  • Let’s import the NumberField we need. At the top of you code add:
from viktor.parametrization import NumberField
  • Under class Parametrization(ViktorParametrization): we will add 2 fields to change the dimensions of the cylinder, and don’t forget to eliminate pass. You code should look like this:
...
class Parametrization(ViktorParametrization):
    """Parametrization of the Radio Tower"""

    # Base of the tower
    base_height = NumberField("Height base", default=50, min=1)
    base_diameter = NumberField("Diameter base", default=3, min=1)

    #pass  <---- DELETE THIS LINE
    ...
  • Again, save the app.py file. Go to your app, refresh the page and see the NumberFields appear in your app.

Did you notice that changing the values does not modify the 3D model? This is because we have not connected the NumberFields to your 3D model yet. We’ll do this next.

All code toghter so far

from viktor.core import ViktorController
from viktor.parametrization import ViktorParametrization, NumberField
from viktor.geometry import Point, Line, CircularExtrusion, Cone, Sphere, Material, Color
from viktor.views import GeometryResult, GeometryView


class Parametrization(ViktorParametrization):
    """Parametrization of the Radio Tower"""

    # Base of the tower
    base_height = NumberField("Height base", default=50, min=1)
    base_diameter = NumberField("Diameter base", default=3, min=1)


class Controller(ViktorController):
    label = 'VIKTOR Tutorial'
    parametrization = Parametrization

    @GeometryView("3D", duration_guess=1)
    def visualize_tower(self, params, **kwargs):
        """Creates the 3D view and visualizes the Radio Tower"""

        # Reference points

        base_start = Point(0, 0, 0)
        base_end = Point(0, 0, 50)

        # Create tower base

        line = Line(base_start, base_end)
        base = CircularExtrusion(diameter=5, line=line)

        return GeometryResult(base)

Connecting the input fields to the 3D model

We’ll connect the input fields to the 3D model to make it dynamic:

  • Change the 3rd value of base_end from a static 50 to params.base_height and the value of diameter to params.base_diameter as shown below:
...

     @GeometryView("3D",duration_guess=1)
        def visualize_tower(self, params, **kwargs):
        """Creates the 3D view and visualizes the Radio Tower"""

        # Reference points
        base_start = Point(0, 0, 0)
        base_end = Point(0, 0, params.base_height)      # <--- add params.base_height here

        # Create tower base
        line = Line(base_start, base_end)
        base = CircularExtrusion(diameter=params.base_diameter, line=line)      # <--- params.base_diameter here

        return GeometryResult(base)
  • Again, save the app.py file. This time, instead of refreshing your app, just change some input values. Did you see that? VIKTOR updated the app, and your 3D model is now dynamic!

Awesome, we already created a fully functioning parametrized model of a beam including a 3D visualisation! If you were
wondering whether this is a good time: Yes, this is a perfect moment to pat yourself on the back!

How does this work?

So what happens in the background? Each time you change an input parameter, VIKTOR reruns the corresponding code and shows the results in the view. In technical words, your code is stateless.

  • All the input parameters are stored in the variable params. That is why it is important to pass it to def visualize_tower(self, params, **kwargs).
  • params is a Munch, which is like a Python dictionary. You can access the data inside params using dot-style notation. So, for example, to access height inside params, you write params.height.

Adding a sphere

At the top of the tower we add a Sphere.

1. Let’s import Sphere

from viktor.geometry import Sphere

2. We will add and input field to determine the Sphere’s radius:

...
class Parametrization(ViktorParametrization):

 base_height = NumberField("Height base", default=50, min=1)
 base_diameter = NumberField("Diameter base", default=3, min=1)
 sphere_radius = NumberField("Radius sphere", default=4, min=1)      # <-- Add this line

3. Go to our @GeometryView and add a Sphere under the lines that define the base. Sphere needs a point and a diameter. Don’t forget to return the Sphere at the end, so it is visualized. Your code should look like this:

@GeometryView("3D", duration_guess=1)
def visualize_tower(self, params, **kwargs):
  """Creates the 3D view and visualizes the Radio Tower"""

  # Reference points

  base_start = Point(0, 0, 0)
  base_end = Point(0, 0, params.base_height)

  # Create tower base

  line = Line(base_start, base_end)
  base = CircularExtrusion(diameter=params.base_diameter, line=line)

  # Create tower cabin

  cabin = Sphere(base_end, params.sphere_radius)  # <-- Add this line

  return GeometryResult([base, cabin]) # <--- put base and cabin in a list, so they are shown.

4. Again. Save the file and refresh the app.

Adding the mast

Now that you are used to VIKTOR, we ramp up the speed and quickly add the mast on top of our tower in the exact same way we have done before.

1. Import the code:

from viktor.geometry import Cone

2. Add the NumberField for the mast height:

mast_height = NumberField("Height mast", default=30, min=1)

3. Draw a cone and don’t forget to return it so it is visualized:

# Tower top

top = Cone(diameter=params.base_diameter, height=params.mast_height, origin=base_end) # <-- Add this line

return GeometryResult([base,cabin,top]) # <-- Update this line

4. Save the file and refresh.

Adding materials

Great, we created the full 3D model of the tower, but it is looking a bit pale. Let’s add some color, we don’t want a plane crashing into it 😉

We will add color, by creating some Material and adding them to the 3D models.

1. Import Material and Color:

from viktor.geometry import Material, Color

2. Add materials as constants under the imports and before the Parametrization class:

...

WHITE = Material("White", color=Color.white())  # <-- Add these lines too
RED = Material("Red", color=Color.red())        # <-- Add these lines too

class Parametrization(ViktorParametrization): # <-- this line is just for your reference

...

3. Add the materials to the different geometries we created before, as shown here:

...

# Create tower base
line = Line(base_start, base_end)
base = CircularExtrusion(diameter=params.base_diameter, line=line, material=WHITE)

# Create tower cabin

cabin = Sphere(base_end, params.sphere_radius, material=WHITE)

# Tower top

top = Cone(diameter=params.base_diameter, height=params.mast_height, origin=base_end, material=RED)

4. Save, refresh, and you should have a colorful tower:

What is next?

If you managed to get this far, you have done a great job and successfully built your first Python app with VIKTOR! VIKTOR has a large collection of functions and visualizations that await exploring. Feel free to go and check out their docs and start making your own engineering apps powered by Python.

And if you need any help, want to request features or share your creations, you can head over to the Community Forum.

Final code for parametric Radio Tower design

Just in case, here you can find the complete code of the app:

from viktor.core import ViktorController
from viktor.parametrization import ViktorParametrization, NumberField
from viktor.geometry import Point, Line, CircularExtrusion, Cone, Sphere, Material, Color
from viktor.views import GeometryResult, GeometryView

WHITE = Material("White", color=Color.white())
RED = Material("Red", color=Color.red())


class Parametrization(ViktorParametrization):

  base_height = NumberField("Height base", default=50, min=1)
  base_diameter = NumberField("Diameter base", default=3, min=1)
  sphere_radius = NumberField("Radius sphere", default=4, min=1)
  mast_height = NumberField("Height mast", default=30, min=1)


class Controller(ViktorController):
  label = 'VIKTOR Tutorial'
  parametrization = Parametrization

  @GeometryView("3D", duration_guess=1)
  def visualize_tower(self, params, **kwargs):
    """Creates the 3D view and visualizes the Radio Tower"""

    print("Params:", params)

    # Reference points

    base_start = Point(0, 0, 0)
    base_end = Point(0, 0, params.base_height)

    # Create tower base

    line = Line(base_start, base_end)
    base = CircularExtrusion(diameter=params.base_diameter, line=line, material=WHITE)

    # Create tower cabin

    cabin = Sphere(base_end, params.sphere_radius, material=WHITE)

    # Tower top

    top = Cone(diameter=params.base_diameter, height=params.mast_height, origin=base_end, material=RED)

    return GeometryResult([base, cabin, top])

Now although this web application is pretty simple, it’s effective and useful. Furthermore, this implementation opens a whole new world of opportunities to design engineering apps using minimal Python code base with VIKTOR which can be applied to pretty much any concept under engineering and science subdomains.

VIKTOR takes care of the cybersecurity, hosting, server administration, management of intangible assets such as  intellectual property and low-level programming and the result is an intuitive app creation process which can hopefully yield fruitful  discussions and/or presentations in projects with parametric design elements.

We will be publishing a number of professional Python tutorials in near future to continue demonstrating Python’s potential in sophisticated use cases in the field and help you gain inspiration that can result in real world outcomes.

You can find the Python source code and information regarding dependencies of this app at our Github Repository: Python-3D-Tower-Web-App source code.

Guest Author
Anande is our guest author who is also a pioneer in parametric design and digital engineering topics. He is based in Netherlands and he has a background in Aerospace Engineering and Mechanical Engineering fields.

Anande takes "Automate the Boring Stuff" philosophy by heart and contributes to increase efficiency in engineering applications with smart Python-based parametric design solutions @VIKTOR.

The post How to Create an app with a 3D model using VIKTOR appeared first on HolyPython.com.

Python Packaging: Hosting Code at Github

15 October 2020 at 04:54

Github Code Sharing and Development Platform

Github is a great platform for open source software. It grew so much in recent years as the interest in computer programming grew but it’s also such a great platform for sharing, collaborating and contributing.

A lot of people even create repos (or repositories) as archiving their information in an open environment. 

It’s common to see lists of links, books, movies, knowledge sources etc although it’s mainly developed as a platform to share code.

So, our advice is don’t be shy, jump on the wagon if you don’t have a Github account yet, you will soon discover it offers so many amazing synergies and it’s not that complicated either.

Github Logo

So, in a nutshell, you host and share code on Github, no matter how advanced or basic it is. There is no such criteria as your code must be extraordinary so don’t be intimidated. You can start with something like:

msg = "Hello World!"
print(msg)

just to test the waters…

Creating a Github Account

You can sign up for Github from the top right corner.

Creating a Github Account is simple. Just go to Github Signup Page. Just pick s unique username, set your password and game is on!

Github Sign Up Page

Once you sign up, you can start a repo. Think of repositories as individual projects, libraries, software, notebooks or products.

You can have any amount of repository in your Github account.

Just click New Repository and it will take you to repository initialization page.

Github New Repository Menu

Creating a Github Repository

Creating a repository is also easy. Just pick up a unique name. This name only has to be unique in your account. It doesn’t have to be globally unique. It will let you know with a green or red pop up message anyway.

You can also write a short description, why not. Although it’s optional it makes sense to at least jot a couple of words.

Recommended Reading:

Steps for Creating a Github Repository

Some important criteria to consider are:

    • Public vs Private: This option is straightforward. If you’d like to keep your repo as private no one will be able to see it except you and people you assign it to. Public repos are open to public view and contribution. You can also change it later.
    • Adding a README file: Checking this option almost always makes sense. It’s great to have a Readme file, content of which also shows up on the main page of your repository. You can always delete it later if you don’t want it or you can change it as well.
    • Adding a gitignore file: This file tells setup file which files not to include in a setup. So, it’s an ignore template. This is genius because there can be lots of irrelevant files like temp files from your IDE or from Python itself that you actually want to avoid in software development.
    • Adding a licence: Adding license can be more serious than it sometimes seems. If you’re in a rush you can always uncheck this option and add a license you choose later.

Thank you for checking out Github component of the Python packaging series. When you are ready you can also consider uploading your unique Python library to PyPI.

If you need some inspiration check out this post:

I’ve been learning coding. What’s next? (Python Edition)

The post Python Packaging: Hosting Code at Github appeared first on HolyPython.com.

Nvidia’s New Maxine Platform is Phenomenal Applied AI Example

8 October 2020 at 02:27

Nvidia just announced an innovative artificial intelligence application a couple of days ago and it’s big deal for a few reasons.

Let’s take a look at what it is and what it does first:

Friends doing teleconference with an open book on laptop

What is Nvidia Maxine?

Nvidia Maxine is an AI powered video streaming platform for developers. While it’s the platform that provides the technology that can be used for video calls or video conferencing, it’s not the end user product such as:

  • Skype, 
  • BlueJeans, 
  • Microsoft Teams, 
  • Google Duo, 
  • Hangouts,
  • Zoom 

It’s more the technology platform that applications could use if they wanted to.

AI became a chic word that’s used in so many products and/or services today but true AI innovation instantly shows off.

Traditionally, current video codecs (such as H.264) can be quite heavy on the internet connection due to its high quality and data size that comes with it. H.264 has been great for videos that are already recorded such as online movies, Netflix, Hulu, Youtube Videos etc. But when it comes to streaming it has the quality we want at a high internet speed cost.

Publicly trading video communications company Zoom has already seen a massive surge in stock prices going up to $485 in the spot market (as of October 7, 2020) from only approximately $70 in early January 2020 shortly before pandemic has broken out ZM:NASDAQ stock price on Google Finance.

What if we could have the support of predictive AI technology that made the application require 1/10 of the data size for a very similar image quality? That’s what Nvidia did.

Zoom shares price chart screenshot from Google Finance.

At this level Zoom’s market capitalization surpasses IBM’s approximate market cap of $110 billion US (IBM Market Capitalization Data on Nasdaq: IBM:NASDAQ) with $125 billion US.

Why is Nvidia Maxine special?

Nvidia’s Maxine implements Generative adversarial network, GANs, to create a deep fake of your own self.

Basically, what happens is, once the deep fake structure is created your movements, mimics, facial expressions, gestures and mannerism are transferred through the AI powered image that’s displayed to your counterparty. Apparently this allows massive amounts of data saving without overwhelming GPU or CPU too much.

We have already been seeing funny (or scary depending on the application and from where you look at) deep fake videos surface the internet recently. This is the immature phase of a technology when it first emerges and nobody is quite sure what to do with it.

Nvidia Maxine vs H.264 Demonstration Screenshot

I believe if Nvidia Maxine is mass adapted, which it definitely might especially if it can achieve its promises like 10x data saving, this will be the beginning of a new are where AI and particularly GAN are becoming mature end-user products and services.

Given the pandemic, more people than ever need this technology at affordable rates and 

Nvidia’s AI innovation couldn’t be timelier.

Given the public activities on Nvidia’s public Github repository and their extensive usage of Pytorch and Tensorflow for machine learning applications, Nvidia is highly likely to be using Python for their Maxine streaming platform as well.

Although Nvidia states the data size savings are up to 10x on the official webpage of Maxine here. In the promotion video Bandwidth usage per frame is announced as almost 900x ( 97KB vs 0.11KB ). There seems to be a confusion here but moving passed that, 10x data reduction without much quality loss is still huge deal.

On top of data savings Nvidia Maxine also promises to convert videos with 360p quality to 720p in a very realistic way. Here is the interesting promotion video from Nvidia AI Research:

Global Internet Usage

As of 2020, approximately 4.5 billion people have some form of internet access while 3.5 billion people still has no internet access whatsoever. 

According to UNESCO data this approximately corresponds to a ratio of 55% to  45% over the global population. This is absolutely a heart-breaking number considering how much we get done through the internet today both for business and pleasure.

During the pandemic times of Covid-19, for 100s of millions of people video conferencing has been a life saver (in addition to yoga, online shopping, dark chocolate, video games, video streaming services and remote work to name a few). Unfortunately, not all of us are equally blessed with the availability of tech at the tip of our fingers. 

On the flip side though, we have some good news. Internet usage stats will continue to get better and according to Cybersecurity Almanac (co-published by Cybersecurity Ventures and Cisco), global internet access percentage will increase to 6 billion in 2022 and all the way up to 7.5 billion in 2030 (90% of the projected population of 6+ yo ).

And, although 55% of the world seems to have access to internet, it doesn’t mean they all have high broadband connections. 6% of internet users in the US and 13% of internet users in Australia still has slow speed internet. (Reference: Weforum Internet Access Article)

Internet access statistics are approximately 9 in 10 in developed countries, 5 in 10 in developing countries and only barely 1 in 10 in underdeveloped countries.

Internet Logo in Front of Store

Also, when the internet is less available it tends to be very expensive and restricted and this can be a huge deal breaker for someone trying to put food on the table.

One thing these numbers expose is that video streaming, whether it’s for entertainment, online education (MOOCs) or video conferencing, is still nearly impossible for billions of people in 2020.

This statistical contrast makes it easier to comprehend the need of innovation in video streaming and to what extent it can change lives.

Future of Nvidia Maxine

It’s probably safe to conclude that this is just the beginning. Beginning of a very powerful and disruptive technology starting to creep into our lives.

Given the potential Maxine platforms enables, it’s probably safe to assume that its adaptation will be widespread very fast. We will watch and see how thing unfold for Nvidia’s Maxine platform.

Although Maxine already has the potential to make video conferencing available for millions of people with poor internet connection and save data at global scale we can see this application going further in terms of technological innovation.

Some of the applications Nvidia’s Maxine implementation can pioneer are:

  • VR
  • 3-D holograms
  • Online News
  • Presentations
  • MOOCs
  • Podcasts
  • Suitable Vlogs

On the flip side, this technology will inevitably also be used by people with malintent and we can see a surge in unpleasant experiences caused by:

  • online telemarketing
  • impersonation fraud 
  • more sophisticated scams
  • pranks
Let’s hope that our averaged out mutual conscious as the human kind continues improving as fast as technologies. We can also do more than hoping though. Best thing to do is familiarize and educate yourself and Holypython already offers a plethora of basic Python lessons, Python tutorials and Python exercises for online practice.
 
Techradar also has a pretty nice coverage on the news of Nvidia Maxine, you can find the Techradar article here.
 

You are also more than welcome to visit our recent Machine Learning section.

The post Nvidia’s New Maxine Platform is Phenomenal Applied AI Example appeared first on HolyPython.com.

How to Use Watermarkd: A Batch Watermarking Library with GUI

2 October 2020 at 17:24

What is watermarking?

Watermarking is a technology of printing less opaque text or symbols on images and items. Believe it or not it’s considered an ancient technology as old as paper since it’s been used on special letters, stamps and printed currency.

As digital imaging has never been such a humongous part of humans’ lives, the usage, creation and demand related to digital images rises as well. Despite the rise of open source and sharing mentality in the digital community, there seems to still be significant room for watermarking applications and so the demand for innovation continues.

Star shaped watermarks among with other patterns on 100 Euro banknote

Why Watermarkd?

Watermarkd is a Python library published by Holypython.com under the open source license Apache-2.0.

In this post we will try to demonstrate what can be accomplished with Watermarkd library.

Watermarkd library allows users to handle watermarking operations in Python directly from the terminal or through use of an optional GUI component.

Since it’s specialized only for watermarking tasks, Watermarkd has a very light graphical user interface. You can probably get an image watermarked (or watermarkd!) during the time it takes to start up a major Image Manipulation software.

It has the most commonly used features when it comes to watermarking and expected to cater to 90% or more of the people looking to watermark an image.

How does Watermarkd work?

Currently, Watermarkd library consists of one sole class named Spread which houses two functions that spread out watermark text on image(s):

  • single function: Spread.single() can be used to apply watermark on a single image (photo).
  • batch function: Spread.batch() can be used to apply watermark to multiple images in a folder.
Here are some images created with Watermarkd on the go.
Medical Field Watermarking
Business confidentiality communicated through watermark

Below you can see various usage types of Watermarkd Library under four titles:

– Single Function Usage

– Batch Function Usage

– Single Function Usage with GUI

– Batch Function Usage with GUI

Single Function Usage

Single function can be used in two main ways:

  • through command line without any interface using its parameters
  • through GUI by enabling gui parameter via True boolean.

as simple as passing True argument to gui as below:

single(gui=True)

Let’s first investigate usage scenarios without gui:

import Watermarkd as wmd

wmd.Spread.single(img_path=r"c:/myimage.png")

This code, once executed, will create a watermarkd image on the user’s Desktop. You don’t have to assign gui parameter to False since it’s the default option. So, basically it’s the same thing as this code:

wmd.Spread.single(gui=False, img_path=r"c:/myimage.png")

Also, please note that, when not using gui, the only parameter you have to pass an argument to is img_path, since all the other parameters have default values and hence, they are optional.

Let’s continue to explore other parameters that can be passed to the single function:

 

  • gui, (bool), default False : enables GUI
  • img_path, (string), mandatory parameter :signifies image path
  • wm_text,  (string), default= “Watermarkd”) : Watermark Text
  • wm_trans, (int [1-255]), default= 85 : Signifies Watermark Transparency 
  • font_size, (int), default= 55) : Watermark Font Size
  • font_name, (string), default= “arial.ttf” : Font Type
  • filename, (string), default=”Watermarkd” : File Name for Saving
  • save_to_path, (string), default=”Desktop/watermarkd_” : Saving Folder Path
  • save_to_suffix, (string), default=”.png” : File Type for Saving 
  • output_filename, (default= r”c:/Users/”+user_path+”/Desktop/watermarkd.png”) : File Name For Saving

Here is another example, in which watermark text is assigned to “Inception”:

from Watermarkd import Spread

f = r"c:/Users/ABC/Desktop/film.png"
Spread.single(img_path=f, wm_text="Inception")
Film Making Industry Watermarking

Batch Function Usage

Batch function is very similar to single function with a few subtle differences. First and foremost, it handles a folder of images rather than a single image. 

So, img_path parameter is exchanged with folder_path parameter.

Otherwise the rest of the differences are mainly internal which you can check out in the source code if you like.

from Watermarkd import Spread

Spread.batch(folder_path=r"c:/User/ABC/New_Photos")

This code will read all the images from the given folder New_Photos, and create an output folder named Watermarkd_ in user’s Desktop and save all the watermarked files there with the default values for optional parameters. Check out a list of parameters below for adjusting different values such as: watermark text, font size, font type, file name, path name, transparency etc.

If you check out the source code, most of the variable names are also conveniently borrowed from the single function.

Regarding the inner workings of the batch function, the main thing is: it doesn’t apply the watermarking algorithm to a single image, instead watermarking algorithm and positioning etc are placed inside a for loop which iterates through the images in the given folder.

So, in simpler words, folder is iterated with a for loop, image file name is taken which becomes the img_path similar to single function, then watermarking is applied and then files is saved and next iteration starts with the next file.

Here is another example:

from Watermarkd import Spread

f=r"c:/User/ABC/New_Photos"
Spread.batch(folder_path=f, wm_text="Photographer A. C. Jonah, #927-654-92**")
  • gui, (bool), default False : enables GUI
  • folder_path, (string), mandatory parameter  :signifies folder path
  • wm_text,  (string), default= “Watermarkd”) : Watermark Text
  • wm_trans, (int [1-255]), default= 85 : Signifies Watermark Transparency 
  • font_size, (int), default= 55) : Watermark Font Size
  • font_name, (string), default= “arial.ttf” : Font Type
  • filename, (string), default=”Watermarkd” : File Name for Saving
  • save_to_path, (string), default=”Desktop/watermarkd_” : Saving Folder Path
  • save_to_suffix, (string), default=”.png” : File Type for Saving 
  • output_filename, (default= r”c:/Users/”+user_path+”/Desktop/watermarkd.png”) : File Name For Saving
More Photo Examples:
Model Promoting Holypython.com Watermarked with Watermarkd
Young Model Posing For Photography Agency with Watermarkd
Univeristy of Utah Campus Watermarked with Watermarkd
New Release Poster for Software

Watermarkd Usage: Single Function with GUI

Usage with GUI is pretty straightforward.

  1. Pick an image file
  2. Type your watermark text
  3. Choose a watermark text size. Options are:
    • Normal (by default)
    • Small
    • Large
    • Value: Let’s a custom font value to be entered. Possibly something like ~150 for high rez images where something like 30 might suffice for a low resolution image.
  4. Transparency: This value defines the transparency (or opacity) of your watermark text. 85 is the default value but you can go all the way down to 0 for a completely transparent text (Watermark would be invisible then). 255, the maximum value will create a solid white watermark text (which is more just a white text than watermark since it’s not transparent at all. It can sometimes be useful nevertheless.)
  5. Save as: Different options to save watermarked image as different image types such as:
    1. png (default)
    2. jpg
    3. gif
    4. bmp
  6. Save to: Path or folder that you’d like to save your watermarked image to. You can either type it or choose it with the help of the Path button.
  7. Filename: Filename you’d like to save your watermarked image under.

Once submit is pressed all the inputs from the user get registered and watermarking process starts.

It can last anywhere between miliseconds to a couple of seconds depending on the resolution of the image. (For the very high resolutions you might need to allow 2-3 seconds which also depends on the availability of computation resources.)

Just as GUI is a new dimension for the Python coder, Packaging also is a new dimension that opens up a whole new world of opportunities and skillset.

To activate GUI component all you have to do is:

Spread.single(gui=True)

You don’t have to pass any other arguments to setting parameters since they’ll be overridden after the GUI is executed.

from Watermarkd import Spread

Spread.single(gui=True)
Single Function's GUI Component in Watermarkd

Watermarkd Usage: Batch Function with GUI

Usage with GUI in batch function is also simple and straightforward. It’s mostly overlapping with single function’s steps. Here are the main differences:

  • Pick a folder instead of a single image through the Browse button.
  • At the bottom file name will be used as a seed to generate multiple images with watermark. For instance, if you choose Work as file name, files will be saved as Work1.jpg, Work2.jpg, Work3.jpg etc.

Once submit is pressed all the inputs from the user get registered and watermarking process will start.

It can last anywhere between miliseconds to a couple of seconds per image depending on the resolution of the image. (For the very high resolutions you might need to allow 2-3 seconds per image which also depends on the availability of computation resources.)  After that time watermarked images will be created in the specified folder or default folder if none is specified.

Activate graphical user interface for bath watermarking a folder of images similar to the code in single() function’s case:

from Watermarkd import Spread

Spread.batch(gui=True)
Watermarkd batch function's GUI window

Further Steps with Watermarkd

As much as I’d love to see Watermarkd getting used by people with different backgrounds such as:

  • Photographers 
  • Bloggers
  • Entrepreneurs and Business Owners
  • Students
  • Teachers
  • Media Agencies
  • Artists, Illustrators

I’d also love to see it being checked out as a learning tool for Python packaging and Python coding in general. It has all the ingredients necessary to comprehend packaging in Python.

Besides packaging topics such as: Pypi repository publishing, Github hosting, licensing etc. It also has fundamental coding topics, but nothing too complicated to discourage an intermediate or even beginner coder, such as:

Additionally, it’s pretty simple to understand and demonstrates all the ingredients needed to publish a library on PyPI (Python Package Index), such as:

    • setup.py
    • requirements.txt
    • License
    • Readme.md
    • __init__.py
Every great coder was a beginner at some point. So, don’t be too shy. One step at a time you too can become great, and create great programs,  there is no doubt about it. Some helpful topics to discover can be:

Such topics open up whole new worlds for a programmer to progress towards, you can see them as paths you can take in your journey(Oftentimes you can combine these paths for a great product as well).

Final Thoughts

If you’re a beginner or intermediate programmer go ahead and create a Github account. Maybe create a trivial repository where you take some notes and save some files if you like, just to start getting familiar with the environment.

If you like Watermarkd or Holypython’s work in general, you’re welcome to visit our Github repos as well. You can read the Watermarkd.py source code along with other necessary files there (setup.py, requirements.txt, __init__.py etc.). 

As simple as it is, I hope Watermarkd library serves as a practical solution for people who might need watermarking and I’d love to see it serve educational purposes for coders and developers who never had a chance to explore packaging and publishing topics related to Python.

Thank you so much for visiting.

ps: I’d like to thank creators and contributors of Pillow the Friendly PIL Fork and PySimpleGUI libraries for creating such fantastic libraries and influencing further developments. Also, a huge thank you to Stack Overflow community for sharing so much expert level knowledge and being so kind.

The post How to Use Watermarkd: A Batch Watermarking Library with GUI appeared first on HolyPython.com.

Python Packaging: Quick Tutorial & Checklist

1 October 2020 at 05:58

Packaging is a very exciting process for the coder. Packaging allows you to share your work with the world and step into a structured, real developer position rather than dealing with bits and pieces of code here and there. And I don’t mean position in a corporate sense here, after all you know your position better than everyone else.

So, this tutorial is about packaging and it can help you navigate the pretty standardized steps of Python packaging and all the intricacies that come with it.

Estimated Time

10 mins

Skill Level

Upper-Intermediate

Exercises

na

Posts from the Series

Course Provider

Provided by HolyPython.com

This page is a practical summary version of all the steps involved in Python library packaging. You can also find links for longer versions of each step with more detailed explanations, comments and remedies for potential pitfalls at the bottom of this page along with a Python packaging checklist.

If you’re reading this tutorial you might already have a cool invention or an improved version of some other program. In that case congratulations! You’re one step closer to creating wealth for yourself and also for the world.

If you don’t have something tangible yet don’t worry, it’s something that comes with sustained practice. I don’t mean to be corny but, just continue coding and exploring and if you stay at it it’s almost guaranteed to happen. When the inspiration is there though, it’s advised not to sleep on it, so when the moment comes, don’t put it off, don’t delay, act and realize!

So, here is a summary of the major Python packaging cornerstones:

  1. Getting the Code Ready for Packaging
  2. Hosting at Github
  3. Open Source Software Licenses
  4. Necessary Files (setup.py, readme.md, license, __init__.py, requirements.txt, setup.cfg)
  5. Local Installation, tests and __init__.py File
  6. Uploading to PyPI with Twine
Long version links at the bottom.
A routine startup portrait, members discussing in front of computers

At some point in your coding journey, eventually you realize the power of publishing. Open source is really cool and it enables sharing technical knowledge and intellectual products at an unprecedented level. But only when you experience first hand:

  • An open source program development on Github
  • Publishing your program on a library like Pypi. Maybe at different stages like:
    • Pre-alpha,
    • Alpha,
    • Beta,
    • Release Candidate,
    • Stable Release)

you really start to feel the incredible power of open source synergies.

It is recommended to every programmer, yet, the knowledge gap seems to prevent many people from getting to that stage.

It’s not that difficult, but there is an open source terminology involved which can be confusing for a lot of people.

This is a huge value loss for everyone, for the programmer, for his environment, eventually for the whole world. Because you never know how far that programmer is going to be able to go if those gaps are bridged and dots are connected.

So, hopefully this Python tutorial empowers coders at the beginning stages of their journey and helps them understand how to package Python code, how to create a library, how to create a module. You might not necessarily be a beginner programmer, but you might not have discovered these topics yet or had the opportunity or occasion for it.

Nevertheless, understanding library structures and entering open source world really expands a coders horizons and we recommend stepping into this knowledge space as soon as possible since the benefits are so high.

You might have just learned how to print “Hello World” or you might actually be producing advanced level programs already and just not have stumbled upon this knowledge yet. In the end, it’s all good. Everyone’s journey is unique and it’s important to embrace and strive to progressively move forward.

 

Let’s check out the general frame of the packaging process and if you’d like to go deeper in any of the steps we have links for that too.

Python Packaging Preparation

After you have a functioning code you will want to do a certain type of cleaning up on it because, code in a library is slightly different than the code in a local .py file that’s used for personal tasks and development. Here are some common practices:

  • Clean up the unnecessary comments
  • Clean up the part or all of print functions not to cloud user’s terminal too much
  • If something can be expressed as a user-defined function, then consider building some functions.
  • Tidy up via Python classes. Try to place everything inside a class or multiple classes. User-defined functions can be run from inside the class when needed. For example:
    • class A:
      • def alpha():
  • A.alpha()

Before we advance, and after certain improvements on your code you also need a few tools for packaging so you might want to get them ready. These are:

  1. A Github account
  2. A PyPI account
  3. Command Prompt that works with Python (or PowerShell), we suggest Anaconda Command Prompt as a practical and complete solution.
  4. twine library installed in Python for uploading to PyPI

Creating a Github Repository

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

python setup.py sdist

Choosing a license

If you’re sharing your code you need a license so that important aspects of usage, development, price, modification, reselling, responsibility, sublicensing etc. are more clear.

Most common open source licenses are:

Apache-2.0

GNU GPL

GNU LGPL

MIT License

BSD Licenses

More on open source licenses here.

Necessary Files

You need a couple of files that you might not be familiar with in order to package your Python library and turn it into an installable repository. These files are:

setup.py

requirements.txt (optional)

readme.md (optional)

setup.cfg (optional)

__init__.py (optional)

So, before being misleading, although it seems only setup.py is obligatory for Python installation and this is technically true, all of the files above are pretty obligatory in practical sense. You don’t really want to miss any of them.

python setup.py sdist

Creating a Release on Github

Github is a great platform for coding collaboration, version management and open source sharing. It’s free and you simply sign up and create a new repository.

You might want to skip the license selection for your project at the very beginning if you’re not informed on the topic yet.

Local Installation & Testing

First off, you can just navigate to the root folder which hosts your package and import the package after launching Python there. This is as simple as navigating with cd command and then launching Python and trying to import the library as intended.

Eventually though you will want to make a real installation to see if everything is going through smoohtly. 

You can use the code below for a local installation, first create a source distribution, it will create a Python egg and source distribution in the folder where your package exists.

Installing a library locally is very simple and straightforward. Just navigate to the package’s root folder where setup.py can be found and execute the command below:

pip install .

This one will properly install the package inside the site-packages folder under your Python installation folder.

It can be more convenient if you don’t want to have to navigate to the specific directory to import your library and it’s still a local installation and has nothing to do with uploading.

After installation is successful, you can just import your library like any other library:

import [library_I_developed]

At this point, you can make the final checks, see if library is installing and functioning as it’s supposed to, you can also check if __init__.py files are functioning properly and creating the correct importing structure you intend for your users.

Uploading to PyPI

If everything is looking absolutely fine it’s time to step into the wild. Wild in this case is Python Package Index or PyPI.

Egg and source distribution terminology can be confusing. Source distribution is just your library packaged in a .zip or .tar.gz compressed archive formats. Installation occurs through these source distributions.

Egg is pretty much the same thing and it’s a zip file, it’s just a bit specialized and distinct so it’s extension is .egg instead of .zip. You can still open it as a zip file and check out the content if you’re curious.

So, let’s create the source distribution:

python setup.py sdist

This will create a source distribution which is basically a tar.gz or zip file as mentioned above.

Now you have a source distribution ready to be shipped to PyPI however, it’s beneficial to add one more step. It takes a couple of seconds but it can save you lots of headache.

Using twine check all the distributions inside the package. If there is an error with the setup.py file or readme.md file or any of the other ones, it will tell you what the error is so you can fix it and ship again.

import twine

twine check dist/*

Once everything clears 100%, it’s time to upload. Just execute the command below:
Don’t forget to import twine if you haven’t before.

twine upload dist/*

You’ll be asked your PyPI username and then password, just type those in order and press enter and your package will be uploaded.

If update to PyPI is successful, that means that now your library can be installed on any computer with Python and internet connection all around the world. All that’s needed is the execution of this simple pip command:

pip install [Library_Name]

Just get rid of the brackets around your library name when you’re actually installing it.

Updating the package in PyPI

You will likely have new versions every now and then just create a new source distribution and repeat the check and upload steps and your package will be updated automatically.

python setup.py sdist
import twine

twine check dist/*
twine upload dist/*

After that you might want to update your library in your computer from PyPI. Just run this code for that:

pip install [Library_Name] --upgrade 

If you still have question, each step included here has its own long version below and maybe the answers you’re looking for are there. It can still be a good idea to check them out even if you don’t have any questions since there are some cool knowledge included regarding each step of Python packaging.

The post Python Packaging: Quick Tutorial & Checklist appeared first on HolyPython.com.

❌