Command-line interfaces are quick and efficient tools to accomplish tasks. Not only that, but it’s also cool to have a command-line interface for any web application you develop to avoid having the end-user getting lost in the vast UI sitemap.
That’s why despite having a feature-rich console, aws has an amazing cli backed by its botocore
module doesn’t it?
If yes, then you should definitely read further.
We can build interactive and immersive command-line interfaces in python using PyInquirer
and other libraries. …
This article discusses about python’s copy module and the nuances of deep and shallow copies. Understanding deep and shallow copies in python was always challenging, especially when dealing with mutable and immutable data types during copy. I have tried my best to put forth the concealed differences of copy with mutable and immutable objects here.
For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.
From the docs:
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. …
Let’s assume that you need to share files from your AWS S3 bucket(private) without providing AWS access to a user. How would you do that? Well, we have pre-signed URLs that are shortly lived, which can be shared and used to access the content shared.
What is a pre-signed URL?
A pre-signed URL gives you temporary access to the object identified in the URL, provided that the creator of the pre-signed URL has permissions to access that object.
That is, if you receive a pre-signed URL to upload an object, you can upload the object only if the creator of the pre-signed URL has the necessary permissions to upload that object. …
I always used to concatenate strings using the “+” operator irrespective of the language I code only to realize later that I was doing them wrong. To be precise, inefficient.
Today, we shall discuss string concatenation in python and how to do it efficiently.
x = "hello"
y = "world"
print(x + y)Output:
helloworld
As we are aware of the fact that string objects are immutable in python, any concatenation operation using the “+” operator could prove to be costly.
Why is it so?
String objects are immutable in python and any concatenation operation creates a new string, copies the old string character by character and then appends the new string to be concatenated. …
Big O is a term used to represent the efficiency of an algorithm. It is mandatory for a programmer to master the basics of Big O to clearly specify how fast or slow his algorithm could perform.
Big O is also used to represent the space taken by a program in-memory during the program execution.
So, in short, Big O is used to represent both the time and space which are popularly identified as
This article predominantly focuses on the time-complexity though we may slightly touch-base on the space complexity too. …
This is a popular interview question on Linked List and also one of the problem statements in cracking the coding interview.
Write code to remove duplicates from an unsorted linked list.
What is a Linked List?
A linked list is a linear data structure which consists of a node object and a reference to the next node.
A singly linked list looks something like this.
Have you ever thought of using a NamedTuple in python?
Today we will discuss what a namedtuple
is and where do we use them. You would have used a tuple
quite often but am pretty sure you would not have used a namedtuple
.
Named tuples are built-in data types in the collections
module. This could be an alternative for your class definitions except that the NamedTuples
are immutable.
Lets look at a quick overview of tuples
in python.
tuples
are one of the built-in data structures/containers in python to store arbitrary objects. …
Occasionally, we may have to create temporary files on our system to write data for a short period of time and then do away with them later. Once such instance is downloading files via http
request, do some modifications and upload them to a remote location or like so.
Of course, we could always create a new file or directory and save the file contents into that. However, we may not clean those files which are no longer required.
Python provides a built-in library to create temporary files and directories on all platforms.We could create file like objects that are platform agnostic and also automatically clean up the files once the execution exits the file context manager. …
The most commonly used web frameworks for building REST APIs with python is Flask and Django. Though we could build APIs with django, the real purpose of Django is building an end to end web application. On the other hand, flask is pretty light weight and can help us quickly build REST APIs. Before jumping into fastapi
let’s quickly revisit a basic flask application.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/", methods = ["GET"])
def index():
return jsonify({"appname" :"firstapp"}),200
if __name__ == "__main__":
app.run(debug=True)
Running the above code should give the following logs and output
* Serving Flask app "myflaskapi" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 207-495-435
127.0.0.1 - - [25/Sep/2020 12:35:03] "GET / HTTP/1.1" …
User code can raise built-in exceptions. Python defines try/except to handle exceptions and proceed with the further execution of program without interruption.
Let’s quickly get to an example of a basic try/except clause
Assuming the file is unavailable, executing the below code will give the output as shown below.
try:
f = open("testfile.txt")
...except FileNotFoundError as e:
print(f" Error while reading file {e} ")Output:
Error while reading file [Errno 2] No such file or directory: 'testfile.txt'
In practical use cases such as connecting to a db or opening a file object, we may need to perform teardown operations such db closure/file closure irrespective of the block getting executed. So finally
is one such block which can be reserved for these operations as it gets executed always. …
About