Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting several variables as same type

The problem: I have a function that takes in parameters n and base, and I would like to assure that both of these parameters are in fact integers. So far I've done the following:

#  Conditions in which the function makes sense.
assert isinstance(n, (int)), 'n must be an integer. n: {0}'.format(n)
assert isinstance(base, (int)), 'base must be an integer. base: {0}'.format(base)
assert not isinstance(n, bool)

The question: This seems tedious, I would like to somehow do something similar to assert isinstance((n, base), (int, int)), 'n and base must be integers. n: {0}, base: {1}'.format(n, base). But this gives me an AssertionError at unexpected times (n and base are both int). Perhaps tuples can't be used? Is there an analogous approach that do work?

Edit: I guess an ideal handling would be to list every parameter that should be of type t, and if one or more fails, print out only the ones failing.

For completeness, under follows the entire code. I don't think it will be useful, but I might be mistaken. It is not part of anything, I was just goofing around with optional arguments after looking at another question in here.

def pascal(n, base=1):
    """Makes a dictionary where the keys are row-indexes in a pascal-trangle
    of size n, and the values are the rows as a list. E.g. pascal(3) should
    return {1 : [1], 2: [1,1], 3: [1,2,1]}.

    pascal(0) should returns an empty dictionary.

    Optional argument 'base=': set an integer as a new base. E.g.
    pascal(3, base=2) should return {1: [2], 2: [2, 2], 3: [2, 4, 2]}"""

    #  Conditions in which the function makes sense.
    #  assert isinstance((n, base), (int, int)), 'n and base must be integers. n: {0}, base: {1}'.format(n, base)
    assert isinstance(n, (int)), 'n must be an integer. n: {0}'.format(n)
    assert isinstance(base, (int)), 'base must be an integer. base: {0}'.format(base)
    assert not isinstance(n, bool)

    if not n:
        return {}
    if n == 1:
        return {1: [base]} #  The basic case.
    else:
        bottom_row = list()
        prev_p = pascal(n-1, base) #  Only do one recursive call!
        for i in range(0, n):
            if i == 0:
                bottom_row.append(prev_p[n-1][i])
            elif i == n-1:
                bottom_row.append(prev_p[n-1][i-1])
            else:
                bottom_row.append(prev_p[n-1][i-1]+prev_p[n-1][i])
        bottom_row = {n: bottom_row}
        pascal_dict = prev_p
        pascal_dict.update(bottom_row)
        return pascal_dict
like image 889
RoyM Avatar asked Jul 18 '26 20:07

RoyM


1 Answers

There isn't a vectorized isinstance.

assert isinstance((n, base), (int, int))

Should be

assert isinstance(n, int) and isinstance(base, int)

If you have a larger quantity of variables ...

for var in [n, base, count, blah, foo, bar]:
    assert isinstance(var, int)

If you don't need individual servicing on them:

assert(all(isinstance(var, int) for var in list))
like image 157
Prune Avatar answered Jul 20 '26 09:07

Prune



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!