<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Modules, Files and JSON on kodkod | Intro Course</title><link>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/</link><description>Recent content in Modules, Files and JSON on kodkod | Intro Course</description><generator>Hugo</generator><language>en</language><copyright>מבואות קודקוד</copyright><atom:link href="https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/index.xml" rel="self" type="application/rss+xml"/><item><title>Modules: Why We Split into Files, import, from and as</title><link>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/01-modules-and-import/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/01-modules-and-import/</guid><description>&lt;h2 id="what-will-we-learn"&gt;What will we learn?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What a &lt;strong&gt;Module&lt;/strong&gt; is.&lt;/li&gt;
&lt;li&gt;Why a project is split into &lt;strong&gt;several files&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Three forms of importing: &lt;code&gt;import&lt;/code&gt;, &lt;code&gt;from ... import&lt;/code&gt;, &lt;code&gt;as&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What &lt;strong&gt;exactly&lt;/strong&gt; happens when Python imports a file.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="why-do-we-need-this"&gt;Why do we need this?&lt;/h2&gt;
&lt;p&gt;We already used modules: &lt;code&gt;import os&lt;/code&gt;, &lt;code&gt;import logging&lt;/code&gt;, &lt;code&gt;import socket&lt;/code&gt;. Today we will write &lt;strong&gt;our own&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Packages, __init__.py and if __name__ == "__main__"</title><link>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/02-packages-and-main-guard/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/02-packages-and-main-guard/</guid><description>&lt;h2 id="what-will-we-learn"&gt;What will we learn?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Package&lt;/strong&gt; — a folder that contains modules.&lt;/li&gt;
&lt;li&gt;The file &lt;strong&gt;&lt;code&gt;__init__.py&lt;/code&gt;&lt;/strong&gt; and its role.&lt;/li&gt;
&lt;li&gt;Importing from inside a Package.&lt;/li&gt;
&lt;li&gt;The special variable &lt;strong&gt;&lt;code&gt;__name__&lt;/code&gt;&lt;/strong&gt;, and the pattern &lt;strong&gt;&lt;code&gt;if __name__ == &amp;quot;__main__&amp;quot;:&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="why-do-we-need-this"&gt;Why do we need this?&lt;/h2&gt;
&lt;p&gt;When there are 3 modules, they can sit in the same folder. When there are 30, you need &lt;strong&gt;folders&lt;/strong&gt;: everything related to users in one folder, everything related to payments in another. That is how all real projects are built, and also all the libraries you will install (FastAPI is a huge Package with dozens of sub-Packages).&lt;/p&gt;</description></item><item><title>Working with Text Files: open, with, Reading and Writing</title><link>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/03-text-files-with/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/03-text-files-with/</guid><description>&lt;h2 id="what-will-we-learn"&gt;What will we learn?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Opening a file&lt;/strong&gt; with &lt;code&gt;open&lt;/code&gt; and &lt;strong&gt;opening modes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Why a file &lt;strong&gt;must be closed&lt;/strong&gt;, and how &lt;strong&gt;&lt;code&gt;with&lt;/code&gt;&lt;/strong&gt; does it automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reading&lt;/strong&gt;: the whole file, line by line.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Writing&lt;/strong&gt; and &lt;strong&gt;appending&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;robust&lt;/strong&gt; path, one that does not depend on where you run from (we promised in the topic on the computer, the operating system and the network!).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="why-do-we-need-this"&gt;Why do we need this?&lt;/h2&gt;
&lt;p&gt;The topic on the computer, the operating system and the network: &amp;ldquo;everything in RAM disappears when the program ends&amp;rdquo;. Files are the basic way to save information on &lt;strong&gt;Storage&lt;/strong&gt;: settings, reports, history, data for import and export. The counter that always went back to 1 in &lt;code&gt;memory_test.py&lt;/code&gt;? Today we will make it survive between runs.&lt;/p&gt;</description></item><item><title>JSON: Reading, Writing and Saving Data</title><link>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/04-json/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://intro-kodkod-6.nm-ambition.com/en/docs/python-modules-files/04-json/</guid><description>&lt;h2 id="what-will-we-learn"&gt;What will we learn?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What &lt;strong&gt;JSON&lt;/strong&gt; is and what it looks like.&lt;/li&gt;
&lt;li&gt;How JSON matches Python&amp;rsquo;s &lt;strong&gt;dict&lt;/strong&gt; and &lt;strong&gt;list&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Saving&lt;/strong&gt; to a file and loading from it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Converting&lt;/strong&gt; between a JSON string and data.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="why-do-we-need-this"&gt;Why do we need this?&lt;/h2&gt;
&lt;p&gt;In the previous lesson we saved text, and for that we invented a &amp;ldquo;format&amp;rdquo; of our own: &lt;code&gt;Dana,90&lt;/code&gt; on every line. Now suppose every student has a &lt;strong&gt;list&lt;/strong&gt; of grades, an &lt;strong&gt;address&lt;/strong&gt; with a city and a street, and an &lt;code&gt;is_active&lt;/code&gt; &lt;strong&gt;flag&lt;/strong&gt;. How do you save that in a line of text? Everyone will invent a different format, and we will have to write complicated parsing code.&lt;/p&gt;</description></item></channel></rss>