Bite-sized actionable content, practical tutorials, and resources for Python programmers.#58HTTP Requests Demystified, Goat vs. Car 🐐🚗, and Python's Dependency DilemmaHi ,Welcome to a brand new issue of PythonPro!In today’sExpert Insight we bring you an excerpt from the recently published book, Learn Python Programming - Fourth Edition, which introduces HTTP requests using Python's requests library, with examples of performing GET and POST requests against the httpbin.org API.News Highlights: Ollama 0.4 adds Python function tools and improved JSON schema with Pydantic; Preswald simplifies metrics setup in VSCode with AI-powered data engineering.My top 5 picks from today’s learning resources:Goodbye Make and Shell, Hello... Python?🛠️➡️🐍🚪🐐🚗Goat or Car? Solving The Monty Hall Problem With Python and NumPyContinuous Integration and Deployment for Python With GitHub Actions🔄📦🚀🔥Python dependency management is a dumpster fireHow to Check if a Python String Contains a Substring🔍🔗📜And, today’s Featured Study, introduces Stateful Behaviour Trees (SBTs), an evolution of Behaviour Trees (BTs), tailored for dynamic and safety-critical systems, and BehaVerify, a tool for scalable formal verification, integrating with Python libraries and tools like nuXmv.Stay awesome!Divya Anne SelvarajEditor-in-ChiefSign Up|Advertise🐍 Python in the Tech 💻 Jungle 🌳🗞️NewsOllama Python library 0.4 with function calling improvements: The version introduces enhanced functionality, including the ability to use Python functions as tools, improved JSON schema generation using Pydantic and more.Preswald – AI Data Engineer in VSCode for Local Testing, Metrics: Preswald is an AI-powered data engineer integrated into VSCode, designed to simplify setting up and managing startup metrics within the codebase.💼Case Studies and Experiments🔬Vector animations with Python: Demonstrates creating dynamic vector animations using Python with Gizeh for vector graphics and MoviePy for video and GIF generation.AnyChart integration for the Financial Trading Dashboard with Python Django: Details integrating AnyChart into a Python Django-based Financial Trading Dashboard, replacing D3.js for treemaps and adding interactive stock charts.📊AnalysisGoodbye Make and Shell, Hello... Python?: Advocates using Python for build and project tooling over traditional Makefiles and shell scripts, emphasizing its benefits for maintainability, debuggability, and flexibility.Constraints are Good: Python's Metadata Dilemma: Discusses Python's metadata challenges, emphasizing the lack of constraints in its system compared to JavaScript's structured approach.🎓Tutorials and Guides🤓Goat or Car? Solving The Monty Hall Problem With Python and NumPy: Explains the Monty Hall problem and demonstrates, using Python and NumPy, that switching doors increases the probability of winning.Continuous Integration and Deployment for Python With GitHub Actions: Covers workflow creation, automated testing, secure credential handling, dependency updates, and deployment to PyPI with practical examples.Python's F-String for String Interpolation and Formatting: Demonstrates their use for interpolating variables, expressions, and formatting into strings efficiently, with enhanced readability and performance.Basic Input and Output in Python: Explains Python's basic input and output functions, focusing on input() for capturing user input and print() for displaying output, with advanced features like formatting and enhanced input collection.Advanced Python Development Workflow in Emacs: Explores Emacs's extensibility, allowing for a personalized development setup comparable to established IDEs like PyCharm or VS Code.Augmented Reality with Python and OpenCV (part 3): Describes improving an augmented reality (AR) application built with Python and OpenCV by implementing a Kalman filter for tracking and stabilization.🎥Let's build a AI Photo Generator with Python and FastAPI: Demonstrates fine-tuning the open-source Flux image generation model using Replicate's AI services to generate personalized AI-generated images at minimal costs.Django and Postgres for the Busy Rails Developer: Shares insights from a Rails developer’s experience with Python, Django, and Postgres, highlighting differences and similarities in runtime management, library use, an more.🔑Best Practices and Advice🔏What Does if __name__ == "__main__" Do in Python?: Details the idiom'spurpose for controlling code execution when a file runs as a script versus when it’s imported as a module, along with usage scenarios and best practices.How to Check if a Python String Contains a Substring: Explains methods to check if a Python string contains a substring, focusing on the in operator for simplicity, along with alternatives like .count(), .index() , and pandas.Python Exceptions: An Introduction: introduces Python exceptions, explaining how to handle errors using try, except, else, and finally blocks, raise exceptions, and create custom ones to ensure robust error management.Python dependency management is a dumpster fire: Advocates for best practices such as using virtual environments, explicit dependency management with tools like Poetry, and avoiding global package installationsSome notes on my experiences with Python type hints and mypy: Discusses the limitations of type aliases versus NewType for preventing type confusion, the inability to use NewType with certain operations, and more.🔍Featured Study: Formalising Stateful Behaviour Trees for Advanced System Verification💥In the paper, "Formalising Stateful Behaviour Trees," presented at FMAS 2024, Serbinowska et al. explore the formalisation and verification of SBTs. The study aims to expand Behaviour Trees' capabilities, ensuring their reliability in dynamic and safety-critical applications through enhanced computational modelling and verification techniques.ContextBTs are modular, hierarchical controllers widely used in robotics and AI for managing complex systems. They organise tasks into a tree structure, enabling flexible and scalable behaviour design. However, traditional BTs lack persistent memory, limiting their use in state-dependent or dynamic environments.SBTs address this gap by incorporating a shared memory (blackboard), allowing them to track auxiliary variables and adapt to environmental changes. This makes them suitable for advanced applications, such as autonomous systems, where predictability and safety are crucial. The study also introduces BehaVerify, a tool designed to formalise and verify SBTs, which integrates with Python libraries and supports model-checking tools.Key Features of SBTsShared Blackboard Memory: SBTs include a persistent shared memory, called a blackboard, which allows tracking of auxiliary variables across tasks and ticks. This feature enables dynamic systems to adapt to changes in their environment.Enhanced Computational Power: The study establishes that SBTs are computationally equivalent to Turing machines when the blackboard uses infinite memory and to finite state automata when memory is constrained. This versatility allows SBTs to model a wide range of system behaviours.Domain-Specific Language (DSL): The authors introduce a DSL specifically designed for creating SBT models. This DSL generates Python-compatible implementations and integrates with tools like nuXmv for formal verification.Scalability: BehaVerify, the tool developed in the study, demonstrates the ability to verify trees with up to 20,000 nodes, outperforming existing tools such as MoVe4BT, which struggles beyond 250 nodes.Fast-Forwarding Mechanism: To reduce computational overhead, BehaVerify condenses the execution of multiple tree ticks into single computational steps, significantly enhancing verification speed and performance.Versatility in Applications: SBTs can model deterministic systems like finite state machines and handle complex, nondeterministic behaviours, making them suitable for safety-critical applications in robotics and AI.What This Means for YouThis study is highly relevant for developers and researchers in robotics, AI, and safety-critical systems. For Python programmers, the integration of BehaVerify with libraries like PyTrees simplifies the design and testing of stateful, autonomous behaviours. The ability to verify temporal logic specifications ensures robust system performance, making SBTs a powerful tool for advanced applications, from autonomous vehicles to robotic mission planning.Examining the DetailsIn key experiments, such as the “Bigger Fish” and “Simple Robot” scenarios, BehaVerify verifies trees with up to 20,000 nodes and handles extensive state spaces. The inclusion of a fast-forwarding mechanism condenses execution steps, significantly improving verification speed. Real-world examples, including a drone tracking moving targets in dynamic environments, illustrate the tool’s practicality and relevance for safety-critical systems.You can learn more by reading the entire paper and accessing BehaVerify.🧠 Expert insight💥Here’s an excerpt from “Chapter 8: Files and Data Persistence” in the book, Learn Python Programming - Fourth Edition by Fabrizio Romano and Heinrich Kruger.Making HTTP requestsIn this section, we explore two examples on HTTP requests. We will use therequestslibrary for these examples, which you can install withpip, and it is included in the requirements file for this chapter.We are going to perform HTTP requests against the httpbin.org API, which,interestingly, was developed by Kenneth Reitz, the creator of the requestslibrary itself.This library is among the most widely adopted:# io_examples/reqs.pyimport requestsurls = { "get": "https://httpbin.org/get?t=learn+python+programming", "headers": "https://httpbin.org/headers", "ip": "https://httpbin.org/ip", "user-agent": "https://httpbin.org/user-agent", "UUID": "https://httpbin.org/uuid", "JSON": "https://httpbin.org/json",}def get_content(title, url): resp = requests.get(url) print(f"Response for {title}") print(resp.json())for title, url in urls.items(): get_content(title, url) print("-" * 40)The preceding snippet should be straightforward. We declare a dictionary of URLs against which we want to perform HTTP requests. We have encapsulated the code that performs the request into theget_content()function. As you can see, we perform a GET request (by usingrequests.get()), and we print the title and the JSON decoded version of the body of the response. Let us spend a few words on this last bit.When we perform a request to a website, or to an API, we get back a response object encapsulating the data that was returned by the server we performed the request against. The body of some responses fromhttpbin.orghappens to be JSON encoded, so instead of getting the body as it is (by readingresp.text) and manually decoding it callingjson.loads()on it, we simply combine the two by leveraging thejson()method of the response object. There are plenty of reasons why therequestspackage has become so widely adopted, and one of them is its ease of use.Now, when you perform a request in your application, you will want to have a much more robust approach in dealing with errors and so on, but for this chapter, a simple example will do. We will see more examples of requests inChapter 14, Introduction to API Development.Going back to our code, in the end, we run aforloop and get all the URLs. When you run it, you will see the result of each call printed on your console, which should look like this (prettified and trimmed for brevity):$ python reqs.pyResponse for get{ "args": {"t": "learn python programming"}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.31.0", "X-Amzn-Trace-Id": "Root=1-123abc-123abc", }, "origin": "86.14.44.233", "url": "https://httpbin.org/get?t=learn+python+programming",}… rest of the output omitted …Notice that you might get a slightly different output in terms of version numbers and IPs, which is fine. Now, GET is only one of the HTTP verbs, albeit one of the most commonly used. Let us also look at how to use the POST verb. This is the type of request you make when you need to send data to the server, for example to request the creation of a resource. Every time you submit a form on the web, you are making a POST request. So, let us try to make one programmatically:# io_examples/reqs_post.pyimport requestsurl = "https://httpbin.org/post"data = dict(title="Learn Python Programming")resp = requests.post(url, data=data)print("Response for POST")print(resp.json())The preceding code is very similar to what we saw before, only this time we don't callget(), butpost(), and because we want to send some data, we specify that in the call. Therequestslibrary offers much more than this. It is a project that we encourage you to check out and explore, as it is quite likely you will be using it too.Running the previous script (and applying some prettifying magic to the output) yields the following:$ python reqs_post.pyResponse for POST{ "args": {}, "data": "", "files": {}, "form": {"title": "Learn Python Programming"}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "30", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.31.0", "X-Amzn-Trace-Id": "Root=1-123abc-123abc", }, "json": None, "origin": "86.14.44.233", "url": "https://httpbin.org/post",}Notice how the headers are now different, and we find the data we sent in the form of key/value pair of the response body.We hope these short examples are enough to get you started, especially with requests. The web changes every day, so it is worth learning the basics and then brushing up every now and then.Learn Python Programming was published in November 2024.Get the eBook for $35.99 $24.99Get the Print Book for $44.99And that’s a wrap.We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. The complete PythonPro archives can be foundhere.If you have any suggestions or feedback, or would like us to find you a Python learning resource on a particular subject, just respond to this email!*{box-sizing:border-box}body{margin:0;padding:0}a[x-apple-data-detectors]{color:inherit!important;text-decoration:inherit!important}#MessageViewBody a{color:inherit;text-decoration:none}p{line-height:inherit}.desktop_hide,.desktop_hide table{mso-hide:all;display:none;max-height:0;overflow:hidden}.image_block img+div{display:none}sub,sup{font-size:75%;line-height:0}#converted-body .list_block ol,#converted-body .list_block ul,.body [class~=x_list_block] ol,.body [class~=x_list_block] ul,u+.body .list_block ol,u+.body .list_block ul{padding-left:20px} @media (max-width: 100%;display:block}.mobile_hide{min-height:0;max-height:0;max-width: 100%;overflow:hidden;font-size:0}.desktop_hide,.desktop_hide table{display:table!important;max-height:none!important}}
Read more