Skip to content

Commit

Permalink
Replace if_else utility function with Python 2.5+'s built in ternar…
Browse files Browse the repository at this point in the history
…y logic.
  • Loading branch information
JustinTArthur committed Apr 2, 2013
1 parent ee85212 commit 82aa2d6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
3 changes: 1 addition & 2 deletions GetworkSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from threading import Thread
from time import sleep, time
from urlparse import urlsplit
from util import if_else
import httplib
import socket
import socks
Expand Down Expand Up @@ -139,7 +138,7 @@ def timeout_response(self, connection, timeout):
def getwork(self, data=None):
try:
self.connection = self.ensure_connected(self.connection, self.server().proto, self.server().host)[0]
self.postdata['params'] = if_else(data, [data], [])
self.postdata['params'] = [data] if data else []
(self.connection, result) = self.request(self.connection, '/', self.headers, dumps(self.postdata))

self.switch.connection_ok()
Expand Down
6 changes: 3 additions & 3 deletions OpenCLMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from struct import pack, unpack, error
from threading import Lock
from time import sleep, time
from util import if_else, uint32, Object, bytereverse, tokenize, \
from util import uint32, Object, bytereverse, tokenize, \
bytearray_to_uint32
import sys

Expand Down Expand Up @@ -77,7 +77,7 @@ def initialize(options):
options.worksize = tokenize(options.worksize, 'worksize')
options.frames = tokenize(options.frames, 'frames', [30])
options.frameSleep = tokenize(options.frameSleep, 'frameSleep', cast=float)
options.vectors = if_else(options.old_vectors, [True], tokenize(options.vectors, 'vectors', [False], bool))
options.vectors = [True] if options.old_vectors else tokenize(options.vectors, 'vectors', [False], bool)

platforms = cl.get_platforms()

Expand Down Expand Up @@ -149,7 +149,7 @@ def nonce_generator(self, nonces):
def mining_thread(self):
say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name))

(self.defines, rate_divisor, hashspace) = if_else(self.vectors, ('-DVECTORS', 500, 0x7FFFFFFF), ('', 1000, 0xFFFFFFFF))
(self.defines, rate_divisor, hashspace) = ('-DVECTORS', 500, 0x7FFFFFFF) if self.vectors else ('', 1000, 0xFFFFFFFF)
self.defines += (' -DOUTPUT_SIZE=' + str(self.output_size))
self.defines += (' -DOUTPUT_MASK=' + str(self.output_size - 1))

Expand Down
18 changes: 9 additions & 9 deletions Switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from struct import pack, unpack
from threading import RLock
from time import time, sleep
from util import if_else, Object, chunks, bytereverse, belowOrEquals, uint32
from util import Object, chunks, bytereverse, belowOrEquals, uint32
import StratumSource
import log
import socks
Expand Down Expand Up @@ -203,19 +203,19 @@ def diff1_found(self, hash_, target):

def status_updated(self, miner):
verbose = self.options.verbose
rate = if_else(verbose, miner.rate, sum([m.rate for m in self.miners]))
estimated_rate = if_else(verbose, miner.estimated_rate, sum([m.estimated_rate for m in self.miners]))
rejected_shares = if_else(verbose, miner.share_count[0], sum([m.share_count[0] for m in self.miners]))
total_shares = rejected_shares + if_else(verbose, miner.share_count[1], sum([m.share_count[1] for m in self.miners]))
rate = miner.rate if verbose else sum([m.rate for m in self.miners])
estimated_rate = miner.estimated_rate if verbose else sum([m.estimated_rate for m in self.miners])
rejected_shares = miner.share_count[0] if verbose else sum([m.share_count[0] for m in self.miners])
total_shares = rejected_shares + miner.share_count[1] if verbose else sum([m.share_count[1] for m in self.miners])
total_shares_estimator = max(total_shares, 1)
say_quiet('%s[%.03f MH/s (~%d MH/s)] [Rej: %d/%d (%.02f%%)]', (if_else(verbose, miner.id()+' ', '') , rate, round(estimated_rate), rejected_shares, total_shares, float(rejected_shares) * 100 / total_shares_estimator))
say_quiet('%s[%.03f MH/s (~%d MH/s)] [Rej: %d/%d (%.02f%%)]', (miner.id()+' ' if verbose else '', rate, round(estimated_rate), rejected_shares, total_shares, float(rejected_shares) * 100 / total_shares_estimator))

def report(self, miner, nonce, accepted):
is_block, hash6, hash5 = self.sent[nonce]
miner.share_count[if_else(accepted, 1, 0)] += 1
hash_ = if_else(is_block, hash6 + hash5, hash6)
miner.share_count[1 if accepted else 0] += 1
hash_ = hash6 + hash5 if is_block else hash6
if self.options.verbose or is_block:
say_line('%s %s%s, %s', (miner.id(), if_else(is_block, 'block ', ''), hash_, if_else(accepted, 'accepted', '_rejected_')))
say_line('%s %s%s, %s', (miner.id(), 'block ' if is_block else '', hash_, 'accepted' if accepted else '_rejected_'))
del self.sent[nonce]

def set_server_index(self, server_index):
Expand Down
4 changes: 2 additions & 2 deletions poclbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from Switch import Switch
from optparse import OptionGroup, OptionParser
from time import sleep
from util import if_else, tokenize
from util import tokenize
from version import VERSION
import log
import socket
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
log.verbose = options.verbose
log.quiet = options.quiet

options.rate = if_else(options.verbose, max(options.rate, 60), max(options.rate, 0.1))
options.rate = max(options.rate, 60) if options.verbose else max(options.rate, 0.1)

options.version = VERSION

Expand Down
6 changes: 0 additions & 6 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ def belowOrEquals(hash_, target):
return False
return True

def if_else(condition, trueVal, falseVal):
if condition:
return trueVal
else:
return falseVal

def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
Expand Down

0 comments on commit 82aa2d6

Please sign in to comment.