<commit_before>import datetime
from uuid import UUID
import util
from struct import pack
import copy
import numpy as np
def leap_year(year):
    if (year % 400) == 0:
        return True
    elif (year % 100) == 0:
        return True
    elif (year % 4) == 0:
        return False
    return False

## NOTE: set_attr methods are currently not implemented. These methods need
## to update the file using reader/mmap. 
class LaspyHeaderException(Exception):
    pass

class EVLR():
    ''' An extended VLR as defined in LAS specification 1.4'''
    def __init__(self, user_id, record_id, VLR_body, **kwargs):
        '''Build the EVLR using the required arguments user_id, record_id, and
        VLR_body. The user can also specify the reserved and description fields if desired.'''
        try:
            self.rec_len_after_header = len(self.VLR_body)
        except(TypeError):
            self.rec_len_after_header = 0
        for key in kwargs:
            if key == "description":
                self.description = str(kwargs[key]) + "\x00"*(32-len(kwargs[key]))
            else:
                self.description = "\x00"*32
            if key == "reserved":
                self.reserved = kwargs[key]
            else:
                self.reserved = 0
            if not key in ["description", "reserved"]:
                raise util.LaspyException("Unknown Argument to VLR: " + str(key))

    def build_from_reader(self, reader):
        '''Build a vlr from a reader capable of reading in the data.'''
        self.reserved = reader.read_words("reserved", "evlr")
        self.user_id = "".join(reader.read_words("user_id", "evlr"))
        self.record_id = reader.read_words("record_id", "evlr")
        self.rec_len_after_header = reader.read_words("rec_len_after_header", "evlr")
        self.description = "".join(reader.read_words("description", "evlr"))
        self.VLR_body = reader.read(self.rec_len_after_header)
        ### LOGICAL CONTENT ###
        self.isEVLR = True
        self.fmt = reader.evlr_formats

    def __len__(self):
        '''Return the size of the vlr object in bytes'''
        return self.rec_len_after_header + 60

    def pack(self, name, val): 
        '''Pack a VLR field into bytes.'''
        spec = self.fmt.lookup[name]
        if spec.num == 1:
            return(pack(spec.fmt, val))
        return(pack(spec.fmt[0]+spec.fmt[1]*len(val), *val))
    
    def to_byte_string(self):
        '''Pack the entire EVLR into a byte string.'''
        out = (self.pack("reserved", self.reserved) + 
               self.pack("user_id", self.user_id) + 
               self.pack("record_id", self.record_id) + 
               self.pack("rec_len_after_header", self.rec_len_after_header) + 
               self.pack("description", self.description) +
               self.VLR_body)
        diff = (self.rec_len_after_header - len(self.VLR_body))
        if diff > 0:
            out += "\x00"*diff
        elif diff < 0:
            raise util.LaspyException("Invalid Data in EVLR: too long for specified rec_len." + 
                                " rec_len_after_header = " + str(self.rec_len_after_header) + 
                                " actual length = " + str(len(self.VLR_body)))
        return(out)


class VLR():
    '''An object to create/read/store data from LAS Variable Length Records.
    Requires three arguments: (user_id, string[16]), (record_id, int2), (VLR_body, any data with len < ~65k)'''
    def __init__(self, user_id, record_id, VLR_body, **kwargs):
        '''Build the VLR using the required arguments user_id, record_id, and VLR_body
        the user can also specify the reserved and description fields here if desired.'''
        self.user_id = str(user_id) + "\x00"*(16-len(str(user_id)))
        self.record_id = record_id
        self.VLR_body = VLR_body
        try:
            self.rec_len_after_header = len(self.VLR_body)
        except(TypeError):
            self.rec_len_after_header = 0
        for key in kwargs:
            if key == "description":
                self.description = str(kwargs[key]) + "\x00"*(32-len(kwargs[key]))
            else:
                self.description = "\x00"*32
            if key == "reserved":
                self.reserved = kwargs[key]
            else:
                self.reserved = 0
            if not key in ["description", "reserved"]:
                raise util.LaspyException("Unknown Argument to VLR: " + str(key))
        self.reserved = 0
        self.isVLR = True
        self.fmt = util.Format("VLR")

    def build_from_reader(self, reader):
        '''Build a vlr from a reader capable of reading in the data.'''
        self.reserved = reader.read_words("reserved")
        self.user_id = "".join(reader.read_words("user_id"))
        self.record_id = reader.read_words("record_id")
        self.rec_len_after_header = reader.read_words("rec_len_after_header")
        self.description = "".join(reader.read_words("description"))
        self.VLR_body = reader.read(self.rec_len_after_header)
        ### LOGICAL CONTENT ###
        self.isVLR = True
        self.fmt = reader.vlr_formats

    def __len__(self):
        '''Return the size of the vlr object in bytes'''
        return self.rec_len_after_header + 54

    def pack(self, name, val): 
        '''Pack a VLR field into bytes.'''
        spec = self.fmt.lookup[name]<commit_msg>clean up VLR and EVLR constructor logic<commit_after>import datetime
