-
Notifications
You must be signed in to change notification settings - Fork 4
Home
You can easily install it with pip
:
$ pip install draftlog
There's not a whole lot to go over, so it'll all be covered here.
First off, how to actually use it. So here's the simplest possible example of it actually doing something:
# First, you'll want to import it
from draftlog.draft import Draft
# Then initialize the object
d = Draft()
# Create a text interval object (we'll get to what that is later)
d.add_text("*programming pun*")
# And run it!
d.start()
Now, if we wanted to stop here, then it would run, but let's add some complexity to the text interval object:
# This time we'll assign a variable to `d.add_text`, which returns an id for finding our object.
# Also, with status as `False`, it means it won't timeout immediately
text = d.add_text("*programming pun*", status=True)
# Then with the id, we can edit the text interval object:
text.update("text", "haha what a funny joke...").update("status", False).after(3)
d.start()
Now, if this was done correctly, it should print out *programming pun*
for 3 seconds and then overwrite it with haha what a funny joke...
. Now, this seems like a lot of work for just rewriting a line, but there's a lot more you can do.
Let's cover what we actually did though. First, we import
ed the module (no surprise there). Then we initialized the Draft
object (also, no big whoop). Next, we assigned the variable text
to the function add_text
. You'll learn that add_text
is pretty much just an alias for the function Draft.add_interval
(which we'll get to soon). Then we used the text
variable to update our object's text after 3 seconds.
Okay, so now let's move on to making our own classes to update. First though, let's go over the basics of what an increment class has to have:
- A function named
increment
that accepts no arguments. This will be called everytime for updating. It should return the text to output and/or overwrite previous text. - An instance variable named
status
that dictates when theDraft
loop will end: -
False
means that it is done being updated,increment
won't be called when it's set to that. -
True
is pretty much the opposite, the loop will not exit until there are noTrue
states
in any of the interval objects. -
None
is a strange inbetweenFalse
andTrue
. It doesn't affect the loop, but itsinterval
function will always be called.
from draftlog.draft import Draft
# Okay, so then let's make just a clock class:
class Clock:
def __init__(self, timeout):
self.timeout = timeout
self.status = True # essential variable `status`
self.time = 0
def interval(self): # and the `interval` function.
self.time += 1
if self.time >= self.timeout:
self.status = False
return self.time
d = Draft()
# Here we will use the `add_interval` function instead:
d.add_interval(Clock(10), 1)
# So the `add_interval` func takes the initialized interval object and
# how often it should be called (we set it as every second).
d.start() # The loop should end after 10 seconds.
The above code is pretty self-explanatory, so let's move on to the Draft.add_loader
function. It's basically an alias for Draft.add_interval
, but it automatically sets your status
to None
so that you don't have to worry about it.
Anyways, here's an example:
# -*- coding: UTF-8 -*-
from draftlog.draft import Draft
class Loader:
def __init__(self, text):
self.frames = "⢄ ⢂ ⢁ ⡁ ⡈ ⡐ ⡠".split(" ")
self.frame = -1
self.text = text
def interval(self):
if self.frame > len(self.frames) - 2:
self.frame = -1
self.frame += 1
return ("{0} " + self.text + " {0}").format(self.frames[self.frame])
class Clock:
def __init__(self, timeout):
self.timeout = timeout
self.status = True # essential variable `status`
self.time = 0
def interval(self): # and the `interval` function.
self.time += 1
if self.time >= self.timeout:
self.status = False
return (" " * 6) + str(self.time)
d = Draft()
d.add_loader(Loader("Tick Tock"), 0.05)
d.add_interval(Clock(10), 1)
d.start()
This one's also pretty self-explanatory. To clear up what the second argument is in Draft.add_loader
is, it's just the time again.
If you want to see some more examples, check out the examples
folder.