Tipping at the Right Time with Python: Learn to Program a Timer
Python Logo
Tipping at the Right Time with Python: Learn to Program a Timer


Sooner or later you made it to Publish0x. Chances are you are interested in cryptos (or gaming and crypto games or publishing or even more). Anyway, you are exploring Publish0x and you wonder: Is there a way to always tip at the right time and maximize tipping abilities?

You can make a program that checks the time for you and tells you it's time to read and tip.

 

Programming a Reminder to Tip

If you are into blockchain stuff learning to how to code should be something you at least thought about.

Here we are going to fuse our interest into crypto, blockchain and programming using Python to program a short script that will tell when it’s time to read something and tip. Basically we are going to build a timer.

I assume you have already setted up a Python environment and are ready to code. (If that’s not the case here’s a more detailed tutorial about getting started with Python through Anaconda).

 

Coding our Timer: The Key Steps

The core idea of our timer is this:

  1. we write down the time we start the timer (call it 'now');
  2. we define a time delta, i.e. how much time do we want the timer to time (call it 'timer');
  3. we add our timer time (say 30 minutes) to the time we started the timer (basically, point 1 time is added to point 2 time). We call this 'future';
  4. we check what’s the time at regular intervals and update the 'now' accordingly;
  5. once current time is beyond the time we defined in point 3 it means that time is over. Basically we check if now > future until it is true;
  6. when this happens, we stop the timer and take action (here we are printing it on the screen and opening publish0x table).

To code our above plan we are going to use a few modules from Python’s standard library:

  • time and datetime to have the timer working;
  • webbrowser to open the publish0x page.

The program has two functions:

  1. the timecounter(tempo) is the actual counter. You enter how many minutes you want to time and it does its job;
  2. the dosomething() function will be called once the time is over, and it will make it easy for you to make something happen once the time is over. Here the function will print something on the screen and open publish0x so that you can search and tip.

 

[Quick Python crash course:

# signals comments;

import is for importing modules:

def something(): defines a function called 'something';

space and indentation matter in Python;

while ... break repeats as long as the while condition holds, and when the break condition is reached, it stops]

 

Here's the code! (If you know better ways of displaying code on publis0x, tell me.)

 

# import what we need
import time
import datetime as dt
import webbrowser

#define what we want to happen once time is over
def dosomething():
    print('Now it is time to read and learn (and tip)!')
    webbrowser.open_new_tab('https://www.publish0x.com/')

#define the timer
def timecounter(tempo):
    #check what the time is when we start (point 1)
    now = dt.datetime.now()
    #we print that time, so we know the program works
    print('starting ', now)
    #define the timer object (point 2)
    timer = dt.timedelta(0,tempo*60) #timedelta takes second so we multiply tempo by 60
    #define when the timer will stop (point 3)
    future = now + timer
    #We print the time to be sure. This allows us to check if it didn't work
    print('Stops at', future)
    
    #here we initialize the timecheck of what's the actual time and if it's beyond that of the timer
    while True: #this loop will go on forever
        if now > future:
        #this means timer is over, so we run our dosomething function
            dosomething()
            #we need to exit the loop with a break, otherwise we're trapped
            break
        else:
        #if the timer is not over it is still running.
        #we have to wait for some time to pass and then update our actual time
            time.sleep(60) #we are checking our time every minute. Feel free to change this
            now = dt.datetime.now() #we are updating the 'now' we check against future

 

So that’s it! Congratulations, you have a timer and it is doing what you want.

 

Further Implementations

There are many features you can add to the timer. Adding features to something is one of the best ways to learn.

  • can you run the timer multiple times with different time-length? (maybe the different times you have to wait before being allowed to tip again…)
  • can you add a sound when the timer is up?
  • can you add some variety to the landing page?
  • what if the user doesn’t see the page opening and the message?

(In the resources there's a great pomodoro-timer building tutorial you may draw inspiration to address all the points).



Programming, Blockchain and Stuff
Programming, Blockchain and Stuff

Writing about programming, Digital Humanities and the blockchain.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.