<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xmlns:tt="http://teletype.in/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Samandar Komilov</title><generator>teletype.in</generator><description><![CDATA[20 y.o. Software Engineer]]></description><image><url>https://img4.teletype.in/files/f4/82/f482ad67-f0a8-464c-b089-807422e23d3b.png</url><title>Samandar Komilov</title><link>https://teletype.in/@void_pointer</link></image><link>https://teletype.in/@void_pointer?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer</link><atom:link rel="self" type="application/rss+xml" href="https://teletype.in/rss/void_pointer?offset=0"></atom:link><atom:link rel="next" type="application/rss+xml" href="https://teletype.in/rss/void_pointer?offset=10"></atom:link><atom:link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></atom:link><pubDate>Thu, 16 Apr 2026 10:57:27 GMT</pubDate><lastBuildDate>Thu, 16 Apr 2026 10:57:27 GMT</lastBuildDate><item><guid isPermaLink="true">https://teletype.in/@void_pointer/binary-relations-in-computer-science</guid><link>https://teletype.in/@void_pointer/binary-relations-in-computer-science?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer</link><comments>https://teletype.in/@void_pointer/binary-relations-in-computer-science?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer#comments</comments><dc:creator>void_pointer</dc:creator><title>Binary Relations, Relational Images, Inverse Relations and Images. Why do we need them in Computer Science?</title><pubDate>Sat, 01 Jun 2024 13:00:59 GMT</pubDate><category>Mathematics</category><description><![CDATA[<img src="https://img2.teletype.in/files/9e/47/9e47727f-8892-417b-9c35-cb8ed9422e8e.png"></img>Binary relations define relations between two objects.]]></description><content:encoded><![CDATA[
  <h3 id="GOOj">Binary Relations</h3>
  <p id="f7U4">Binary relations define relations between two objects.</p>
  <section style="background-color:hsl(hsl(34,  84%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="daL5">📌 A <strong>binary relation</strong> associates elements of one set called <em>domain</em> with elements of another set called <em>codomain</em>. A <strong>function</strong> is a special case of binary relation.</p>
  </section>
  <p id="4pEV">Well, we say &quot;relation, relation&quot; everytime, but what is the relation actually?</p>
  <section style="background-color:hsl(hsl(170, 33%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="RYc3">👉 A <strong>relation </strong>between 2 sets, say A and B, is <u>a set of ordered pairs</u> (p, q) where p∈A and q∈B and is a subset of Cartesian Product A×B. For example, A = {1, 2} and B = {a, b} are sets and R = {(1, a), (1, b), (2, b)} is a relation. </p>
    <p id="faHG">Types of relations:<br />- <strong>Total relation</strong> - Cartesian Product of two sets.<br />- <strong>Empty relation</strong> - No element of A is related to any element of B, ∅<br />- <strong>Functional relation</strong> -  Each element of A is related to at most 1 element of B, f.</p>
  </section>
  <p id="kvh3">There are 5 types of binary relations:</p>
  <figure id="bAwY" class="m_column">
    <img src="https://img2.teletype.in/files/9e/47/9e47727f-8892-417b-9c35-cb8ed9422e8e.png" width="791" />
    <figcaption>Types of relations</figcaption>
  </figure>
  <p id="LSLs"></p>
  <h3 id="N6Jb">Relational Images</h3>
  <p id="6BOD">The idea of the image of a set under a function extends directly to relations.</p>
  <section style="background-color:hsl(hsl(55,  86%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="iFpO">📌 The <strong>image</strong> of a set Y under a relation R written R(Y), is the set of elements of the codomain B of R that are related to some element in Y.</p>
  </section>
  <p id="NHKQ">Formal definition: Given a relation R between two sets A (the domain) and B (the codomain), and a subset S⊆A. The image of S under R is the set </p>
  <p id="ABBv" data-align="center">R(S) = {b∈B ∣ ∃a∈S such that (a,b)∈R}</p>
  <p id="0re9">Example: A = {1, 2, 3}, B = {a, b, c} and subset S = {1, 3}. Relation between A and B is R = {(1, a), (2, b), (1, c), (3, a)}. So the image of S under relation R becomes:</p>
  <p id="vYMj" data-align="center">R(S) = {a, c}</p>
  <p id="wDvd">Which means, the elements of the subset S is related to only these elements in a set B.</p>
  <p id="Zxje"></p>
  <h3 id="xuhX"><strong>❓ Why we need this in Computer Science?</strong></h3>
  <p id="Fo6A">This concept is fundamental and very crucial in Computer Science due to its broad applicability to various fields like databases, graph theory and algorithms. Let&#x27;s consider each:</p>
  <p id="c9mB"><strong>1. Database management</strong></p>
  <p id="dqP5">In relational databases, the relationships between different tables (relations) are essential. For example, consider a database with tables for <code>Users</code> and <code>Orders</code>. The relationship can be represented by a relation R where each user is linked to their orders. If S is a set of users, <code>R(S)</code> would yield all orders related to these users.</p>
  <pre id="V1yV">Users = {Alice, Bob, Charlie}
