Python Libraries: Expanding the Functions

in STEMGeeks3 years ago

Apologies but this will be too heavy for most readers. For Python coders, it may be semi-legible.

I recently wrote a couple of functions to further support the RandomVoter script. One reason is to stop my head from falling into decay as I now haven't worked for around 9 months.

PythonLibs - Splash.jpg

They say IT still has great demand, but not for my niche. At my age I don’t know whether I can be arsed to re-skill and if Splinterlands Land takes off next year, then maybe I will never need to.

Learning Python has been quite fun, and it’s an easy language once you get your head around it. I struggle more with the BEEM library due to inadequate documentation.

Technical people are not re-known for making great docs, and I am a testament to that.

I am unsure whether to make a GitHub repository for all these bits of code. I don’t mind sharing but my confidence as a programmer has never been great.

In the commercial world, I have written many scripts that have been used but have always seen myself as a hack n' slash coder, and that more orthodox coders would sneer at my badly written efforts.

The main thing is that I can get things to work, and that is more important to me than structure and order.

RedLine.png

Function detecttag

I have never been a great fan of Actifit posts. I see some that a quite meaty in content and I do upvote these posts if I follow the authors and they are to a good standard.

What I don't want happening is for the RandomVoter script to upvote them as quite often I won't look or inspect their innards.

I needed a way to detect the tags used from the post object and then skip the vote if #actifit was present.

There used to be a method ‘get_tags_used_by_author’ which no longer works. @foxon mentioned in a discord session this had been discounted in HF24.

image.png

Thanks to @steevc who has used an alternative method I dissected his GitHub code and figured out an easier way.

“tags” is an attribute of the comment property but tough to find in Holger's documentation, or at least with examples.

strnovotetag holds the tag for pending posts that I don't want to curate.

Like many of my functions, it returns a Boolean value for the result code.

Next, I needed to add the Def into my main script with some If commands to skip voting depending on the results.

RedLine.png

Function isfollowed

My list of votees does contain some accounts that I follow. While I am happy that the four smaller accounts curate these authors, I did not want my large @slobberchops one included.

I generally curate my followers manually and wield larger votes than the default weight of 5% used with RandomVoter.

It would be great if I could detect the accounts on the list, check if @slobberchops is following them, and skip that vote.

This way all accounts benefit from the smaller votes and I can manually hand out bigger ones to accounts I follow.

image.png

This one was quite easy to lift from Holger's documentation and took less than 30 minutes to craft in my usual style.

strvotee and my account in strmainvotingaccount.I used @meesterboom as the followed account is the string

Like detecttag this function returns a Boolean value and can easily be adapted into the main script, or so I thought!

RedLine.png

After some tinkering to the main script, the release notes added and an updated version (2.0) yielded the result I was looking for worked.

Incorporating the functions into the main script took longer than writing both routines. Perhaps it is my bad structure, which I am fully aware of.

Vote.JPG

The voting code needed to be relocated into a separate function as I am not a fan of duplicating code. It looks a little more structured now and contains the call to the check posts that have already been voted.

The issue I had was to ensure both functions would be enumerated correctly and I needed to resort to using a Boolean variable named slobbervote which would be set to False if the isfollowed function returned False.

IncorporatedIntoMain.JPG

Maybe there is a more elegant solution to my solution but as I have said before, I am still learning and am no coding genius.

To test I needed accounts that have posted in the last 24 hours, one that I follow and one that is an Actifit post.

GetRandAccount.JPG

For this I can comment out the getrandaccount function that returns.., what do you know!.. a random account to vote on, and hard code in other accounts.

NoVoteAsI Follow.JPG

Thanks, @chekohler for being my guinea pig on this one. I can see it works and @chekohler will now get random votes from my smaller accounts but not the @slobberchops account as I tend to manually curate his work.

NoVoteAndIgnoresActifit.JPG

@trezzahn is another author who I follow and exclusively uses the Actifit tag. This account was perfect for testing my other function, or was it?

Like the previous example, the code exited when it discovered I follow @trezzahn. There is no requirement to check for the Actifit tag as I follow this author.

For the final test, I needed an author who was on my vote list, who I didn't follow, and who used ActiFit tags in their post.

ActiFit posts.JPG

I couldn't find one immediately so scanned ALL ActiFit posts. This is why I don't want to auto-curate these posts. Most of them are sparse..

I could say shit, but each to their own!

ActifitNOVOTE.JPG

Sorry @likeablespoon321 but I can't vote for your one line of text, but if you see this, respond, and don't give me any shit I may give you a comment vote. Love the name, but 321?

RedLine.png

  • Earn currency while you play brewing virtual beer with CryptoBrewMaster
  • Earn currency while you play and become a global Rock Star with Rising Star


CurieCurator.jpg

Sort:  

Nice work! I always like seeing code-related posts on hive.

<p dir="auto"><span><a href="/@steevc">@steevc makes a good point about the loops! <p dir="auto">I've become quite accustomed to the JavaScript <code>map(), <code>filter(), and <code>reduce() style of dealing with lists, so would probably struggle a bit going back to python. <p dir="auto">Posted using <a href="https://www.proofofbrain.io/@chasmic-cosm/re-slobberchops-l2kbq1hz" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Proof of Brain

Nice work! I always like seeing code-related posts on hive.

Thanks, though I fear for most it will be like reading Greek (for non-Greeks).

I've become quite accustomed to the JavaScript map(), filter(), and reduce() style of dealing with lists, so would probably struggle a bit going back to python.

I have a lot to learn, and with my background being VBS it helps little. The methods you describe are quite alien to me, but at least now I am aware of them.

Python has versions of those functions too. I've not done much with JavaScript, so can't really compare the languages. Fortunately I get to use Python at work.

!BEER

It's all good fun. Python and beem is a powerful combination, but it can be hard to work out what functions to use. I need to play with them some more. I could do some stuff around tenkminnows to make it more useful.

<p dir="auto">You could check the length of posts as part of deciding if they are worthy of a vote. I know some bots count words. <p dir="auto">A couple of Python things. If a function returns a list you can check if something is in there like this rather than looping: <pre><code>from beem.account import Account print('steevc' in Account("slobberchops").get_following()) <p dir="auto">You can do a lot in one line. It may be quicker than a loop if that matters. <p dir="auto">When checking a boolean you don't really need the == True, but it does no harm if you find it easier to read.

You could check the length of posts as part of deciding if they are worthy of a vote. I know some bots count words.

This was something I was going to incorporate into the next change. I have yet to find an example in Holger's docs.

You can do a lot in one line. It may be quicker than a loop if that matters.

I once wrote a VB6 Forms app when working for HBOS (Now Lloyds Banking Group), and had to hand over the source when I left. I feel sorry for the person who had to dissect what I had done. It worked but even I struggled to read it!

When checking a boolean you don't really need the == True, but it does no harm if you find it easier to read.

image.png

This I do know and the function calls rely on True using the just the If statement, but on other occasions its stated more obviously.

In the proper coding world, I guess it has to conform all one way or another?

There has to be a compromise between being concise and being readable, but doing things in one line can be more efficient. There's a lot you can do with lists without resorting to a loop. Can always add a comment to make it clear what was intended.

There's a lot you can do with lists without resorting to a loop.

I know, still learning. I come from a VBS background and it shows.

I like that Python gives you options. You can write in the same style as some other languages, but you gradually learn the more 'Pythonic' ways.

Sparse is the new shit!

Looks some fairly reasonable code up there. I have always been of the kind that writes small bits to get things done. One day I hope to get a bit pythony

Voting on those one-liner Actifit posts would be embarrassing! I noticed some use AFIT and my code skips over those and votes.. DOH! I think I need to tweak it again.

You can check for intersections of sets to see if a post has any of a number of tags. I use that in proofofbrian.

<pre><code>if c.is_main_post() and tracktags.intersection(post['tags']) and author not in notified:

I will have a mess with this, otherwise it's back to looping!

tracktags is a set in that case. Otherwise I'd need nested loops to check all the combinations. I will use loops in some cases. I know there's a thing called 'code golf' where people try to implement something in as few characters as possible. That will end up with unreadable code. I try to make mine maintainable by others.

I know there's a thing called 'code golf' where people try to implement something in as few characters as possible.

I remember this from the Perl days.. crazy!

I know sometimes people make real posts on Actifit but I cant do it. There is so much dross that when Isee one I flee for the hills!

Technical people are not re-known for making great docs,

