<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# === bitcoin.blockchain --------------------------------------------------===
# Copyright © 2012 by its contributors. See AUTHORS for details.
#
# Distributed under the MIT/X11 software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
# ===----------------------------------------------------------------------===

from datetime import datetime, timedelta
import numbers
from struct import pack, unpack

try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

from python_patterns.utils.decorators import Property

from .crypto import merkle
from .mixins import HashableMixin, SerializableMixin
from .script import Script
from .serialize import (
    serialize_varchar, deserialize_varchar,
    serialize_hash, deserialize_hash,
    serialize_list, deserialize_list)
from .utils import target_from_compact

__all__ = [
    'OutPoint',
    'Input',
    'Output',
    'Transaction',
    'Merkle',
    'Block',
]

# ===----------------------------------------------------------------------===

class OutPoint(SerializableMixin):
    def __init__(self, asset, hash=0, n=0xffffffff, *args, **kwargs):
        super(OutPoint, self).__init__(*args, **kwargs)
        self.asset = asset
        self.hash = hash
        self.n = n

    def serialize(self):
        result  = serialize_hash(self.hash, 32)
        result += pack('<I', self.n)
        return result
    @classmethod
    def deserialize(cls, asset, file_):
        initargs = {}
        initargs['hash'] = deserialize_hash(file_, 32)
        initargs['n'] = unpack('<I', file_.read(4))[0]
        return cls(asset, **initargs)

    def set_null(self):
        self.hash = 0
        self.n = 0xffffffff
    def is_null(self):
        return self.hash==0 and self.n==0xffffffff

    def is_valid(self):
        if self.hash<0 or self.hash>=2**256:
            return False
        if self.n<0 or self.n>=2**32:
            return False
        return True

    def __eq__(self, other):
        return self.hash==other.hash and self.n==other.n
    def __repr__(self):
        return 'OutPoint(hash=%064x, n=%d)' % (
            self.hash,
            self.n==0xffffffff and -1 or self.n)

# ===----------------------------------------------------------------------===

class Input(SerializableMixin):
    def __init__(self, asset, prevout=None, scriptSig=None,
                 nSequence=0xffffffff, *args, **kwargs):
        if prevout is None:
            prevout = self.deserialize_prevout(asset, StringIO('\x00'*32 + '\xff'*4))
        if scriptSig is None:
            scriptSig = kwargs.pop('coinbase', Script())
        super(Input, self).__init__(*args, **kwargs)
        self.asset = asset
        self.prevout = prevout
        self.scriptSig = scriptSig
        self.nSequence = nSequence

    def serialize(self):
        result  = self.prevout.serialize()
        if hasattr(self.scriptSig, 'serialize'):
            result += self.scriptSig.serialize()
        else:
            result += self.scriptSig # <-- coinbase
        result += pack('<I', self.nSequence)
        return result
    @staticmethod
    def deserialize_prevout(asset, file_):
        return OutPoint.deserialize(asset, file_)
    @classmethod
    def deserialize(cls, asset, file_):
        initargs = {}
        initargs['prevout'] = cls.deserialize_prevout(asset, file_)
        str_ = deserialize_varchar(file_)
        initargs['nSequence'] = unpack('<I', file_.read(4))[0]
        if initargs['prevout'].is_null() and initargs['nSequence']==0xffffffff:
            initargs['coinbase'] = str_ # <-- coinbase
        else:
            initargs['scriptSig'] = Script.deserialize(StringIO(str_))
        return cls(asset, **initargs)

    def is_final(self):
        return self.nSequence==0xffffffff

    def is_valid(self):
        if not self.prevout.is_valid():
            return False
        if isinstance(self.scriptSig, str):
            len_ = len(self.scriptSig)
            if len_<2 or len_>100:
                return False
        else:
            if not self.scriptSig.is_valid():
                return False
        if self.nSequence<0 or self.nSequence>=2**32:
            return False
        return True

    def __eq__(self, other):
        return (self.prevout   == other.prevout   and
                self.scriptSig == other.scriptSig and
                self.nSequence == other.nSequence)
    def __repr__(self):
        nSequence_str = (self.nSequence!=0xffffffff
            and ', nSequence=%d' % self.nSequence
             or '')
        return 'Input(prevout=%s, %s=%s%s)' % (
            repr(self.prevout),
            self.prevout.is_null() and 'coinbase' or 'scriptSig',
            repr(self.scriptSig),
            nSequence_str)

# ===----------------------------------------------------------------------===
<commit_msg>Code reorganization (moving methods around).<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# === bitcoin.blockchain --------------------------------------------------===
# Copyright © 2012 by its contributors. See AUTHORS for details.
#
# Distributed under the MIT/X11 software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
# ===----------------------------------------------------------------------===

