8 Aug 2019

positional-only-parameter in Python

In recent release of python 3.8, new feature  positional-only-parameter is introduced.
I am going to cover motivations behind positional-only-parameter.
There are already some functions which takes positional arguments only like range(), id(), len().

def add(x, y):
    return x+y


Motivations:
1. Programmer does not want to expose the arguments to caller.
  in add(x=3, y=4)  x and y directly exposed to caller.

2. Tomorrow if programmer want to refactor the code and changes parameters name then existing call to the function will break.
if add(x, y) is changed to add(a, b) then add(x=3, y=4) will break. As a result of this programmer has to maintain the parameter's name.

3. add(*args, **kwargs) is not clear to caller as *args can take number of arguments without specifying any meaning of those.

4 When subclass overrides method of base class and changes the name of parameters.
class Sup(object):
  def add_one(self, x)
    return x+1

class Sub(Sup):
  def add_one(self, y)
    return y+1

Sub().add_one(x=1)

which results in error:
TypeError: add_one() got an unexpected keyword argument 'x'

5. Maintaining the logical order of arguments.
def get_args(a, b, c):
   return (a, b, c)

get_args(a=1, c=3, b=2)
get_args(c=9, a=7, b=5)


We can clearly see order of arguments is not followed in calls. positional-only-parameter will allow to maintain the order of calls.


Hope you like this article, Thanks for reading.

29 Mar 2019

Feature request for python's requests library

Recently as apart of day job I had to count the number of API requests were made to keep check of throttling limits. As a standard for python codebase, requests library has been used.
I wrote some code which will maintain the API URLs and their respective counts.
That got thing done.

After a while I realized, there should be a way to intercept the request. Just like in there is middleware in Django for request. Then checked the documentation of requests and found event-hooks.

Going through the docs found out that hook is available only for response.  And they have intentions to add other hooks as well mentioned in TODO.
Found this opportunity to contribute one of the best libraries of python and without any delay I created github issue

Hopefully library maintainers will have a look and will add as a feature request officially.

8 Mar 2019

Learning Elixir with side project

Recently I have been attracted to functional programming , the idea of creating an application which is entirely composed of functions with no side-effects and working with immutable data structures.
Just bumped into Elixir, tried couple of days and yeah really liked it.

How to learn
Idea is to identify problem(s) which I care about and solve those, it could very tiny thing.
This way I need not to struggle with motivation to keep carry on.

Identifying problem

I maintain IMDB watchlist and look for torrent sites if that movie from my watchlist is available or not. It's a mundane thing and i do not want spend time just to check that.
So I choose this to automate that and start my learning path for Elixir.

Building project
I created issues on project and started work on those.
One part of my brain telling me  is it correct way to do ? Other part was saying just make it work.  
More so coming from python background and writing code in it for more than six years, first thing comes to mind while writing code is to make pythonic since I know how to make it work.
But with new language had to suppress the urge to write idiomatic code from Day 1,  I can improve it while doing refactor.
It took a couple of weeks doing small improvements and iterating over. Whenever I was stuck on something took help from StackOverflow.

Learnings

1. Pattern matching is super cool. It really helps to write clean code.
2. Pipe operator. Another programming construct which helps to write clean code.
3. Mix, a build tool. Need to explore it more.
4. Tried out third-party libraries like HTTPoison, Floki and others.
5. Learning ideas from Joe Armstrong thesis

Feedback
Once the stable version of project is build, I decided to take feedback from elixir community.I approached  elixir-langelixir-forum, gitter channels  lisbon-elixir, awesome-elixir,
local-elixir-meetup-group
Really appreciate their time and feedback on project.

Thanks for reading.