Python Forum

Full Version: How get attributes of maps from loop statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! So I am working on a model which will locate irrigation areas to its nearest water source on a global level.
This is a piece of my script of the river areas all over the world, categorized for each continent;

landmask_regions= { \
		'Africa': ['mask_M01.map', 'mask_M02.map', 'mask_M03.map', \
			'mask_M05.map', 'mask_M06.map', 'mask_M07.map', 'mask_M08.map', \
			'mask_M09.map'], \
		'Asia': ['mask_M04.map', 'mask_M05.map', 'mask_M10.map', 'mask_M11.map', \
			'mask_M12.map', 'mask_M13.map', 'mask_M14.map', 'mask_M15.map', \
			'mask_M16.map', 'mask_M17.map', 'mask_M18.map', 'mask_M19.map', \
			'mask_M20.map', 'mask_M21.map', 'mask_M22.map', 'mask_M23.map'], \
		'Europe': ['mask_M26.map', 'mask_M28.map', 'mask_M32.map', 'mask_M33.map', \
			'mask_M34.map'], \
		'N-America': ['mask_M25.map', 'mask_M35.map', 'mask_M36.map', 'mask_M37.map', \
			'mask_M38.map', 'mask_M39.map', 'mask_M40.map', 'mask_M41.map', \
			'mask_M42.map', 'mask_M43.map', 'mask_M44.map', 'mask_M45.map', 'mask_M46.map'], \
		'Australia': ['mask_M47.map', 'mask_M48.map', 'mask_M49.map'], \
		'S-America': ['mask_M24.map', 'mask_M50.map', 'mask_M51.map', \
			'mask_M52.map', 'mask_M53.map']}

    choice = 'Europe'
	if choice == 'whole_world':
			maps = sum(landmask_regions.values(), [])
			print "processing world maps"
			for region in maps:
				print region
	elif choice in landmask_regions:
			maps = landmask_regions[choice]
			print "processing continent"
			for region in maps:
				print region
	elif choice.endswith('.map'):
			maps = [choice]
			print "processing river area"
			for region in maps:
				print region	
So in my script, I have created methods that call one another to complete the following tasks:
- Read all the maps together (for a global map)
- Read all the maps of one region (for each continent)
- Read one specific region within a continent (for one river area)

Now I would like to get those maps (in this case from Europe), and to be able to 'read' them; this implies getting the attributes (such as location, x/y coordinates etc.) I was thinking of just using the 'getattr' function, but I'm not sure how to do that here.
Can someone maybe provide me with tips on how to adjust my script in order to do that? I hope my question is clear!
Thank you!
This doesn't answer your question, but makes your code much more readable, and corrects
PEP8 inconsistencies.

You can save your dictionary as a json file with:
import json

landmask_regions= { \
        'Africa': ['mask_M01.map', 'mask_M02.map', 'mask_M03.map', \
            'mask_M05.map', 'mask_M06.map', 'mask_M07.map', 'mask_M08.map', \
            'mask_M09.map'], \
        'Asia': ['mask_M04.map', 'mask_M05.map', 'mask_M10.map', 'mask_M11.map', \
            'mask_M12.map', 'mask_M13.map', 'mask_M14.map', 'mask_M15.map', \
            'mask_M16.map', 'mask_M17.map', 'mask_M18.map', 'mask_M19.map', \
            'mask_M20.map', 'mask_M21.map', 'mask_M22.map', 'mask_M23.map'], \
        'Europe': ['mask_M26.map', 'mask_M28.map', 'mask_M32.map', 'mask_M33.map', \
            'mask_M34.map'], \
        'N-America': ['mask_M25.map', 'mask_M35.map', 'mask_M36.map', 'mask_M37.map', \
            'mask_M38.map', 'mask_M39.map', 'mask_M40.map', 'mask_M41.map', \
            'mask_M42.map', 'mask_M43.map', 'mask_M44.map', 'mask_M45.map', 'mask_M46.map'], \
        'Australia': ['mask_M47.map', 'mask_M48.map', 'mask_M49.map'], \
        'S-America': ['mask_M24.map', 'mask_M50.map', 'mask_M51.map', \
            'mask_M52.map', 'mask_M53.map']}

with open('landmarks.json', 'w') as jp:
    json.dump(landmask_regions, jp)
and modify your code like (cleaner):
import json


def main():
    with open('landmarks.json') as jp:
        landmask_regions = json.load(jp)

    choice = 'Europe'
    if choice == 'whole_world':
        maps = sum(landmask_regions.values(), [])
        print "processing world maps"
        for region in maps:
            print region
    elif choice in landmask_regions:
        maps = landmask_regions[choice]
        print "processing continent"
        for region in maps:
            print region
    elif choice.endswith('.map'):
        maps = [choice]
        print "processing river area"
        for region in maps:
            print region


if __name__ == '__main__':
    main()
In the code you posted the maps are just strings, like 'mask_MO1.map'. I'm assuming this points to some sort of file with map data? If so, you'd have to load that data into Python somehow, and how you accessed the information for each map would depend on how it was loaded into Python.
Yeah that's exactly what I'm struggling with to be honest.
Because I have 'imported' these files now just as strings, and I hoped to read them in as maps after I have first indentified the names I need (so depending on the continent/area etc.).
But I feel like I'm stuck on how to properly do that.