How to use namedlist - 10 common examples

To help you get started, we’ve selected a few namedlist examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gurnec / Undo_FFG / namedlist.py View on Github external
def _fields_and_defaults(typename, field_names, default, rename):
    # field_names must be a string or an iterable, consisting of fieldname
    #  strings or 2-tuples. Each 2-tuple is of the form (fieldname,
    #  default).

    # Keeps track of the fields we're adding, with their defaults.
    fields = _Fields(default)

    # Validates field and type names.
    name_checker = _NameChecker(typename)

    if isinstance(field_names, _basestring):
        # No per-field defaults. So it's like a collections.namedtuple,
        #  but with a possible default value.
        field_names = field_names.replace(',', ' ').split()

    # If field_names is a Mapping, change it to return the
    #  (field_name, default) pairs, as if it were a list.
    if isinstance(field_names, _collections.Mapping):
        field_names = field_names.items()

    # Parse and validate the field names.
github gurnec / Undo_FFG / namedlist.py View on Github external
def _fields_and_defaults(typename, field_names, default, rename):
    # field_names must be a string or an iterable, consisting of fieldname
    #  strings or 2-tuples. Each 2-tuple is of the form (fieldname,
    #  default).

    # Keeps track of the fields we're adding, with their defaults.
    fields = _Fields(default)

    # Validates field and type names.
    name_checker = _NameChecker(typename)

    if isinstance(field_names, _basestring):
        # No per-field defaults. So it's like a collections.namedtuple,
        #  but with a possible default value.
        field_names = field_names.replace(',', ' ').split()

    # If field_names is a Mapping, change it to return the
    #  (field_name, default) pairs, as if it were a list.
    if isinstance(field_names, _collections.Mapping):
        field_names = field_names.items()

    # Parse and validate the field names.

    # field_names is now an iterable. Walk through it,
    # sanitizing as needed, and add to fields.
github niksaz / dota2-expert-demo / deepq / dqn.py View on Github external
import numpy as np
import tensorflow as tf
from namedlist import namedlist

sys.path.append('../')

from deepq import ReplayRewardShaper, Estimator, StatePreprocessor, persistence
from dotaenv import DotaEnvironment
from dotaenv.codes import ATTACK_TOWER, STATE_DIM

STATE_SPACE = STATE_DIM
ACTION_SPACE = ATTACK_TOWER + 1
MAX_PRIORITY = 10
EPS_PRIORITY = 1e-9

Transition = namedlist(
    'Transition',
    ['state', 'action', 'next_state', 'done', 'reward', 'priority'])


class PrioritizedReplayBuffer:
    """Reference paper: https://arxiv.org/pdf/1511.05952.pdf.
    """

    def __init__(self, replay_memory_size, alpha, beta0, reward_shaper, discount_factor, save_dir):
        """Initializes the replay buffer and caps the memory size to replay_memory_size.
        """
        self.replay_memory = deque(maxlen=replay_memory_size)
        self.alpha = alpha
        self.beta0 = beta0
        self.reward_shaper = reward_shaper
        self.discount_factor = discount_factor
github HanGuo97 / tf-library / TFLibrary / DDPG / ddpg.py View on Github external
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import nn_ops

from tensorflow.python.ops import variables
from tensorflow.python.ops import gradients_impl
from tensorflow.python.training import adam as adam_ops
from tensorflow.python.training import gradient_descent as gd_ops
from tensorflow.python.ops import variable_scope as vs

from tensorflow.python.framework import dtypes
from tensorflow.contrib import layers as contrib_layers


Experience = namedlist("Experience",
    ("State", "Action", "Reward", "NextState"))


class SingleEpisodeDDPGController(object):
    def __init__(self,
                 sess,
                 num_units,
                 num_actions,
                 batch_size,
                 learning_rate,
                 tau,
                 gamma,
                 actor_activation=math_ops.sigmoid,
                 critic_activation=None,
                 opitmizer_name="adam",
                 max_gradient_norm=5.0,
github Coldcard / firmware / stm32 / bootloader / secel_config.py View on Github external
def make_bitmask(name, defs):
        '''
            Name a list of bit widths and names, and convert into a class.
        '''
        rv = namedlist(name, (n for w,n in defs), default=0)

        assert sum(w for w,n in defs) == 16

        @classmethod
        def unpack(cls, ss):
            v = struct.unpack("<H", ss)[0]
            pos = 0 
            rv = {}
            for w,n in defs:
                rv[n] = (v >> pos) & ((1<<w)-1)
                pos += w
            assert pos == 16
            return cls(**rv)

        def pack(self):
            ss = 0
github antevens / cerlet / cerlet / __init__.py View on Github external
the CA has told it when to try again), output a delay size in seconds, a
            newline, and a "cookie" value, and exit with status 5.  The daemon will try
            again after the specified amount of time has passed.

        * If the CA indicates that the client needs to try again using a different
            key pair in the signing request (for example, if its policy limits the
            number of times a particular key pair can be enrolled, or the length of
            time one can be in service), exit with status 17.  The daemon will generate
            a new key pair and try again.

        * If the helper does not understand what is being asked of it, exit with
            status 6.  You should never return this value for "SUBMIT" or "POLL", but
            it is mentioned here so that we can refer to this list later.
        """
        logger.debug('Submitting certificate signing request')
        CSR = namedlist.namedlist('CSR', 'data form')
        csr = CSR(data = csr or self.environment['CERTMONGER_CSR'], form='pem')
        domains = domains or [self.environment['CERTMONGER_REQ_SUBJECT']]

        try:
            cert, chain = self.client.obtain_certificate_from_csr(domains, csr)
            sys.stdout.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert.body))
            return self.EXIT_ISSUED

        except acme.messages.Error as e:
            logger.error(e)
            sys.stdout.write(str(e))
            return self.EXIT_REJECTED

        except acme.error.ClientError as e:
            logger.error(e)
            sys.stdout.write(str(e))
github ARTFL-Project / text-pair / lib / textpair / compare_ngrams.py View on Github external
from multiprocess import Pool

from namedlist import namedlist

docIndex = namedlist("docIndex", "doc_id, ngrams, ngram_length")
indexedNgram = namedlist("indexedNgram", "n_index, start_byte, end_byte")
ngramMatch = namedlist("ngramMatch", "source target ngram")
matchingParams = namedlist(
    "matchingParams",
    """matching_window_size, max_gap, flex_gap, minimum_matching_ngrams, minimum_matching_ngrams_in_window,
    common_ngrams_limit, minimum_matching_ngrams_in_docs, context_size, banal_ngrams, merge_on_byte_distance,
    merge_on_ngram_distance, passage_distance_multiplier, duplicate_threshold, source_batch, target_batch,
    output_path, num_workers, sorting_field, debug""",
)
position = namedlist("position", [("start_byte", 0), ("end_byte", 0), ("start_ngram_index", 0), ("end_ngram_index", 0)])
Alignment = namedlist(
    "Alignment", [("source", position()), ("target", position()), ("total_matching_ngrams", 0), ("banality", False)]
)
matchValues = namedlist(
    "matchValues",
    [
        ("in_alignment", False),
        ("matches_in_current_alignment", 0),
        ("matches_in_current_window", 0),
        ("source_anchor", 0),
        ("last_source_position", 0),
        ("target_anchor", 0),
        ("last_target_position", 0),
        ("previous_source_index", 0),
        ("common_ngram_matches", 0),
        ("max_source_gap", 0),
        ("max_target_gap", 0),
github antevens / cerlet / cerlet / __init__.py View on Github external
# Set User Agent
        self.defaults['user_agent'] = user_agent

        # Override account directory
        self.defaults['accounts_dir'] = os.path.join(paths['config_dir'],
                                                     certbot.constants.ACCOUNTS_DIR,
                                                     self.environment['CERTMONGER_REQ_HOSTNAME'])

        # Create any directories which don't exist with correct
        # permisisons/owner/group and set in config
        for key, path in paths.iteritems():
            mkdirp(path, permission_mode=0o700)
            self.defaults[key] = path

        # Create config object from defaults in certbot and assign defaults
        Config = namedlist.namedlist('Config', ' '.join(self.defaults.keys()))
        self.namespace = Config(**self.defaults)
        self.config = certbot.configuration.NamespaceConfig(namespace=self.namespace)
        zope.component.provideUtility(self.config)

        # Configure displayer depending on if we have a tty or not
        if sys.stdout.isatty():
            self.displayer = certbot.display.util.NoninteractiveDisplay(sys.stdout)
        else:
            self.displayer = certbot.display.util.NoninteractiveDisplay(open(os.devnull, "w"))

        zope.component.provideUtility(self.displayer)

        # Set up Certbot Account Storage, at some point this should be moved
        # and stored by IPA in LDAP. For now we create separate accounts per
        # host and store details on the filesystem.
        account_storage = certbot.account.AccountFileStorage(self.config)
github ARTFL-Project / text-pair / lib / textpair / compare_ngrams.py View on Github external
("last_target_position", 0),
        ("previous_source_index", 0),
        ("common_ngram_matches", 0),
        ("max_source_gap", 0),
        ("max_target_gap", 0),
        ("source_window_boundary", 0),
        ("target_window_boundary", 0),
        ("current_alignment", Alignment()),
        ("previous_alignment", None),
        ("first_match", None),
        ("last_match", None),
        ("debug", False),
    ],
)
alignmentsPerDoc = namedlist("alignmentsPerDoc", "doc_id, matches, duplicates")
CombinedAlignments = namedlist("CombinedAlignments", "source_id, alignments")


class compareNgrams:
    def __init__(
        self,
        source_files,
        source_metadata,
        target_files=None,
        target_metadata=None,
        output_path="./output",
        workers=4,
        sort_field="year",
        source_batch=1,
        target_batch=1,
        source_common_ngrams=None,
        target_common_ngrams=None,
github ARTFL-Project / text-pair / lib / textpair / vector_similarity.py View on Github external
from dill import load, dump
import numpy as np
from gensim.corpora.dictionary import Dictionary
from gensim.corpora.textcorpus import TextCorpus
from gensim.models import TfidfModel
from gensim.models.phrases import Phraser, Phrases
from gensim.similarities import LevenshteinSimilarityIndex
from gensim.similarities.docsim import SparseMatrixSimilarity, MatrixSimilarity
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from namedlist import namedlist
from text_preprocessing import PreProcessor, Token, Tokens
from tqdm import tqdm

TAGS = re.compile(r"<[^>]+>")

PASSAGE_GROUP = namedlist(
    "PassageGroup", [("vector", []), ("start_byte", 0), ("end_byte", 0), ("filename", None), ("metadata", {})]
)
MERGED_GROUP = namedlist("MergedGroup", [("source", PASSAGE_GROUP()), ("target", PASSAGE_GROUP()), ("similarity", 0.0)])

PHILO_TEXT_OBJECT_LEVELS = {"doc": 1, "div1": 2, "div2": 3, "div3": 4, "para": 5, "sent": 6, "word": 7}

TEMP_DIR = os.getcwd()


def fast_cosine(X, Y):
    return np.inner(X, Y) / np.sqrt(np.dot(X, X) * np.dot(Y, Y))


class CorpusLoader(TextCorpus):
    """Base class for linking gensim's TextCorpus and the text-preprocessing output"""

namedlist

Similar to namedtuple, but instances are mutable.

Apache-2.0
Latest version published 2 years ago

Package Health Score

61 / 100
Full package analysis

Similar packages