[Tutor] Assessing local variable outside function

2016-10-28 Thread nils wagenaar
Hello,


Could i use a variable defined in a function in another function?

I have now:


def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound, LonLowBound):
 nc=netCDF4.Dataset(file)
 lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:]
 latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)
 lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound)
 data=nc.variables['Runoff'][1000, latselect, lonselect]
 return data; return latselect; return lonselect


So, i want to use latselect and lonselect in a different function where i 
interpolate for the subsetted area.


Nils
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python on gmane

2016-10-28 Thread Random832
On Thu, Oct 27, 2016, at 20:40, Jim Byrnes wrote:
> Is comp.lang.python available on gmane?
> 
> I've googled and found references to it being on gmane but I can't find 
> it there. I'd like to use gmane because Comcast doesn't do usenet
> anymore.

I don't know about the current viability of gmane in general, but it's
called "gmane.comp.python.general" on gmane.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assessing local variable outside function

2016-10-28 Thread Alan Gauld via Tutor
On 28/10/16 02:38, nils wagenaar wrote:
> Hello,
> 
> 
> Could i use a variable defined in a function in another function?

By returning it to the caller.

> def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound, LonLowBound):
>  nc=netCDF4.Dataset(file)
>  lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:]
>  latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)
>  lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound)
>  data=nc.variables['Runoff'][1000, latselect, lonselect]
>  return data; return latselect; return lonselect

The syntax for return is

return value

And you can only have one return on the line.

But value can be a tuple so to do what you want:

 return data, latselect, lonselect

And your caller can use something like


dat,lat,lon = DatasetToSubset()

The other way to do it is to create a class containing
all the functions that use the same data and put the
shared variables as instance attributes.

class LatAndLon:# think of a better name! :-)
   def __init__(self, file, lat=None, lon=None):
  self.file = file
  self.lat = lat
  self.lon = lon

   def datasetToSubset(self, .):
 nc = ...
 ...
 self.lat = ...
 self.lon = ...
 self.data = ...

   def another_function(self,...):
 if self.lat == 42:
self.process(data)
 elis self.lon > 66:
self.format()
 #etc...

Then create an instance and call the methods as needed

vals = LatAndLon()
vals.datasetToSubset()
vals.another_function()

That way the data stays outside the global namespace
but the functions that use it can all see it.
You will likely find that this greatly reduces the
number of params you need to pass to each function
since you set them up once, when you create the
instance, and don't need to keep passing them
into the functions. This makes the code easier
to write and maintain.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assessing local variable outside function

2016-10-28 Thread Peter Otten
nils wagenaar wrote:

> Hello,
> 
> 
> Could i use a variable defined in a function in another function?
> 
> I have now:
> 
> 
> def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound,
> LonLowBound):
>  nc=netCDF4.Dataset(file)
>  lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:]
>  latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)
>  lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound)
>  data=nc.variables['Runoff'][1000, latselect, lonselect]
>  return data; return latselect; return lonselect

It doesn't help that you put all return statements on the same line, only 
the first

   return data

is executed; the other two are unreachable code.

> So, i want to use latselect and lonselect in a different function where i
> interpolate for the subsetted area.

In Python while you can return only one value you can easily combine 
multiple values into one tuple. Instead of

>  return data; return latselect; return lonselect

write

   return data, latselect, lonselect

When you call the function you can either access the parts of the tuple with 
their respective index

result = DatasetToSubset(...)

lat = result[1]

or use a language feature called "unpacking" to break the tuple into the 
individual values:

data, lat, lon = DatasetToSubset(...)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assessing local variable outside function

2016-10-28 Thread Ben Finney
Alan Gauld via Tutor  writes:

> On 28/10/16 02:38, nils wagenaar wrote:
> > Could i use a variable defined in a function in another function?

My answer would be: You can't because Python variables don't exist
outside their namespace.

You can make the object available in various ways, but not the variable.

> By returning it to the caller.

That's somewhat misleading. Returning the *object* would not grant
access to the local *variable*.

Nils, it's important to realise that a variable in Python is not
tractable: you can't hand them around, you can't access the name itself.
A Python variable exists only in its namespace, and can't move.

The variable is (at any given point) bound to an object; you can get
*other* variables bound to the same object by explicitly doing that.
Alan suggests one way.

Whether that meets your request to “use a variable defined in a function
in another function” will have to wait for you to check how the Python
data model actually works. Does that answer it, or do you need something
different?

-- 
 \ “Dare to be naïve.” —Richard Buckminster Fuller, personal motto |
  `\   |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python on gmane

2016-10-28 Thread Jim Byrnes

On 10/27/2016 09:54 PM, Random832 wrote:

On Thu, Oct 27, 2016, at 20:40, Jim Byrnes wrote:

Is comp.lang.python available on gmane?

I've googled and found references to it being on gmane but I can't find
it there. I'd like to use gmane because Comcast doesn't do usenet
anymore.


I don't know about the current viability of gmane in general, but it's
called "gmane.comp.python.general" on gmane.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



That worked.  Not sure how I over looked it the other times I was trying 
to subscribe.  Thanks much.


Regards,  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python on gmane

2016-10-28 Thread Danny Yoo
>>>
>>> Is comp.lang.python available on gmane?
>>>
>>> I've googled and found references to it being on gmane but I can't find
>>> it there. I'd like to use gmane because Comcast doesn't do usenet
>>> anymore.
>>
>>
>> I don't know about the current viability of gmane in general, but it's
>> called "gmane.comp.python.general" on gmane.

Ah, excellent!

Can someone correct the link on the wiki?
https://wiki.python.org/moin/CompLangPython. (I'm away from a keyboard at
the moment.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python on gmane

2016-10-28 Thread Danny Yoo
Ah.  The wiki link does point to the expected place after all.

I think, then, that the initial assessment is accurate, that Gmane is still
recovering their archives, and that eventually the link will work again.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assessing local variable outside function

2016-10-28 Thread nils wagenaar
Thank you all! It is clear now:)

Verstuurd vanaf mijn iPhone

> Op 28 okt. 2016 om 19:31 heeft Ben Finney  het 
> volgende geschreven:
> 
> Alan Gauld via Tutor  writes:
> 
>>> On 28/10/16 02:38, nils wagenaar wrote:
>>> Could i use a variable defined in a function in another function?
> 
> My answer would be: You can't because Python variables don't exist
> outside their namespace.
> 
> You can make the object available in various ways, but not the variable.
> 
>> By returning it to the caller.
> 
> That's somewhat misleading. Returning the *object* would not grant
> access to the local *variable*.
> 
> Nils, it's important to realise that a variable in Python is not
> tractable: you can't hand them around, you can't access the name itself.
> A Python variable exists only in its namespace, and can't move.
> 
> The variable is (at any given point) bound to an object; you can get
> *other* variables bound to the same object by explicitly doing that.
> Alan suggests one way.
> 
> Whether that meets your request to “use a variable defined in a function
> in another function” will have to wait for you to check how the Python
> data model actually works. Does that answer it, or do you need something
> different?
> 
> -- 
> \ “Dare to be naïve.” —Richard Buckminster Fuller, personal motto |
>  `\   |
> _o__)  |
> Ben Finney
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] run local script on a remote machine

2016-10-28 Thread Steven D'Aprano
If you're trying to do remote command execution, you should forget about 
rolling your own, and use a well-established RPC solution.

I can recommend either of pyro or rpyc:

https://pypi.python.org/pypi/Pyro4
https://pypi.python.org/pypi/rpyc


If you need a standard library solution, you can try:

https://docs.python.org/3/library/xmlrpc.html




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor