<commit_before>from __future__ import print_function

# Standard library packages
import codecs
import json
import logging
import os
import re
import textwrap
import traceback

# get basestring in py3

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    unicode = str
    basestring = (str, bytes)
else:
    # 'unicode' exists, must be Python 2
    bytes = str
    basestring = basestring

# Required third-party packages available on PyPi:

import numpy as np

# internal lasio imports

from . import exceptions
from .las_items import (
    HeaderItem, CurveItem, SectionItems, OrderedDict)
from . import defaults
from . import reader
from . import writer

logger = logging.getLogger(__name__)


class LASFile(object):

    '''LAS file object.

    Keyword Arguments:
        file_ref (file-like object, str): either a filename, an open file 
            object, or a string containing the contents of a file.

    See :meth:`lasio.las.LASFile.read` and :func:`lasio.reader.open_file` for
    additional keyword arguments you can use here.

    '''

    def __init__(self, file_ref=None, **kwargs):

        self._text = ''
        self.index_unit = None
        default_items = defaults.get_default_items()
        self.sections = {
            'Version': default_items['Version'],
            'Well': default_items['Well'],
            'Curves': default_items['Curves'],
            'Parameter': default_items['Parameter'],
            'Other': str(default_items['Other']),
        }

        if not (file_ref is None):
            self.read(file_ref, **kwargs)

    def read(self, file_ref, null_subs=True, ignore_data=False, 
             ignore_header_errors=False, **kwargs):
        '''Read a LAS file.

        Arguments:
            file_ref (file-like object, str): either a filename, an open file 
                object, or a string containing the contents of a file.

        Keyword Arguments:
            null_subs (bool): if True, replace invalid values with np.nan
            ignore_data (bool): if True, do not read in any of the actual data, 
                just the header metadata. False by default.
            ignore_header_errors (bool): ignore LASHeaderErrors (False by 
                default)

        See :func:`lasio.reader.open_file` for additional keyword arguments you
        can use here.

        '''

        file_obj = reader.open_file(file_ref, **kwargs)
        self.raw_sections = reader.read_file_contents(
            file_obj, ignore_data=ignore_data)
        if hasattr(file_obj, "close"):
            file_obj.close()

        def add_section(pattern, name, **sect_kws):
            raw_section = self.match_raw_section(pattern)
            drop = []
            if raw_section:
                self.sections[name] = reader.parse_header_section(raw_section, 
                                                                  **sect_kws)
                drop.append(raw_section["title"])
            else:
                logger.warning("Header section %s regexp=%s was not found."
                               % (name, pattern))
            for key in drop:
                self.raw_sections.pop(key)

        add_section("~V", "Version", version=1.2, 
                    ignore_header_errors=ignore_header_errors)

        # Establish version and wrap values if possible.

        try:
            version = self.version['VERS'].value
        except KeyError:
            logger.warning('VERS item not found in the ~V section')
            version = None

        try:
            wrap = self.version['WRAP'].value
        except KeyError:
            logger.warning('WRAP item not found in the ~V section')
            wrap = None

        # Validate version
        try:
            assert version in (1.2, 2)
        except AssertionError:
            logger.warning('LAS version is %s -- neither 1.2 nor 2' % version)
            if version < 2:
                version = 1.2
            else:
                version = 2

        add_section("~W", "Well", version=version, 
                    ignore_header_errors=ignore_header_errors)

        # Establish NULL value if possible.

        try:
            null = self.well['NULL'].value
        except KeyError:
            logger.warning('NULL item not found in the ~W section')
            null = None

        add_section("~C", "Curves", version=version, 
                    ignore_header_errors=ignore_header_errors)<commit_msg>deal with missing VERS<commit_after>from __future__ import print_function

# Standard library packages
import codecs
import json
import logging
import os
import re
import textwrap
import traceback

# get basestring in py3

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    unicode = str
    basestring = (str, bytes)