Orders = {Order1, Order2, Order3, Order4}
R = {(Alice, Order1), (Bob, Order2), (Charlie, Order3), (Alice, Order4)}
S={Alice}
R(S)={Order1,Order4} - Image of S under relation R.</pre>
  <p id="ayuP">This is exactly the thing which finding all orders made by Alice.</p>
  <p id="DPty"><strong>2. Graph Theory</strong></p>
  <p id="5RpY">The image of a set under a relation in graph terms describes adjacency. Let&#x27;s say we have a graph with some nodes and edges (relations). We can find all nodes which are directly reachable from some node (in this case A) using images:</p>
  <pre id="wz86">Nodes = {A, B, C, D}
Edges (relation) = {(A, B), (A, C), (B, D), (C, B)}
S={A}
R(S)={B,C} - Nodes directly reachable from node A.</pre>
  <p id="gwhc"><strong>3. Algorithms</strong></p>
  <p id="9i1y">Many algorithms involve mapping elements from one set to another to identify properties or solve problems. For instance, in sorting algorithms, the relation might define how elements compare to each other, and understanding the image of a set under this relation can help optimize sorting.</p>
  <pre id="mpWa">array = {3, 1, 4, 1, 5, 9, 2}
Relation R based on &#x27;&lt;&#x27;
S = {4}
R(S) = {5, 9} - elements greater than 4</pre>
  <p id="xL2Q">Here, we have a relation &quot;greater than&quot; and we filtered the array for greater that 4.</p>
  <p id="nyCA"></p>
  <h3 id="Q7jY">Inverse Relation</h3>
  <section style="background-color:hsl(hsl(55,  86%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="CoCT">📌 An <strong>inverse relation</strong> of a binary relation R between two sets A and B is a new relation R^(-1) from B to A. For any pair (a,b) in R, the pair (b,a) will be in R^(-1). Essentially, it reverses the direction of the relation.</p>
  </section>
  <p id="wMpO">Consider the relation R  = {(1, a), (2, b), (3, a)} from set A = {1, 2, 3} to set B = {a, b}. The inverse relation becomes</p>
  <p id="88f8" data-align="center">R^(-1) = {(a, 1), (a, 3), (b, 2)}</p>
  <p id="sYAU"></p>
  <h3 id="baAL">Inverse Image</h3>
  <section style="background-color:hsl(hsl(55,  86%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="2DyZ">📌 The <strong>inverse image</strong> of a set under a relation (or function) relates to inverse relations. For a set Y⊆B and a relation R from A to B, the inverse image of Y under R, written as R^(-1)(Y), is the set of all elements in A that relate to elements in Y via R.</p>
  </section>
  <p id="kJOM">To understand this concept clearly, I&#x27;ll give an example in relational databases.</p>
  <p id="pNfa">Consider a database with two tables, <code>Employees</code> and <code>Departments</code>. Each employee is assigned to one department, establishing a relation R from <code>Employees</code> to <code>Departments</code>. The inverse relation R^{-1} can be used to find all employees in a specific department. If you know the department ID, you can easily retrieve all employees linked to that department, effectively using R^{-1}.</p>
  <pre id="aYlY">R = {(Sam, Development), (Tom, HR), (Martin, Development)}
