コード例 #1
0
ファイル: test_schema.py プロジェクト: sobolevn/schema
def test_test():
    def unique_list(_list):
        return len(_list) == len(set(_list))

    def dict_keys(key, _list):
        return list(map(lambda d: d[key], _list))

    schema = Schema(Const(And(Use(partial(dict_keys, "index")), unique_list)))
    data = [{"index": 1, "value": "foo"}, {"index": 2, "value": "bar"}]
    assert schema.validate(data) == data

    bad_data = [{"index": 1, "value": "foo"}, {"index": 1, "value": "bar"}]
    with SE:
        schema.validate(bad_data)
コード例 #2
0
def main():
    args = docopt(__doc__)
    schema = Schema({
        '<input-file>':
        And(
            Const(os.path.exists, error='Input file should exist'),
            Const(lambda x: os.access(x, os.R_OK),
                  error='No read permissions')),
        '--output':
        And(
            Const(os.path.exists, error='Output path should exist'),
            Const(lambda x: os.access(x, os.W_OK),
                  error='No write permissions')),
        '--words-count':
        Use(int, error='Number of words must be positive integer'),
        str:
        bool
    })
    try:
        args = schema.validate(args)
    except SchemaError as e:
        print(__doc__)
        exit('Error: {}'.format(e))

    matrix_path = args['<input-file>']
    out_path = args['--output']
    is_revcomp = args['--revcomp']
    n_words = args['--words-count']

    unit_width = 300
    unit_height = 600
    draw_logo(matrix_path,
              unit_width=unit_width,
              unit_height=unit_height,
              out_path=out_path,
              revcomp=is_revcomp,
              words=n_words)
コード例 #3
0
ファイル: solution.py プロジェクト: rcj4747/AdventOfCode
def _validate_fields(passport_dict: Dict[str, str]) -> bool:
    """Validate the passport fields against a schema"""

    def validate_height(height_str: str) -> bool:
        match = re.match(r'(?P<qt>[0-9]+)(?P<units>cm|in)', height_str)
        if not match:
            return False
        height, units = match.groups()
        height = int(height)
        if units == 'cm':
            return 150 <= height <= 193
        elif units == 'in':
            return 59 <= height <= 76
        return False

    eye_colors = ('amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth')
    schema = Schema({
        'byr': And(Use(int), lambda x: 1920 <= x <= 2002,
                   error='Birth year (byr) not between 1920 & 2020 inclusive'),
        'iyr': And(Use(int), lambda x: 2010 <= x <= 2020,
                   error='Issue year (iyr) not between 2010 & 2020 inclusive'),
        'eyr': And(Use(int), lambda x: 2020 <= x <= 2030,
                   error='Expiration (eyr) year not between 2020 & 2030 inclusive'),
        'hgt': And(str, validate_height,
                   error='Height (hgt) not in range 150cm-192cm or 59in-76in'),
        'hcl': And(str, lambda x: re.match(r'^#[0-9a-f]{6}', x),
                   error='Hair color (hcl) not hex rgb'),
        'ecl': And(str, lambda x: x in ('amb', 'blu', 'brn', 'gry',
                                        'grn', 'hzl', 'oth'),
                   error=f'Eye color not in {eye_colors}'),
        'pid': And(Const(And(Use(str), lambda x: len(x) == 9)),
                   Use(int),
                   error='Passport ID (pid) not a 9-digit number'),
        Optional('cid'): And(str, error='Optional Country ID (cid) not a string'),
    })

    try:
        schema.validate(passport_dict)
    except SchemaError as se:
        pprint(passport_dict)
        print(f'{se}\n')
        return False

    return True
コード例 #4
0
    def _validate_PO(self, **kw):
        """
        validate required fields are present and validate types
        uses schema to validate and return validated kw
        """
        # following vars are used for validation
        #ISO-8601 format for date and datetime.
        rdatetime = r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(([+-]\d\d:\d\d)|Z)?$'
        toleranceList = ('AllowOverRun', 'AllowUnderrun',
                         'AllowOverrunOrUnderrun', 'ExactOnly')
        uomList = [
            'BX', 'CA', 'DZ', 'EA', 'KT', 'PR', 'PK', 'RL', 'ST', 'SL', 'TH'
        ]

        conact_schema = Schema({
            Optional("attentionTo"):
                And(lambda s: var_check(s, 35),
                    error='"attentionTo" should evaluate to varchar(35)'),
            Optional("companyName"):
                And(lambda s: var_check(s, 35),
                    error='"companyName" should evaluate to varchar(35)'),
            Optional("address1"):
                And(lambda s: var_check(s, 35),
                    error='"address1" should evaluate to varchar(35)'),
            Optional("address2"):
                And(lambda s: var_check(s, 35),
                    error='"address2" should evaluate to varchar(35)'),
            Optional("address3"):
                And(lambda s: var_check(s, 35),
                    error='"address3" should evaluate to varchar(35)'),
            Optional("city"):
                And(lambda s: var_check(s, 30),
                    error='"city" should evaluate to varchar(30)'),
            Optional("region"):
                And(lambda s: var_check(s, 3),
                    error='"region" should evaluate to varchar(3)'),
            Optional("postalCode"):
                And(lambda s: var_check(s, 10),
                    error='"postalCode" should evaluate to varchar(10)'),
            Optional("country"):
                And(lambda s: var_check(s, 2),
                    error='"country" should evaluate to varchar(2)'),
            Optional("email"):
                And(lambda s: var_check(s, 128),
                    error='"email" should evaluate to varchar(128)'),
            Optional("phone"):
                And(lambda s: var_check(s, 32),
                    error='"phone" should evaluate to varchar(32)'),
            Optional("comments"): str
        })

        quantity_schema = Schema({
            "uom": And(str, lambda s: s in uomList),
            "value": And(Use(to_decimal_4), validDecimal_12_4)
        })
        third_party_schema = Schema({
            "accountName":
                And(lambda s: var_check(s, 64),
                    error='"accountName" should evaluate to varchar(64)'),
            "accountNumber":
                And(lambda s: var_check(s, 64),
                    error='"accountNumber" should evaluate to varchar(64)'),
            "ContactDetails": conact_schema
        })
        # schema used for validating PO version 1.0.0
        v1_0_0_schema = Schema({
            "wsVersion": And(str, len),
            "id": And(str, len),
            Optional("password"): str,
            "PO": {
                "orderType":
                    And(str, lambda s: s in ('Blank', 'Sample', 'Simple', 'Configured')),
                "orderNumber":
                    And(lambda s: var_check(s, 64),
                        error='"orderNumber" should evaluate to varchar(64)'),
                "orderDate":
                    And(Const(Use(fromisoformat)),
                        Regex(r'{}'.format(rdatetime))),
                Optional("lastModified"):
                    And(Const(Use(fromisoformat)),
                        Regex(r'{}'.format(rdatetime))),
                "totalAmount": And(Use(to_decimal_4), validDecimal_12_4),
                Optional("paymentTerms"):  str,
                "rush": Use(xml_bool),
                "currency":
                    And(lambda s: var_check(s, 3),
                        error='"currency" should evaluate to varchar(3)'),
                Optional("DigitalProof"): {
                    "DigitalProofAddressArray": [{
                        "DigitalProofAddress": {
                            "type":
                                And(lambda s: var_check(s, 64),
                                    error='"type" should evaluate to varchar(64)'),
                            "email":
                                And(lambda s: var_check(s, 128),
                                    error='"email" should evaluate to varchar(128)'
                                    ),
                            "lineItemGroupingId": int
                        }
                    }],
                    "required": Use(xml_bool)
                },
                Optional("OrderContactArray"): [{
                    "Contact": {
                        Optional("accountName"):
                            And(lambda s: var_check(s, 64),
                                error='"accountName" should evaluate to varchar(64)'),
                        Optional("accountNumber"):
                            And(lambda s: var_check(s, 64),
                                error='"accountNumber" should evaluate to varchar(64)'),
                        "contactType":
                            And(str, lambda s: s in [
                                    'Art', 'Bill', 'Expeditor', 'Order', 'Sales',
                                    'Ship', 'Sold'
                                ]),
                        "ContactDetails": conact_schema
                    }
                }],
                "ShipmentArray": [{
                    "Shipment": {
                        Optional("shipReferences"):
                            [lambda s: var_check(s, 64)],
                        Optional("comments"): str,
                        Optional("ThirdPartyAccount"): third_party_schema,
                        "allowConsolidation": Use(xml_bool),
                        "blindShip": Use(xml_bool),
                        "packingListRequired": Use(xml_bool),
                        "FreightDetails": {
                            "carrier":
                            And(lambda s: var_check(s, 64),
                                error='"carrier" should evaluate to varchar(64)'
                                ),
                            "service":
                            And(lambda s: var_check(s, 64),
                                error='"service" should evaluate to varchar(64)'
                                )
                        },
                        "ShipTo": {
                            "customerPickup": Use(xml_bool),
                            "ContactDetails": conact_schema,
                            "shipmentId": int
                        }
                    }
                }],
                "LineItemArray": [{
                    "LineItem": {
                        "lineNumber": int,
                        Optional("lineReferenceId"):
                            And(lambda s: var_check(s, 64),
                                error='"lineReferenceId" should evaluate to varchar(64)'),
                        "description": str,
                        "lineType": And(str, lambda s: s in ['New', 'Repeat', 'Reference']),
                        Optional("Quantity"): quantity_schema,
                        Optional("fobid"):
                            And(lambda s: var_check(s, 64),
                                error='"fobid" should evaluate to varchar(64)'),
                        "ToleranceDetails": {
                            Optional('uom'): And(str, lambda s: s in uomList),
                            Optional("value"): And(Use(to_decimal_4), validDecimal_12_4),
                            "tolerance": And(str, lambda s: s in toleranceList)
                        },
                        "allowPartialShipments": Use(xml_bool),
                        Optional("unitPrice"): And(Use(to_decimal_4), validDecimal_12_4),
                        "lineItemTotal": And(Use(to_decimal_4), validDecimal_12_4),
                        Optional("requestedShipDate"):
                            And(Const(Use(fromisoformat)),
                                Regex(r'{}'.format(rdatetime))),
                        Optional("requestedInHands"):
                            And(Const(Use(fromisoformat)),
                                Regex(r'{}'.format(rdatetime))),
                        Optional("referenceSalesQuote"):
                            And(lambda s: var_check(s, 64),
                                error='"referenceSalesQuote" should evaluate to varchar(64)'),
                        Optional("Program"): {
                            Optional("id"):
                                And(lambda s: var_check(s, 64),
                                    error='"id" should evaluate to varchar(64)'),
                            Optional("name"):
                                And(lambda s: var_check(s, 64),
                                    error='"name" should evaluate to varchar(64)')
                        },
                        Optional("endCustomerSalesOrder"):
                            And(lambda s: var_check(s, 64),
                                error= '"endCustomerSalesOrder" should evaluate to varchar(64)'),
                        Optional("productId"):
                            And(lambda s: var_check(s, 64),
                                error='"productId" should evaluate to varchar(64)'),
                        Optional("customerProductId"):
                            And(lambda s: var_check(s, 64),
                                error='"customerProductId" should evaluate to varchar(64)'),
                        Optional("lineItemGroupingId"): int,
                        Optional("PartArray"): [{
                            "Part": {
                                Optional("partGroup"):
                                    And(lambda s: var_check(s, 64),
                                        error='"partGroup" should evaluate to varchar(64)'),
                                "partId":
                                    And(lambda s: var_check(s, 64),
                                        error='"partId" should evaluate to varchar(64)'),
                                Optional("customerPartId"):
                                    And(lambda s: var_check(s, 64),
                                        error='"customerPartId" should evaluate to varchar(64)'),
                                "customerSupplied": Use(xml_bool),
                                Optional("description"): str,
                                "Quantity": quantity_schema,
                                Optional("locationLinkId"): [int],
                                Optional("unitPrice"): And(Use(to_decimal_4), validDecimal_12_4),
                                Optional("extendedPrice"): And(Use(to_decimal_4), validDecimal_12_4),
                                Optional("ShipmentLinkArray"): [{
                                    "ShipmentLink": {
                                        "Quantity": quantity_schema,
                                        "shipmentId": int
                                    }
                                }]
                            }
                        }],
                        Optional("Configuration"): {
                            Optional("ChargeArray"): [{
                                "Charge": {
                                    Optional("chargeName"):
                                        And(lambda s: var_check(s, 128),
                                            error='"chargeName" should evaluate to varchar(128)'),
                                    Optional("description"): str,
                                    Optional("extendedPrice"): And(Use(to_decimal_4), validDecimal_12_4),
                                    Optional("unitprice"): And(Use(to_decimal_4), validDecimal_12_4),
                                    "chargeId":
                                        And(lambda s: var_check(s, 64),
                                            error='"chargeId" should evaluate to varchar(64)'),
                                    "chargeType":
                                        And(str, lambda s: s in
                                            ['Freight', 'Order', 'Run', 'Setup']),
                                    "Quantity": quantity_schema
                                }
                            }],
                            Optional("LocationArray"): [{
                                "Location": {
                                    Optional("locationName"):
                                        And(lambda s: var_check(s, 128),
                                            error='"locationName" should evaluate to varchar(128)'),
                                    "DecorationArray": [{
                                        "Decoration": {
                                            Optional("decorationName"):
                                                And(lambda s: var_check(s, 128),
                                                    error='"decorationName" should evaluate to varchar(128)'),
                                            "Artwork": {
                                                Optional("instructions"): str,
                                                Optional("refArtworkId"):
                                                    And(lambda s: var_check(s, 64),
                                                        error='"refArtworkId" should evaluate to varchar(64)'),
                                                Optional("totalStitchCount"): int,
                                                Optional("ArtworkFileArray"): [{
                                                    "ArtworkFile": {
                                                        "artworkType":
                                                            And(str, lambda s: s in [
                                                                'ProductionReady', 'VirtualProof',
                                                                'SupplierArtTemplate', 'NonProductionReady'
                                                            ]),
                                                        "fileLocation":
                                                            And(lambda s: var_check(s, 1024),
                                                                error='"fileLocation" should evaluate to varchar(1024)'),
                                                        "fileName":
                                                            And(lambda s: var_check(s, 256),
                                                                error='"fileName" should evaluate to varchar(256)'),
                                                        "transportMechanism":
                                                            And(str, lambda s: s in [
                                                                'Email', 'Url', 'Ftp', 'ArtworkToFollow'
                                                            ]),
                                                    }
                                                }],
                                                Optional("description"): str,
                                                Optional("Dimensions"): {
                                                    Optional("diameter"):
                                                        And(Use(to_decimal_4),validDecimal_12_4),
                                                    Optional("height"):
                                                        And(Use(to_decimal_4),validDecimal_12_4),
                                                    Optional('uom'):
                                                        And(str, lambda s: s in uomList),
                                                    Optional("width"):
                                                        And(Use(to_decimal_4),validDecimal_12_4),
                                                    "useMaxLocationDimensions": Use(xml_bool),
                                                    "geometry":
                                                        And(str, lambda s: s in [
                                                            'Circle',
                                                            'Other',
                                                            'Rectangle'
                                                        ])
                                                },
                                                Optional("Layers"): {
                                                    "colorSystem":
                                                        And(
                                                            str, lambda s: s in [
                                                                'Cmyk', 'Other',
                                                                'Pms', 'Rgb',
                                                                'Thread'
                                                            ]),
                                                    "LayerOrStopArray": [{
                                                        "LayerOrStop":{
                                                        "color":
                                                            And(lambda s: var_check(s, 64),
                                                                error='Layer "color" should evaluate to varchar(64)'),
                                                        "nameOrNumber":
                                                            And(lambda s: var_check(s, 64),
                                                                error='"nameOrNumber" should evaluate to varchar(64)'),
                                                        "description": str
                                                        }}
                                                    ]
                                                },
                                                Optional("TypesetArray"): [{
                                                    "Typeset":{
                                                        Optional("fontSize"): Use(Decimal),
                                                        Optional("font"):
                                                            And(lambda s: var_check(s, 64),
                                                                error='"font" should evaluate to varchar(64)'),
                                                        "sequenceNumber": int,
                                                        "value":
                                                            And(lambda s: var_check(s, 1024),
                                                            error='Typset "value" should evaluate to varchar(1024)')
                                                    }
                                                }]
                                            },
                                            "decorationId": int
                                        }
                                    }],
                                    "locationLinkId": int,
                                    "locationId": int
                                }
                            }],
                            Optional("referenceNumberType"):
                                And(
                                    str, lambda s: s in [
                                        'PurchaseOrder', 'SalesOrder',
                                        'JobOrWorkOrder'
                                    ]),
                            Optional("referenceNumber"):
                                And(lambda s: var_check(s, 64),
                                    error='"referenceNumber" should evaluate to varchar(64)'),
                            "preProductionProof": Use(xml_bool),
                        }
                    }
                }],
                "termsAndConditions": str,
                Optional("salesChannel"):
                    And(lambda s: var_check(s, 64),
                        error='"salesChannel" should evaluate to varchar(64)'),
                Optional("promoCode"):
                    And(lambda s: var_check(s, 64),
                        error='"promoCode" should evaluate to varchar(64)'),
                Optional("TaxInformationArray"): [{
                    "TaxInformation": {
                        "taxJurisdiction":
                            And(lambda s: var_check(s, 64),
                                error='"taxJurisdiction" should evaluate to varchar(64)'),
                        "taxExempt": Use(xml_bool),
                        "taxId":
                            And(lambda s: var_check(s, 64),
                                error='"taxId" should evaluate to varchar(64)'),
                        "taxType":
                            And(lambda s: var_check(s, 64),
                                error='"taxType" should evaluate to varchar(64)'),
                        Optional("taxAmount"): Use(Decimal)
                    }
                }]
            }
        })

        # run the validation
        v1_0_0_schema.validate(kw)
コード例 #5
0
                f"    user_data2 is \"{self.user_data['user_data2']}\"")
        if not udat:
            udat.append(f"    No user data has been defined")
        retval.extend(udat)
        return "\n".join(retval) + "\n"


schema = Schema({
    "name":
    And(str, len),
    "target_addr":
    Use(ipaddress.ip_address),
    "target_port":
    And(Use(int), lambda n: 0 <= n <= 65535),
    "frequency":
    Or(callable, Const(And(Use(float), lambda n: 0 < n))),
    "length":
    Or("none", And(Use(int), lambda n: 0 < n)),
    "source":
    Or(callable, Use(to_source), Use(from_callable)),
    "total":
    Or("infinity", And(Use(int), lambda n: 0 < n)),
    Optional("delay"):
    And(Or(int, float), Use(float), lambda f: f > 0.0),
    Optional("user_data1"):
    str,
    Optional("user_data2"):
    str,
})

コード例 #6
0
def segmentation_start():
    args = docopt(__doc__)
    if args['--test']:
        args['<file>'] = os.path.join(os.path.dirname(__file__), 'tests/test.tsv')

    schema = Schema({
        '<file>': And(
            Const(os.path.exists, error='Input file should exist'),
            Use(open, error='Input file should be readable'),
            Use(lambda x: parse_input_file(x, int(args['--allele_reads_tr']), args["--force-sort"]),
                error='Wrong input file format')
        ),
        '--boundary-penalty': Use(
            lambda x: float(x), error='Boundary penalty coefficient should be non negative integer'
        ),
        '--badmap': Or(
            Const(lambda x: x is None and not args['visualize']),
            And(
                Const(os.path.exists, error='Badmap file should exist'),
                Const(lambda x: os.access(x, os.R_OK), error='No read permission for badmap file')
            )),
        '--output': And(
            Const(os.path.exists, error='Output path should exist'),
            Const(lambda x: os.access(x, os.W_OK), error='No write permissions')
        ),
        '--states': Use(
            check_states, error='''Incorrect value for --states.
            Must be "," separated list of numbers or fractions in the form "x/y", each >= 1'''
        ),
        '--allele_reads_tr': Use(int, error='Allelic reads threshold must be integer'),
        str: bool
    })
    try:
        args = schema.validate(args)
    except SchemaError as e:
        print(__doc__)
        exit('Error: {}'.format(e))

    snps_collection, chromosomes_order, full_name = args['<file>']
    file_name = os.path.splitext(os.path.basename(full_name))[0]
    if not args['visualize']:
        badmap_file_path = args['--output']
        if os.path.isdir(badmap_file_path):
            badmap_file_path += file_name + '.bed'

        verbose = not args['--quiet']
        mode = 'corrected'
        t = time.perf_counter()
        GS = GenomeSegmentator(snps_collection=snps_collection,
                               chromosomes_order=chromosomes_order,
                               out=badmap_file_path,
                               segmentation_mode=mode,
                               states=args['--states'],
                               b_penalty=args['--boundary-penalty'],
                               verbose=verbose,
                               allele_reads_tr=args['--allele_reads_tr']
                               )
        try:
            GS.estimate_BAD()
        except Exception as e:
            raise e
        print('Total time: {} s'.format(time.perf_counter() - t))
    else:
        badmap_file_path = args['--badmap']
    if args['--visualize'] or args['visualize']:
        init_from_snps_collection(snps_collection=snps_collection, BAD_file=badmap_file_path)
コード例 #7
0
ファイル: configuration.py プロジェクト: CSCI-2952-F/qbox
# Messages in `onFailure` will always inherit headers/bodies from their parent message - if headers
# and bodies are specified under `onFailure`, then headers will be upserted and bodies will be
# overwritten.
#
# Messages in `matchSuccessRequest` are what responses are compared to to mark the transaction
# as succeeded. Any response that does not match that criteria is automatic grounds for the
# transaction as a whole to fail.

COMPENSATING_TRANSACTION_SCHEMA = And(
    Const(
        HTTP_REQUEST_SCHEMA,
        Schema(
            {
                "timeout":
                And(int, lambda timeout: timeout >= 0),
                Optional("maxRetriesOnTimeout"):
                And(int, lambda maxRetries: maxRetries >= 0),
                "isSuccessIfReceives":
                Schema([HTTP_RESPONSE_SCHEMA]),
            },
            ignore_extra_keys=True,
        ),
    ), )

TRANSACTION_SCHEMA = And(
    Const(
        HTTP_REQUEST_SCHEMA,
        Schema(
            {
                "timeout":
                And(int, lambda timeout: timeout >= 0),
                Optional("maxRetriesOnTimeout"):