Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant find my problem ...
#1
I cant find the problem
When the script should not write to output file it does

wtm.py
#!/usr/bin/env python2.7
# _*_ coding: utf-8 _*_
#### damNmad+papampi+hurvajs77 Whattomine auto switch written by papampi + hurvajs77, forked from smartminer by damNmad

import json
import requests
import sys
import urllib

configFile = "./WTM.json"
topCoinLogFile = "./WTM_top_coin"

# load config
cfg = json.loads(open(configFile).read())
requestUrl = urllib.unquote(urllib.unquote(cfg["WTM_URL"]))
minimumDifference = float(cfg["WTM_MIN_DIFFERENCE"])
includedCoins = cfg["WTM_COINS"].upper()
delimiter = ";"
# load included coins
includedCoins = includedCoins.strip(delimiter)

if not includedCoins:
    print "No incluted coins. Please, check 1bash script for WTM settings."
    sys.exit()

includedCoins = includedCoins.split(delimiter)


def saveTopCoin(data):
    logFile = open(topCoinLogFile, "w")
    logFile.write(data)
    logFile.close()
    return

# try load previous top coin
try:
    with open(topCoinLogFile) as contentFile:
        content = contentFile.read()
except:
    content = "-:0"

topCoin = content.split(":")
print "Currently mining coin: %s, profit: %s" % (topCoin[0], topCoin[1])

try:
    httpResponse = requests.get(requestUrl)
except:
    print("Can not get data from WhatToMine.com.")
    raise

try:
    data = httpResponse.json()['coins']
    data = data.values()
except:
    print "Invalid JSON"
    raise

# filter WTM coins by user selection only
for i in reversed(data):
    if i["tag"] not in includedCoins:
        data.remove(i)

# calculate coin profitability
newProfits = {}
for i in data:
    newProfits[i["tag"]] = i["profitability"]
newProfits = sorted(newProfits.items(), key=lambda x: x[1], reverse=True)

# save current profit
print "New profits"
profitLog = open("current-profit", "w")
for i, j in newProfits:
    profitLog.write("%s:%s\n" % (i, j))
    print str(i) + ": " + str(j) + " %"
profitLog.close()

# is currently mining coin same as a new the most profitability coin?
if newProfits[0][0] == topCoin[0]:
    print "Same coin"
    saveTopCoin(str(newProfits[0][0]) + ":" + str(newProfits[0][1]))
    sys.exit()

if (float(newProfits[0][1]) - minimumDifference) < float(topCoin[1]):
    # try find actual top coin and compare their profit with maximum of current profits
    try:
        topCoinNewProfit = filter(lambda x: x["tag"] == topCoin[0], data)[0]
        if (float(newProfits[0][1]) - minimumDifference) > float(topCoinNewProfit["profitability"]):
            print "Currently mining %s coin is no longer profitability %s" % (topCoin[0], topCoin[1])
            print "Switching to new %s coin %s" % (newProfits[0][0], newProfits[0][1])
        else:
            print "Currently mining coin is still more profitability (with subtracted difference) than new profit coin"
            print "Continuing with mining %s coin" % topCoin[0]
    except:
        print "Top coin was not found in list of included coins"
        sys.exit()
else:
    # current profit is higher that currently mining
    print "Found %s coin with higher profitability %s" % (newProfits[0][0], newProfits[0][1])

saveTopCoin(str(newProfits[0][0]) + ":" + str(newProfits[0][1]))
config file WTM.json:
{
  "WTM_URL": "https://whattomine.com/coins.json?",
  "WTM_COINS": "ZEC;MONA;VTC;KDM;ZEN;ZCL;SIB;",
  "WTM_MIN_DIFFERENCE": "30"
}
Currently mining coin: ZEN, profit: 151
New profits
MONA: 157 %
ZEN: 137 %
ZCL: 120 %
ZEC: 115 %
VTC: 109 %
SIB: 29 %
Currently mining coin is still more profitability (with subtracted difference) than new profit coin
Continuing with mining ZEN coin
But when I check the WTM_top_coin , it is changed to new top, in this example MONA
Reply
#2
your last line (# 100) must be indented one more level
by the way, I'm not sure that condition on line 87 will ever be evaluated true, if the condition on line 83 is True. I may be wrong, but at the moment, looking at it, I think so.
Reply
#3
When I apply indent it does not write to file when it should switch

Currently mining coin: ZEC, profit: 120
New profits
ZCL: 134 %
ZEC: 121 %
ZEN: 105 %
VTC: 98 %
MONA: 76 %
SIB: 48 %
Currently mining ZEC coin is no longer profitability 134
Switching to new ZCL coin 134
But the WTM_top_coin has still ZEC values
It seems exactly line 87 problem
what you suggest ?
Reply
#4
I'm not sure if I understand what you try to achieve in the if block (lines 83-95). In any case there you don't have any call tosaveTopCoin, so within this block it never writes to file.

If I understand right - lines 78-81 will write if new most profitable coin is same as the one currently mined
and lines 96-100 will write if the new most profitable coin is more profitable than current one with difference bigger than or at least equal to some min. margin
So for the if block (83-95) it remains to deal with the case when new coin is more profitable, but the difference is less than or equal to the min. margin
Reply
#5
Wow... My bad, exactly missed the call to save topcoin for lines 83-92
Will add it and see how it goes
Reply
#6
So I fixed the errors,
Now I want to add some more functionality but gives me error

I want to add
currency="USD"
exchrate=float(json.loads(urllib2.urlopen("https://blockchain.info/ticker").read())[currency]["last"])
# calculate coin revenue
newRev = {}
for i in data:
    newRev[i["tag"]] = i["btc_revenue"]
newRev = sorted(newRev.items(), key=lambda x: x[1], reverse=True)

# save current revenue
RevLog = open("WTM_current_revenue", "w")
for i in newRev:
    RevLog.write("%s:%s\n" % (i[0], (i[1])*exchrate))
    print i[0] + ": " + i[1]*exchrate + " USD"
RevLog.close()
But gives me this error:
#python2.7 WTM_AUTO_SWITCH2.py
Currently mining coin: FTC, profit: 185
5694.14
Traceback (most recent call last):
  File "WTM_AUTO_SWITCH2.py", line 87, in <module>
    RevLog.write("%s:%s\n" % (i[0], (i[1])*exchrate))
TypeError: can't multiply sequence by non-int of type 'float'
Can any one help me fix "can't multiply sequence by non-int of type 'float "

Edit:

fixed it by :

    newRev[i["tag"]] = float(i["btc_revenue"])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can you find the problem in my code? ivinjjunior 3 3,357 Apr-20-2019, 12:37 AM
Last Post: ivinjjunior

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020