from datetime import datetime, timedelta
import numbers
from struct import pack, unpack

try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

from python_patterns.utils.decorators import Property

from .crypto import merkle
from .mixins import HashableMixin, SerializableMixin
from .script import Script
from .serialize import (
    serialize_varchar, deserialize_varchar,
    serialize_hash, deserialize_hash,
    serialize_list, deserialize_list)
from .utils import target_from_compact

__all__ = [
    'OutPoint',
    'Input',
    'Output',
    'Transaction',
    'Merkle',
    'Block',
]

# ===----------------------------------------------------------------------===

class OutPoint(SerializableMixin):
    def __init__(self, asset, hash=0, n=0xffffffff, *args, **kwargs):
        super(OutPoint, self).__init__(*args, **kwargs)
        self.asset = asset
        self.hash = hash
        self.n = n

    def serialize(self):
        result  = serialize_hash(self.hash, 32)
        result += pack('<I', self.n)
        return result
    @classmethod
    def deserialize(cls, asset, file_):
        initargs = {}
        initargs['hash'] = deserialize_hash(file_, 32)
        initargs['n'] = unpack('<I', file_.read(4))[0]
        return cls(asset, **initargs)

    def set_null(self):
        self.hash = 0
        self.n = 0xffffffff
    def is_null(self):
        return self.hash==0 and self.n==0xffffffff

    def is_valid(self):
        if self.hash<0 or self.hash>=2**256:
            return False
        if self.n<0 or self.n>=2**32:
            return False
        return True

    def __eq__(self, other):
        return self.hash==other.hash and self.n==other.n
    def __repr__(self):
        return 'OutPoint(hash=%064x, n=%d)' % (
            self.hash,
            self.n==0xffffffff and -1 or self.n)

# ===----------------------------------------------------------------------===

class Input(SerializableMixin):
    def __init__(self, asset, prevout=None, scriptSig=None,
                 nSequence=0xffffffff, *args, **kwargs):
        if prevout is None:
            prevout = self.deserialize_prevout(asset, StringIO('\x00'*32 + '\xff'*4))
        if scriptSig is None:
            scriptSig = kwargs.pop('coinbase', Script())
        super(Input, self).__init__(*args, **kwargs)
        self.asset = asset
        self.prevout = prevout
        self.scriptSig = scriptSig
        self.nSequence = nSequence

    def serialize(self):
        result  = self.prevout.serialize()
        if hasattr(self.scriptSig, 'serialize'):
            result += self.scriptSig.serialize()
        else:
            result += self.scriptSig # <-- coinbase
        result += pack('<I', self.nSequence)
        return result
    @staticmethod
    def deserialize_prevout(asset, file_):
        return OutPoint.deserialize(asset, file_)
    @classmethod
    def deserialize(cls, asset, file_):
        initargs = {}
        initargs['prevout'] = cls.deserialize_prevout(asset, file_)
        str_ = deserialize_varchar(file_)
        initargs['nSequence'] = unpack('<I', file_.read(4))[0]
        if initargs['prevout'].is_null() and initargs['nSequence']==0xffffffff:
            initargs['coinbase'] = str_ # <-- coinbase
        else:
            initargs['scriptSig'] = Script.deserialize(StringIO(str_))
        return cls(asset, **initargs)

    def is_valid(self):
        if not self.prevout.is_valid():
            return False
        if isinstance(self.scriptSig, str):
            len_ = len(self.scriptSig)
            if len_<2 or len_>100:
                return False
        else:
            if not self.scriptSig.is_valid():
                return False
        if self.nSequence<0 or self.nSequence>=2**32:
            return False
        return True

    def is_final(self):
        return self.nSequence==0xffffffff

    def __eq__(self, other):
        return (self.prevout   == other.prevout   and
                self.scriptSig == other.scriptSig and
                self.nSequence == other.nSequence)
    def __repr__(self):
        nSequence_str = (self.nSequence!=0xffffffff
            and ', nSequence=%d' % self.nSequence
             or '')
        return 'Input(prevout=%s, %s=%s%s)' % (
            repr(self.prevout),
            self.prevout.is_null() and 'coinbase' or 'scriptSig',
            repr(self.scriptSig),
            nSequence_str)

# ===----------------------------------------------------------------------===

{'fraction_non_alphanumeric': 0.18006657923938263, 'fraction_numerical': 0.010491274084535459, 'mean_word_length': 1.9314015375517446, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 16, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1836916', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2772, 'n_words': 666}