O
O
Oleg2018-05-21 18:45:29
Python
Oleg, 2018-05-21 18:45:29

Parameter self in python how to assign it to pass parameter?

Hello
, could you tell me what I'm doing wrong?

# -*- coding: utf-8 -*-

import http.client
import urllib.request, urllib.parse, urllib.error
import json
import hashlib
import hmac
import time

class api:
    __api_key	= '';
    __api_secret= '';
    __nonce_v	= 1;
    __wait_for_nonce = False

def __init__(self,api_key,api_secret,wait_for_nonce=False):
    self.__api_key = api_key
    self.__api_secret = api_secret
    self.__wait_for_nonce = wait_for_nonce

def __nonce(self):
    if self.__wait_for_nonce: time.sleep(1)
    self.__nonce_v = str(time.time()).split('.')[0]

def __signature(self, params):
    sig = hmac.new(self.__api_secret.encode(), params.encode(), hashlib.sha512)
    return sig.hexdigest()

def __api_call(self,method,params):
    self.__nonce()
    params['method'] = method
    params['nonce'] = str(self.__nonce_v)
    params = urllib.parse.urlencode(params)
    headers = {"Content-type" : "application/x-www-form-urlencoded",
              "Key" : self.__api_key,
        "Sign" : self.__signature(params)}
    conn = http.client.HTTPSConnection("wex.nz")
    conn.request("POST", "/tapi", params, headers)
    response = conn.getresponse().read().decode()
    data = json.loads(response)
    conn.close()
    return data
  
def get_param(self, couple, param):
    conn = http.client.HTTPSConnection("wex.nz")
    conn.request("GET", "/api/3/"+couple+"/"+param)
    response = conn.getresponse().read().decode()
    data = json.loads(response)
    conn.close()
    return data
 
def getInfo(self):
    return self.__api_call('getInfo', {})

def TransHistory(self, tfrom, tcount, tfrom_id, tend_id, torder, tsince, tend):
    params = {
    "from"	: tfrom,
    "count"	: tcount,
    "from_id"	: tfrom_id,
    "end_id"	: tend_id,
    "order"	: torder,
    "since"	: tsince,
    "end"	: tend}
    return self.__api_call('TransHistory', params)
 
def TradeHistory(self, tfrom, tcount, tfrom_id, tend_id, torder, tsince, tend, tpair):
    params = {
    "from"	: tfrom,
    "count"	: tcount,
    "from_id"	: tfrom_id,
    "end_id"	: tend_id,
    "order"	: torder,
    "since"	: tsince,
    "end"	: tend,
    "pair"	: tpair}
    return self.__api_call('TradeHistory', params)

def ActiveOrders(self, tpair):
    params = { "pair" : tpair }
    return self.__api_call('ActiveOrders', params)

def Trade(self, tpair, ttype, trate, tamount):
    params = {
    "pair"	: tpair,
    "type"	: ttype,
    "rate"	: trate,
    "amount"	: tamount,}
    return self.__api_call('Trade', params)
  
def CancelOrder(self, torder_id):
    params = { "order_id" : torder_id }
    return self.__api_call('CancelOrder', params)

called the module like this
# -*- coding: utf-8 -*-
import btceapipython
btceapipython.Trade(self, tpair='usd_rur', ttype='buy',trate=55,tamount=1)
writes
btceapipython.Trade(self, tpair='usd_rur', ttype='buy',trate=55,tamount=1)
NameError: name 'self' is not defined
self can't be a name and a parameter, what would the code need to send a purchase request?
how to understand this lack of selfie

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-05-21
@VenusV

Your functions outside the class are due to incorrect indentation.
The class structure should be like this:

class api:
    def __init__(self,api_key,api_secret,wait_for_nonce=False):
        pass
    def Trade(self, tpair, ttype, trate, tamount):
        pass

Correct work with classes looks something like this:
from btceapipython import api
api_key = "your key"
api_secret = "your secret"
myapi = api(api_key,api_secret)
myapi.Trade(tpair='usd_rur', ttype='buy',trate=55,tamount=1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question