else:
    # 'unicode' exists, must be Python 2
    bytes = str
    basestring = basestring

# Required third-party packages available on PyPi:

import numpy as np

# internal lasio imports

from . import exceptions
from .las_items import (
    HeaderItem, CurveItem, SectionItems, OrderedDict)
from . import defaults
from . import reader
from . import writer

logger = logging.getLogger(__name__)


class LASFile(object):

    '''LAS file object.

    Keyword Arguments:
        file_ref (file-like object, str): either a filename, an open file 
            object, or a string containing the contents of a file.

    See :meth:`lasio.las.LASFile.read` and :func:`lasio.reader.open_file` for
    additional keyword arguments you can use here.

    '''

    def __init__(self, file_ref=None, **kwargs):

        self._text = ''
        self.index_unit = None
        default_items = defaults.get_default_items()
        self.sections = {
            'Version': default_items['Version'],
            'Well': default_items['Well'],
            'Curves': default_items['Curves'],
            'Parameter': default_items['Parameter'],
            'Other': str(default_items['Other']),
        }

        if not (file_ref is None):
            self.read(file_ref, **kwargs)

    def read(self, file_ref, null_subs=True, ignore_data=False, 
             ignore_header_errors=False, **kwargs):
        '''Read a LAS file.

        Arguments:
            file_ref (file-like object, str): either a filename, an open file 
                object, or a string containing the contents of a file.

        Keyword Arguments:
            null_subs (bool): if True, replace invalid values with np.nan
            ignore_data (bool): if True, do not read in any of the actual data, 
                just the header metadata. False by default.
            ignore_header_errors (bool): ignore LASHeaderErrors (False by 
                default)

        See :func:`lasio.reader.open_file` for additional keyword arguments you
        can use here.

        '''

        file_obj = reader.open_file(file_ref, **kwargs)
        self.raw_sections = reader.read_file_contents(
            file_obj, ignore_data=ignore_data)
        if hasattr(file_obj, "close"):
            file_obj.close()

        def add_section(pattern, name, **sect_kws):
            raw_section = self.match_raw_section(pattern)
            drop = []
            if raw_section:
                self.sections[name] = reader.parse_header_section(raw_section, 
                                                                  **sect_kws)
                drop.append(raw_section["title"])
            else:
                logger.warning("Header section %s regexp=%s was not found."
                               % (name, pattern))
            for key in drop:
                self.raw_sections.pop(key)

        add_section("~V", "Version", version=1.2, 
                    ignore_header_errors=ignore_header_errors)

        # Establish version and wrap values if possible.

        try:
            version = self.version['VERS'].value
        except KeyError:
            logger.warning('VERS item not found in the ~V section.')
            version = None

        try:
            wrap = self.version['WRAP'].value
        except KeyError:
            logger.warning('WRAP item not found in the ~V section')
            wrap = None

        # Validate version.
        #
        # If VERS was missing and version = None, then the file will be read in
        # as if version were 2.0. But there will be no VERS HeaderItem, meaning
        # that las.write(..., version=None) will fail with a KeyError. But
        # las.write(..., version=1.2) will work because a new VERS HeaderItem
        # will be created.

        try:
            assert version in (1.2, 2, None)
        except AssertionError:
            if version < 2:
                version = 1.2
            else:
                version = 2
        else:
            if version is None:
                logger.info('Assuming that LAS VERS is 2.0')
                version = 2

        add_section("~W", "Well", version=version, 
                    ignore_header_errors=ignore_header_errors)

        # Establish NULL value if possible.

        try:
            null = self.well['NULL'].value
        except KeyError:
            logger.warning('NULL item not found in the ~W section')
            null = None

        add_section("~C", "Curves", version=version, 
                    ignore_header_errors=ignore_header_errors)
{'fraction_non_alphanumeric': 0.0859757395190466, 'fraction_numerical': 0.0036177910193658227, 'mean_word_length': 1.9510204081632654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4063474', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2448, 'n_words': 791}