<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ngalim Siregar</title>
    <description>The latest articles on DEV Community by Ngalim Siregar (@nsiregar).</description>
    <link>https://dev.to/nsiregar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F75019%2F78940755-981a-45bc-8a37-d4e4008a4b13.jpeg</url>
      <title>DEV Community: Ngalim Siregar</title>
      <link>https://dev.to/nsiregar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nsiregar"/>
    <language>en</language>
    <item>
      <title>Profiling Your Python Code</title>
      <dc:creator>Ngalim Siregar</dc:creator>
      <pubDate>Wed, 05 Jan 2022 04:34:59 +0000</pubDate>
      <link>https://dev.to/nsiregar/profiling-your-python-code-4865</link>
      <guid>https://dev.to/nsiregar/profiling-your-python-code-4865</guid>
      <description>&lt;p&gt;Sometimes you want to profile just a single function in your Python program. To profile single function on Python program, we can use &lt;code&gt;cProfile&lt;/code&gt; to profile single function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create_array():
  arr=[]
  for i in range(0,400000):
    arr.append(i)

def print_statement():
  print('Array created successfully')


def main():
  create_array()
  print_statement()

if __name__ == '__main__':
    import cProfile, pstats
    profiler = cProfile.Profile()
    profiler.enable()
    main()
    profiler.disable()
    stats = pstats.Stats(profiler).sort_stats('ncalls')
    stats.print_stats()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will profile method &lt;code&gt;create_array&lt;/code&gt; and &lt;code&gt;print_statement&lt;/code&gt;. By using &lt;code&gt;pprofiler&lt;/code&gt; &lt;a href="https://pypi.org/project/pprofiler/" rel="noopener noreferrer"&gt;library&lt;/a&gt; we can simplify those step to single decorator call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pprofiler import pprof

...

@pprof()
def main():
  create_array()
  print_statement()

if __name__ == '__main__':
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default &lt;code&gt;pprofiler&lt;/code&gt; will sort by &lt;code&gt;ncalls&lt;/code&gt;, we can use other sorting key by define it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pprofiler import pprof

...

@pprof(sort_by='time')
def main():
  create_array()
  print_statement()

if __name__ == '__main__':
    main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8srjpsimrt13k9gphl83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8srjpsimrt13k9gphl83.png" alt="pprofiler in action"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>SQLAlchemy Events</title>
      <dc:creator>Ngalim Siregar</dc:creator>
      <pubDate>Tue, 17 Aug 2021 08:07:17 +0000</pubDate>
      <link>https://dev.to/nsiregar/sqlalchemy-events-3hm0</link>
      <guid>https://dev.to/nsiregar/sqlalchemy-events-3hm0</guid>
      <description>&lt;p&gt;During the normal operation in SQLAlchemy ORM objects may be created, updated, and destroyed. SQLAlchemy provides hooks into this object life cycle so that you can control your application and its data.&lt;/p&gt;

&lt;p&gt;These callbacks allow you to trigger logic before or after an alteration of an object's state.&lt;/p&gt;

&lt;p&gt;Here is a sample of Flask-SQLAlchemy Model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

    def __repr__(self):
        return '&amp;lt;User %r&amp;gt;' % self.username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to add callback to lowercase username before save, we can use &lt;code&gt;sqlalchemy.event.listens_for&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy import event


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

    def __repr__(self):
        return '&amp;lt;User %r&amp;gt;' % self.username


@event.listens_for(User, "before_insert")
def lowercase(mapper, connection, target):
    target.username = target.username.lower()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;sqlalchemy-events&lt;/code&gt; &lt;a href="https://pypi.org/project/sqlalchemy-events/"&gt;library&lt;/a&gt;, we can simplify those event listen to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy_events import listen_events, on


@listen_events
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

    @on("before_insert")
    def lowercase_username(mapper, conn, self):
        self.username = self.username.lower()

    def __repr__(self):
        return '&amp;lt;User %r&amp;gt;' % self.username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>sqlalchemy</category>
      <category>flask</category>
    </item>
  </channel>
</rss>
