How to search for vector in list of input arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UnOrThOdOx
    New Member
    • Mar 2012
    • 3

    How to search for vector in list of input arguments

    Hi guys,

    I am trying to write a module transfering from MATLAB. In order to avoid unnecessary errors, I do some input arguments checkings before processing. The user is allowed to put in only 3 arguments, that's why the last argument is a list instead of a fixed variable. Nevertheless, user has only 2 possibilities either he gives 3 or 4 inputs.

    The point is I'd like to check if there is any vector in the list (last argument). How could I do this? Before when it was the fixed argument, I used "isvector(d )" but it is not possible to use this with a list then I get this error.

    AttributeError: 'tuple' object has no attribute 'shape'

    Code:
    def check(a, b, c, *arg): 
    
    if len(arg) > 1:
                raise TypeError ("Incorrect number of input arguments.")
    
    if not isvector(b) or not isvector(arg):
                raise TypeError ("Transfer function must be specified by two vectors.")
    Could someone please provide me an advice how I could go around this problem?


    Thank you very much in advance
    Regards
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You could write a separate function to check the contents of the list. Using isinstance instead of isvector and checking for a str:
    Code:
    >>> def isstrin(seq):
    ... 	for item in seq:
    ... 		if isinstance(item, str):
    ... 			return True
    ... 	return False
    ... 
    >>> seq = [1,2,3,4,['a', 'b', 'c']]
    >>> isstrin(seq)
    False
    >>> seq = [1,2,"this is a str",4,['a', 'b', 'c']]
    >>> isstrin(seq)
    True
    >>>

    Comment

    Working...