Steem Node ReCycler Function: Easy Way to Cycle through Your Node List w PythonsteemCreated with Sketch.

in #steemdev6 years ago

Wanted to share a quick function I spun up after getting frustrated with node issues last week.

Steem Node ReCycler

It's nothing fancy and it's really not exclusively for cycling. You can use it to cycle through any Python list variable for whatever reason. I just happen to use it to cycle my nodes upon error. The code is as follows:

def node_recycler(nodes):
    myorder = []
    node_count = len(nodes)
    myorder.append(2)
    i=3
    while i < node_count+1:
        myorder.append(i)
        i+=1
    myorder.append(1)
    nodes_new = [nodes[i-1] for i in myorder]
    return nodes_new

In order to use it, just call the function during an exception which will return the new list of nodes which have been cycled with the first being moved to the last and the rest shuffled forward. For instance, if you had a list of 5 nodes, the order would now be [2,3,4,5,1].

You may then create a new Steem instance and input the new node list variable you had assigned. It should look like this.

nodes = node_recycler(nodes)
steem = Steem(nodes)

Too easy, right! Hope, this helps someone that would have otherwise been getting ready to pull out their hair.



Don't do that.

Sort:  

Beep! Beep! This humvee will be patrolling by and assisting new veterans, retirees, and military members here on Steem. @shadow3scalpel will help by upvoting posts from a list of members maintained by @chairborne and responding to any questions replied to this comment.

You can do the same with next_node

steem = Steem(nodes)
steem.steemd.next_node()

Now the second node in the nodes list is used.

Wow that is much easier. Awesome. Always glad to trim things down.

By the way, you ever get that SP/ Voting percent to Rshares version of Beem ready? I could really use it for the Steem Flag Rewards bot.

Yes its available in the newest version of beem. You can use it besides steem-python

from beem import Steem as beem_Steem
stm = beem_Steem(node=nodes) # you can use the same nodes as for python-steem
vote_pct = stm.rshares_to_vote_pct(rshares, steem_power=100, voting_power=10000)
# Instead of Steem power, vests can be given:
vote_pct = stm.rshares_to_vote_pct(rshares, vests=1e8, voting_power=10000)

from beem import Steem as beem_Steem prevents conflicts with from steem import Steem. There can only one Class called Steem imported.

Here is a short version of node_recycler

nodes = nodes[1:] + [nodes[0]]

Wow. Much cleaner. What's the 1: mean in an array list? Is there a term for it? Thanks, @holger80!

nodes is a list, you can access parts of the list by this.
E.g.:

>>> x = [1,2,3]
>>> x[1:]
[2,3]
>>> x[:-1]
[1,2]
>>> x[1:2]
[2]
>>> x[::2]
[1,3]

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63651.41
ETH 2679.55
USDT 1.00
SBD 2.80