from uuid import UUID
import util
from struct import pack
import copy
import numpy as np
def leap_year(year):
    if (year % 400) == 0:
        return True
    elif (year % 100) == 0:
        return True
    elif (year % 4) == 0:
        return False
    return False

## NOTE: set_attr methods are currently not implemented. These methods need
## to update the file using reader/mmap. 
class LaspyHeaderException(Exception):
    pass

class EVLR():
    ''' An extended VLR as defined in LAS specification 1.4'''
    def __init__(self, user_id, record_id, VLR_body, **kwargs):
        '''Build the EVLR using the required arguments user_id, record_id, and
        VLR_body. The user can also specify the reserved and description fields if desired.'''
        try:
            self.VLR_body = VLR_body
            self.rec_len_after_header = len(self.VLR_body)
        except(TypeError):
            self.rec_len_after_header = 0
        if "description" in kwargs:
            self.description = str(kwargs["description"]) + "\x00"*(32-len(kwargs["description"]))
        else:
            self.description = "\x00"*32
        if "reserved" in kwargs:
            self.reserved = kwargs["reserved"]
        else:
            self.reserved = 0 

    def build_from_reader(self, reader):
        '''Build a vlr from a reader capable of reading in the data.'''
        self.reserved = reader.read_words("reserved", "evlr")
        self.user_id = "".join(reader.read_words("user_id", "evlr"))
        self.record_id = reader.read_words("record_id", "evlr")
        self.rec_len_after_header = reader.read_words("rec_len_after_header", "evlr")
        self.description = "".join(reader.read_words("description", "evlr"))
        self.VLR_body = reader.read(self.rec_len_after_header)
        ### LOGICAL CONTENT ###
        self.isEVLR = True
        self.fmt = reader.evlr_formats

    def __len__(self):
        '''Return the size of the vlr object in bytes'''
        return self.rec_len_after_header + 60

    def pack(self, name, val): 
        '''Pack a VLR field into bytes.'''
        spec = self.fmt.lookup[name]
        if spec.num == 1:
            return(pack(spec.fmt, val))
        return(pack(spec.fmt[0]+spec.fmt[1]*len(val), *val))
    
    def to_byte_string(self):
        '''Pack the entire EVLR into a byte string.'''
        out = (self.pack("reserved", self.reserved) + 
               self.pack("user_id", self.user_id) + 
               self.pack("record_id", self.record_id) + 
               self.pack("rec_len_after_header", self.rec_len_after_header) + 
               self.pack("description", self.description) +
               self.VLR_body)
        diff = (self.rec_len_after_header - len(self.VLR_body))
        if diff > 0:
            out += "\x00"*diff
        elif diff < 0:
            raise util.LaspyException("Invalid Data in EVLR: too long for specified rec_len." + 
                                " rec_len_after_header = " + str(self.rec_len_after_header) + 
                                " actual length = " + str(len(self.VLR_body)))
        return(out)


class VLR():
    '''An object to create/read/store data from LAS Variable Length Records.
    Requires three arguments: (user_id, string[16]), (record_id, int2), (VLR_body, any data with len < ~65k)'''
    def __init__(self, user_id, record_id, VLR_body, **kwargs):
        '''Build the VLR using the required arguments user_id, record_id, and VLR_body
        the user can also specify the reserved and description fields here if desired.'''
        self.user_id = str(user_id) + "\x00"*(16-len(str(user_id)))
        self.record_id = record_id
        self.VLR_body = VLR_body
        try:
            self.rec_len_after_header = len(self.VLR_body)
        except(TypeError):
            self.rec_len_after_header = 0
        if "description" in kwargs:
            self.description = str(kwargs["description"]) + "\x00"*(32-len(kwargs["description"]))
        else:
            self.description = "\x00"*32
        if "reserved" in kwargs:
            self.reserved = kwargs["reserved"]
        else:
            self.reserved = 0 
        self.reserved = 0
        self.isVLR = True
        self.fmt = util.Format("VLR")

    def build_from_reader(self, reader):
        '''Build a vlr from a reader capable of reading in the data.'''
        self.reserved = reader.read_words("reserved")
        self.user_id = "".join(reader.read_words("user_id"))
        self.record_id = reader.read_words("record_id")
        self.rec_len_after_header = reader.read_words("rec_len_after_header")
        self.description = "".join(reader.read_words("description"))
        self.VLR_body = reader.read(self.rec_len_after_header)
        ### LOGICAL CONTENT ###
        self.isVLR = True
        self.fmt = reader.vlr_formats

    def __len__(self):
        '''Return the size of the vlr object in bytes'''
        return self.rec_len_after_header + 54

    def pack(self, name, val): 
        '''Pack a VLR field into bytes.'''
        spec = self.fmt.lookup[name]
{'fraction_non_alphanumeric': 0.11609096964506613, 'fraction_numerical': 0.010086592444571319, 'mean_word_length': 2.327002215891105, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6387795', 'n_tokens_mistral': 3306, 'n_tokens_neox': 3126, 'n_words': 827}