ProgrammingPro #4: Python 3.10 Runtime in Lambda, Cobra 1.7 Framework, Nanobind
Hi,
I hope you’ve been doing well! Thank you being a part of the ProgrammingPro community and sharing your thoughts on the last issue.
For those who’ve recently joined us, in this newsletter, we detail the most relevant industry insights, actionable tutorials, and useful tools and resources for developers and software engineers.
In today’s issue:
News and Analysis
Secret Knowledge: Articles & Trending Repos
Tutorial: Exploring Thread Support in C++
After Hours (Game): Decipherism
My promise with this newsletter is to act as your personal assistant and help you navigate and keep up-to-date with what's happening in the world of software development. What did you think of today's newsletter? Please consider taking the short survey below to share your thoughts and you will get a free PDF of the “Cybersecurity: The Beginner’s Guide” eBook upon completion.
Thanks for reading.
Until next time!
Kartikey Pandey
Editor-in-Chief
Complete the Survey. Get a Packt eBook for Free!
News and Analysis
Python 3.10 Runtime Now Available in AWS Lambda: With this release, Python developers can now take advantage of new features and improvements introduced in Python 3.10 when creating serverless applications on Lambda.
PyPI Introduces “Trusted Publishers”: PyPI package maintainers can adopt a new, more secure “OIDC authenticated” publishing method that does not require long-lived passwords or API tokens to be shared with external systems.
ChatGPT Releases Incognito Mode: OpenAI announced a new feature for ChatGPT that lets you disable your chat history. It’s ChatGPT equivalent of incognito mode in your browser. Any conversations that take place while chat history is disabled will not be used to train OpenAI’s models or appear in the history sidebar.
DeepFloyd IF Code Released: The next image model from Stability AI has a code release. It is a multi-stage model with a frozen T5 text encoder and two super resolution models. One big win here is the ability to render extremely high quality text. The models available in this codebase have known limitations and biases. Please refer to the model card for more information.
Secret Knowledge: Articles & Trending Repos
How to Improve your Django Code with pre-commit: This tutorial covers a variety of tools you can attach to your repo’s pre-commit hook to validate your code. Although the article is from a Django perspective, all but one of the tools covered is Django-agnostic.
Cobra 1.7: A Framework for Building Modern CLI Apps: A significant project in the Go CLI space oriented around structure, subcommand based CLIs, flags, generating man pages, etc. It was originally created for Hugo but is now used by numerous projects.
Jet 2.10: Type Safe SQL Builder with Code Generation: A database access solution consisting of a type-safe SQL builder with code generation and automatic query result data mapping. It supports PostgreSQL, MySQL, CockroachDB, MariaDB and SQLite.
Example of LLM Prompting for Programmers: Learn by example how to use chain of thought and general knowledge prompting with ChatGPT when writing self-testing code.
How to Build Reproducible Python environments with XARs: XAR is an archiving format that can contain a tree of files. This article details how they can be used to package Python environments for deploy-ability.
Python Packages - A Primer for Data People: This article discusses what Python packages are and how to use them. It also cover specific topics that will help you understand what’s involved in structuring a Python project, and how this translates to more complex builds such as data pipelines and orchestrators.
nanobind - Tiny and Efficient C++/Python Bindings: nanobind is a small binding library that exposes C++ types in Python and vice versa. Benchmarks show up to ~4× faster compile time, ~5× smaller binaries, and ~10× lower runtime overheads compared to pybind11. nanobind also outperforms Cython in important metrics.
Self-healing Code and the Obvious Issue: In this article, Gynvael Coldwind reflects on Wolverine (when you run Python scripts with Wolverine, when they crash, GPT-4 edits them and explains what went wrong) and the use of ‘self-healing’ programs that can repair themselves with the help of AI. This article also highlights how a simple script could be used by a malicious actor to trick the program and add a prompt injection that might fix the code in an undesirable way.
Tutorial: Exploring Thread Support in C++
Prior to C++11, threads were completely out of the scope of C++ as a language. Developers could use platform-specific libraries, such as pthreads or the Win32 application programming interface (API). Since each library has its own behavior, porting applications to another platform required significant development and testing efforts.
C++11 introduced threads as part of the C++ standard and defined a set of classes to create multithreaded applications in its standard library.
In this tutorial, we will learn how to use C++ to spawn multiple concurrent threads in a single application.
How to do it...
In this tutorial, we will learn how to create two worker threads that run concurrently.
In your ~/test working directory, create a subdirectory called threads.
Use your favorite text editor to create a threads.cpp file in the threads subdirectory. Copy the code snippet into the threads.cpp file:
#include <chrono>
#include <iostream>
#include <thread>
void worker(int index) {
for (int i = 0; i < 10; i++) {
std::cout << "Worker " << index << " begins" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "Worker " << index << " ends" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
int main() {
std::thread worker1(worker, 1);
std::thread worker2(worker, 2);
worker1.join();
worker2.join();
std::cout << "Done" << std::endl;
}
Create a file called CMakeLists.txt in the loop subdirectory, with the following content:
cmake_minimum_required(VERSION 3.5.1)
project(threads)
add_executable(threads threads.cpp)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_CXX_FLAGS "--std=c++11")
target_link_libraries(threads pthread)
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabi-g++)
How it works...
In this application, we defined a function called worker. To keep the code simple, it does not do much useful work, only printing Worker X starts and Worker X ends 10 times, with 50 milliseconds' delay between the messages.
In the main function, we create two worker threads, worker1 and worker2:
std::thread worker1(worker, 1);
std::thread worker2(worker, 2);
We pass two parameters into the thread constructors:
A function that runs in the thread.
A parameter for the function. Since we pass the previously defined worker function as a thread function, the parameter should match its type—in our case, it is int.
This way, we defined two worker thread that do the same job but have different indices—1 and 2.
The threads start running immediately as soon as they are created; there is no need to call any additional methods to start them. They are executed completely concurrently.
The output from our worker thread is mixed, and sometimes garbled, such as Worker Worker 1 ends2 ends. This happens because output to the Terminal is also working concurrently.
Since worker threads are executed independently, the main thread has nothing to do after creating the worker thread. However, if the execution of the main thread reaches the end of the main function, the program terminates. To avoid this, we added calls to the join method for each of our worker threads. This method blocks until the thread terminates. This way, we exit the main program only after both of the worker threads complete their work.
This tutorial is an extract from the book Embedded Programming with Modern C++ Cookbook written by Igor Viarheichyk and published by Packt Publishing.
After Hours..
Decipherism: A puzzle game where you solve the encoding machine ciphers. You'll hack a Penth Hexagon using some retro-style hacking device with bright green consoles.