Ain't that the truth! This looks pretty cool. I think I would need to see the whole code to really understand what is going on. I can't figure out if you have to run this manually or if it is all automated. I'd love to dig into this some, but I honestly don't even know what I would do with it.

While I would love to share the whole script, the writing style is quite haphazard and badly structured. I found this when trying to incorporate these functions in. Last night my head was on fire thinking on how to get all this working and I had to take a break.

The more I practice, the easier it becomes mostly due to re-usable code. If I can remember that I have done it before then its easy to adopt it.

I'd likely be lost. It has been so long since I have coded. I tried to follow along with what you posted, but many of the pieces didn't seem that intuitive to me. I guess once you learn the language it probably isn't too bad.

I have trouble following some of it. It is not second nature to me (yet) as other languages have become.

This is great work. Python is such a great go-to for so many things.

I also had enough problems with BEEM that I ultimately took the robot I had running offline. I'm glad you're having better luck than I did (I did not have the patience to dive as deep into the weeds with it as you are).

Awesome work. Everyone should learn python.

I also had enough problems with BEEM that I ultimately took the robot I had running offline.

For the first day or two I couldn't get anything to work, it was seriously frustrating. Then bits started to function and it went from there. I have a background in scripting which helps a lot.

No response from likeable spoon yet then?

Not a word, I am not surprised!

Quite cool. Instead of give the author one last post and check for actifit, perhaps you can give 3 strikes and struck out if all 3 were actifit. Just an idea.
Unfortunately, I found not all the function in the beem documented works as expected but sometimes you have to try the function before you figure out you decide it is not working.
Technically, I think you can get rid of the detecttag function by using list comprehension or the any() function. Have a look at this:
https://www.geeksforgeeks.org/python-test-if-string-contains-element-from-list/

I think some of Holger's function no longer work, but as he's now AWOL we will never know. I plan to add AFIT to the detecttag function and 'any' will come in useful.

Python is quite new to me, I'm still learning a lot of new things.

To me, sometimes, you just need to imagine there is a simpler way to do it and search for it.
Especially when you think need to loop through a list or dataframe, then likely the search will find something useful.

Anyone who puts enough comments in their code that I can skim through it and understand what it's supposed to do (without requiring coffee and other stimulants) ... is writing absolutely glorious code.

Not only do you have comments, you have Prints!!!! I can watch the output stream and actually know where/if it crashed!

IRL, One of my functions is to take 20-30 year old code (mainly "short" scripts) from whatever language it was to a current language. Most folks don't include comments in their code ... "since everyone understands what this perl function does". If I'm fluent in the code, it's usually simple ... if not, insert coffee here.

At least now, I can lean heavily on Google to assist me ... instead of having to open up the Tomes of Knowledge. (Code reference manuals suck)

Perl... it still makes me shudder. I never got to grips with it.., and it was around 2003 when I was looking at an enormous script written by someone else and thinking... christ!

I know where you are coming from. If I don't add the comments, I will forget how it works, never mind anyone else.

Perl, Pascal, C, VB .... They all make me shudder if they aren't documented. But perl. A hash of a hash of a hash ... c'mon!!!! Without comments!!! It's a nightmare.

image.png

I have to admit ... when I think of the term "Multidimensional Hash", this is the last thing that comes to my mind!

Hrmmmm...... What does come to mind ....

image.png
Source - Legal in Canada

Good to see you coding again Slobberchops, got to keep those brain cells moving!

onw

This is probably something you could get into, I know you are capable. I saw that VB6 app you wrote in 2014 for monitoring BitCoin.. though I didn't know what the hell it did! Looked good though.

That would be the VB.NET app that traded alt coin currency pairs on the Cryptsy.com Exchange via Cryptsy's Restful API.

I had fun writing that, then decided to convert it to C#.NET.

Then Cryptsy fell by the wayside amd I lost my whopping $40 investment.

That was shortly after I got bored of trying to mine cr*p coins with graphics cards lol.

I also tried mining Bitcoin with a Raspberry Pi, a 49 port USB 2.0 Hub and 9 USB Ant Miners, those were the days.

(before any one gets excited and thinks I'm rich, I'm not. Ant Miners where not the ideal hardware for raking it in mining Bitcoin in 2014)

After Cryptsy fell, I got bored with it all and returned to real life, then some guys took me for brunch and started talking about NFT's. That's when it all started again.

