473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

follow-up: python function taking scalar or list argument

Hi,

as a newby to python but otherwise not totally unexperienced programmer, I would like to bring up the question once more how I can best determine if a function argument is a) scalar or vector, and b) a "simple" python float or a numpy float type.

The previous post on this topic had a simple function "twice (x)" which can easily be vectorized through list comprehension, for example. In my case, I am working on a function that converts wind direction and speed to vector components u and v (and its inverse counterpart). This entails a couple of "if" checks and more than one line of code.

I wish to apply this function either in a "pocket calculator" fashion, where a user can call it with simple scalar arguments (example: print ddff_to_uv(230.,8.5)) or in other programs which might read very lengthy data vectors (for example from a weather model) nand these would typically be numpy ndarrays then.

As far as I can see, the easiest solution would be to simply package any argument into an ndarray:
Expand|Select|Wrap|Line Numbers
  1. def ddff_to_uv(dd, ff):
  2.     mydd = np.array(dd)
  3.     myff = np.array(ff)
  4.  
and then proceed without hassle. However, this seems extremly inefficient if the input is already a (very large) ndarray.

If I want to be more smart about this, I don't see a good way of testing yet:
* checking dd.size only works if dd is a numpy array or float (other types are not of interest), a common python float doesn't have a size attribute.
* testing for isinstance(dd, list) doesn't work on a numpy array.
* testing for isinstance(dd, (list, ndarray)) could work, but then there may well be other derived types coming up which I don't know about yet.
* hasattr(dd, '__iter__') would return true for both an ndarray and a python list; so this seems to point in the right direction. But I cannot use array syntax on a python list of float values, so I would need to convert them into ndarray first -- which again seems inefficient if dd already is an ndarray.

My present solution is the following function:
Expand|Select|Wrap|Line Numbers
  1. import numpy as np
  2. def isvector(arg):
  3.     res = 0
  4.     if hasattr(arg, "__iter__"): res = 1
  5.     if isinstance(arg, np.ndarray): res = 2
  6.     return res
  7.  
Thus, 2 is returned for a numpy array, 1 for any other iterable (which I then assume to be a list of numerical values), and 0 for any non-iterable (which I assume to be a scalar float (or integer) value.

Any comments or siggestions for improvement?
Oct 5 '11 #1
1 2851
bvdet
2,851 Expert Mod 2GB
I don't know much about numpy. Having said that it seems like your solution is well thought out. I can offer a slight improvement though:
Expand|Select|Wrap|Line Numbers
  1. import numpy as np
  2. def isvector(arg):
  3.     if isinstance(arg, np.ndarray):
  4.         return 2
  5.     elif hasattr(arg, "__iter__"):
  6.         return 1
  7.     return 0
Test for np.ndarray first, otherwise it will never be reached.
Oct 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Philip TAYLOR | last post by:
Configuring a new instance of IIS, I noticed that it allows an HTML-formatted document trailer to be appended to every document served. Unfortunately, on checking its behaviour, I find that it...
2
by: clusardi2k | last post by:
Hello, I fork a child process. How can I tell a debugger which process to follow? Thank you, Christopher Lusardi
2
by: Joris Gillis | last post by:
Hi everyone, I have this nasty little problem to which I can't find a solution: Consider an anchor <a name="Top" id="Top">Top</a> How can I follow/execute this link from within javascript?...
2
by: mike | last post by:
regards: I follow the following steps to converting from HTML to XHTML http://webpageworkshop.co.uk/main/xhtml_converting My parser is http://htmlparser.sourceforge.net/ Xhtml version is 1.0...
1
by: oketz1 | last post by:
Hi I am writing an application very similar to notepad and I have to implement the status bar feature, which mean that I have to follow after the cursor is there any event that raised when...
3
by: MPR | last post by:
Hi guys; I'm using an AxSHDocVw.AxWebBrowser control to show a web page. I need to know if i can ( and how? ) i do the following things: 1 - I need to follow 1 link in the webpage diplayed...
0
by: woodglass | last post by:
I want to use object.hyperlink.Follow to follow a hyperlink to a web page. The web page requests a username & password. Can I programmatically supply both instead of having to type them in ?....
3
by: Rizvi | last post by:
The Grammer is given: S ---> id=E; E ---> E+T | E-T | T T ---> T*F | T/F | F F ---> P^F | P P ---> -P | L L ---> (E) | id | num -------------------------------- I've fond the following...
0
by: 008worker | last post by:
Do you want the science article you may follow the following the link. Do you want something about science?if yes you may follow the following link.it is really intresting and useful. ...
33
by: castironpi | last post by:
I am concerned by the lack of follow-through on some responses to recent ideas I have described. Do I merely have a wrong understanding of group policy? Is it a good policy (defined with respect...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.