<commit_before>from .common import APIError, headers
import datetime, time
import requests
import hmac, hashlib
from decimal import Decimal

class Poloniex:

    def __init__(self, apikey=None, secret=None):
        '''
        Initialize class when you want to use private commands.
        '''

        if apikey is not None and secret is not None:
            self.apikey = apikey.encode("utf-8")
            self.secret = secret.encode("utf-8")
            self.nonce = int(time.time())

    url = 'https://poloniex.com/'
    public_commands = ['returnTicker', 'returnOrderBook', 'returnTradeHistory',
                       'returnChartData', 'return24hVolume', 'returnLoanOrders',
                       'returnCurrencies']

    private_commands = ['returnBalances', 'returnCompleteBalances',
                        'returnDepositAddresses', 'generateNewAddress',
                        'returnDepositsWithdrawals', 'returnOpenOrders',
                        'returnTradeHistory', 'returnAvailableAccountBalances',
                        'returnTradableBalances', 'returnOpenLoanOffers',
                        'returnOrderTrades', 'returnActiveLoans',
                        'returnLendingHistory', 'createLoanOffer',
                        'cancelLoanOffer', 'toggleAutoRenew', 'buy', 'sell',
                        'cancelOrder', 'moveOrder', 'withdraw', 'returnFeeInfo',
                        'transferBalance', 'returnMarginAccountSummary',
                        'marginBuy', 'marginSell', 'getMarginPosition',
                        'closeMarginPosition']

    time_limit = datetime.timedelta(days=365)  # Poloniex will provide just 1 year of data
    delimiter = "_"
    case = "upper"
    headers = headers
    try:  # at Poloniex, fees may vary per user (https://poloniex.com/fees/)
        taker_fee, maker_fee = self.get_fee_info()["takerFee"], self.get_fee_info()["makerFee"]
    except:
        taker_fee, maker_fee = "0.0025", "0.0015"
    api_session = requests.Session()

    @property
    def get_nonce(self):
        '''return nonce integer'''

        self.nonce += 1
        return self.nonce

    @classmethod
    def format_pair(cls, pair):
        '''formats pair string in format understood by remote API'''

        if not cls.delimiter in pair and len(pair) > 5:
            pair = pair.replace("-", cls.delimiter)

        if not pair.isupper():
            pair = pair.upper()

        return pair

    @classmethod
    def api(cls, params):
        '''API calls'''

        assert params["command"] in cls.public_commands

        try:
            result = cls.api_session.get(cls.url + "public?", params=params,
                                         headers=cls.headers, timeout=(5, 8))
            assert result.status_code == 200
            return result.json()
        except requests.exceptions.RequestException as e:
            raise APIError(e)

    def private_api(self, data):
        '''private API methods which require authentication'''

        assert data["command"] in self.private_commands

        if not self.apikey or not self.secret:
            raise ValueError("A Key and Secret needed!")

        data["nonce"] = self.get_nonce  # add nonce to post data
        pdata = requests.compat.urlencode(data).encode("utf-8")
        self.headers.update(
            {"Sign": hmac.new(self.secret, pdata, hashlib.sha512).hexdigest(),
             "Key": self.apikey
             })

        try:
            result = requests.post(self.url + "tradingApi", data=data,
                                   headers=self.headers, timeout=(5, 8))
            assert result.status_code == 200
            return result.json()
        except requests.exceptions.RequestException as e:
            return APIError(e)

    ### Public methods ##

    @classmethod
    def get_markets(cls):
        '''return all supported markets.'''
<commit_msg>poloniex make timeout a class atribute<commit_after>from .common import APIError, headers
import datetime, time
import requests
import hmac, hashlib
from decimal import Decimal

class Poloniex:

    def __init__(self, apikey=None, secret=None):
        '''
        Initialize class when you want to use private commands.
        '''

        if apikey is not None and secret is not None:
            self.apikey = apikey.encode("utf-8")
            self.secret = secret.encode("utf-8")
            self.nonce = int(time.time())

    url = 'https://poloniex.com/'
    public_commands = ['returnTicker', 'returnOrderBook', 'returnTradeHistory',
                       'returnChartData', 'return24hVolume', 'returnLoanOrders',
                       'returnCurrencies']

    private_commands = ['returnBalances', 'returnCompleteBalances',
                        'returnDepositAddresses', 'generateNewAddress',
                        'returnDepositsWithdrawals', 'returnOpenOrders',
                        'returnTradeHistory', 'returnAvailableAccountBalances',
                        'returnTradableBalances', 'returnOpenLoanOffers',
                        'returnOrderTrades', 'returnActiveLoans',
                        'returnLendingHistory', 'createLoanOffer',
                        'cancelLoanOffer', 'toggleAutoRenew', 'buy', 'sell',
                        'cancelOrder', 'moveOrder', 'withdraw', 'returnFeeInfo',
                        'transferBalance', 'returnMarginAccountSummary',
                        'marginBuy', 'marginSell', 'getMarginPosition',
                        'closeMarginPosition']

    time_limit = datetime.timedelta(days=365)  # Poloniex will provide just 1 year of data
    delimiter = "_"
    case = "upper"
    headers = headers
    timeout = (5, 8)
    try:  # at Poloniex, fees may vary per user (https://poloniex.com/fees/)
        taker_fee, maker_fee = self.get_fee_info()["takerFee"], self.get_fee_info()["makerFee"]
    except:
        taker_fee, maker_fee = "0.0025", "0.0015"
    api_session = requests.Session()

    @property
    def get_nonce(self):
        '''return nonce integer'''

        self.nonce += 1
        return self.nonce

    @classmethod
    def format_pair(cls, pair):
        '''formats pair string in format understood by remote API'''

        if not cls.delimiter in pair and len(pair) > 5:
            pair = pair.replace("-", cls.delimiter)

        if not pair.isupper():
            pair = pair.upper()

        return pair

    @classmethod
    def api(cls, params):
        '''API calls'''

        assert params["command"] in cls.public_commands

        try:
            result = cls.api_session.get(cls.url + "public?", params=params,
                                         headers=cls.headers, timeout=cls.timeout)
            assert result.status_code == 200
            return result.json()
        except requests.exceptions.RequestException as e:
            raise APIError(e)

    def private_api(self, data):
        '''private API methods which require authentication'''

        assert data["command"] in self.private_commands

        if not self.apikey or not self.secret:
            raise ValueError("A Key and Secret needed!")

        data["nonce"] = self.get_nonce  # add nonce to post data
        pdata = requests.compat.urlencode(data).encode("utf-8")
        self.headers.update(
            {"Sign": hmac.new(self.secret, pdata, hashlib.sha512).hexdigest(),
             "Key": self.apikey
             })

        try:
            result = requests.post(self.url + "tradingApi", data=data,
                                   headers=self.headers, timeout=self.timeout)
            assert result.status_code == 200
            return result.json()
        except requests.exceptions.RequestException as e:
            return APIError(e)

    ### Public methods ##

    @classmethod
    def get_markets(cls):
        '''return all supported markets.'''

{'fraction_non_alphanumeric': 0.10787431624475258, 'fraction_numerical': 0.008395878386973667, 'mean_word_length': 2.080721003134796, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '771054', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2090, 'n_words': 528}