<?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>Forem: Mo'men Ahmed</title>
    <description>The latest articles on Forem by Mo'men Ahmed (@mo2men_1).</description>
    <link>https://forem.com/mo2men_1</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%2F609656%2F7a0d08f1-d365-4dbc-9d36-72c7b7dba7d6.jpeg</url>
      <title>Forem: Mo'men Ahmed</title>
      <link>https://forem.com/mo2men_1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mo2men_1"/>
    <language>en</language>
    <item>
      <title>Migrate from redis server to a new one</title>
      <dc:creator>Mo'men Ahmed</dc:creator>
      <pubDate>Sun, 19 Sep 2021 15:34:17 +0000</pubDate>
      <link>https://forem.com/mo2men_1/migrate-from-redis-server-to-a-new-one-1in1</link>
      <guid>https://forem.com/mo2men_1/migrate-from-redis-server-to-a-new-one-1in1</guid>
      <description>&lt;p&gt;If you have some data stored on a redis server, and for any reason, you need to migrate the data to another redis server, then you may use MIGRATE command in a redis-cli terminal.&lt;/p&gt;

&lt;h1&gt;
  
  
  The MIGRATE command
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;MIGRATE destination-host destination-port "" destination-db-index timeout [COPY] [REPLACE] [AUTH password] [AUTH2 username password] [KEYS key [key ...]]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Options:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;COPY -- Do not remove the key from the local(source) instance.&lt;/li&gt;
&lt;li&gt;REPLACE -- Replace existing key on the remote(destination) instance.&lt;/li&gt;
&lt;li&gt;KEYS -- If the key argument is an empty string, the command will instead migrate all the keys that follow the KEYS option (see the above section for more info).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more info check MIGRATE redis docs &lt;a href="https://redis.io/commands/migrate"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;Suppose you want to migrate data from source-host to destination-host, you should open redis-cli by writing &lt;strong&gt;redis-cli&lt;/strong&gt; in a terminal in the &lt;strong&gt;source&lt;/strong&gt; redis server machine, then run the following command replacing the host and port with the right values:&lt;br&gt;
&lt;code&gt;MIGRATE destination-host destination-port "" 0 5000 COPY KEYS key1 key2&lt;/code&gt;&lt;/p&gt;

</description>
      <category>redis</category>
    </item>
    <item>
      <title>Detecting outliers in a time series using tsmoothie in Python</title>
      <dc:creator>Mo'men Ahmed</dc:creator>
      <pubDate>Tue, 25 May 2021 10:28:11 +0000</pubDate>
      <link>https://forem.com/mo2men_1/detecting-outliers-in-a-time-series-using-tsmoothie-in-python-436b</link>
      <guid>https://forem.com/mo2men_1/detecting-outliers-in-a-time-series-using-tsmoothie-in-python-436b</guid>
      <description>&lt;h1&gt;
  
  
  What are outliers?
&lt;/h1&gt;

&lt;p&gt;Outliers are data points that are far from most of the other data points.&lt;/p&gt;

&lt;p&gt;In this example of time series, all the points outside the blue band can be considered as outliers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FZQ2M0iw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1og51id3bf6io500ozcd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FZQ2M0iw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1og51id3bf6io500ozcd.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How to get a list of outliers in a time series?
&lt;/h1&gt;

&lt;p&gt;Here we will use a library called &lt;a href="https://pypi.org/project/tsmoothie/"&gt;tsmoothie&lt;/a&gt;.&lt;br&gt;
It is a python library for time-series smoothing and outlier detection in a vectorized way.&lt;/p&gt;

&lt;p&gt;On the time series in the figure: we can see that we have 4 outliers, we can get them by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import numpy as np
from tsmoothie.utils_func import sim_randomwalk
from tsmoothie.smoother import LowessSmoother

data = df['value'].values.reshape(1, -1)

# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.1, iterations=1)
smoother.smooth(data)

# generate intervals
low, up = smoother.get_intervals('prediction_interval')

points = smoother.data[0]
up_points = up[0]
low_points = low[0]

for i in range(len(points)-1, 0, -1):
    current_point = points[i]
    current_up = up_points[i]
    current_low = low_points[i]
    if current_point &amp;gt; current_up or current_point &amp;lt; current_low:
        print(f'found an outlier value: {current_point}')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Considering that the data frame df has a column named 'value'.&lt;br&gt;
So, we get this output after running the code:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ToYX8eFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bspaxq5jnn2azatuja69.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ToYX8eFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bspaxq5jnn2azatuja69.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>One way to implement a time-series using Redis</title>
      <dc:creator>Mo'men Ahmed</dc:creator>
      <pubDate>Sun, 23 May 2021 16:02:45 +0000</pubDate>
      <link>https://forem.com/mo2men_1/one-way-to-implement-a-time-series-using-redis-18ja</link>
      <guid>https://forem.com/mo2men_1/one-way-to-implement-a-time-series-using-redis-18ja</guid>
      <description>&lt;h1&gt;
  
  
  What is Redis?
&lt;/h1&gt;

&lt;p&gt;Redis stands for 'REmote DIctionary Server', so the name itself implies a dictionary (key-value pairs).&lt;/p&gt;

&lt;h1&gt;
  
  
  Why use Redis?
&lt;/h1&gt;