R^(-1) = querying for the &quot;Department&quot; retrieves &quot;Sam&quot; and &quot;Martin&quot;.</pre>
  <p id="AJpE"></p>
  <h3 id="c7xb">Finite Cardinality</h3>
  <section style="background-color:hsl(hsl(323, 50%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="t9sR">A finite set is one that has only a finite number of elements. This number of ele-<br />ments is the “size” or <strong>cardinality</strong> of the set. If A is a finite set, the cardinality jAj of A is the number of elements in A.</p>
  </section>
  <p id="kNNR">We can then redefine the binary relations in finite sets:</p>
  <ol id="71zr">
    <li id="XfDK">If A surjective B, then |A| &gt;= |B|</li>
    <li id="matA">If A injective B, then |A| &lt;= |B|</li>
    <li id="DulJ">If A bijective B, then |A| = |B|</li>
  </ol>
  <p id="WgvV"></p>
  <p id="b5mL"><strong>❓ How many subsets of a finite set?</strong></p>
  <p id="SHkC">✅ There are 2^n subsets in a n-element set.</p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@void_pointer/builtin-python-functions</guid><link>https://teletype.in/@void_pointer/builtin-python-functions?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer</link><comments>https://teletype.in/@void_pointer/builtin-python-functions?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer#comments</comments><dc:creator>void_pointer</dc:creator><title>Python Built-in Functions reference</title><pubDate>Sat, 10 Feb 2024 15:36:45 GMT</pubDate><category>python</category><description><![CDATA[In this small article, we will try to study as much as possible Built-in functions of Python. Here is the Python official documentation on the subject:]]></description><content:encoded><![CDATA[
  <p id="nOIT">In this small article, we will try to study as much as possible Built-in functions of Python. Here is the Python official documentation on the subject:</p>
  <p id="9hRM"><a href="https://docs.python.org/3/library/functions.html" target="_blank">https://docs.python.org/3/library/functions.html</a></p>
  <p id="n43d">👉<code>abs(x)</code> - absolute value of a number. Everyone knows it, right :)</p>
  <p id="R3Sv">👉<code>all(iterable)</code> - returns <strong>True</strong> if all elements of the <em>iterable object (list, tuple, dictionary)</em> are True, otherwise returns <strong>False</strong>. Equivalent to:</p>
  <pre id="2VNI">def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True</pre>
  <p id="gNao">👉<code>any()</code> - returns <strong>True</strong> if any item in an <em>iterable object</em> are true, otherwise it returns <strong>False</strong>. Equivalent to:</p>
  <pre id="06tS">def any(iterable):
    for element in iterable:
        if element:
            return True
    return False</pre>
  <p id="Bj7c">👉<code>ascii(object)</code> - returns a readable version of an object. Replaces none-ascii characters with <u>escape character</u>.</p>
  <pre id="2qQJ">x = ascii(&quot;Hi Såm!&quot;)
print(x)

# Output: Hi S\e5m!</pre>
  <p id="7rSY">👉<code>bin(x)</code> - Return the binary version of the number.</p>
  <pre id="Rf6M">binary_number = bin(20)
print(binary_number)

# 0b10100</pre>
  <p id="tb85">👉<code>bool(object)</code> - returns the boolean value of a specified object. The function returns False if:</p>
  <ul id="noNy">
    <li id="GLDh">The object is empty, like [], {}, ()</li>
    <li id="Lke7">The object is False</li>
    <li id="liNH">The object is 0</li>
    <li id="Eg5d">The object is None</li>
  </ul>
  <p id="enq2">Otherwise, it returns True. Keep in mind that, using negative number also returns True:</p>
  <pre id="JxFD">print(bool(-4))

# True</pre>
  <section style="background-color:hsl(hsl(236, 74%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="Nz8W">👉<code>bytearray()</code> - </p>
  </section>
  <section style="background-color:hsl(hsl(236, 74%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="aum6">👉<code>bytes()</code> - </p>
  </section>
  <p id="uu5K">👉<code>callable(object)</code> - returns <strong>True</strong> if the specified object is callable, otherwise it returns <strong>False</strong>.</p>
  <pre id="ozdr">def func():
    return 5

print(callable(func))

# True</pre>
  <p id="VV3J">When it returns False? Variables are not callable, for example.</p>
  <p id="7diZ">👉<code>chr(x)</code> - converts an integer to its unicode character and returns it.</p>
  <pre id="h3dh">print(chr(97))
