[Python Tips] Knowing what exists

in #programming6 years ago

Being a good programmer isn't just about knowing how to do everything or even being very good at it. One of the most valuable skills of a good programmer is just knowing something exists. Sometimes it is some obscure not often used feature of the language that when the opportunity comes, it can save a lot of time and frustration.

Steem-Python example

A good example of this is when programming with Steem-Python. One frustration is many data points are returned as strings and not as proper floats.

When you get the sbd_balance of an account, it will be returned as 100 SBD and not 100 which can be directly used in calculations. This is easily fixed with strip() or rstrip(), but if you knew about Amount the job is far easier.

from steem.amount import Amount

my_sbd_balance = '10000000 SBD'
total_sbd_value_usd =  Amount(my_vests).amount * CURRENT_SBD_PRICE

This is a lot easier than always remembering to .rstrip(' SBD'), while they are both fairly easy, amount works with SBD, STEEM, and VESTS. It also has some logic to handle precision and other functionality.

If you look up the Amount class, you will find it just does a split(" ") and unpacks the output into two variables. It makes for cleaner and more understandable code. If you don't know this class exists, you might use split(), you might use rstrip, or you might use a totally different method. Someone reading your code or helping you out might do something totally different.

Python reverse()

Another example is reverse(), this is a newer python feature that allows you to reverse an iterable object. This is extremely helpful when needed, but many people still use slice syntax (which doesn't solve all the time this comes up). Not only is reverse() cleaner and more understandable, it can remove a lot of excess code.

How to 10X your development

Just knowing certain features or libraries exist, even if you don't fully understand how to use them is an extremely powerful skill set. It can speed up your coding 10X and get you out of coding ruts.

New releases

Keeping up to date with new releases (for example Python 3.6 offers a lot of new powerful functionality) can help expose you to many new features that were developed to solve pain points.

My Python Tips Series

Sort:  

Sometimes the measure of a good programmer is knowing what exists in the language, but also knows what exists in the ecosystem around the language and deciding what to use. Because sometimes community created functionality (libraries, API's and methods) can end up being better than the native ones.

Take Javascript for example, plenty of native methods for manipulating collections (arrays, maps and so on) but then you have Lodash (a Javascript toolbelt) with its own implementations of native methods which in many cases outperform the native version, such as iterating collections and search through an array of objects by object key.

The biggest improvement any developer can make is learning to read the documentation, read it before you do anything. Far too many times I see people get into trouble and the answers are right there in the documentation, but they don't read it.

Where were you when I was tearing my hair out with regex and shit a month ago!? Although, plodding my way through my @runburgundy project was an exercise in getting to know the documentation for steem-python

Probably the same place I am now ;)

good post, I upvote and resteem my friends yes.

I am really bad in programming.

Too_Good...
This is unique and something different.@themarkymark

Happy Bdayyyyyy !!!!!! Wish you another wonderful year on steemit!