&lt;p&gt;Redis is used as a database cache to save data in a fast way in the form of key-value pairs. Also, wide use for it is for sending messages between different systems through a shared Redis database.&lt;/p&gt;

&lt;h1&gt;
  
  
  What types of data can be stored in Redis?
&lt;/h1&gt;

&lt;p&gt;Redis provides data structures such as strings, hashes, lists, sets, and sorted sets.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to use Redis?
&lt;/h1&gt;

&lt;p&gt;First, you need to install it from their &lt;a href="https://redis.io/download" rel="noopener noreferrer"&gt;website&lt;/a&gt;. Then you can choose to work in redis-cli or any language client, here we use python redis client. You can install redis for python by &lt;code&gt;pip install redis&lt;/code&gt;. This python library has all redis-cli commands as functions, &lt;a href="https://gist.github.com/LeCoupa/1596b8f359ad8812c7271b5322c30946#file-redis_cheatsheet-bash" rel="noopener noreferrer"&gt;This&lt;/a&gt; is a cheatsheet for Redis commands.&lt;/p&gt;

&lt;h1&gt;
  
  
  A great VS Code extension for Redis
&lt;/h1&gt;

&lt;p&gt;I use this extension in VS Code, it can view all the contents in Redis in a nice and easy way.&lt;/p&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%2F870fcyglhat36v3n0hej.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%2F870fcyglhat36v3n0hej.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  An Example on Redis string data type
&lt;/h1&gt;

&lt;p&gt;First, we make a Redis instance by &lt;code&gt;r = redis.Redis(redisServer, 6379)&lt;/code&gt;&lt;br&gt;
then we set a value to a key and get it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;r.set('name', 'Ali')
r.get('name')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so, now we have a key called 'name' and has a value called 'Ali'.&lt;/p&gt;

&lt;h1&gt;
  
  
  An Example on Redis hash data type
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# r.hset('price', 'time', 'value')
r.hset('price', '12:20:00', '250.0')
r.hset('price', '12:20:15', '250.7')
r.hset('price', '12:20:30', '252.0')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the value of any time by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;r.hget('price', '12:20:15')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so, it will return the value '250.7'.&lt;/p&gt;

&lt;p&gt;You can also get all values and keys by:&lt;br&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%2Fejrjpnklbpi0cagp6o9o.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%2Fejrjpnklbpi0cagp6o9o.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Redis Time-series using Hash
&lt;/h1&gt;

&lt;p&gt;The idea about making a time-series in Redis is to have a key for each column you have, and use a hash data type for each one, each hash has a field and a value in the format {field: value}. We put the timestamp as the field. &lt;/p&gt;

&lt;p&gt;An example will be like this:&lt;br&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%2Fcnh2eb8vey5hqniil7qp.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%2Fcnh2eb8vey5hqniil7qp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>redis</category>
    </item>
    <item>
      <title>How std::cin works in C++?</title>
      <dc:creator>Mo'men Ahmed</dc:creator>
      <pubDate>Fri, 09 Apr 2021 20:06:34 +0000</pubDate>
      <link>https://forem.com/mo2men_1/how-std-cin-works-in-c-34pn</link>
      <guid>https://forem.com/mo2men_1/how-std-cin-works-in-c-34pn</guid>
      <description>&lt;p&gt;&lt;strong&gt;std::cin&lt;/strong&gt; is used to get user input from external source.&lt;/p&gt;

&lt;p&gt;When the user enters input, that data is put in a buffer inside of std::cin. The buffer is used to hold user input while it’s waiting to be extracted to variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;std::cin extraction steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If there is data already in the input buffer, that data is used for extraction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the input buffer contains no data, the user is asked to input data for extraction. When the user hits enter, a ‘\n’ character will be placed in the input buffer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;operator&amp;gt;&amp;gt; extracts as much data from the input buffer as it can into the variable (ignoring any leading whitespace characters, such as spaces, tabs, or ‘\n’).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any data that can not be extracted is left in the input buffer for the next extraction.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Extraction succeeds if at least one character is extracted from the input buffer. Any unextracted input is left in the input buffer for future extractions. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int x {0};
std::cin &amp;gt;&amp;gt; x;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user enters: "22m", the variable x is filled with "22" leaving the "m" alone in the buffer.&lt;br&gt;
Extraction fails if the input data does not match the type of the variable being extracted. &lt;br&gt;
For example, if the user enters "m" extraction would fail because "m" can not be extracted to an integer variable. But then what would be the value of x?&lt;br&gt;
&lt;strong&gt;Before c++ 11:a failed extraction would not modify the variable being extracted to.&lt;br&gt;
However, as of C++11, a failed extraction due to invalid input will cause the variable to be zero-initialized.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Look at this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// uint16_t is a typedef for unsigned short
std::uint16_t x {0}; // max is UINT16_MAX (65535)
std::cin &amp;gt;&amp;gt; x;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when a user enters 65537, std::cin goes immediately into failure mode, but also assigns the closest in-range value to the variable. Consequently, x is left with the assigned value of 65535. Additional inputs are skipped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before C++11, a failed extraction would not modify the variable being extracted. &lt;br&gt;
However, as of C++11, an out-of-range failed extraction will cause the variable to be set to the closest in-range value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also note that when std::cin goes to failure mode, it will skip any other extractions, so you should put it back to the normal mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (std::cin.fail())
{
    std::cin.clear();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cpp</category>
    </item>
  </channel>
</rss>