# a</pre>
  <section style="background-color:hsl(hsl(323, 50%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="U1xI">👉<code>compile()</code> - </p>
    <p id="go8Z">👉<code>eval()</code> - </p>
    <p id="f7l8">👉<code>exec()</code> -</p>
  </section>
  <p id="MhgP">👉<code>classmethod()</code> - converts the method into class method. In newer versions of Python, it is used as decorator, like <code>@classmethod</code>. </p>
  <pre id="VWd6">class Student:
  marks = 0

  def compute_marks(self, obtained_marks):
    marks = obtained_marks
    print(&#x27;Obtained Marks:&#x27;, marks)

# convert compute_marks() to class method
Student.print_marks = classmethod(Student.compute_marks)</pre>
  <p id="IXWy">👉<code>complex(real, imaginary)</code> - returns a complex number object by taking real and imaginary parts as floats. </p>
  <pre id="DMFC">print(complex(2.3, 5.4))
# (2.3 + 5.4j)</pre>
  <p id="IPhb">In addition, we can convert strings to complex numbers, but in this case we should omit the second parameter:</p>
  <pre id="aAtC">print(complex(&quot;2+3j&quot;))
# (2+3j)</pre>
  <p id="lAQa">👉<code>delattr(object, name)</code> - removes an attribute of an Object (if the object allows it).</p>
  <pre id="YU4q">class Coordinate:
  x = 10
  y = -5
  z = 0

point1 = Coordinate()
print(&#x27;x = &#x27;,point1.x)
print(&#x27;y = &#x27;,point1.y)
print(&#x27;z = &#x27;,point1.z)

delattr(Coordinate, &#x27;z&#x27;)

# Raises Error
print(&#x27;z = &#x27;,point1.z)</pre>
  <p id="rLXC">👉<code>dict(**kwarg)</code> - does not return any value. Instead, it creates a new dictionary according to keyword arguments given to it. Let&#x27;s consider various ways of doing so:</p>
  <pre id="u9HQ">numbers = dict(x=5, y=0)
print(&#x27;numbers =&#x27;, numbers)

# numbers = {&#x27;y&#x27;:0, &#x27;x&#x27;:5}</pre>
  <pre id="SHRB">numbers = dict([(&#x27;x&#x27;, 5), (&#x27;y&#x27;, -5)])
print(&#x27;numbers =&#x27;,numbers)

# numbers = {&#x27;y&#x27;:-5, &#x27;x&#x27;:5}</pre>
  <p id="LkKA">Also, we can create an empty dictionary:</p>
  <pre id="Gi7s">print(dict())

# {}</pre>
  <p id="6pfg">👉<code>dir(object)</code> - returns the list of valid attributes of the passed object.</p>
  <p id="W8al">👉<code>divmod(num1, num2)</code> - takes two numbers as arguments and returns their quotient and remainder in a tuple.</p>
  <pre id="NNC9">result = divmod(8, 3)
print(&#x27;Quotient and Remainder = &#x27;,result)

# Output: Quotient and Remainder =  (2, 2)</pre>
  <p id="8Zg1">👉<code>enumerate(iterable, start=0)</code> - adds a counter to an iterable and returns it as an enumerate object (iterator with index and the value).</p>
  <pre id="a126">langs = [&#x27;Uzbek&#x27;, &#x27;English&#x27;, &#x27;Turkish&#x27;]
enum_langs = enumerate(langs)
print(list(enum_length))

# [(0, &#x27;Uzbek&#x27;), (1, &#x27;English&#x27;), (2, &#x27;Turkish&#x27;)]</pre>
  <p id="JSsn">Keep in mind that, when printing always convert the result to list, since the function returns <u>enumerate object</u>.</p>
  <p id="gE3U">👉<code>staticmethod()</code> - converts the given method of an object to static method.</p>
  <pre id="1Fhs">class Calculator:
  def add_numbers(num1, num2):
    return num1 + num2

Calculator.add_numbers = staticmethod(Calculator.add_numbers)
sum = Calculator.add_numbers(5, 7)
print(&#x27;Sum:&#x27;, sum)

# Output: Sum: 12</pre>
  <p id="c0Yy">👉<code>filter(function, iterable)</code> - selects elements from an iterable based on the result of a given function.</p>
  <pre id="Epqo">def check_even(number):
    if number % 2 == 0:
          return True  
    return False

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers_iterator = filter(check_even, numbers)
even_numbers = list(even_numbers_iterator)
print(even_numbers)

# Output: [2, 4, 6, 8, 10]</pre>
  <p id="XQR6">👉<code>float()</code> - returns a floating point number from a number or a string.</p>
  <p id="A5Ab">👉<code>format(value, format_spec)</code> - returns a formatted value based on the specified formatter. It can be <code>&#x27;b&#x27;</code> - binary, <code>&#x27;d&#x27;</code> - decimal or some other. You can find on doc.</p>
  <pre id="dObC">val = 0xf3
bval = format(val, &#x27;b&#x27;)
dval = format(val, &#x27;d&#x27;)
print(bval, dval)

# 11110011 243</pre>
  <p id="rqFe">👉<code>frozenset(iterable)</code> - frozen set is just an immutable version of a <a href="https://www.programiz.com/python-programming/set" target="_blank">Python set</a> object. While elements of a set can be modified at any time, elements of the frozen set <u>remain the same</u> after creation.</p>
  <p id="l6hW">Due to this, frozen sets can be used as keys in <a href="https://www.programiz.com/python-programming/dictionary" target="_blank">Dictionary</a> or as elements of another set. But like sets, it is not ordered (the elements can be set at any index).</p>
  <pre id="1e8i">mylist = [&#x27;apple&#x27;, &#x27;banana&#x27;, &#x27;cherry&#x27;]
x = frozenset(mylist)
print(x)

# frozenset({&#x27;apple&#x27;, &#x27;cherry&#x27;, &#x27;banana&#x27;})</pre>
  <p id="00Qo">👉<code>getattr(object, name)</code> - returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.</p>
  <pre id="joA5">class Student:
  marks = 88
  name = &#x27;Sheeran&#x27;

person = Student()
name = getattr(person, &#x27;name&#x27;)
print(name)
marks = getattr(person, &#x27;marks&#x27;)
print(marks)

# Sheeran
# 88</pre>
  <p id="7wMj">👉<code>globals()</code> - returns a dictionary with all the global variables and symbols for the current program.</p>
  <p id="mgXQ">👉<code>hasattr()</code> - returns true if an object has the given named attribute and false if it does not.</p>
  <pre id="nigw">class Person:
    age = 23
    name = &quot;Adam&quot;

person = Person()
print(&quot;Person&#x27;s age:&quot;, hasattr(person, &quot;age&quot;))
print(&quot;Person&#x27;s salary:&quot;, hasattr(person, &quot;salary&quot;))

# Person&#x27;s age: True
# Person&#x27;s salary: False</pre>
  <p id="NzZG">👉<code>help()</code> - use it when you need some help.</p>
  <p id="CvQg">👉<code>hex(int)</code> - converts an integer to the corresponding hexadecimal number in string form and returns it.</p>
  <pre id="9iux">num = 45
print(hex(num))

# 0x2d</pre>
  <p id="uSyR">👉<code>hash(object)</code> - returns the hash value of an object if it has one.</p>
  <pre id="mwXA">text = &#x27;Python Programming&#x27;
hash_value = hash(text)
print(hash_value)

# Output: -966697084172663693</pre>
  <p id="Ua5S">👉<code>input(&#x27;prompt&#x27;)</code> - takes input from the user and returns it.</p>
  <p id="1kny">👉<code>id()</code> - returns the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same <a href="https://docs.python.org/3/library/functions.html#id" target="_blank"><code>id()</code></a> value. </p>
  <p id="j74q">In other words, this is the address of the object in memory.</p>
  <pre id="nWxg">print(&quot;id of 5 =&quot;, id(5))

# 140472391630016</pre>
  <p id="QIgz">👉<code>isinstance(object, classinfo)</code> - checks if the object (first argument) is an instance or subclass of classinfo class (second argument).</p>
  <pre id="4pOG">numbers = [1, 2, 3, 4, 2, 5]
result = isinstance(numbers, list)
print(result)

# Output: True</pre>
  <p id="muLY">👉<code>int(x)</code> - converts a number or a string to its equivalent integer.</p>
  <p id="Pvgh">👉<code>issubclass(class, classinfo)</code> - checks whether the given <code>class</code> is subclass of <code>classinfo</code>. </p>
  <pre id="SXoB">class Polygon:
  def __init__(polygonType):
    print(&#x27;Polygon is a &#x27;, polygonType)

class Triangle(Polygon):
  def __init__(self):

    Polygon.__init__(&#x27;triangle&#x27;)
    
print(issubclass(Triangle, Polygon))
print(issubclass(Triangle, list))
print(issubclass(Triangle, (list, Polygon)))
print(issubclass(Polygon, (list, Polygon)))

# True
# False
# True
# True</pre>
  <p id="1luv">Take into account that every class is considered subclass of itself.</p>
  <section style="background-color:hsl(hsl(323, 50%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="VN8k">👉<code>iter()</code> - returns an iterator for the given argument. </p>
    <p id="Dw06">👉<code>next()</code> - returns the next item from the iterator.</p>
  </section>
  <p id="7nzU">👉<code>list(iterable)</code> - returns a <a href="https://www.programiz.com/python-programming/list" target="_blank">list</a> in Python.</p>
  <pre id="qqhB">text = &#x27;Python&#x27;
print(list(text))
print(type(text_list))

# [&#x27;P&#x27;, &#x27;y&#x27;, &#x27;t&#x27;, &#x27;h&#x27;, &#x27;o&#x27;, &#x27;n&#x27;]
# &lt;class &#x27;list&#x27;&gt; </pre>
  <p id="0MB8">👉<code>locals()</code> - returns a <a href="https://www.programiz.com/python-programming/dictionary" target="_blank">dictionary</a> with all the local variables and symbols for the current program.</p>
  <p id="WVpa">👉<code>len(iterable)</code> - returns the number of items (length) in an object.</p>
  <p id="W2lK">👉<code>max(iterable)</code> - returns the largest item in an iterable.</p>
  <p id="vXmM">👉<code>min(iterable)</code> - returns the smallest item in an iterable.</p>
  <section style="background-color:hsl(hsl(55,  86%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="ZeWv">👉<code>memoryview()</code> - is a safe way to expose the buffer protocol in Python.</p>
    <p id="lUrg"><strong>Python Buffer Protocol - t</strong>he buffer protocol provides a way to access the internal data of an object. This internal data is a memory array or a buffer.</p>
    <p id="Btxt">The buffer protocol allows one object to expose its internal data (buffers) and the other to access those buffers without intermediate copying.</p>
    <p id="cjHi">This protocol is only accessible to us at the C-API level and not using our normal codebase.</p>
    <p id="J8PS">So, in order to expose the same protocol to the normal Python codebase, memory views are present.</p>
    <p id="9MG6"><a href="https://www.programiz.com/python-programming/methods/built-in/memoryview" target="_blank">https://www.programiz.com/python-programming/methods/built-in/memoryview</a></p>
  </section>
  <p id="4WiF">👉<code>map(function, iterables)</code> - executes a given function to each element of an iterable (such as <a href="https://www.programiz.com/python-programming/list" target="_blank">lists</a>, <a href="https://www.programiz.com/python-programming/tuple" target="_blank">tuples</a>, ...)</p>
  <pre id="V9i2">numbers = [1,2,3,4]

def square(number):
  return number * number

squared_numbers = map(square, numbers)

result = list(squared_numbers)
print(result)

# Output: [1,4,9,16]</pre>
  <p id="pwgm">Keep in mind that, after applying this function, you have to convert the result into list or  some other alternative type, since by default it returns <u>map object</u>.</p>
  <p id="HVge">👉<code>object()</code> - returns a featureless object. </p>
  <p id="E6QN">👉<code>oct(int)</code> - returns an octal string from the given integer number.</p>
  <pre id="hlNU"># decimal to octal
print(&#x27;oct(10) is:&#x27;, oct(10))

# 0o12</pre>
  <p id="n7TM">👉<code>ord()</code> - returns an integer representing the Unicode character.</p>
  <pre id="Ema7">character = &#x27;P&#x27;
unicode_char = ord(character)
print(unicode_char)

# Output: 80</pre>
  <section style="background-color:hsl(hsl(170, 33%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="SejO">👉<code>open(file)</code> - returns a file object which can used to read, write and modify the file. If the file is not found, it raises the <code>FileNotFoundError</code> exception.</p>
    <pre id="OxO7">f = open(&quot;path_to_file&quot;, mode = &#x27;r&#x27;, encoding=&#x27;utf-8&#x27;)</pre>
  </section>
  <p id="fkWU">👉<code>pow()</code> - computes the power of a number.</p>
  <pre id="sYiu">print(pow(3, 4))
# Output:  81</pre>
  <p id="OW46">👉<code>print()</code> - Used to write &quot;Hello World!&quot; on the screen :))</p>
  <section style="background-color:hsl(hsl(323, 50%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="YHkY">👉<code>property()</code> - returns the property attribute from the given getter, setter, and deleter. (It is actually a decorator)</p>
  </section>
  <p id="DkCZ">👉<code>range()</code> - generates a sequence of numbers. By default, the sequence starts at 0, increments by 1, and stops before the specified number.</p>
  <pre id="aKIa">nums = range(4)
for i in nums:
    print(i, end=&quot; &quot;)
    
# 0 1 2 3</pre>
  <section style="background-color:hsl(hsl(55,  86%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="Q74x">👉<code>repr(object)</code> - returns a printable representation of the given object.</p>
  </section>
  <p id="o8eG">👉<code>reversed()</code> - computes the reverse of a given sequence object and returns it in the form of a list.</p>
  <pre id="h9jk">seq = &#x27;Python&#x27;ng
print(list(reversed(seg)))

# Output: [&#x27;n&#x27;, &#x27;o&#x27;, &#x27;h&#x27;, &#x27;t&#x27;, &#x27;y&#x27;, &#x27;P&#x27;]</pre>
  <p id="y3mQ">👉<code>round(number)</code> - rounds a number to the nearest integer.</p>
  <p id="NhCg">👉<code>set()</code> - creates a set from an iterable object.</p>
  <pre id="IPj3">print(set([1,2,3]))

# {1,2,3}</pre>
  <p id="uFpR">👉<code>setattr()</code> - sets the value of the attribute of an object.</p>
  <pre id="nuPk">class Student:
  marks = 88
  name = &#x27;Sheeran&#x27;

person = Student()
setattr(person, &#x27;name&#x27;, &#x27;Adam&#x27;)
setattr(person, &#x27;marks&#x27;, 78)

print(person.name)
print(person.marks)

# Adam
# 78</pre>
  <section style="background-color:hsl(hsl(170, 33%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="FyL4">👉<code>slice()</code> - returns a slice object that is used to slice any sequence (string, tuple, list, range, or bytes).</p>
    <pre id="7s8T">text = &#x27;Python Programing&#x27;

sliced_text = slice(6)
print(text[sliced_text])

# Output: Python</pre>
    <p id="cQyK"><a href="https://www.programiz.com/python-programming/methods/built-in/slice" target="_blank">https://www.programiz.com/python-programming/methods/built-in/slice</a></p>
  </section>
  <p id="xycQ">👉<code>sorted(iterable)</code> - sorts the elements of the given iterable in <u>ascending order</u> and returns it.</p>
  <p id="otVl">👉<code>str(object)</code> - returns the string representation of a given object.</p>
  <p id="HViZ">👉<code>sum(iterable)</code> - adds the items of an iterable and returns the sum.</p>
  <p id="Hezr">👉<code>tuple(iterable)</code> - creates a tuple from a given iterable object.</p>
  <p id="3G1u">👉<code>type()</code> - either returns the type of the object or returns a new type object based on the arguments passed.</p>
  <section style="background-color:hsl(hsl(323, 50%, var(--autocolor-background-lightness, 95%)), 85%, 85%);">
    <p id="lWf7">👉<code>vars()</code> - returns the <code>__dict__</code> (dictionary mapping) attribute of the given object.</p>
  </section>
  <p id="7a26">👉<code>zip(*iterables)</code> - takes iterables (can be zero or more), aggregates them in a <a href="https://www.programiz.com/python-programming/tuple" target="_blank">tuple</a>, and returns it.</p>
  <pre id="oNqp">languages = [&#x27;Java&#x27;, &#x27;Python&#x27;, &#x27;JavaScript&#x27;]
versions = [14, 3, 6]

result = zip(languages, versions)
print(list(result))

# Output: [(&#x27;Java&#x27;, 14), (&#x27;Python&#x27;, 3), (&#x27;JavaScript&#x27;, 6)]</pre>
  <p id="koR1">That&#x27;s all.</p>
  <hr />
  <p id="IyyE"><em>So, today we considered 66 out of 68 built-in functions in Python. I admit that some of them are highlighted and even without description, but that&#x27;s not reasonless. Some methods need more thorough research and usage cases to grasp the whole meaning and hence I just highlighted them. Next time, I try to cover them as well on separate articles.</em></p>
  <p id="7KbT"><em>🏁 Enough for today, thanks for your attention!</em></p>

]]></content:encoded></item><item><guid isPermaLink="true">https://teletype.in/@void_pointer/django-inspectdb-problem</guid><link>https://teletype.in/@void_pointer/django-inspectdb-problem?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer</link><comments>https://teletype.in/@void_pointer/django-inspectdb-problem?utm_source=teletype&amp;utm_medium=feed_rss&amp;utm_campaign=void_pointer#comments</comments><dc:creator>void_pointer</dc:creator><title>Django: inspectdb command auto generating ID fields? Here is why</title><pubDate>Sat, 27 Jan 2024 06:22:13 GMT</pubDate><media:content medium="image" url="https://img2.teletype.in/files/5a/ee/5aee7313-e5b8-4c3e-9c87-49b93c6c1b71.png"></media:content><category>django</category><description><![CDATA[<img src="https://img4.teletype.in/files/bc/d3/bcd313a1-8c45-472e-8db3-fe007e4d5d8d.png"></img>To begin with, I prefer to share official documentation of inspectdb :]]></description><content:encoded><![CDATA[
  <p id="krGm">To begin with, I prefer to share official documentation of <code>inspectdb</code> :</p>
  <p id="lnjf"><a href="https://docs.djangoproject.com/en/5.0/howto/legacy-databases/" target="_blank">https://docs.djangoproject.com/en/5.0/howto/legacy-databases/</a></p>
  <p id="mo6m">Well, usually we work with databases using DBMS tools, like Navicat. Which means, there is rare writing models one-by-one in Django ORM. In such cases, we use <code>inspectdb</code> command of Django to make our life easier. I hope you got the point about this command at least by reading the documentation above. Now, I&#x27;ll turn into a problem and start creating a new table, say <code>user</code>:</p>
  <figure id="fIDC" class="m_column">
    <img src="https://img4.teletype.in/files/bc/d3/bcd313a1-8c45-472e-8db3-fe007e4d5d8d.png" width="1004" />
    <figcaption>&#x60;user&#x60; table creation</figcaption>
  </figure>
  <p id="mQlz">Let&#x27;s assume that you have already connected your database to Django project:</p>
  <pre id="47ZM">DATABASES = {
    &#x27;default&#x27;: {
        &#x27;ENGINE&#x27;: &#x27;django.db.backends.postgresql_psycopg2&#x27;,
        &#x27;NAME&#x27;: &#x27;&lt;your_db_name&gt;&#x27;,
        &#x27;USER&#x27;: &#x27;&lt;your_db_user&gt;&#x27;,
        &#x27;PASSWORD&#x27;: &#x27;&lt;your_db_password&gt;&#x27;,
        &#x27;HOST&#x27;: &#x27;&lt;your_db_host&gt;&#x27;,
        &#x27;PORT&#x27;: &#x27;5432&#x27;
    }
}</pre>
  <p id="ArWO">and there is at least one app on that project. I created a test app <code>psql_app</code> and now run <code>python manage.py inspectdb &gt; psql_app/models.py</code>.  Nothing happens on Command prompt, but when you open your models.py you can see generated tables:</p>
  <figure id="olCZ" class="m_original">
    <img src="https://img3.teletype.in/files/62/e5/62e5dd8e-1f35-4bd0-992f-9c8c9e9f99ce.png" width="723" />
    <figcaption>Generated &#x60;user&#x60; table</figcaption>
  </figure>
  <p id="EQOO">Well, that&#x27;s normal. But stop! ID field is popped up somehow! When we work with ORM after generating the table, undoubtedly encounter problems related to this ID field, and that is the issue you are facing with too.</p>
  <p id="dqjD">Before turning into a solution, let&#x27;s create another table <code>courses</code> :</p>
  <figure id="oqoM" class="m_column">
    <img src="https://img3.teletype.in/files/2a/18/2a18dc59-f895-46f0-845e-c1763f0a39ba.png" width="1010" />
    <figcaption>&#x60;course&#x60; table generation</figcaption>
  </figure>
  <p id="PJYF">We should point out that in this case we set the ID field type to <code>serial</code> . To learn more about serial:</p>
  <p id="88gN"><a href="https://www.tutorialsteacher.com/postgresql/serial-type" target="_blank">https://www.tutorialsteacher.com/postgresql/serial-type</a></p>
  <p id="pPSQ">When we save the table, the type becomes <code>int4</code> automatically, but:</p>
  <figure id="p1zF" class="m_column">
    <img src="https://img1.teletype.in/files/8f/21/8f21f96e-4cfa-43b0-a60c-8cc111637cbb.png" width="1065" />
    <figcaption>&#x60;course&#x60; table, sequence generation</figcaption>
  </figure>
  <p id="2XOE">As we can see, postgresql auto generates a sequence for us. Now, again we run <code>inspectdb</code> and see the result:</p>
  <figure id="oW6q" class="m_column">
    <img src="https://img1.teletype.in/files/05/1e/051e7fd3-47c1-4f45-acc9-05229136e133.png" width="656" />
    <figcaption>Generated &#x60;course&#x60; table</figcaption>
  </figure>
  <p id="nvo4">ID field is not present! So we solved the problem. It is very essential to pay attention on types of fields while creating a database tables using both DBMS tools and ORMs. Thanks for your attention.</p>

]]></content:encoded></item></channel></rss>