<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vivek K. Singh on Medium]]></title>
        <description><![CDATA[Stories by Vivek K. Singh on Medium]]></description>
        <link>https://medium.com/@vivekhere?source=rss-d2e6bd6b5964------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Kqb9TSN2R2ZL8LsIeHpJvw.png</url>
            <title>Stories by Vivek K. Singh on Medium</title>
            <link>https://medium.com/@vivekhere?source=rss-d2e6bd6b5964------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 05:01:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vivekhere/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to export Google NotebookLM Saved notes as PDF]]></title>
            <link>https://vivekhere.medium.com/how-to-export-google-notebooklm-saved-notes-as-pdf-10b5ce6c6c10?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/10b5ce6c6c10</guid>
            <category><![CDATA[notebooklm]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Sat, 26 Apr 2025 08:33:21 GMT</pubDate>
            <atom:updated>2025-04-26T08:33:21.988Z</atom:updated>
            <content:encoded><![CDATA[<p>Exporting Google NotebookLM Notes Is a Mess — So I Wrote a Python Script for It.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jp6pVGFajYQna0bakdg9UA.png" /></figure><p>Google’s <strong>NotebookLM</strong> is a handy tool for interacting with your documents and takes notes from it <strong>but if you’ve tried exporting your saved notes, you’ll quickly run into a frustrating problem.</strong></p><p>There’s <strong>no export option in the app itself</strong>. You can’t easily back up your notes, search through them offline, or move them to another tool. The only official way to get your notes is by <strong>downloading them all at once</strong>, but this comes with a big downside — it dumps everything together into a single file. This <strong>ruins the structure of individual notes</strong> and strips away important formatting, making the notes messy and harder to work with.</p><p>I faced this issue myself, so I decided to fix it.</p><p>I created a simple <strong>Python script</strong> that grabs your exported NotebookLM notes and neatly restructures them. It separates each note cleanly, preserves the original formatting as much as possible, and makes it easier to search or convert them into any format you want.</p><h3>How My Script Solves It</h3><p>Since NotebookLM doesn’t offer any public API, I had to build the solution differently.</p><p>The script works by:</p><ul><li>Parsing the raw <strong>HTML structure</strong> from the downloaded notes</li><li>Converting each note into clean, readable <strong>Markdown</strong></li><li>Then exporting them as <strong>individual PDF files</strong></li></ul><p>This way, each note stays separate, well-formatted, and easy to search or archive.</p><h3>How to work with the script</h3><p>First open the NotebookLM Notebook whose notes you want to export. In the Studio section you’ll find an option to <strong>Convert all notes to source.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*VUccTdkDqyjqOUSb8D79Sg.png" /></figure><p>Clicking this option will create a new file in your sources section. In the next step you have to get the Source of this file to feed it into the Script. The Script uses bs4 to traverse through the source and converts it to MarkDown.</p><p>You can grab this source code by using the inspect element feature of your browser:</p><p>Press F12 or Right click on the screen and select Inspect Option. This will open DevTools which shows all HTML code that renders the HTML. In the top-left corner of the DevTools window, there is a option of Hover to Select, click on that and now you can just hover over the elements in the sources Section to get the HTML code of the Notes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TnJROhex10WRPRYd_QLFIA.png" /></figure><p>In the above Image you can see that all the code of notes is put in a div tag with class elements-container . There we have it the only manual step in this process. We just need to copy this element and paste it in a text file notes.txt . Right click on the HTML section and click copy Element:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*HyJ52owYsRq0jy2FyMnnaQ.png" /></figure><p>And save the file with the name notes.txt . You could also change the name of the file but then you would also have to make changes in the script.</p><p>The script requires Python 3.10 or above version installed in your system and you need to install the following dependencies:</p><pre>pip install beautifulsoup4==4.13.4 markdown_pdf==1.7</pre><p><a href="https://gist.github.com/vivekthedev/5bfe0655986d775d6d07661974ce414e">Download the script </a>and save it in the same location as the notes.txt file</p><p>Open the terminal and run python export_note.py and let the script do its work. Once finished you can see all the PDF files it created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*sxck8hcWYKkiDrtyDnF0uA.png" /></figure><p>Also the PDF files preserve the MD formatting with Headings, Bullet lists and Code block:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*L-DHc9NCOVBLUzQnf4-7fw.png" /><figcaption>One of the output PDF file from my notes</figcaption></figure><p>If you’ve been stuck with the same problem, I hope this saves you some time and frustration. Always fun turning annoying gaps in tools into working solutions. If you face any problems, DM’s are <a href="https://www.linkedin.com/in/vivekthedev">open</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10b5ce6c6c10" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Effortless Database Migrations: Why Alembic is Your Python Must-Have]]></title>
            <link>https://python.plainenglish.io/effortless-database-migrations-why-alembic-is-your-python-must-have-a7d36471cc27?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7d36471cc27</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[database-migration]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[alembic]]></category>
            <category><![CDATA[fastapi]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Wed, 21 Feb 2024 08:17:21 GMT</pubDate>
            <atom:updated>2024-02-26T01:42:57.007Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a87xfgq5Zhm3_OxCkSWAxA.png" /></figure><p>SQL databases operate by defining specific fields, data types, and relationships across various models. These characteristics make SQL databases ideal for tasks like data science and record management. In Python-based web applications, SQL databases are commonly chosen, especially with Object Relational Manager (ORM) tools like SQLAlchemy. These tools facilitate the creation, querying, and modification of SQL models using Python.</p><p>However, altering a production database after it’s been defined can be difficult. Frameworks such as Django provide their own database migration systems to simplify and streamline this process. It introduces various checks to ensure database updates are performed smoothly. But, Python Frameworks such as Flask and FastAPI do not come with their own database migration system, this is where Alembic comes into play. <a href="https://alembic.sqlalchemy.org/en/latest/">Alembic </a>is a database Migration system for usage with the SQLAlchemy Database Toolkit.</p><p>It allows you to make changes to your databases structure in a secure way using only Python without writing a single SQL statement. Additionally, with Alembic you can rollback to the previous version of your database definition with just a few commands. Alembic make migrations in Database really easy with intuitive syntax. It make easier to evolve our database with our application.</p><p>In this tutorial, you will learn to use Alembic in a FastAPI application. We will cover most important features like upgrade, downgrade, addcolumn and dropcolumn to enhance our FastAPI application. For this tutorial, we have already created a sample application in FastAPI which has a CRUD functionality on a book table having the following fields.</p><pre>id: int<br>title: string<br>rating: integer<br>price: float<br>published: boolean</pre><h3>Setting Up the starter code</h3><h4>Cloning Application from GitHub</h4><p>In order to setup the starter application, you have to first clone the application in your local directory using this command:</p><p>git clone https://github.com/vivekthedev/blog-tuts.git</p><h4>Creating virtual environment</h4><p>Once you have the application on your system, create a python virtual environment in order to isolote the dependencies from the system site packages. You can create a virtual environment using the following command:</p><p>python -m venv env</p><p>After running the above command, run env\Scripts\activate if you are on Windows else run source env/bin/activate if you are on Mac/Linux in order to activate your virtual environment.</p><h4>Installing Dependencies</h4><p>Once you activate the virtual envrironment you can install dependencies present in requirements.txt file by running pip install -r requirements.txt command.</p><h4>Running and Testing Starter Application</h4><p>At this point you are ready to run and test the application. In the same terminal, run uvicorn main:app --reload to start the application on the default address of http://127.0.0.1:8000/ , visit http://127.0.0.1:8000/docs to test your application on the Swagger UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*8FvjKiu1P0HiG6W4_12HDA.png" /><figcaption>Web Output of the http://127.0.0.1:8000/</figcaption></figure><h3>What are Database Migrations?</h3><p>As mentioned earlier, SQL databases have a strict structure with predefined rows and columns, which makes altering the database more challenging. For application developers, this process may require writing SQL statements, ensuring constraints like NOT NULL or UNIQUE are met, and specifying DEFAULT values. Moreover, if issues arise during the update process, reverting to the previous database state can be difficult, especially if TRANSACTION command aren’t explicitly defined when executing SQL statements.</p><p>Alembic solves this problem by providing an easier and intuitive way to evolve your database without writing any line of SQL code. Additionally, working with Alembic is much easier compared to manually changing database structure through SQL or via GUI application.</p><p>Each database update is stored in a Python file, providing flexibility to customize as needed. Reverting to a specific database state is as easy as executing a simple command, making the process more manageable and efficient.</p><h3>Working with Alembic</h3><p>To start working with Alembic, you first need to install Alembic using the following command. Also make sure to run the command in the same virtual environment as you FastAPI starter application:</p><p>pip install alembic</p><p>Once you install Alembic, you now have a alembic Command Line Tool to configure your database. To integrate Alembic with your project run the following command in the same terminal:</p><p>alembic init alembic-conf</p><p>The above command creates a directory named alembic-conf in your project directory and generates the following files:</p><ul><li>alembic-conf/versions/ : This directory stores various versions of your database, you will use this directory in order to examine or update various states of your database.</li><li>alembic-conf/env.py : This file is used by alembic to run migrations, it defines how the migrations should be made on the database.</li><li>alembic-conf/script.py.mako : It is a template file which is used to create new database versions in the versions directory. In other words every file/migration in versions directory is created using this template file.</li><li>alembic-conf/README : Contains information about the projects or the alembic. You can use it as changelog or writing something informative about the project.</li><li>alembic.ini : This is the configuration file where you define you various setting in order to configure your migration process.</li></ul><h4>Configuring Database with Alembic</h4><p>To enable Alembic to make changes to your Database, it requires knowledge of the location of your database service, which is defined by the SQLALCHEMY_DATABASE_URL. To do this, locate the database.pyfile within your project and copy the value sqlite:///books.sqlite3 . Then, navigate to Line 63 of the alembic.ini file and paste this value just after the variable sqlalchemy.url. Make sure to NOT enclose it in quotes .</p><p>Now open alembic-conf/env.py file, and populate it with the following code:</p><pre>import sys<br>import models <br>sys.path.append(&quot;..&quot;)</pre><p>After the above imports find the target_metadata variable and set it to models.Base.metadata .</p><p>Once you set up the above two configurations successfully, you are now ready to create you alembic revisions in order to make changes in your database.</p><h4>Creating Alembic Revisions</h4><p>For your first Alembic revision, you will create a new column in your Books table named Description which should be of type String. In order to create a revision file, open the terminal and type the following command.</p><pre>alembic revision -m &quot;create book description&quot;</pre><p>After running the above command, you will see that a new file with the name “&lt;hash_value&gt;_create_book_description.py” has been created. This file will contain our revision logic which we will define in upgrade() function in the file.</p><p>Open the newly created file and in the upgrade() function type the following code:</p><pre>def upgrade() -&gt; None:<br>    op.add_column(&#39;books&#39;, sa.Column(&#39;description&#39;, sa.String(), server_default=&quot;A short Description&quot;), nullable=False)</pre><p>In the provided code, you’ve created a new revision that, when applied, adds a new column named ‘description’ of type string with a default value of “A short description”. It’s considered good practice to define the downgrade() function at the same time as the upgrade() function to ensure a smooth downgrading process in the future.</p><p>To implement this, in the downgrade() function, we will reverse the actions performed in the upgrade() function, which in this case involves removing the ‘description’ column from the ‘books’ table. To achieve this, type the following code inside the downgrade() function:</p><pre>def downgrade() -&gt; None:<br>  op.drop_column(&#39;books&#39;, &#39;description&#39;)</pre><p>This code will instruct Alembic to remove the ‘description’ column from the ‘books’ table when the downgrade process is initiated.</p><p>In this file, you can see another variable revision which contains the hash value of the revision file. Copy this value as it will be useful when applying the revisions to the database.</p><h4>Applying Revisions</h4><p>We have successfully created revision to make changes in the database, in order to apply these changes run the following command in your terminal:</p><pre>alembic upgrade &lt;hash_value&gt;</pre><p>Note that “&lt;hash_value&gt;” should be replaced with the hash value of your revision file, which you copied in the previous section. When you execute this command, you will receive confirmation messages indicating that your revisions have been applied successfully:</p><pre>INFO  [alembic.runtime.migration] Context impl SQLiteImpl.<br>INFO  [alembic.runtime.migration] Will assume non-transactional DDL.<br>INFO  [alembic.runtime.migration] Running upgrade  -&gt; e9cbb68b0547, create book description</pre><p>To verify that the changes have been applied to our database, we can run the application using the following command:</p><pre>uvicorn main:app --reload</pre><p>After that, proceed to <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a> to view the books stored in our database. However, you may notice that the models do not display the newly created ‘description’ field for the book. This is because FastAPI fetches all model information from the models.py file. To ensure FastAPI fetches the ‘description’ field as well, you need to add a new line in the models.py file. Open the models.py file and append this line at the end of the model:</p><pre>class Books(Base):<br>    __tablename__ = &quot;books&quot;<br>    id = Column(Integer, primary_key=True, index=True)<br>    title = Column(String)<br>    price = Column(Float)<br>    author = Column(String)<br>    rating = Column(Integer)<br>    published = Column(Boolean)<br>    description = Column(String) # &lt;- This line</pre><p>Now open the <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000/</a>, you will see the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*Je0NtB_bWm3mpvvKAuUVMw.png" /><figcaption>A new column description got added to the models</figcaption></figure><p>You can also use sqlite3 command line tool or <a href="https://inloop.github.io/sqlite-viewer/">SQL Viewer</a> tool in order to inspect changes in your database.</p><p>With that, you have successfully created and applied your very first migration using Alembic. This functionality proves to be highly useful for a production-level application, as it allows for seamless database schema modifications.</p><p>For your next migration, the objective is to create a new table called ‘reviews’. This next part will serve as a learning opportunity to understand how to create a new table using Alembic Operations. Eventually, we will remove this table to demonstrate the downgrade functionality of Alembic, showcasing its ability to rollback changes.</p><h3>Creating Table with Alembic</h3><p>In order to create a new revision, open the terminal and type the following following command:</p><pre>alembic revision -m &quot;create reviews table&quot;</pre><p>A new file with the name &lt;hash_value&gt;_create_reviews_table should now be generated in the “alembic-conf/versions” directory. Open this file, and you’ll notice that it contains a variable called “down_revision” with a value corresponding to the hash value of the previous migration file. This setup makes it easy to track our migrations and ensures that Alembic can keep the upgrade and downgrade processes in sync.</p><h4>Upgrade and Downgrade function</h4><p>Now let’s create Operations for creating a new table. Open the newly created migration file and populate the upgrade function with the following code:</p><pre>def upgrade() -&gt; None:<br>    op.create_table(<br>        &quot;reviews&quot;,<br>        sa.Column(&quot;id&quot;, sa.Integer(), primary_key=True, index=True),<br>        sa.Column(&quot;author&quot;, sa.String(50), nullable=False),<br>        sa.Column(&quot;content&quot;, sa.String(200)),<br>        sa.Column(&quot;timestamp&quot;, sa.TIMESTAMP, server_default=sa.func.now()),<br>    )</pre><p>In the above code, we are creating a new table named reviews<br>which has the following columns:</p><ul><li>id : An Integer type field for primary_key data of each row.</li><li>author : This column stores the author of the review and is defined as a String(50) type. It means that the column value can’t exceed the length of 50 characters.</li><li>content : Another String field which stores the actual review content. This column cannot exceed the character length of 200.</li><li>timestamp : A field with default value of the current timestamp which signifies the date time when a particular review was created.</li></ul><p>As stated above, we should always write the downgrade function in order to revert the changes done by upgrade function. Write the following code in the downgrade function:</p><pre><br>def downgrade() -&gt; None:<br>    op.drop_table(&#39;reviews&#39;)</pre><p>The above code is pretty self-explainatory, we are just removing the table ‘reviews’ that we created in upgrade function.</p><h4>Applying the Migration</h4><p>To apply the migration, first copy the hash value from the migration file and then open your terminal and run the following command:</p><pre>alembic upgrade &lt;hash_value&gt;</pre><p>Once you’ve executed the provided command, you’ll receive a confirmation message in your terminal indicating that the latest migration has been successfully applied, resulting in the creation of the new “reviews” table. However, as our FastAPI application currently lacks any endpoints that query the “reviews” table, you won’t be able to observe these changes directly from the application.</p><p>To inspect the changes, we’ll utilize an SQL Viewer Tool. Simply open the SQL Viewer Tool in your browser. Upon loading the page, you’ll notice a “Drop Here” text. Click on it and navigate to your project directory, where you’ll find the database file named ‘books.sqlite3’. Select this database file, and the SQL tool will read all the tables within it. You’ll then be able to view all the tables available in the dropdown menu within the tool’s interface. This allows you to inspect the newly created “reviews” table and any other tables present in your database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*hDWho-RmOYJnxamj6C3eIA.png" /></figure><p>In the above image you can a reviews table which currently has no rows as we haven’t populated it with data.</p><h3>Downgrade with Alembic</h3><p>Downgrading with Alembic is straightforward. Simply enter the downgrade command along with the identifier of the desired state in the terminal. Alembic will automatically handle all the necessary steps to revert your application to the specified state.</p><p>Alembic supports alembic downgrade &lt;hashvalue&gt; syntax, but the more common (and easier) way to downgrade the application involves relative identifiers using the -N and +N syntax. Let’s do it practically to understand how it works. Open your terminal and type the following command:</p><pre>alembic downgrade -1</pre><p>The above command instructs Alembic to take the database to a state one step previous from the current state. In other words it reverts the last applied migration to the database. Naturally, when you put -2 instead of -1 it reverts the last two migration and so on.</p><p>After executing the command, you’ll receive a confirmation message in the terminal indicating that the downgrade operation ran successfully. To verify this, you can open the SQLite Viewer Tool and upload the database again. Upon inspection, you’ll notice that the dropdown menu displaying all the tables no longer includes the ‘reviews’ table. This confirms that the database has indeed been reverted back to its previous state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*_RBvF11X8dG4ZUxT7DLH0Q.png" /></figure><p>You can also see in the above image that there is no review table in the database. Make sure to reupload the database to the viewer tool in order for them to reflect in your browser.</p><h3>Conclusion</h3><p>Alembic is a really powerful tool to add database migration using SQLAlchemy. It make migration process more intuitve, easy and secure. Developers can easily manage and rollback to a different state in the database with ease. Alembic also supports operation like Batch processing, raw sql execution, adding constraints and much more. You can read about all the operations provided by Alembic in the <a href="https://alembic.sqlalchemy.org/en/latest/index.html">Documentation</a>.</p><p>In this tutorial you learnt about Alembic and how you can use it in your python projects to easily manage your database. You learnt upgrading and downgrading in Alembic, creating columns, tables and and reverting changes. With these functionalities Alembic becomes a must-have when working with Python/SQLAlchemy project in order to easily manage your database.</p><p>Hope you like the tutorial, in case you have any question, shoot them in the comments. I will try my best to answer them.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a><strong> | </strong><a href="https://blog.cubed.run/"><strong>Cubed</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7d36471cc27" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/effortless-database-migrations-why-alembic-is-your-python-must-have-a7d36471cc27">Effortless Database Migrations: Why Alembic is Your Python Must-Have</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazing itertools functions — PyTip09]]></title>
            <link>https://python.plainenglish.io/amazing-itertools-functions-pytip09-e68fa1e6414?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/e68fa1e6414</guid>
            <category><![CDATA[advance-python]]></category>
            <category><![CDATA[itertools]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Fri, 30 Dec 2022 17:23:02 GMT</pubDate>
            <atom:updated>2023-01-01T14:07:46.926Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazing itertools functions — PyTip09</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6RyFA52TpU8BiST8sznFQA.png" /></figure><p>The itertools module in Python is a standard library that provides functions for creating and working with iterators. Iterators are objects that can be iterated upon, and they represent a stream of data. The itertools module provides a range of functions for performing various operations on iterators, such as filtering, grouping, and combining. This can save time and effort by allowing you to perform complex operations with just a few lines of code. Overall, the itertools module is a useful tool for working with iterators and sequences of data in Python, and it can help you write more efficient, concise, and functional code.</p><p>In this tutorial you will learn some of the most useful fuctions provided by itertool module in Python. To use itertools create a Python file or Open the REPL and type the following.</p><pre><br>&gt;&gt;&gt; import itertools</pre><p>The itertools module is now imported and now you can use any function using itertools.func_name syntax.</p><h3>#1 — count</h3><p>The count function in itertools module is used to provide a never ending generator which you can use anywhere in the program, the best thing about generators is that they remember their state. The count() function takes two arguments — start and step.</p><pre>&gt;&gt;&gt; import itertools as it<br>&gt;&gt;&gt; infinite_gen = it.count(43, 89)<br>&gt;&gt;&gt; for i in range(5):<br>        print(next(infinite_gen))</pre><p>The above code produces following output:</p><pre>43<br>132<br>221<br>310<br>399</pre><p>In the above code you can see that infinite_gen returned five values with each value larger than the previous one by 89. The best thing about this is that you can use this same generator anywhere in the program and it will remember its state as 399.</p><h3>#2 — cycle</h3><p>The cycle function is pretty simple — its sole purpose is to repeat a iterator passed to it over and over again. It is also a generator so it also remembers its state.</p><p>Below is the example of how to use this function:</p><pre>&gt;&gt;&gt; import itertools as it<br>&gt;&gt;&gt; rep = it.cycle([&#39;repeat&#39;, &#39;me&#39;, &#39;again&#39;])<br>&gt;&gt;&gt; for i in range(5):<br>...     print(next(rep))<br>...<br>repeat<br>me<br>again<br>repeat<br>me</pre><h3>#3 — chain</h3><p>The chain() function is used to make two or more iterables in a single sequence. This function does not modify the original iterators rather it just create a new one using the iterators passed in the argument.</p><pre>&gt;&gt;&gt; import itertools as it<br>&gt;&gt;&gt; combined_list = it.chain(&#39;ABC&#39;,&#39;DEF&#39;)<br>&gt;&gt;&gt; list(combined_list)<br>[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;]</pre><p>In the above output you can see that combined_list returns the elemenst of both the iterators passed to it.</p><h3>#4 — tee</h3><p>The tee() function in python is used to make an independent copies of the original iterator instantly.</p><p>This function take two arguments — iterable and the number of times is need to be copied.</p><p>Below is the example of how to use this:</p><pre>&gt;&gt;&gt; import itertools as it<br>&gt;&gt;&gt; a1, a2, a3 = it.tee([1,2,3,4], 3)<br>&gt;&gt;&gt; for i in a1: print(i)<br>...<br>1<br>2<br>3<br>4<br>&gt;&gt;&gt; for i in a2: print(i)<br>...<br>1<br>2<br>3<br>4</pre><p>In the above program you created three copies of original iterator in a1, a2, a3.</p><p>In conclusion, the itertools module is a powerful tool that provides a range of functions for working with iterators and sequences of data in Python. Whether you need to generate infinite sequences of data, repeat elements or iterables, or filter, group, and combine data, the itertools module has you covered. By using the amazing features of the itertools module, you can write more efficient, concise, and functional code in Python.</p><p>In the upcoming blogs I intend to share my learning on more Advanced stuff about DevOps, Python and Django. If it interests you then consider <a href="https://twitter.com/vivekthedev">following</a> me.</p><h4><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>.</em></h4><p><em>Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em>, and</em></strong><em> </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></p><h4><strong><em>Looking to scale awareness and adoption for your tech startup? </em></strong><em>Check out </em><a href="https://circuit.ooo/?utm=publication-post-cta"><strong><em>Circuit</em></strong></a><em>.</em></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e68fa1e6414" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/amazing-itertools-functions-pytip09-e68fa1e6414">Amazing itertools functions — PyTip09</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker Swarm: Initial Steps — DevOps02]]></title>
            <link>https://vivekhere.medium.com/docker-swarm-initial-steps-devops02-af220d0dd3b5?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/af220d0dd3b5</guid>
            <category><![CDATA[devops-practice]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-swarm]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Mon, 28 Nov 2022 12:00:17 GMT</pubDate>
            <atom:updated>2022-11-28T12:00:17.163Z</atom:updated>
            <content:encoded><![CDATA[<h3>Docker Swarm: Initial Steps — DevOps02</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fhF_tKcC8_8y5dX_ZM0KYQ.png" /></figure><p>Docker swarm is a native clustering tool for Docker that turns a pool of Docker hosts into a single, virtual host. With swarm, administrators and developers can easily manage a cluster of Docker hosts as a single entity, allowing them to centrally deploy and manage applications and containers. Additionally, swarm makes it easy to scale your applications and containers by adding or removing hosts from your cluster.</p><p>In this tutorial, you will learn the fundamentals of Docker Swarm and how you can start working with it. In order to follow this tutorial you must have docker installed in your machine and also you should be aware of basic docker concepts and commands.</p><h3>Docker Swarm: Overview</h3><p>Docker Swarm is a orchestration tool like Kubernetes, it is used to distribute the workload of the containers to multiple nodes. Docker Swarm follows Manager/Worker approach in order to achieve this.</p><p>The Manager Node in a Swarm is responsible to maintain Worker Nodes, it is also responsible for upscaling or downscaling the worker nodes. It is recommended to have multiple manager nodes in your Swarm, however, only a single manager is allow to work on worker nodes.</p><p>Since Docker Swarm works with multiple different node, you cannot work with it using Docker Desktop, you can either initialize it in GCP or AWS, or you could also use <a href="https://labs.play-with-docker.com/">Docker Playground</a> to access it. For this tutorial I will be using Docker Playground in order to create Swarm instances.</p><h3>Initializing Docker Swarm</h3><p>To start Docker Swarm, open your terminal and type the following command:</p><pre>$ docker swarm init --advertise-addr 192.168.0.7 --listen-addr 192.168.0.7</pre><p>Running the above command will produce the following output:</p><pre>Swarm initialized: current node (ybu9t9kosfbaxota19g28674k) is now a manager.<br><br>To add a worker to this swarm, run the following command:<br><br>    docker swarm join --token SWMTKN-1-13odsn16y9u0d835w3dnma13iphl0h3w38viyzv77dut7p44m5-4ic2nhq4iruo2kwwnt3trsflq 192.168.0.8:2377<br><br>To add a manager to this swarm, run &#39;docker swarm join-token manager&#39; and follow the instructions.</pre><p>In the above output, you can see that docker provided commands which should be run in order to add worker and manager nodes to your current swarm.</p><p>The --advertise-addr command is the IP that other node should use in order to connect with this manager and --listen-addr is the IP that the current manager should listen to.</p><p>To confirm that the node is created, you can run docker node ls command which produces the following output:</p><pre>ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION<br>ybu9t9kosfbaxota19g28674k *   node1      Ready     Active         Leader           20.10.17</pre><p>In the above output you can see that there is only one node created with Leader Status which means this node can make decisions about adding the new worker node or discarding any node from the Swarm.</p><h3>Add Worker Nodes to the Swarm</h3><p>In order to add worker node, run the following command:</p><pre>docker swarm join-token worker</pre><p>The above command produces following output:</p><pre>To add a worker to this swarm, run the following command:<br><br>    docker swarm join --token SWMTKN-1-13odsn16y9u0d835w3dnma13iphl0h3w38viyzv77dut7p44m5-4ic2nhq4iruo2kwwnt3trsflq 192.168.0.8:2377</pre><p>The output return the command which should be run in order to create a worker node in this Swarm. Now to create a new worker node, you first need to create a new Instance in the Docker Playground. You can do it by pressing Add New Instance button at the left pane as shown in the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*558Xo3UYImG7h24WEdD0mQ.png" /><figcaption>Zoomed Screenshot of Docker Playground</figcaption></figure><p>After creating the new instance run the above join command to make this instance as a worker node.</p><pre>$ docker swarm join --token SWMTKN-1-13odsn16y9u0d835w3dnma13iphl0h3w38viyzv77dut7p44m5-4ic2nhq4iruo2kwwnt3trsflq 192.168.0.8:2377</pre><p>After executing the above command, Docker return a confirmation message saying This node joined as a worker .</p><p>Now run the docker node ls command to view all the created nodes. Running this command should produce the following output:</p><pre>ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION<br>ybu9t9kosfbaxota19g28674k *   node1      Ready     Active         Leader           20.10.17<br>yuggya9ggqo4hwnx2mpwbpo5i     node2      Ready     Active                          20.10.17</pre><p>Now repeat the above process of Creating a New Instance and Adding it to the Swarm as a worker one more time. After doing this, the output of docker node ls command should return the following.</p><pre>ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION<br>ybu9t9kosfbaxota19g28674k *   node1      Ready     Active         Leader           20.10.17<br>yuggya9ggqo4hwnx2mpwbpo5i     node2      Ready     Active                          20.10.17<br>kyqu4liznocghzv3ctrvihfq3     node3      Ready     Active                          20.10.17              </pre><h3>Adding Manager Nodes</h3><p>The manager node in a Docker Swarm can be added in a similar way as Worker Nodes. To add Manager Node run the following command in node1 which is the current Leader in your Swarm.</p><pre>docker swarm join-token manager</pre><p>The above command returns the command which should be executed in order to create manager nodes in your Swarm.</p><pre>To add a manager to this swarm, run the following command:<br><br>    docker swarm join --token SWMTKN-1-13odsn16y9u0d835w3dnma13iphl0h3w38viyzv77dut7p44m5-6zio6dbxe1c5yufw4zf9kzfi0 192.168.0.8:2377</pre><p>Now copy the above command and create two new managers. After doing this your docker node ls output should like this:</p><pre>ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION<br>ybu9t9kosfbaxota19g28674k *   node1      Ready     Active         Leader           20.10.17<br>yuggya9ggqo4hwnx2mpwbpo5i     node2      Ready     Active                          20.10.17<br>kyqu4liznocghzv3ctrvihfq3     node3      Ready     Active                          20.10.17<br>vui946751xkbbjlhcq7f7s44l     node4      Ready     Active         Reachable        20.10.17<br>g3vfag2nptvskqx15c4s6bm31     node5      Ready     Active         Reachable        20.10.17</pre><h3>Conclusion</h3><p>In this tutorial you learned about the Swarm Mode in Docker and how to get started with it. Swarm mode is meant to keep the application highly available by orchestrating all the Managers and Workers. There is so much more to Docker Swarm, in this tutorial you just went over the basics.</p><p>In the upcoming blogs I intend to share my learning on more Advanced stuff about DevOps, Python and Django. If it interests you then consider <a href="https://twitter.com/vivekthedev">following</a> me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af220d0dd3b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Query Expression in Django — DjangoTip03]]></title>
            <link>https://vivekhere.medium.com/query-expression-in-django-djangotip03-21532ee9ee26?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/21532ee9ee26</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-tips]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[python-web-developer]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Tue, 22 Nov 2022 12:31:38 GMT</pubDate>
            <atom:updated>2022-11-22T12:31:38.107Z</atom:updated>
            <content:encoded><![CDATA[<h3>Query Expression in Django — DjangoTip03</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GeqotN6jQKjcRst40JpQqw.png" /></figure><p>A query expression is a set of clauses that defines a particular search. In Django, a query expression is used to filter the results of a queryset. Django ORM is one of the most incredible aspects of Django. It makes queryset searching, filtering, and sorting really simple.</p><p>In this article you will learn about some query expression available to you in Django. The below Django Demo Project uses the following models.pyfile:</p><pre>from django.db import models<br>from django.contrib.auth.models import User<br><br>class Post(models.Model):<br>    title = models.CharField(max_length=200)<br>    content = models.TextField()<br>    published = models.DateTimeField(auto_now_add=True)<br>    writer = models.ForeignKey(User, on_delete=models.CASCADE)<br>    likes = models.PositiveIntegerField()<br><br>    class Meta:<br>        ordering = (&quot;-published&quot;,)<br>        get_latest_by = &quot;published&quot;<br>        unique_together = [<br>            [&quot;title&quot;, &quot;content&quot;],<br>        ]</pre><h3>Func()</h3><p>The Func() expressions in Django ORM allows the queryset to annotate an additional field after performing functions on the field. In the below example you will change the title of the Post objects to Uppercase.</p><pre>&gt;&gt;&gt; from main.models import Post<br>&gt;&gt;&gt; from django.db.models import Func<br>&gt;&gt;&gt; qs = Post.objects.all().annotate(title_upper=Func(&#39;title&#39;, function=&#39;UPPER&#39;))<br>&gt;&gt;&gt; qs.first().title<br>&#39;Post 3&#39;<br>&gt;&gt;&gt; qs.first().title_upper<br>&#39;POST 3&#39;</pre><p>In the above example you can see that a new field got added to the instances of queryset where the text is uppercase. For more information about the annotate you can give a read to <a href="https://medium.com/@vivekhere/amazing-features-of-django-orm-day5of30-djangotip02-6e0f4fdfa834">this article</a>.</p><h3>Subquery()</h3><p>In Subquery() expression used to add a query that is nested inside another query. The below example use Subquery() to annotate each Post instance with a writer_joined attribute.</p><pre>&gt;&gt;&gt; from django.contrib.auth.models import User<br>&gt;&gt;&gt; from main.models import Post<br>&gt;&gt;&gt; from django.db.models import Subquery<br><br>&gt;&gt;&gt; users = User.objects.all()<br>&gt;&gt;&gt; posts = Post.objects.annotate(writer_joined=Subquery(users.values(&#39;date_joined&#39;)))<br>&gt;&gt;&gt; posts.first().writer_joined.ctime()<br>&#39;Sun Nov 13 07:11:00 2022&#39;</pre><h3>Aggregate</h3><p>Django provide many built-in Aggregation Functions like Avg , Sum , Min and Max . Below is the example, how to use these functions:</p><pre>&gt;&gt;&gt; from main.models import Post<br>&gt;&gt;&gt; from django.db.models import Max, Min, Count, Avg<br>&gt;&gt;&gt; Post.objects.aggregate(Max(&#39;likes&#39;))<br>{&#39;likes__max&#39;: 25}<br>&gt;&gt;&gt; Post.objects.aggregate(Min(&#39;likes&#39;))<br>{&#39;likes__min&#39;: 13}<br>&gt;&gt;&gt; Post.objects.aggregate(Avg(&#39;likes&#39;))<br>{&#39;likes__avg&#39;: 17.666666666666668}<br>&gt;&gt;&gt; Post.objects.aggregate(Count(&#39;likes&#39;))<br>{&#39;likes__count&#39;: 3}</pre><p>I sincerely hope you enjoyed reading this article. Django offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower </a>if this sounds like something you’d be interested in.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21532ee9ee26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are __slots__ in Python? — Day7of30 PyTip08]]></title>
            <link>https://python.plainenglish.io/what-are-slots-in-python-day7of30-pytip08-5b507bb1645c?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b507bb1645c</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[core-python]]></category>
            <category><![CDATA[python-web-developer]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Mon, 21 Nov 2022 03:49:21 GMT</pubDate>
            <atom:updated>2022-11-22T15:06:00.264Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are __slots__ in Python? — PyTip08</h3><h4>Learn about __slots__ and why it is used in Python.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cyy7X_LKeGsZ3dGfeV83gA.png" /></figure><p>There are several special attributes in Python classes that help to define how the class behaves. In Python, a class has lot of built-in special attributes one of which is __slots__. In this tutorial you will learn about __slots__ and why it is used in Python. You will go into detail in understanding __slots__ and how it does what it does?</p><h3>The Problem</h3><p>Every class in Python allocates the memory to Python Object at runtime, this is why Python’s class manages a special attribute __dict__ which store all the instance variables. You can confirm this by looking at the below example.</p><pre>&gt;&gt;&gt; class Animal:<br>...     def __init__(self, name, color):<br>...             self.name = name<br>...             self.color = color<br>...<br>&gt;&gt;&gt; a = Animal(&quot;Togo&quot;, &quot;Brown&quot;)<br>&gt;&gt;&gt; a.__dict__<br>{&#39;name&#39;: &#39;Togo&#39;, &#39;color&#39;: &#39;Brown&#39;}</pre><p>In the above code you can see that the instance variable name and color are stored in __dict__ , although this is an excellent approach but it becomes problematic when dealing with huge number of objects (we are talking thousands or even millions), then this dynamic memory allocation slows the computer and probability of crash rises.</p><p>You can see the dynamic nature of __dict__ from the below example, where you can add any attribute in your instance whenever you want.</p><pre>&gt;&gt;&gt; a.__dict__<br>{&#39;name&#39;: &#39;Togo&#39;, &#39;color&#39;: &#39;Brown&#39;}<br>&gt;&gt;&gt; a.height = &quot;2ft&quot;<br>&gt;&gt;&gt; a.__dict__<br>{&#39;name&#39;: &#39;Togo&#39;, &#39;color&#39;: &#39;Brown&#39;, &#39;height&#39;: &#39;2ft&#39;}</pre><h3>Solution</h3><p>Python’s __slots__ attribute is a powerful tool that can help you save memory when working with objects. By specifying the attributes that can be defined for an object, you can control how much memory is allocated for that object. This can be particularly useful when working with large data sets.</p><p>Below example show how you can work with __slots__ :</p><pre>&gt;&gt;&gt; class Animal:<br>...     __slots__ = (&quot;name&quot;, &quot;color&quot;)<br>...     def __init__(self, name, color):<br>...             self.name = name<br>...             self.color = color<br>...<br>&gt;&gt;&gt; a = Animal(&quot;Togo&quot;, &quot;Brown&quot;)<br>&gt;&gt;&gt; a.__slots__<br>(&#39;name&#39;, &#39;color&#39;)</pre><p>To use __slots__ , just define a variable with the same name in class definition. You will notice that when you start working with __slots__ , your Class Instance can now no longer create dynamic variables at runtime and also the __dict__ variable is now no longer available. You con confirm this by running the following code:</p><pre>&gt;&gt;&gt; a.height = &quot;2ft&quot;<br>Traceback (most recent call last):<br>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>AttributeError: &#39;Animal&#39; object has no attribute &#39;height&#39;<br><br>&gt;&gt;&gt; a.__dict__<br>Traceback (most recent call last):<br>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>AttributeError: &#39;Animal&#39; object has no attribute &#39;__dict__&#39;. Did you mean: &#39;__dir__&#39;?</pre><p>In the above code output you can see that at dynamic declaration of the instance is no longer there. Also when accessing __dict__ , it returns an error saying there is no attribute __dict__ .</p><p>This is how __slots__ are used in Python, you can use them whenever you are certain that you won’t need any other attributes for your class and you are working with a huge number of records.</p><p>I sincerely hope you enjoyed reading this article. Python offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower </a>if this sounds like something you’d be interested in.</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em>. Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b507bb1645c" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/what-are-slots-in-python-day7of30-pytip08-5b507bb1645c">What are __slots__ in Python? — Day7of30 PyTip08</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazing collections data structures in Python — Day6of30 #PyTip07]]></title>
            <link>https://python.plainenglish.io/amazing-collections-data-structures-in-python-day6of30-pytip07-a528c4955c62?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/a528c4955c62</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[modules]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Tue, 15 Nov 2022 07:14:25 GMT</pubDate>
            <atom:updated>2022-11-29T16:27:23.330Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazing collections data structures in Python — #PyTip07</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X8xnbWAZTXxU6iwmob88HA.png" /></figure><p>The collections module is a built-in module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers. It has classes for working with ordered sets, mutable and immutable mappings, and stacks. It also has a deque class for working with queues.</p><p>In this article you will see some amazing Data Structures provided by the collections module.</p><h3>defaultdict</h3><p>A defaultdict is a dictionary-like object that has a default value for keys that don’t exist. A normal dictionary has a method called setdefault() but it requires you to set the default value on every new key access. defaultdict solves this problem by specifying the default value at the time of dictionary declaration. Below is the example of how to use defaultdict.</p><pre>&gt;&gt;&gt; from collections import defaultdict <br>&gt;&gt;&gt; a = defaultdict(str)<br>&gt;&gt;&gt; a[&#39;1&#39;] = &quot;one&quot;<br>&gt;&gt;&gt; a[&#39;2&#39;] = &quot;two&quot;<br>&gt;&gt;&gt; a[&#39;3&#39;] = &quot;three&quot;<br>&gt;&gt;&gt; a[&#39;1&#39;]<br>&#39;one&#39;<br>&gt;&gt;&gt; a[&#39;4&#39;]<br>&#39;&#39;</pre><p>In the above REPL you can see that the defaultdict takes a callable object or None as the first arguments which is used as a fallback if the keys are not present in the dictionary.</p><h3>namedtuple</h3><p>A tuple in python is used as a immutable data type to store records. Elements in a tuple are accessed using their index which is not good sometimes, the namedtuplesolves this issue by introducing named fields for the tuple. Below is the example of how to use namedtuple.</p><pre>&gt;&gt;&gt; from collections import namedtuple<br>&gt;&gt;&gt; Person = namedtuple(&#39;Person&#39;, &quot;name, age, gender, location&quot;)<br>&gt;&gt;&gt; john = Person(&quot;John&quot;, 21, &quot;M&quot;, &quot;New York&quot;)<br>&gt;&gt;&gt; john<br>Person(name=&#39;John&#39;, age=21, gender=&#39;M&#39;, location=&#39;New York&#39;)<br>&gt;&gt;&gt; tim = Person(&quot;Tim&quot;, 25, &quot;M&quot;, &quot;San Francisco&quot;)<br>&gt;&gt;&gt; john.name, tim.name<br>(&#39;John&#39;, &#39;Tim&#39;)<br>&gt;&gt;&gt; john.age &gt; tim.age<br>False<br>&gt;&gt;&gt; john.name = &#39;Yuam&#39;<br>Traceback (most recent call last):<br>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>AttributeError: can&#39;t set attribute</pre><p>In the above code you can see that the tuple is immutable and also access to the values using the attributes.</p><h3>deque</h3><p>The collections module in Python also provides an in-built doubly-ended queue which is used to add or remove elements from both ends. Stack and Queue implementation can be done very easily using this collection.</p><pre>&gt;&gt;&gt; from collections import deque<br>&gt;&gt;&gt; dq = deque()<br>&gt;&gt;&gt; dq<br>deque([])<br>&gt;&gt;&gt; dq.append(&quot;one&quot;)<br>&gt;&gt;&gt; dq.append(&quot;two&quot;)<br>&gt;&gt;&gt; dq.appendleft(&quot;zero&quot;)<br>&gt;&gt;&gt; dq<br>deque([&#39;zero&#39;, &#39;one&#39;, &#39;two&#39;])<br>&gt;&gt;&gt; dq.pop()<br>&#39;two&#39;<br>&gt;&gt;&gt; dq.popleft()<br>&#39;zero&#39;<br>&gt;&gt;&gt; dq<br>deque([&#39;one&#39;])</pre><p>In the above code you can see that the deque can be accessed from both left and right end of the data structure.</p><p>I sincerely hope you enjoyed reading this article. Python offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower </a>if this sounds like something you’d be interested in.</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em>, </em></strong><em>and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong><em> Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a528c4955c62" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/amazing-collections-data-structures-in-python-day6of30-pytip07-a528c4955c62">Amazing collections data structures in Python — Day6of30 #PyTip07</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Amazing features of Django ORM — Day5of30 — DjangoTip02]]></title>
            <link>https://python.plainenglish.io/amazing-features-of-django-orm-day5of30-djangotip02-6e0f4fdfa834?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e0f4fdfa834</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python-web-developer]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[django-orm]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 08:57:44 GMT</pubDate>
            <atom:updated>2022-12-09T01:04:02.411Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazing features of Django ORM — DjangoTip02</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kn2IzEEvyrZXEdRP-4AN_g.png" /></figure><p>One of the most amazing features of Django is the ORM. Django ORM is a great way to manage databases. It provides a clean and simple interface that makes it easy to work with data. Additionally, Django ORM lets you easily map objects to your database tables and vice versa. This makes it easy to access and update your data.</p><p>In this post you will learn some of the amazing features that the ORM provides which makes working with databases much more easy in Django.</p><h3>annotate()</h3><p>Annotate in Django provides any additional field to each object in the queryset after evaluating the Expression. Below is the example of how to use annotate in your queryset.</p><pre>&gt;&gt;&gt; from django.contrib.auth.models import User<br>&gt;&gt;&gt; from django.db.models import Count<br><br>&gt;&gt;&gt; qs = User.objects.annotate(articles = Count(&#39;post&#39;))<br>&gt;&gt;&gt; qs.first().articles<br>3</pre><p>In the above query, with the help of annotate() and Count() Django automatically created a reverse relation to Post models and returned the number of articles written by each User.</p><h3>values()</h3><p>values() functions is useful when there are a lot of fields in the models and you only need a few of them. You can use values() to specify the fields that you want to retrieve.</p><pre>&gt;&gt;&gt; from main.models import Post<br>&gt;&gt;&gt; qs = Post.objects.values(&quot;title&quot;, &quot;published&quot;).first()<br>&gt;&gt;&gt; qs[&#39;title&#39;], qs[&#39;published&#39;].strftime(&quot;%d %b %Y&quot;)<br>(&#39;Post 3&#39;, &#39;13 Nov 2022&#39;)</pre><p>In the above query, Django returns a queryset with each instance in the form of a dictionary which can be easily accessed.</p><h3>Q Objects</h3><p>Q Objects in Django ORM gives you much more control over the WHERE clause. It prevents you in reducing multiple database access. With Q Objects AND (&amp;), OR (|) and NOT (~) queries can be added very easily.</p><pre>&gt;&gt;&gt; from django.db.models import Q<br>&gt;&gt;&gt; from main.models import Post<br>&gt;&gt;&gt; qs = Post.objects.filter(Q(content__icontains=&#39;python&#39;) &amp; Q(title__startswith=&#39;How to&#39;) | Q(title__startswith==&#39;how to&#39;))</pre><p>I sincerely hope you enjoyed reading this article. Django offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower</a> if this sounds like something you’d be interested in.</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>.</em></p><p><em>Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong></p><p><strong><em>Interested in scaling your software startup</em></strong><em>? Check out </em><a href="https://circuit.ooo/?utm=publication-post-cta"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e0f4fdfa834" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/amazing-features-of-django-orm-day5of30-djangotip02-6e0f4fdfa834">Amazing features of Django ORM — Day5of30 — DjangoTip02</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Guide to Installing  Jenkins using Docker — Day4of30]]></title>
            <link>https://vivekhere.medium.com/guide-to-installing-jenkins-using-docker-day4of30-e903f3477bd4?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/e903f3477bd4</guid>
            <category><![CDATA[install-jenkins]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[devops-practice]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Sat, 12 Nov 2022 10:03:57 GMT</pubDate>
            <atom:updated>2023-01-01T10:06:01.605Z</atom:updated>
            <content:encoded><![CDATA[<h3>Guide to Installing Jenkins using Docker — DevOps01</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-W1CCV19byG3mMI48YKp1A.png" /></figure><p>Jenkins is one of the most popular continuous integration tool that helps developers to automatically build and test code changes, and then deploy them into production. Jenkins helps to automate the software development process, and can be used to monitor the execution of external programs and trigger events such as building a new version of a software program or deploying a new application into production.</p><p>In this particular article you will learn how you can install Jenkins using Docker in your machine.</p><h3>Prerequisites</h3><p>You should have Docker installed on your machine in order to follow along with this post. You don’t need to be an expert on Docker; just being familiar with a few fundamental commands will do.</p><p>If you don’t have docker installed then you can follow <a href="https://docs.docker.com/get-docker/">this</a> guide to install docker in your system.</p><h3>Step 1 — Understanding Docker Pull/Run</h3><p>You receive a docker CLI tool on your terminal once Docker is installed. Any image from the <a href="https://hub.docker.com/">Docker Hub</a> can be downloaded and launched using this CLI during Docker runtime<br>While the docker runcommand can be used to create running containers, docker pull just pulls an image from the repository. In this article, you will launch <a href="https://hub.docker.com/r/jenkins/jenkins">Jenkins</a> using the docker run command.</p><h3>Step 2 — Running Docker Run Command</h3><p>After opening your terminal type the following command to install Jenkins as a container in Docker.</p><pre>$ docker run -p 8080:8080 -p 50000:50000 -d -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts</pre><p>After executing the above command, it will produce the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nUmqrGDH36KpCddKyhAPhA.png" /></figure><p>After successful installation you can run docker ps command to confirm that jenkins container is running in your machine. Before going any further lets breakdown the command we ran on the terminal.</p><p>You can see from the command above that we downloaded Jenkins from a remote repository using docker run and then turned it into a container. Jenkins runs by default on port 8080, thus we bound port 8080 to our local machine, which is why the -p 8080:8080is present. The second port -p 50000:50000is for the master and worker nodes of Jenkins; while we don’t need this port for this tutorial, Jenkins requires it. The -dflag instructs Docker to continue operating the container even if the terminal is closed. It stands for detach mode. The flag-v jenkins_home:/var/jenkins_home is used to specify the volume in docker. This is used to persist all the jobs that we define on the constainer even when we stop the container.</p><h3>Step 3 — Working in the UI</h3><pre>$ docker ps<br>CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                              NAMES<br>3f32d437b011   jenkins/jenkins:lts   &quot;/usr/bin/tini -- /u…&quot;   3 minutes ago   Up 3 minutes   0.0.0.0:8080-&gt;8080/tcp, 0.0.0.0:50000-&gt;50000/tcp   modest_pare</pre><p>Once you see the above ouput after running docker ps , goto localhost:8080 in your browser to sign into Jenkins UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KWgVybZlAPyfdTHGr6K7Jw.png" /><figcaption>localhost:8080</figcaption></figure><p>You must input the Admin Password stored in the docker volumes you just defined on the screen above. Go to the terminal and perform the following steps to access the password.</p><pre>$ docker exec -it 3f32d437b011 bash<br>jenkins@3f32d437b011:/$ cat /var/jenkins_home/secrets/initialAdminPassword<br>6998f4fee3cf49389ce53c52c0334a67<br>jenkins@3f32d437b011:/$</pre><p>In the above command you started an interactive bash in the Jenkins Container using execcommand with -it flag. Note that Container ID3f32d437b011 would be different in your system. You can check your ID using docker ps command.</p><p>After installing the recommended plugins, the jenkins UI will show a form to create a user, just fill in the the details and click on Save and Continue. After clicking Start Jenkins in the Next screen, you will see the first look at Jenkins Dashboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fTfYsrBfCp-R2B30McOQjw.png" /><figcaption>New User Form in Jenkins</figcaption></figure><p>From this Dashboard you can create your Build Jobs, Views and automation scripts using Groovy, which I may discuss in coming blogs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ebCMg3d1SfKtEywxUiHHAA.png" /><figcaption>Jenkins Dashboard</figcaption></figure><p>I sincerely hope you enjoyed reading this article. Docker offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower</a> if this sounds like something you’d be interested in.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e903f3477bd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Customize Python Built-in Data Types — Day3of30 #PyTip06]]></title>
            <link>https://python.plainenglish.io/how-to-customize-python-built-in-data-types-day3of30-pytip06-42834ff44845?source=rss-d2e6bd6b5964------2</link>
            <guid isPermaLink="false">https://medium.com/p/42834ff44845</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Vivek K. Singh]]></dc:creator>
            <pubDate>Fri, 11 Nov 2022 08:40:01 GMT</pubDate>
            <atom:updated>2022-11-29T16:31:20.267Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Customize Python Built-in Data Types — PyTip06</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hoemoeXH8s41mDHQtkX0_w.png" /></figure><p>You may have come across the statement <em>“Everything in Python is an Object” </em>during your Python coding adventure. In this article, you will learn how to use this Python feature to your advantage and learn how to increase the usefulness of the built-in data types in Python.</p><p>Although you may always extend the built-in data types in Python by inheriting them to your own Data Types but Python suggests using the UserDict, UserList, and UserString from the collections module.</p><h3>Customizing Dictionaries</h3><p>In the first example you will make Python Dictionary that converts non-string keys into strings and your new customized dictionary will return None when it encounters KeyError .</p><pre>from collections import UserDict</pre><pre>class Dict(UserDict):</pre><pre>    def __missing__(self, k):<br>        return None</pre><pre>    def __getitem__(self, k):<br>        k = str(k)<br>        return super().__getitem__(k)</pre><p>In the above custom dictionary you have overriden all the required functions in order to achieve your goal. Start a Python REPL using the interactive mode execution.</p><pre>$ python -i custom.py<br>&gt;&gt;&gt; a = {&#39;1&#39;:&#39;one&#39;, &#39;2&#39;:&#39;two&#39;}</pre><pre>&gt;&gt;&gt; a[&#39;1&#39;]<br>&#39;one&#39;</pre><pre>&gt;&gt;&gt; a[1]<br>Traceback (most recent call last):<br>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>KeyError: 1</pre><pre>&gt;&gt;&gt; a[12]<br>Traceback (most recent call last):<br>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>KeyError: 12</pre><p>As you can see from the output of the aforementioned code, the standard dictionary produces a key error when looking up a non-string key or a key that doesn’t exist. Let’s now attempt to accomplish the same thing using the custom dictionary you just programmed.</p><pre>&gt;&gt;&gt; b = Dict({&#39;1&#39;:&#39;one&#39;, &#39;2&#39;:&#39;two&#39;})<br>&gt;&gt;&gt; b[&#39;1&#39;]<br>&#39;one&#39;<br>&gt;&gt;&gt; b[1]<br>&#39;one&#39;<br>&gt;&gt;&gt; b[12]<br>&gt;&gt;&gt;</pre><p>As you can see from the code above, the dictionary gives values for a non-string key. It also returns None when you search for a key that doesn’t exist.</p><p>To make a custom data types for List and String you can check the available functions using dir() and help() built-in function.</p><p>I sincerely hope you enjoyed reading this article. Python offers a ton of other valuable features like these, which I intend to discuss in my upcoming blogs. Consider becoming a <a href="https://twitter.com/vivekthedev">follower </a>if this sounds like something you’d be interested in.</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><strong><em>, </em></strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><strong><em>, </em></strong><em>and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><strong><em>.</em></strong><em> Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42834ff44845" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/how-to-customize-python-built-in-data-types-day3of30-pytip06-42834ff44845">How to Customize Python Built-in Data Types — Day3of30 #PyTip06</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>