Best Python code snippet using pandera_python
thing.py
Source:thing.py  
1#2# Copyright (C) 2021 Vaticle3#4# Licensed to the Apache Software Foundation (ASF) under one5# or more contributor license agreements.  See the NOTICE file6# distributed with this work for additional information7# regarding copyright ownership.  The ASF licenses this file8# to you under the Apache License, Version 2.0 (the9# "License"); you may not use this file except in compliance10# with the License.  You may obtain a copy of the License at11#12#   http://www.apache.org/licenses/LICENSE-2.013#14# Unless required by applicable law or agreed to in writing,15# software distributed under the License is distributed on an16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17# KIND, either express or implied.  See the License for the18# specific language governing permissions and limitations19# under the License.20#21from abc import ABC22from typing import List, Union, TYPE_CHECKING23import typedb_protocol.common.transaction_pb2 as transaction_proto24from typedb.api.concept.thing.attribute import Attribute25from typedb.api.concept.thing.thing import Thing, RemoteThing26from typedb.common.exception import TypeDBClientException, MISSING_IID, MISSING_TRANSACTION, GET_HAS_WITH_MULTIPLE_FILTERS27from typedb.common.rpc.request_builder import thing_get_has_req, thing_get_relations_req, \28    thing_get_playing_req, thing_set_has_req, thing_unset_has_req, thing_delete_req29from typedb.concept.concept import _Concept, _RemoteConcept30from typedb.concept.proto import concept_proto_reader, concept_proto_builder31from typedb.concept.type.role_type import _RoleType32if TYPE_CHECKING:33    from typedb.api.connection.transaction import _TypeDBTransactionExtended, TypeDBTransaction34class _Thing(Thing, _Concept, ABC):35    def __init__(self, iid: str, is_inferred: bool):36        if not iid:37            raise TypeDBClientException.of(MISSING_IID)38        self._iid = iid39        self._is_inferred = is_inferred40    def get_iid(self):41        return self._iid42    def is_inferred(self) -> bool:43        return self._is_inferred44    def as_thing(self) -> "Thing":45        return self46    def __str__(self):47        return "%s[%s:%s]" % (type(self).__name__, self.get_type().get_label(), self.get_iid())48    def __eq__(self, other):49        if other is self:50            return True51        if not other or type(self) != type(other):52            return False53        return self.get_iid() == other.get_iid()54    def __hash__(self):55        return hash(self._iid)56class _RemoteThing(_RemoteConcept, RemoteThing, ABC):57    def __init__(self, transaction: Union["_TypeDBTransactionExtended", "TypeDBTransaction"], iid: str, is_inferred: bool):58        if not transaction:59            raise TypeDBClientException.of(MISSING_TRANSACTION)60        if not iid:61            raise TypeDBClientException.of(MISSING_IID)62        self._transaction_ext = transaction63        self._iid = iid64        self._is_inferred = is_inferred65        self._hash = hash((self._transaction_ext, iid))66    def get_iid(self):67        return self._iid68    def is_inferred(self) -> bool:69        return self._is_inferred70    def as_thing(self) -> "RemoteThing":71        return self72    def get_has(self, attribute_type=None, attribute_types: List = None, only_key=False):73        if [bool(attribute_type), bool(attribute_types), only_key].count(True) > 1:74            raise TypeDBClientException.of(GET_HAS_WITH_MULTIPLE_FILTERS)75        if attribute_type:76            attribute_types = [attribute_type]77        return (concept_proto_reader.attribute(a) for rp in self.stream(thing_get_has_req(self.get_iid(), concept_proto_builder.types(attribute_types), only_key))78                for a in rp.thing_get_has_res_part.attributes)79    def get_relations(self, role_types: list = None):80        if not role_types:81            role_types = []82        return (concept_proto_reader.thing(r) for rp in self.stream(thing_get_relations_req(self.get_iid(), concept_proto_builder.types(role_types)))83                for r in rp.thing_get_relations_res_part.relations)84    def get_playing(self):85        return (_RoleType.of(rt) for rp in self.stream(thing_get_playing_req(self.get_iid()))86                for rt in rp.thing_get_playing_res_part.role_types)87    def set_has(self, attribute: Attribute):88        self.execute(thing_set_has_req(self.get_iid(), concept_proto_builder.thing(attribute)))89    def unset_has(self, attribute: Attribute):90        self.execute(thing_unset_has_req(self.get_iid(), concept_proto_builder.thing(attribute)))91    def delete(self):92        self.execute(thing_delete_req(self.get_iid()))93    def is_deleted(self):94        return not self._transaction_ext.concepts().get_thing(self.get_iid())95    def execute(self, request: transaction_proto.Transaction.Req):96        return self._transaction_ext.execute(request).thing_res97    def stream(self, request: transaction_proto.Transaction.Req):98        return (rp.thing_res_part for rp in self._transaction_ext.stream(request))99    def __str__(self):100        return type(self).__name__ + "[iid:" + str(self._iid) + "]"101    def __eq__(self, other):102        if other is self:103            return True104        if not other or type(self) != type(other):105            return False106        return self._transaction_ext is other._transaction_ext and self._iid == other._iid107    def __hash__(self):...test_schema_inference.py
Source:test_schema_inference.py  
1# pylint: disable=W02122"""Unit tests for schema inference module."""3from typing import Type, Union4import pandas as pd5import pytest6import pandera as pa7from pandera import schema_inference8def _create_dataframe(9    multi_index: bool = False, nullable: bool = False10) -> pd.DataFrame:11    if multi_index:12        index = pd.MultiIndex.from_arrays(13            [[1, 1, 2], ["a", "b", "c"]],14            names=["int_index", "str_index"],15        )16    else:17        index = pd.Index([10, 11, 12], name="int_index")18    df = pd.DataFrame(19        data={20            "int": [1, 2, 3],21            "float": [1.0, 2.0, 3.0],22            "boolean": [True, False, True],23            "string": ["a", "b", "c"],24            "datetime": pd.to_datetime(["20180101", "20180102", "20180103"]),25        },26        index=index,27    )28    if nullable:29        df.iloc[0, :] = None30    return df31@pytest.mark.parametrize(32    "pandas_obj, expectation",33    [34        [pd.DataFrame({"col": [1, 2, 3]}), pa.DataFrameSchema],35        [pd.Series([1, 2, 3]), pa.SeriesSchema],36        # error cases37        [int, TypeError],38        [pd.Index([1, 2, 3]), TypeError],39        ["foobar", TypeError],40        [1, TypeError],41        [[1, 2, 3], TypeError],42        [{"key": "value"}, TypeError],43    ],44)45def test_infer_schema(46    pandas_obj,47    expectation: Type[Union[pa.DataFrameSchema, pa.SeriesSchema, TypeError]],48) -> None:49    """Test that convenience function correctly infers dataframe or series."""50    if expectation is TypeError:51        with pytest.raises(TypeError, match="^pandas_obj type not recognized"):52            schema_inference.infer_schema(pandas_obj)53    else:54        assert isinstance(55            schema_inference.infer_schema(pandas_obj), expectation56        )57@pytest.mark.parametrize(58    "multi_index",59    [False, True],60)61def test_infer_dataframe_schema(multi_index: bool) -> None:62    """Test dataframe schema is correctly inferred."""63    dataframe = _create_dataframe(multi_index=multi_index)64    schema = schema_inference.infer_dataframe_schema(dataframe)65    assert isinstance(schema, pa.DataFrameSchema)66    if multi_index:67        assert isinstance(schema.index, pa.MultiIndex)68    else:69        assert isinstance(schema.index, pa.Index)70    with pytest.warns(71        UserWarning,72        match="^This .+ is an inferred schema that hasn't been modified",73    ):74        schema.validate(dataframe)75    # modifying an inferred schema should set _is_inferred to False76    schema_with_added_cols = schema.add_columns({"foo": pa.Column(pa.String)})77    assert schema._is_inferred78    assert not schema_with_added_cols._is_inferred79    assert isinstance(80        schema_with_added_cols.validate(dataframe.assign(foo="a")),81        pd.DataFrame,82    )83    schema_with_removed_cols = schema.remove_columns(["int"])84    assert schema._is_inferred85    assert not schema_with_removed_cols._is_inferred86    assert isinstance(87        schema_with_removed_cols.validate(dataframe.drop("int", axis=1)),88        pd.DataFrame,89    )90@pytest.mark.parametrize(91    "series",92    [93        pd.Series([1, 2, 3]),94        pd.Series([1.0, 2.0, 3.0]),95        pd.Series([True, False, True]),96        pd.Series(list("abcdefg")),97        pd.Series(list("abcdefg"), dtype="category"),98        pd.Series(pd.to_datetime(["20180101", "20180102", "20180103"])),99    ],100)101def test_infer_series_schema(series: pd.Series) -> None:102    """Test series schema is correctly inferred."""103    schema = schema_inference.infer_series_schema(series)104    assert isinstance(schema, pa.SeriesSchema)105    with pytest.warns(106        UserWarning,107        match="^This .+ is an inferred schema that hasn't been modified",108    ):109        schema.validate(series)110    # modifying an inferred schema should set _is_inferred to False111    schema_with_new_checks = schema.set_checks(112        [pa.Check(lambda x: x is not None)]113    )114    assert schema._is_inferred115    assert not schema_with_new_checks._is_inferred...ped_utils.py
Source:ped_utils.py  
...32        if mom is None and dad is None:33            continue34        35        # ignore people whose parents are based on their own ped line36        # if mom._is_inferred() or dad._is_inferred():37        #     continue38        39        trio = Trio(x, dad, mom)40        # trio.add_person(x)41        # trio.add_person(mom)42        # trio.add_person(dad)43        # trio.set_mom(mom, x)44        # trio.set_dad(dad, x)45        46        trios.append(trio)47    48    return trios49def get_top_level_trios(all_trios):50    r_trios = []51    for t in all_trios:52        flag = 153        for i in t.get_parent():54            if i is not None and (i.mom != '0' or i.dad != '0'):55                flag = 056                break57        if flag == 1:58            r_trios.append(t)59    return r_trios60def get_as_parent_trios(person: Person, all_trios: List[Trio]):61    r_trios = []62    for t in all_trios:63        for i in t.get_parent():64            if i.id == person.id:65                if t not in r_trios:66                    r_trios.append(t)67    return r_trios68def get_as_child_trio(person: Person, trios: List[Trio]):69    for t in trios:70        if t.get_child().id == person.id:71            return t72def get_next_level_trios(all_trios: List[Trio], current_level_trios: List[Trio]) -> List[Trio]:73    r_trios = []74    for t in current_level_trios:75        n_l_t = get_as_parent_trios(t.child, all_trios)76        for item in n_l_t:77            if item not in r_trios:78                r_trios.append(item)79    return r_trios80def get_bottom_level_trios(all_trios):81    r_trios = []82    for t in all_trios:83        if len(get_as_parent_trios(t.child, all_trios)) == 0:84            r_trios.append(t)85    return r_trios86def get_prev_level_trios(all_trios: List[Trio], current_level_trios: List[Trio]) -> List[Trio]:87    r_trios = []88    for t in current_level_trios:89        for i in t.get_parent():90            p_t = get_as_child_trio(i, all_trios)91            if p_t is not None:92                r_trios.append(p_t)93    return r_trios94def get_probands(family):95    """ find probands within a Family96    97    Returns:98        list of probands (as peds.Person objects)99    """100    probands = []101    for x in family:102        # arbitrarily define probands as individuals who are affected, and do103        # not have any children in the family. This could be a bit looser,104        # to cope with multigenerational familes, but will do for now.105        if x.is_affected():106            if len(list(family.get_children(x))) == 0:107                probands.append(x)108            else:109                mom = family.get_mom(x)110                dad = family.get_dad(x)111                112                if mom is None and dad is None:113                    continue114                115                # ignore people whose parents are based on their own ped line116                if mom._is_inferred() or dad._is_inferred():117                    continue118                119                probands.append(x)120    ...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
