How to use __attrs_post_init__ method in hypothesis

Best Python code snippet using hypothesis

markov.py

Source:markov.py Github

copy

Full Screen

...27 model = attr.ib(default=None)28 order = attr.ib(default=None)29 smoothing = attr.ib(default=None)30 settings = attr.ib(default=config.MarkovSettings())31 def __attrs_post_init__(self):32 if not isinstance(self.settings, config.MarkovSettings):33 self.settings = config.MarkovSettings()34 self.model = self.settings.model if self.model is None else self.model35 self.order = self.settings.order if self.order is None else self.order36 self.smoothing = (37 self.settings.smoothing if self.smoothing is None else self.smoothing38 )39@attr.s40class MarkovWord(Markov):41 """42 Use 2nd or 3rd order ngrams (1st or 2nd order dependency) to calculate word entropies.43 Notes44 -----45 Markov model is trained from list of tokens at time of construction.46 Entropy or entropy lists are calculated from the trained Markov model.47 Parameters48 ----------49 tokens : [[str]]50 list of tokens, each token a list of character segments51 tokens/words used to fit the Markov model.52 model : str, optional53 Markov word model option from nltk. The default is "kni".54 kni = Kneser Ney interpolated,55 wbi = Witten Bell interpolated56 lp = Laplace,57 ls = Lidstone,58 ml = Maximum likelihood without smoothing.59 order : int, optional60 ngram order of model which counts symbol emitted and number61 of conditioning symbols. The default is 3.62 smoothing : float, optional63 Smoothing quantity for use by 'kni' or 'ls'. The default is 0.5.64 """65 tokens = attr.ib(default=[], repr=False)66 def __attrs_post_init__(self):67 super().__attrs_post_init__()68 # NLTK recommended padding explicitly for training.69 train, vocab = pp.padded_everygram_pipeline(self.order, self.tokens)70 # Define the vocabulary. Allow for out of vocabulary, thus cutoff=2.71 vocab = lm.Vocabulary(vocab, unk_cutoff=2)72 # Define and then train the language model.73 options = {74 "kni": (lm.KneserNeyInterpolated, {"discount": self.smoothing}),75 "wbi": (lm.WittenBellInterpolated, {}),76 "lp": (lm.Laplace, {}),77 "ls": (lm.Lidstone, {"gamma": self.smoothing}),78 "mle": (lm.MLE, {}),79 }80 self.lm = options[self.model][0](81 order=self.order, vocabulary=vocab, **options[self.model][1]82 )83 self.lm.fit(train)84 def calculate_entropies(self, tokens):85 """86 Calculate entropies for list of tokens.87 Parameters88 ----------89 tokens : [[str]]90 list of tokens, each token a list of characters.91 Returns92 -------93 entropies : [float]94 calclated token entropies.95 """96 # Verified by experiment - use order for left and order 2 for right.97 # Use of order 2 for right stops calculation of first pad symbol.98 # This is in order to be consistent with neural imlementation.99 padded_tokens = [100 list(101 pp.pad_both_ends(102 pp.pad_both_ends(token, pad_right=False, n=self.order),103 pad_left=False,104 n=2,105 )106 )107 for token in tokens108 ]109 # Convert lists of padded tokens to lists of ngrams.110 ngrams_lst = [list(ngrams(seq, self.order)) for seq in padded_tokens]111 entropies = [self.lm.entropy(segs) for segs in ngrams_lst]112 return entropies113 def calculate_entropy(self, token):114 """115 Calculate per sound average entropy of single token.116 Parameters117 ----------118 token : [str]119 Token as list of character segments.120 Returns121 -------122 float123 calcalated token entropy.124 """125 return self.calculate_entropies([token])[0]126@attr.s127class DualMarkov(Markov):128 """129 Construct Markov models of native and loan distributions.130 Notes131 -----132 Fit dual Markov models - one to native data, another to loan data.133 Parameters134 ----------135 data : [[str, [str], int]]136 List of language tokens in format:137 identifier,138 token as list of character segments,139 binary (0, 1) indicator of borrowed word status.140 model : str, optional141 Model type from MarkovWord class. The default is 'kni'.142 order : int, optional143 Order from MarkovWord class. The default is 3.144 smoothing : float, optional145 smoothing from MarkovWord class. The default is 0.5.146 """147 def __attrs_post_init__(self):148 super().__attrs_post_init__()149 nativetokens = [token for _, token, status in self.data if status == 0]150 self.native = MarkovWord(151 tokens=nativetokens,152 data=self.data,153 model=self.model,154 order=self.order,155 smoothing=self.smoothing,156 )157 loantokens = [token for _, token, status in self.data if status == 1]158 self.loan = MarkovWord(159 tokens=loantokens,160 data=self.data,161 model=self.model,162 order=self.order,163 smoothing=self.smoothing,164 )165 def predict_tokens(self, tokens):166 """167 Predict loan word statuses for a list of tokens.168 Parameters169 ----------170 tokens : [[str]]171 List of tokens as list of character segments.172 Returns173 -------174 predictions : int (binary 0,1)175 List of loan word predictions corresponding to tokens.176 """177 nativeentropies = self.native.calculate_entropies(tokens)178 loanentropies = self.loan.calculate_entropies(tokens)179 predictions = [180 int(loanS < nativeS)181 for nativeS, loanS in zip(nativeentropies, loanentropies)182 ]183 return predictions184 def predict_data(self, data):185 ids = [row[0] for row in data]186 tokens = [row[1] for row in data]187 predictions = self.predict_tokens(tokens)188 return list(zip(ids, tokens, predictions))189 def predict(self, token):190 # Insert '' for identifer before invoking predict_data191 return self.predict_data([["", token]])[0]192@attr.s193class NativeMarkov(Markov):194 """195 Unsupervised language model approach.196 """197 p = attr.ib(default=None)198 def __attrs_post_init__(self):199 super().__attrs_post_init__()200 self.p = self.settings.p if self.p is None else self.p201 nativetokens = [token for _, token, status in self.data if not status]202 self.nativemodel = MarkovWord(203 tokens=nativetokens,204 data=self.data,205 model=self.model,206 order=self.order,207 smoothing=self.smoothing,208 )209 nativeentropies = self.nativemodel.calculate_entropies(nativetokens)210 entropies = sorted(nativeentropies)211 idx = min((len(entropies) - 1) * self.p, len(entropies) - 1)212 self.ref_limit = (entropies[math.floor(idx)] + entropies[math.ceil(idx)]) / 2213 def predict_tokens(self, tokens):...

