<?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 Surya Teja on Medium]]></title>
        <description><![CDATA[Stories by Surya Teja on Medium]]></description>
        <link>https://medium.com/@suryasr007?source=rss-297da5375947------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bmpjde9ixiAhg36MS6xLCg.jpeg</url>
            <title>Stories by Surya Teja on Medium</title>
            <link>https://medium.com/@suryasr007?source=rss-297da5375947------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:34:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryasr007/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[Python Dictionaries and their operations]]></title>
            <link>https://medium.com/writearobot/python-dictionaries-and-their-operations-36827306cc69?source=rss-297da5375947------2</link>
            <guid isPermaLink="false">https://medium.com/p/36827306cc69</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[dictionary]]></category>
            <dc:creator><![CDATA[Surya Teja]]></dc:creator>
            <pubDate>Mon, 07 May 2018 14:50:55 GMT</pubDate>
            <atom:updated>2018-05-07T14:50:55.069Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fiK56UyaPUffkjMhrq6-5g.jpeg" /></figure><p>The whole point of programming concludes to one inevitable out-coming i.e., we write instructions to manipulate/control DATA.</p><p>Python provides many data structures to store data, Dictionary is one among them which store the data as a key-value pair. According to official documentation, there are many definitions but, I find the below definition easy to understand.</p><blockquote><em>Dictionaries are a type of data structures, which are indexed by keys. It can also be defined as an unordered set of key: value pairs. Immutable(not dynamic) types like strings and numbers can always be keys.</em></blockquote><h3>Basic operations on Dictionaries</h3><p>As mentioned in thefirst point, python provides no.of ways to manipulate/control data. Below mentioned are few basic methods used on dictionaries.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/961a1b1ccfd2ff87c59b9c46b89fae03/href">https://medium.com/media/961a1b1ccfd2ff87c59b9c46b89fae03/href</a></iframe><h3>Loops on Dictionaries</h3><p>On dictionaries, python provides three basic methods to loop through the items of the dictionary.</p><ul><li>dict.items()</li><li>dict.keys()</li><li>dict.values()</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8324163d4f41579eb34e29f8af6775de/href">https://medium.com/media/8324163d4f41579eb34e29f8af6775de/href</a></iframe><h3>Other methods on Dictionaries</h3><p>Python also provides methods which can be really helpful in reducing the size of code by using reusable modules.</p><p><strong>Comparing dictionaries</strong></p><p>The compare method <em>cmp()</em> is used to compare length of two dictionaries. The method returns 0 if both dictionaries are equal, 1 if dic1 &gt; dict2 and -1 if dict1 &lt; dict2.</p><pre>&gt;&gt;&gt; Boys = {&#39;sunny&#39;: 26,&#39;Chary&#39;:18,&#39;Robin&#39;:25}<br>&gt;&gt;&gt; Girls = {&#39;pari&#39;:22} <br>&gt;&gt;&gt; print(cmp(Girls, Boys))<br>-1</pre><p><strong>get(key[, default]) Method</strong></p><p>Return the value for key if the key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.</p><p>get() is very useful function as we don’t need to loop through the dictionary items to check whether a certain key is present in the dictionary. For nested dictionaries, it reduces a lot of pain to loop and checks as well as it makes the code more readable.</p><pre># dict.get( key, default )<br>&gt;&gt;&gt; Boys.get(‘sunny’)<br>26<br>&gt;&gt;&gt; Boys.get(‘surya’, ‘Not Found’)<br>Not Found<br>&gt;&gt;&gt; Boys.get(‘surya’)<br>None</pre><p><strong>Merging two dictionaries to one dictionary</strong></p><p>There are many methods to merge two dictionaries to a single dictionary. Using the multiple arguments ‘**’ operator we can achieve this.</p><pre>&gt;&gt;&gt;<strong> </strong>x = {‘a’: <strong>1</strong>, ‘b’: <strong>2</strong>}<br>&gt;&gt;&gt;<strong> </strong>y = {‘b’: <strong>3</strong>, ‘c’: <strong>4</strong>}<br>&gt;&gt;&gt;<strong> </strong>z = {**x, **y}<br>{‘a’: 1, ‘b’: 3, ‘c’: 4}</pre><p><em>Note</em>: This method is applicable only in python 3.5. When there are conflicting keys, the interpreter chooses the latest value.</p><p><strong>json.dumps()</strong></p><p>The standard string representation for dictionaries is hard to read. Using the json.dumps we can make the output as JSON with more readability.</p><pre># The standard string representation for dictionary:<br>&gt;&gt;&gt;<strong> </strong>obj = {&#39;a&#39;: <strong>‘asdasd’</strong>, &#39;b&#39;:’rgggsd’, &#39;c&#39;:’asdsd’}<br>&gt;&gt;&gt;<strong> </strong>obj<br>{&#39;a&#39;: <strong>‘asdasd’</strong>, &#39;b&#39;:’rgggsd’, &#39;c&#39;:’asdsd’}</pre><pre>&gt;&gt;&gt;<strong> import</strong> <strong>json</strong><br>&gt;&gt;&gt;<strong> print</strong>(json.dumps(obj, indent=<strong>4</strong>))<br>{<br>    &quot;c&quot;: “asdsd”,<br>    &quot;b&quot;: 42,<br>    “a”: <strong>“asdasd”</strong><br>}</pre><p>json.dumps will accept multiple parameters. As shown above, we can give indent level and we can supply other parameters like sort_keys.</p><pre>&gt;&gt;&gt;<strong> print</strong>(json.dumps(obj, indent=<strong>4</strong>, sort_keys=True))<br>{<br>    ”a”: ”<strong>asdasd</strong>”,<br>    ”b”:”rgggsd”,<br>    ”c”:”asdsd”<br>}</pre><p><strong>json.loads()</strong></p><p>Loads performs the vice-versa of the dumps. It takes the JSON input and converts it back to the dictionary.</p><pre>Jobj = {<br>    ”a”: ”<strong>asdasd</strong>”,<br>    ”b”:”rgggsd”,<br>    ”c”:”asdsd”<br>}</pre><pre>&gt;&gt;&gt;<strong> import</strong> <strong>json</strong><br>&gt;&gt;&gt;<strong> print</strong>(json.loads(jobj))<br>{‘a’: <strong>‘asdasd’</strong>, ‘b’:’rgggsd’, ‘c’:’asdsd’}</pre><p><strong>Dictionary as switch/case statements</strong></p><p>Python doesn’t have switch/case statements as default. But as python has first-class functions, they can be used to emulate switch/case statements. Following is an example of implementation of switch/case statement using the dictionary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/050fcf652d2c88836969cdc5eed7ac9d/href">https://medium.com/media/050fcf652d2c88836969cdc5eed7ac9d/href</a></iframe><h3>Conclusion:</h3><p>Python dictionaries very powerful when compared to other data types like lists, tuples and sets. In other languages, these are also called as “associative memories” or “associative arrays”. There are lot more properties and methods for dictionaries which are used as per requirement.</p><p><strong>Sources:</strong></p><ul><li><a href="https://docs.python.org/3/tutorial/datastructures.html">5. Data Structures</a></li><li><a href="https://dbader.org/">Python Training by Dan Bader - dbader.org</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36827306cc69" width="1" height="1" alt=""><hr><p><a href="https://medium.com/writearobot/python-dictionaries-and-their-operations-36827306cc69">Python Dictionaries and their operations</a> was originally published in <a href="https://medium.com/writearobot">writearobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi Threading in Python using ‘Activity’ Application]]></title>
            <link>https://medium.com/writearobot/multi-threading-in-python-using-activity-application-a8df7b360505?source=rss-297da5375947------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8df7b360505</guid>
            <category><![CDATA[activity]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Surya Teja]]></dc:creator>
            <pubDate>Mon, 23 Apr 2018 11:03:01 GMT</pubDate>
            <atom:updated>2018-04-23T11:03:01.175Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QJ5vo5byVkqFb39A." /><figcaption>Photo by <a href="https://unsplash.com/@rosiekerr?utm_source=medium&amp;utm_medium=referral">Rosie Kerr</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Threads in computer world are described as simple executors (which execute the given instructions). These are light weight and used to perform tasks asynchronously.</p><p>Eg: Different tabs in browser act as different threads</p><p>In general, At OS level, threads have their own resources but at abstract level they uses same code, data and files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*1C4e7YzuhSZQcwUf." /><figcaption>Source: <a href="https://www.studytonight.com/">https://www.studytonight.com</a></figcaption></figure><p><strong>Multi threading in activity application:</strong></p><p><a href="https://github.com/sayanarijit/activity">Activity</a> is an application developed by my good friend <a href="https://github.com/sayanarijit">Arijit</a>. It performs bulk server command execution and validations over ssh asynchronously.</p><p>It was developed in python 3.6. The core concept of this module is that, it uses multi threading to perform command execution in parallel on all the given servers because of which it consumes less time and performs tasks in an organised and efficient way.</p><p>To understand multi threading, Lets dive into some code which is part of activity app, which performs ping check on all the hosts asynchronously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05bc2d20a6cb8d2095a9ed93af9d2b72/href">https://medium.com/media/05bc2d20a6cb8d2095a9ed93af9d2b72/href</a></iframe><p>The above defined function is a callable function, which performs the required activity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24adc0ccfbe75fc5d5480f8504572632/href">https://medium.com/media/24adc0ccfbe75fc5d5480f8504572632/href</a></iframe><p>To understand this we should know a brief background of how parallel execution works in python.</p><p><strong>concurrent.futures — Launching parallel tasks</strong></p><p>The <a href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures">concurrent.futures</a> module provides a high-level interface for asynchronously executing callables which is a In-built module.</p><p>In our program, _ping_single(args): is a executing callable function</p><p>The asynchronous execution can be performed using threads and processes. Sub function for threads in <a href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures">concurrent.futures</a> is <strong>ThreadPoolExecutor</strong> and processes is <strong>ProcessPoolExecutor</strong>.</p><p>To import required module</p><p>from concurrent.futures import ThreadPoolExecutor, as_completed</p><p>Once imported,</p><p>with ThreadPoolExecutor(max_workers=PARALLEL_LIMIT) as pool:</p><p>The above line is used to initialize the threads. It is initialized with `with`, so that when execution of a threads complete, the resources of the thread are freed and then shutdown the Executor.</p><pre>futures = [pool.submit(_ping_single, [h ,self.user, self.reportid]) for h in self.hosts]</pre><p>The execution starts once the callable function is submitted along with arguments as shown above. In the above line hosts, as well as required arguments are submitted along with callable function.</p><p>We can use pool.map to submit the tasks to threads, The difference would be seen in while capturing the output.</p><pre>for f in as_completed(futures):<br>    output.append(f.result())</pre><p>Once the threads execution is started, we can capture the output using as_completed or wait function.</p><p>The as_completed() function takes an iterable of Future objects and starts yielding values as soon as the futures (threads) start resolving. The main difference between the aforementioned map method with as_completed is that map returns the results in the order in which we pass the iterables. That is the first result from the map method is the result for the first item. On the other hand, the first result from the as_completed function is from whichever future completed first.</p><p>The wait() function would return a named tuple which contains two set — one set contains the futures which completed (either got result or exception) and the other set containing the ones which didn’t complete.</p><blockquote>Note:</blockquote><blockquote>If the threads are not configured properly, There will be performance issues like deadlock, memory leaks.</blockquote><p><strong>Conclusion:</strong></p><p>Multi threading if configured properly, is an efficient and organized way to reduce the time taken for execution as well improve performance of the program.</p><p>For more explanation on multithreading please refer the below article: <a href="https://medium.com/@bfortuner/python-multithreading-vs-multiprocessing-73072ce5600b">https://medium.com/@bfortuner/python-multithreading-vs-multiprocessing-73072ce5600b</a></p><p>Source:</p><ul><li><a href="https://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures">concurrent.futures - Launching parallel tasks - Python 3.7.4rc1 documentation</a></li><li><a href="http://masnun.com/2016/03/29/python-a-quick-introduction-to-the-concurrent-futures-module.html">Python: A quick introduction to the concurrent.futures module</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8df7b360505" width="1" height="1" alt=""><hr><p><a href="https://medium.com/writearobot/multi-threading-in-python-using-activity-application-a8df7b360505">Multi Threading in Python using ‘Activity’ Application</a> was originally published in <a href="https://medium.com/writearobot">writearobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basic setup and working with raspberry pi’s RPI GPIO Module of python]]></title>
            <link>https://medium.com/writearobot/basic-setup-and-working-with-raspberry-pis-rpi-gpio-module-of-python-d6bb3ea0d9ea?source=rss-297da5375947------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6bb3ea0d9ea</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Surya Teja]]></dc:creator>
            <pubDate>Mon, 16 Apr 2018 14:50:35 GMT</pubDate>
            <atom:updated>2018-04-17T09:46:04.124Z</atom:updated>
            <content:encoded><![CDATA[<p>Our first post is about basic setup and working with raspberry pi’s RPI GPIO Module of python.</p><p><strong>Brief intro to Raspberry Pi:</strong></p><p>Raspberry Pi is a credit card size computer designed for education. The first model was launched in 2012 which was basically used for education</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yEkMTqLi4ZzCcPd6Hl7yaw.jpeg" /><figcaption>Source: <a href="https://www.raspberrypi.org/">https://www.raspberrypi.org</a></figcaption></figure><p>Features:</p><ul><li><em>Small size</em></li><li><em>Low-cost Device</em></li><li><em>Complete linux computer</em></li><li><em>Low power consumption</em></li></ul><p><strong>GPIO pins:</strong></p><p>There are 40 powerful General Purpose Input/Output(GPIO) pins which are part of core components of the Raspberry pi. There are two 5V pins and two 3.3v, ground pins and general purpose pins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BCO8L5pCB4LCak1y." /><figcaption>Source: <a href="https://www.raspberrypi.org/documentation/usage/gpio/images/gpio-numbers-pi2.png">https://www.raspberrypi.org/documentation/usage/gpio/images/gpio-numbers-pi2.png</a></figcaption></figure><p><strong>RPI GPIO Module:</strong></p><p>Python can be used write applications in Raspberry pi. It comes with a powerful package manager called PIP which can be used to install third party packages.</p><p>RPI GPIO Module is a third party package which can be used to configure and control the GPIO pins.</p><p>The basic setup is pretty easy. After setup we define whether a pin’s value should be high or low.</p><h4>Basic setup</h4><ul><li>Make sure that python&gt;2.7 is installed in the raspberry Pi.</li><li>Install the module using PIP</li></ul><pre>sudo pip install RPi.GPIO</pre><pre># If you face any problem in installing the package install python-dev and try again</pre><ul><li>Import the modules to script</li><li>Set the pin numbering schemes (explained below)</li></ul><pre>GPIO.setmode(GPIO.BCM)</pre><ul><li>Configure the required pins</li></ul><pre>GPIO.setup(18, GPIO.OUT)</pre><pre># PIN 18(BCM) is now used as output</pre><p><strong>Pin numbering schemes:</strong></p><p>There are two types of pin numbering schemes</p><p><em>GPIO.BOARD: </em>Board numbering scheme</p><pre>The <strong>GPIO.BOARD</strong> option specifies that you are referring to the pins by the number of the pin the the plug i.e the numbers printed on the board.</pre><p><em>GPIO.BCM:</em> Broadcom chip-specific pin numbers.</p><pre>The <strong>GPIO.BCM</strong> option means that you are referring to the pins by the “Broadcom SOC channel” number.</pre><p>Once the basic setup is completed, Based on the requirement we set the pins to high or low</p><pre># Import modules<br>import RPi.GPIO as GPIO<br>import time<br>Import atexit</pre><pre>#PIN setup<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setup(4, GPIO.OUTPUT)</pre><pre># Loop to send HIGH and LOW to the mentioned pin<br>While True:<br>    GPIO.output(4,GPIO.HIGH)<br>    time.sleep(1)<br>    GPIO.output(4,GPIO.LOW)<br>    time.sleep(1)</pre><pre>#Garabage Cleaner<br>def clean_up():<br>    GPIO.cleanup()</pre><pre>atexit.register(clean_up)</pre><p>The above program sends HIGH signal to 4th pin(BCM) for one second and LOW signal for next one second continuously until the program is exited. To verify the output of the process we can connect LED and test it.</p><p>The first 5 lines of the program are basic setup and the following loop continuously sends HIGH and LOW signal to the 4th pin until the program is existed.</p><p>GPIO.cleanup() function is used as garbage cleaner and is recommended to use in every program which uses GPIO pins of raspberry pi.</p><p>atexit.register() function is used to trigger a function before exiting.</p><p>Based on the requirement of application, we can the set the pin in different modes.</p><p>Eg: To generate a Analog Output</p><pre>pwm <strong>=</strong> GPIO.PWM(18, 1000)<br>pwm.start(50)</pre><p><strong>Execution:</strong></p><p>Once the program is completed, It can run by providing following command into the terminal</p><pre>sudo python &lt;program_name&gt;.py</pre><p>To exit the program</p><pre>press ctrl+c</pre><p>Sources:</p><p>Rpi.GPIO Module: <a href="https://pypi.python.org/pypi/RPi.GPIO">https://pypi.python.org/pypi/RPi.GPIO</a></p><p>Raspberry Pi Documentation: <a href="https://www.raspberrypi.org/documentation/usage/gpio/images/gpio-numbers-pi2.png">https://www.raspberrypi.org/documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6bb3ea0d9ea" width="1" height="1" alt=""><hr><p><a href="https://medium.com/writearobot/basic-setup-and-working-with-raspberry-pis-rpi-gpio-module-of-python-d6bb3ea0d9ea">Basic setup and working with raspberry pi’s RPI GPIO Module of python</a> was originally published in <a href="https://medium.com/writearobot">writearobot</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>