Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
beem
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
beem
Commits
82267483
Commit
82267483
authored
7 years ago
by
Holger Nahrstaedt
Browse files
Options
Downloads
Patches
Plain Diff
New example and fix unit test
parent
13225ce2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
beem/blockchain.py
+1
-4
1 addition, 4 deletions
beem/blockchain.py
examples/print_votes.py
+16
-34
16 additions, 34 deletions
examples/print_votes.py
tests/test_blockchain.py
+12
-1
12 additions, 1 deletion
tests/test_blockchain.py
with
29 additions
and
39 deletions
beem/blockchain.py
+
1
−
4
View file @
82267483
...
...
@@ -102,10 +102,7 @@ class Blockchain(object):
last_block
=
self
.
get_current_block
()
time_diff
=
last_block
.
time
()
-
date
block_number
=
math
.
floor
(
last_block
.
identifier
-
time_diff
.
total_seconds
()
/
block_time_seconds
)
if
block_number
>
last_block
.
identifier
:
return
last_block
.
identifier
else
:
block_number
return
block_number
def
block_time
(
self
,
block_num
):
"""
Returns a datetime of the block with the given block
...
...
This diff is collapsed.
Click to expand it.
examples/print_votes.py
+
16
−
34
View file @
82267483
from
__future__
import
print_function
import
sys
sys
.
path
.
append
(
'
../
'
)
from
datetime
import
timedelta
import
time
import
io
from
beem.
notify
import
Notify
from
beem.
blockchain
import
Blockchain
from
beem.utils
import
parse_time
import
logging
log
=
logging
.
getLogger
(
__name__
)
logging
.
basicConfig
(
level
=
logging
.
INFO
)
class
TestBot
:
def
__init__
(
self
):
self
.
notify
=
None
self
.
blocks
=
0
self
.
hourcount
=
0
self
.
start
=
time
.
time
()
self
.
last
=
time
.
time
()
def
new_block
(
self
,
block
):
print
(
block
[
"
timestamp
"
])
timestamp
=
parse_time
(
block
[
"
timestamp
"
])
chunk
=
10
self
.
blocks
=
self
.
blocks
+
1
if
self
.
blocks
%
chunk
==
0
:
self
.
notify
.
close
()
now
=
time
.
time
()
duration
=
now
-
self
.
last
total_duration
=
now
-
self
.
start
speed
=
int
(
chunk
*
1000.0
/
duration
)
*
1.0
/
1000
avspeed
=
int
(
self
.
blocks
*
1000
/
total_duration
)
*
1.0
/
1000
self
.
last
=
now
def
hour
(
self
):
self
.
hourcount
=
self
.
hourcount
+
1
now
=
time
.
time
()
total_duration
=
str
(
timedelta
(
seconds
=
now
-
self
.
start
))
print
(
"
* HOUR mark: Processed
"
+
str
(
self
.
hourcount
)
+
"
blockchain hours in
"
+
total_duration
)
if
self
.
hourcount
==
1
*
24
:
print
(
"
Ending eventloop
"
)
self
.
notify
.
close
()
class
DemoBot
(
object
):
def
vote
(
self
,
vote_event
):
w
=
vote_event
[
"
weight
"
]
if
w
>
0
:
print
(
"
Vote by
"
,
vote_event
[
"
voter
"
],
"
for
"
,
vote_event
[
"
author
"
])
else
:
if
w
<
0
:
print
(
"
Downvote by
"
,
vote_event
[
"
voter
"
],
"
for
"
,
vote_event
[
"
author
"
])
else
:
print
(
"
(Down)vote by
"
,
vote_event
[
"
voter
"
],
"
for
"
,
vote_event
[
"
author
"
],
"
CANCELED
"
)
if
__name__
==
"
__main__
"
:
tb
=
TestBot
()
notify
=
Notify
(
on_block
=
tb
.
new_block
)
tb
.
notify
=
notify
notify
.
listen
()
\ No newline at end of file
tb
=
DemoBot
()
blockchain
=
Blockchain
()
for
vote
in
blockchain
.
stream
(
opNames
=
[
"
vote
"
]):
tb
.
vote
(
vote
)
This diff is collapsed.
Click to expand it.
tests/test_blockchain.py
+
12
−
1
View file @
82267483
...
...
@@ -4,11 +4,12 @@ from __future__ import print_function
from
__future__
import
unicode_literals
from
builtins
import
super
import
unittest
from
datetime
import
datetime
,
timedelta
import
pytz
from
pprint
import
pprint
from
beem
import
Steem
from
beem.blockchain
import
Blockchain
from
beem.block
import
Block
from
datetime
import
datetime
from
beem.instance
import
set_shared_steem_instance
wif
=
"
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
"
...
...
@@ -40,3 +41,13 @@ class Testcases(unittest.TestCase):
block
=
b
.
get_current_block
()
self
.
assertTrue
(
isinstance
(
block
,
Block
))
self
.
assertEqual
(
num
,
block
.
identifier
)
def
test_estimate_block_num
(
self
):
bts
=
self
.
bts
b
=
Blockchain
(
steem_instance
=
bts
)
last_block
=
b
.
get_current_block
()
num
=
last_block
.
identifier
now
=
last_block
.
time
()
date
=
now
-
timedelta
(
seconds
=
60
*
3
)
est_block_num
=
b
.
get_estimated_block_num
(
date
)
self
.
assertEqual
(
est_block_num
,
num
-
60
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment