def _init_(slef, buffer_size, batch_size): """Initializer a ReplayBuffer object. Params ===== buffer_size: maxium size of buffer batch_size: size of each training batch """ self.memory = deque(maxlen=buffer_size) # internal memory self.batch_size = batch_size self.experience = namedtuple("Experience", field_names=["state", "action", "reward", "next_state", "done"])
try: from rauth import OAuth1Session, OAuth1Service, OAuth2Session, OAuth2Service except ImportError: print("Please import Rauth:\n\n") print("http://rauth.readthedocs.org/en/latest/\n") raise try: import requests except ImportError: print("Please pip install request:\n\n") print("http://docs.python-requests.org/en/master/\n") raise ConfigProperty = namedtuple("Config", 'sandbox production') class QuickBooks(object): """A wrapper class around Python's Rauth module for Quickbooks the API""" scopes = { 'accounting': 'com.intuit.quickbooks.accounting', 'payments': 'com.intuit.quickbooks.payment' } access_token = '' access_token_secret = '' consumer_key = '' consumer_secret = '' company_id = 0 callback_url = ''
def __init__(self, dataname, datalist): self.data = namedtuple(dataname, datalist) self.compress = '' self.format = '' self.size = 0
from collection import namedtuple Rule = namedtuple('Rule', ['regexp', 'conv_func']) class Converter(object): """ Class that convert string given a structure of rules """ def __init__(self, rules): self.rules = [] for pattern, conversion in rules.iteritems(): rule = Rule(regexp=re.compile(pattern), ) self.rules.append() def convert(self, str): res = '' return res
"""Computes the convex hull of a set of 2D points. Input: an iterable sequence of (x, y) pairs representing the points. Output: a list of vertices of the convex hull in counter-clockwise order, starting from the vertex with the lexicographically smallest coordinates. Implements Andrew's monotone chain algorithm. O(n log n) complexity. """ from collection import namedtuple import matplotlib.pyploy as plt import random Point = namedtuple('Point', 'x y') class ConvexHull(object): _points = [] _hull_points = [] def __init__(self): pass def add(self, point): self._points.append(point) def _get_orientation(self, origin, p1, p2): difference = ((p2.x - origin.x) * (p1.y - origin.y)) - ((p1.x - origin.x) - (p2.y - origin.y)) return difference
class Block(collection.namedtuple('Block', ['scope', 'unit_fn', 'args'])): 'A namned tuple describing a ResNet block.'
# Example for GPT2LMHeadModel # CURRENLTY: outputs = model(input_ids) # To get certain outputs logits = outputs[0] # ...but could also be logits = outputs[1] if loss is defined past = outputs[1] # PROPOSITION: use namedtuple from collection import namedtuple # Define namedtuple Outputs = namedtuple('Outputs', ['logits', 'past', 'attentions']) # Return in forward() function of GPTLMHeadModel an namedtuple # create namedtuple # logits = [1, 2] - two logits # past = ([0,0], [1,5]) - two layers # attentions = ([7, 8], [4, 7]) outputs = Outputs(logits, past, attentions) # THEN... outputs = model(input_ids) logits = outputs[0] = outputs.logits # backward compatible and can also be accesed via outputs.logits past = outputs[1] = outputs.past # ... attentions = outputs[2] = outputs.attentions # ...
#!/usr/bin/env python # encoding: utf-8 # pylint: disable=C0111 import sys from collection import namedtuple """ generate log txt file with git log --pretty=format:"# %at %an" --name-status > log.txt Then run this script """ Change = namedtuple('Change', ['mod', 'file']) class Commit(object): def __init__(): self.date = None self.author = None self.changes = [] def main(): with open('log.txt', 'r') as input_f: for line in input_f.readlines(): if line.strip() == '': # empty line continue if line.startwith('#'): # new commit
from dataclasses import dataclass from typing import List from collection import namedtuple SKILL_TYPE = namedtuple('Skill type', 'carpentry literature power') @dataclass(frozen=True) class Skill: abilities = List[str] @dataclass(frozen=True) class Carpentry(Skill): abilities = ['hammer', 'cut', 'measure', 'build'] @dataclass(frozen=True) class Literature(Skill): abilities = ['reading', 'writing', 'vocabulary'] @dataclass(frozen=True) class Power(Skill): abilities = ['telepathy', 'flight', 'magic', 'invincibility']
from collection import defaultdict dd=defaultdict(int) for key in s: dd[key]+=1 ordereddict from collection import Ordereddict dict1=Ordereddict() from collection import namedtuple point =namedtuple('point','x,y') pt1=point(1,2) pt2=point(3,4) dot_product=(pt1.x*pt2.x) zip print zip([1,2,3,4,5,6],'Hacker') all(['a'<'b','b'<'c'])