Posts

Python Libraries: Sending an Argument via the Command Line

avatar of @slobberchops
25
@slobberchops
·
0 views
·
3 min read

The Random Voter script is still being developed and now sends random votes to over 200 different accounts. If you want to be considered as an addition and you don't write a big pile of dogshit then comment below. I am always looking for good deserving authors.

My surplus accounts, @chops.support, @dismayedworld, @bingbabe, and @wanker all send 100% votes, and if my main account is flush with voting power, adds a further 5% vote to the mix.

My memory occasionally drifts back to March 2018, STEEM, and the random vote of @hr1 who I had the pleasure of shaking hands during STEEMFest 3 in Krakow. @hr1 voted people, mostly new people at random, had a massive pile of STEEM and was a kind of saviour to me.

...'@hr1 does exist... accompanied with a smiling @detlev, I thought him a myth until I witnessed this sight'...

How cool would it be to be 'another @hr1' and to dish out votes to other struggling HIVE authors who consistently deliver?

I never for one minute thought I would ever be in the position to do so, and @hr1’s algorithm was for new posts, whereas mine is whenever I run the script.

*...'@the hr1 account now sits alone with little HP. How things change'...

What I wanted was the ability to change the @slobberchops vote weight without changing the core script code.

I am familiar with other scripting languages and knew this would be in the form of an argument on the command line.

The way that I learn new functions, methods, or routines is to get examples from the internet. Figure out how they work, and then write them into your code.

Once you have done it, then you have a working example that can then be re-used.

I tried here first and the code kept giving me an error.

Source

It appears some variants of Python don't require brackets around print statements, whereas Anaconda does. You learn something every day.

*print ('Number of arguments:', len(sys.argv), 'arguments.')

Adding the brackets so it would work I was initially confused about the type of property sys.argv was. The len method generally tells us the length and a little homework tells me it’s an array.

The output from this script now made sense and so I was looking to add a second argument which would be a variable integer containing the vote weight.

Another search brought me the answer, although many other more over-complex solutions were presented to me.

Source

I like to keep it simple otherwise when looking back at old code I will forget what I did.

It made sense; this line of code if true meant that no parameter was sent on the command line and that the @slobberchops account should vote at a 5% weight.

*if len(sys.argv) == 1:

All I needed to do was incorporate this into my script and I could determine the vote weight, and add a little more if I had some to spare.

This line of code calls the function, sends the account name from a list, and expects two return values.

Unlike some other scripting languages, Python allows the programmer to return more than a single value; most useful!

The function has now been modified to check, in hardcoding terms for the @slobberchops account, intercept anything other than the first argument and utilize it (if it exists).

To make this a little clearer if the @slobberchops is above a voting power of 87% then it will vote at 5% unless an alternate numeric parameter was sent via the command line

This would be equal to the property found in the sys.argv[1]. Like many other languages, the first element in Python arrays starts at 0.

*...'typically I would use an optional argument of '10' to send 10% weight votes instead of 5% during times when I have excess VP'...

The other voting accounts will cease to vote below 75% VP and will always vote at a weight of 100%.

I hope this is a little clearer by example. I learned to code without courses, books, exams. Over the years I have found you either have the head for it or you don't.

If this seems complex then you may be the type who would never understand it. I have tried umpteen times to teach other 'techies' the basics of VBScript and failed.