Thanks Slobberchops :)

onw / b8l

Posted Using LeoFinance Beta

I wish I had taken more notice in 2014 when you showed me all this stuff. Did you manage to mine any BTC with those piddly bits of hardware?

I mined one tenth of a Bitcoin, worth $30.00 at the time, I bought a USB Ant Miner with it. Yes BTC was $300.00 each then lol.

I should have kept it.

You live and learn...

onw - Splinterlands
b8l - RisingStar

Posted Using LeoFinance Beta

Reading this makes me want to delve back into Python! I’ve been trying to do it off and on for the past 3 years and have had pretty good luck reading it and understanding what the code is supposed to do. Where I have challenges is writing something for myself. I need to find a good use case for it. I could use this type of script here as a basis for what I want to do.

How are you getting the account and posting key in there so it will actually vote? Id be interested to check this out if you do post it on GitHub. I think I started one and dropped a couple small projects I was working on but haven’t been able to pick it back up much of late. I incorporate some learning time in my work week, this might give me some motivation to give it a crack!

What software are you using? I’ve grown fond of pycharm myself but I know there’s lots out there.

I'm using an old version of PrimalScript for my editor. It's hardly ideal and is from around 2007. Software is W10 and Anaconda. I had issues getting all the libraries working, but that's a one time thing.

Your coding looks pretty good to me. I think your self-esteem may be a bit shot, with you saying you're unemployed for nine months now.

Easy solution: Make a list of 10 freelance platforms, like Fiverr and Freelancer, and post what you can do there. You'll be flooded with work in no time.

Judging by your article here you can express yourself better than many programmers I know, so you'll deal easily with clients coming to you via those freelance platforms.

Hope your fortunes turn fast for the better. 😁

like Fiverr and Freelancer,

I did look on there, but they want the work for peanuts. I can get more on HIVE posting my stuff!

I agree, so far, for me, this is a difficult reading)

Posted Using LeoFinance Beta

This is really great, you know your onion despite you haven't coded for like 9 months.

Thanks for sharing this insightful and motivating post.

Cheers

woah this is actually my first time seeing so much code in a post and I'm stoked. Nice!

I'm a complete noob when it comes to Python, but I appreciate the time spent walking through the code rather than just dumping it for others to parse through. I'm definitely curious about this kind of scripting, so I've bookmarked this for later reference (after I learn up some ;P).

!1UP

Simple but effective little script. I like it! Some other folks already pointed out gains you can make on the efficiency end so I won't reiterate those. Real nice though, I've got to get better about leveraging scripts to automate shit I hate (like posting to Twitter) one of these days

I am very interested in learning this stuff I wish I had the opportunity to really get into programming when I was younger but I only learned hypertext. Really great work thanks for all your efforts keep it up

I prefer doing little work, the index() method is very useful with high efficiency

Your content has been voted as a part of Encouragement program. Keep up the good work!

Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for new Proposal
Delegate HP and earn more

Congratulations @slobberchops! Your post has been a top performer on the Hive blockchain and you have been rewarded with the following badge:

<table><tr><td><img src="https://images.hive.blog/60x60/http://hivebuzz.me/badges/toppayoutday.png" /><td>Post with the highest payout of the day. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@slobberchops" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto"><strong><span>Check out the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hive-122221/@hivebuzz/pud-202205"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg" /><td><a href="/hive-122221/@hivebuzz/pud-202205">Hive Power Up Day - May 1st 2022 <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/199" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!
Don-1UP-Cheers-Cartel-250px.png

You have received a 1UP from @entrepidus!


@leo-curator, @ctp-curator, @ccc-curator, @stem-curator, @vyb-curator, @pob-curator, @neoxag-curator, @pal-curator
And they will bring !PIZZA 🍕 The following @oneup-cartel family members will soon upvote your post:
<p dir="auto"><sup><a href="https://peakd.com/hive-102223/@flauwy/the-curation-cartel-1up-trigger-smart-voting-mana-and-high-delegation-returns-for-14-different-tribes" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Learn more about our delegation service to earn daily rewards. Join the family on <a href="https://discord.gg/H5mxvTrexm" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Discord.

Run the python inside of telos as a smart contract hosted on dstor like ipfs or aw3 so its always on