Full Screen

Full Screen

nodes.py

Source:nodes.py Github

copy

Full Screen

...30@attr.s31class Literal(Expression):32 type: Optional[str] = attr.ib()33 value: Union[str, list] = attr.ib()34 def __attrs_post_init__(self):35 self.value = escaped_name(self.value)36@attr.s37class Value(Expression):38 value: str = attr.ib()39 def __attrs_post_init__(self):40 self.value = escaped_name(self.value)41@attr.s42class LiteralList(Expression):43 type: str = attr.ib()44 value: List[Value] = attr.ib(factory=list)45@attr.s46class ExtendedAttribute(Ast):47 name: str = attr.ib()48 arguments: List[str] = attr.ib(factory=list)49 rhs: Optional[Union[Literal, LiteralList]] = attr.ib(default=None)50 type: str = attr.ib(default='extended-attribute')51@attr.s52class IdlType(Ast):53 idl_type: Union[List['IdlType'], 'IdlType', str] = attr.ib()54 type: Optional[str] = attr.ib(default=None)55 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)56 nullable: bool = attr.ib(default=False)57 union: bool = attr.ib(default=False)58 generic: str = attr.ib(default='')59 def __attrs_post_init__(self):60 self.idl_type = escaped_name(self.idl_type)61@attr.s62class Argument(Ast):63 name: str = attr.ib()64 idl_type: IdlType = attr.ib()65 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)66 default: Optional[Union[Literal, Value]] = attr.ib(default=None)67 type: str = attr.ib(default='argument')68 optional: bool = attr.ib(default=False)69 variadic: bool = attr.ib(default=False)70 def __attrs_post_init__(self):71 self.name = escaped_name(self.name)72@attr.s73class Operation(Member):74 name: str = attr.ib()75 idl_type: Optional[IdlType] = attr.ib()76 arguments: List[Argument] = attr.ib(factory=list)77 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)78 type: str = attr.ib(default='operation')79 special: str = attr.ib(default='')80@attr.s81class Iterable_(Member): # pylint: disable=invalid-name82 idl_type: List[IdlType] = attr.ib()83 arguments: List[Argument] = attr.ib()84 async_: bool = attr.ib()85 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)86 type: str = attr.ib(default='iterable')87 readonly: bool = attr.ib(default=False)88@attr.s89class Attribute(Member):90 idl_type: IdlType = attr.ib()91 name: str = attr.ib()92 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)93 type: str = attr.ib(default='attribute')94 readonly: bool = attr.ib(default=False)95 special: str = attr.ib(default='')96 def __attrs_post_init__(self):97 self.name = escaped_name(self.name)98@attr.s99class Const(Member):100 name: str = attr.ib()101 value: Expression = attr.ib()102 idl_type: IdlType = attr.ib()103 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)104 type: str = attr.ib(default='const')105@attr.s106class Constructor(Member):107 arguments: List[Argument] = attr.ib(factory=list)108 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)109 type: str = attr.ib(default='constructor')110@attr.s111class MapLike(Member):112 readonly: bool = attr.ib()113 idl_type: List[IdlType] = attr.ib()114 arguments: List[Argument] = attr.ib(factory=list)115 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)116 type: str = attr.ib(default='maplike')117 async_: bool = attr.ib(default=False)118@attr.s119class SetLike(Member):120 readonly: bool = attr.ib()121 idl_type: List[IdlType] = attr.ib()122 arguments: List[Argument] = attr.ib(factory=list)123 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)124 type: str = attr.ib(default='setlike')125 async_: bool = attr.ib(default=False)126@attr.s127class Field(Member):128 idl_type: IdlType = attr.ib()129 name: str = attr.ib()130 default: Optional[Literal] = attr.ib()131 required: bool = attr.ib(default=False)132 type: str = attr.ib(default='field')133 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)134@attr.s135class CallbackInterface(Definition):136 name: str = attr.ib()137 members: List[Member] = attr.ib()138 inheritance: Optional[str] = attr.ib()139 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)140 type: str = attr.ib(default='callback interface')141 partial: bool = attr.ib(default=False)142@attr.s143class Callback(Definition):144 name: str = attr.ib()145 idl_type: IdlType = attr.ib()146 arguments: List[Argument] = attr.ib(factory=list)147 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)148 type: str = attr.ib(default='callback')149@attr.s150class Interface(Definition):151 members: List[Member] = attr.ib()152 name: str = attr.ib()153 inheritance: Optional[str] = attr.ib(default=None)154 partial: bool = attr.ib(default=False)155 type: str = attr.ib(default='interface')156 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)157 def __attrs_post_init__(self):158 self.name = escaped_name(self.name)159 self.inheritance = escaped_name(self.inheritance)160@attr.s161class InterfaceMixin(Definition):162 members: List[Member] = attr.ib()163 name: str = attr.ib()164 inheritance: Optional[str] = attr.ib(default=None)165 partial: bool = attr.ib(default=False)166 type: str = attr.ib(default='interface mixin')167 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)168 def __attrs_post_init__(self):169 self.name = escaped_name(self.name)170 self.inheritance = escaped_name(self.inheritance)171@attr.s172class Enum(Definition):173 name: str = attr.ib()174 type: str = attr.ib(default='enum')175 values: List[Literal] = attr.ib(factory=list)176 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)177@attr.s178class Dictionary(Definition):179 name: str = attr.ib()180 inheritance: Optional[str] = attr.ib(default=None)181 members: List[Member] = attr.ib(factory=list)182 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)183 type: str = attr.ib(default='dictionary')184 partial: bool = attr.ib(default=False)185 def __attrs_post_init__(self):186 self.name = escaped_name(self.name)187@attr.s188class Includes(Definition):189 target: str = attr.ib()190 includes: str = attr.ib()191 type: str = attr.ib(default='includes')192 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)193 def __attrs_post_init__(self):194 self.target = escaped_name(self.target)195 self.includes = escaped_name(self.includes)196@attr.s197class Typedef(Definition):198 idl_type: IdlType = attr.ib()199 name: str = attr.ib()200 type: str = attr.ib(default='typedef')201 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)202 def __attrs_post_init__(self):203 self.name = escaped_name(self.name)204@attr.s205class Namespace(Definition):206 members: List[Member] = attr.ib()207 name: str = attr.ib()208 inheritance: Optional[str] = attr.ib(default=None)209 partial: bool = attr.ib(default=False)210 type: str = attr.ib(default='namespace')211 ext_attrs: List[ExtendedAttribute] = attr.ib(factory=list)212 def __attrs_post_init__(self):...

Full Screen

Full Screen

interface.py

Source:interface.py Github

copy

Full Screen

...14 # add project class field15 link_set:str=None16 link_id:str=None17 # with @attr.s, use __attrs_post_init__ instead of __init__18 def __attrs_post_init__(self):19 super(ProjDataInfoObject, self).__attrs_post_init__()20 21@attr.s(auto_attribs=True)22class ProjResearchDataObject(un_v1_2.ResearchArrayDataObject):23 # mapping field to extended...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful