Exemple #1
0
 def learn(symbol, data=None, file=None, tell=False):
     if file: data = unpackbits(fromfile(file, dtype='ubyte'))
     check, to = None, 0.
     if tell: check = tell * len(data)
     train, limit = symbol.train, symbol.limit
     set, coded, time = [], [], symbol.time
     while time:
         set.append(list())
         time -= 1
     for n, bit in enumerate(data.tolist()):
         if tell and n % check == 0:
             print '{:.0%}|'.format(to),
             to += tell
         if symbol.same: symbol._make([[n % symbol.bits]] + set[1:])
         else: symbol._make(set)
         coded.append(symbol())
         symbol + (bit, train, limit)
         base = set[0]
         if len(base) == symbol.bits:
             if symbol.blur: base = symbol.blur.resize(base)
             set.insert(1, packbits(base).tolist())
             set.pop(-1)
             set[0] = [bit]
         else:
             base.append(bit)
     symbol._last = data
     return dict(code=record(coded, by=data),
                 error=record(abs(coded - data), by=data))
def namespace(sharing=None, owner=None, app=None, **kwargs):
    """This function constructs a Splunk namespace.

    Every Splunk resource belongs to a namespace. The namespace is specified by
    the pair of values ``owner`` and ``app`` and is governed by a ``sharing`` mode.
    The possible values for ``sharing`` are: "user", "app", "global" and "system",
    which map to the following combinations of ``owner`` and ``app`` values:

        "user"   => {owner}, {app}

        "app"    => nobody, {app}

        "global" => nobody, {app}

        "system" => nobody, system

    "nobody" is a special user name that basically means no user, and "system"
    is the name reserved for system resources.

    "-" is a wildcard that can be used for both ``owner`` and ``app`` values and
    refers to all users and all apps, respectively.

    In general, when you specify a namespace you can specify any combination of
    these three values and the library will reconcile the triple, overriding the
    provided values as appropriate.

    Finally, if no namespacing is specified the library will make use of the
    ``/services`` branch of the REST API, which provides a namespaced view of
    Splunk resources equivelent to using ``owner={currentUser}`` and
    ``app={defaultApp}``.

    The ``namespace`` function returns a representation of the namespace from
    reconciling the values you provide. It ignores any keyword arguments other
    than ``owner``, ``app``, and ``sharing``, so you can provide ``dicts`` of
    configuration information without first having to extract individual keys.

    :param sharing: The sharing mode (the default is "user").
    :type sharing: "system", "global", "app", or "user"
    :param owner: The owner context (the default is "None").
    :type owner: ``string``
    :param app: The app context (the default is "None").
    :type app: ``string``
    :returns: A :class:`splunklib.data.Record` containing the reconciled
        namespace.

    **Example**::

        import splunklib.binding as binding
        n = binding.namespace(sharing="user", owner="boris", app="search")
        n = binding.namespace(sharing="global", app="search")
    """
    if sharing in ["system"]:
        return record({'sharing': sharing, 'owner': "nobody", 'app': "system" })
    if sharing in ["global", "app"]:
        return record({'sharing': sharing, 'owner': "nobody", 'app': app})
    if sharing in ["user", None]:
        return record({'sharing': sharing, 'owner': owner, 'app': app})
    raise ValueError("Invalid value for argument: 'sharing'")
Exemple #3
0
def namespace(sharing=None, owner=None, app=None, **kwargs):
    """This function constructs a Splunk namespace.

    Every Splunk resource belongs to a namespace. The namespace is specified by
    the pair of values ``owner`` and ``app`` and is governed by a ``sharing`` mode.
    The possible values for ``sharing`` are: "user", "app", "global" and "system",
    which map to the following combinations of ``owner`` and ``app`` values:

        "user"   => {owner}, {app}

        "app"    => nobody, {app}

        "global" => nobody, {app}

        "system" => nobody, system

    "nobody" is a special user name that basically means no user, and "system"
    is the name reserved for system resources.

    "-" is a wildcard that can be used for both ``owner`` and ``app`` values and
    refers to all users and all apps, respectively.

    In general, when you specify a namespace you can specify any combination of
    these three values and the library will reconcile the triple, overriding the
    provided values as appropriate.

    Finally, if no namespacing is specified the library will make use of the
    ``/services`` branch of the REST API, which provides a namespaced view of
    Splunk resources equivelent to using ``owner={currentUser}`` and
    ``app={defaultApp}``.

    The ``namespace`` function returns a representation of the namespace from
    reconciling the values you provide. It ignores any keyword arguments other
    than ``owner``, ``app``, and ``sharing``, so you can provide ``dicts`` of
    configuration information without first having to extract individual keys.

    :param sharing: The sharing mode (the default is "user").
    :type sharing: "system", "global", "app", or "user"
    :param owner: The owner context (the default is "None").
    :type owner: ``string``
    :param app: The app context (the default is "None").
    :type app: ``string``
    :returns: A :class:`splunklib.data.Record` containing the reconciled
        namespace.

    **Example**::

        import splunklib.binding as binding
        n = binding.namespace(sharing="user", owner="boris", app="search")
        n = binding.namespace(sharing="global", app="search")
    """
    if sharing in ["system"]:
        return record({'sharing': sharing, 'owner': "nobody", 'app': "system" })
    if sharing in ["global", "app"]:
        return record({'sharing': sharing, 'owner': "nobody", 'app': app})
    if sharing in ["user", None]:
        return record({'sharing': sharing, 'owner': owner, 'app': app})
    raise ValueError("Invalid value for argument: 'sharing'")
Exemple #4
0
 def learn(symbol, data=None, file=None, tell=False):
     if file: data = unpackbits(fromfile(file, dtype = 'ubyte'))
     check, to = None, 0.
     if tell: check = tell*len(data)
     train, limit = symbol.train, symbol.limit
     set, coded, time = [], [], symbol.time
     while time: set.append(list()); time -= 1
     for n, bit in enumerate(data.tolist()):
         if tell and n%check==0: print '{:.0%}|'.format(to),; to+=tell
         if symbol.same: symbol._make([[n%symbol.bits]]+set[1:])
         else: symbol._make(set)
         coded.append(symbol())
         symbol + (bit, train, limit)
         base = set[0]
         if len(base) == symbol.bits:
             if symbol.blur: base = symbol.blur.resize(base)
             set.insert(1, packbits(base).tolist())
             set.pop(-1)
             set[0] = [bit]
         else: base.append(bit)
     symbol._last = data
     return dict(code=record(coded, by=data), error=record(abs(coded-data), by=data))
Exemple #5
0
 def load(this, store=['subject', 'channels', 'units', 'sampling'], **data):
     find = default(data)
     tell = find(True, 'verbose')
     if this.hasnt('start'): this.open(verbose=tell)
     if this.has('start', 'sampling', 'time', 'channels'):
         start = find(this.time[0], 'start', 'at')
         end = find(this.time[1], 'end')
         gap = find(None, 'time', 'span')
         use = find(float32, 'type')
         if type(start) is time: start = this.time[0] + start
         init = start - this.time[0]
         if gap and type(gap) is time: end = start + gap
         if end > this.time[1]: end = this.time[1]
         gap = end - start
         init = int(init.seconds * this.sampling) + this.start[1]
         ticks = int(gap.seconds * this.sampling)
         channels = len(this.channels)
         with open(this.file, 'r') as file:
             if tell: print 'seeking record start...',
             this._seek(file, init)
             if tell: print 'done\nreading lines...',
             lines = [[0] * channels]
             last = lines[-1]
             while ticks:
                 line = file.readline()
                 if line == '': break
                 line = array([t for t in line.split('\t')
                               if t != 'OFF'][2:-1])
                 if len(line) == channels:
                     line[line == 'SHORT'] = nan
                     line[line == 'AMPSAT'] = nan
                     if nan in line:
                         to = correct = line.tolist()
                         while nan in correct:
                             position = to.index(nan)
                             correct[position] = last[position]
                         line = array(correct)
                     lines.append(line.astype(use))
                     last = lines[-1]
                     ticks -= 1
             if tell: print 'done'
             data = create(record(lines[1:]).T,
                           template=this,
                           start=start,
                           end=end,
                           gap=ticks / this.sampling)
             data.clear(*list(this.sets - set(store)))
             return data
     elif tell:
         print 'invalid source!'
    def request(self, url, message, **kwargs):
        """Issues an HTTP request to a URL.

        :param url: The URL.
        :type url: ``string``
        :param message: A dictionary with the format as described in
            :class:`HttpLib`.
        :type message: ``dict``
        :param kwargs: Additional keyword arguments (optional). These arguments
            are passed unchanged to the handler.
        :type kwargs: ``dict``
        :returns: A dictionary describing the response (see :class:`HttpLib` for
            its structure).
        :rtype: ``dict``
        """
        response = self.handler(url, message, **kwargs)
        response = record(response)
        if 400 <= response.status:
            raise HTTPError(response)
        return response
Exemple #7
0
    def request(self, url, message, **kwargs):
        """Issues an HTTP request to a URL.

        :param url: The URL.
        :type url: ``string``
        :param message: A dictionary with the format as described in
            :class:`HttpLib`.
        :type message: ``dict``
        :param kwargs: Additional keyword arguments (optional). These arguments
            are passed unchanged to the handler.
        :type kwargs: ``dict``
        :returns: A dictionary describing the response (see :class:`HttpLib` for
            its structure).
        :rtype: ``dict``
        """
        response = self.handler(url, message, **kwargs)
        response = record(response)
        if 400 <= response.status:
            raise HTTPError(response)
        return response