Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can i clean this code ?
#1
Hi, im trying to clean my code and make less of and i would like to know if its possible to clean it ( im having issues with finding solutions when i dont know what to look for )

My code:
    def area_zips(zipcode):
        zips = {"010": "Springfield", "011": "Springfield" , .... ,"997": "Alaska", "998": "Alaska", "999": "Alaska"}
        return zips[zipcode[:3]]

    def regions(region):
        states = {"ME": "Northeast", "NH": "Northeast", ... , "WA": "West", "WY": "West"}
        return states[region]
ZIPCODES = almost 1000 of them and some are repeatable as in example but they needed and same with the states.
Can i combine multiple abbreviation to have less code ?
Something like this:
    def area_zips(zipcode):
        zips = {"010", "011": "Springfield" , .... ,"997", "998", "999": "Alaska"}
        return zips[zipcode[:3]]
Same with Zips.
Thank You.
Reply
#2
FYI: you can get your zipcode data here: https://www.unitedstateszipcodes.org/
BSDevo likes this post
Reply
#3
(Oct-27-2023, 11:22 PM)Larz60+ Wrote: FYI: you can get your zipcode data here: https://www.unitedstateszipcodes.org/

I dont need zipcode data, i have. I want to minimize my code.
From: "010": "Springfield", "011": "Springfield", "012": "Springfield", "013": "Springfield" to "010","011", "012", "013": "Springfield"
Obviously if its possible.
Reply
#4
You cannot make a dictionary like that, and why would you? The space saving would be miniscule and how could you look anything up? You could make a reverse dictionary where the key "Springfiled" has multiple values, but don't reverse the dictionary just to save space. Organize the dictionary so it is most useful.

Normally things like the zip code or state abbreviations dictionary would be loaded from supporting files. Zip codes change over time and you shouldn't have to change your program because of population movement. If you really want them to be code, I suggest putting each in their own module that you import into the main module. At least that isolates the changes.
Reply
#5
(Oct-28-2023, 04:09 AM)deanhystad Wrote: You cannot make a dictionary like that, and why would you? The space saving would be miniscule and how could you look anything up? You could make a reverse dictionary where the key "Springfiled" has multiple values, but don't reverse the dictionary just to save space. Organize the dictionary so it is most useful.

Normally things like the zip code or state abbreviations dictionary would be loaded from supporting files. Zip codes change over time and you shouldn't have to change your program because of population movement. If you really want them to be code, I suggest putting each in their own module that you import into the main module. At least that isolates the changes.

I know what you mean about zip codes.
My zip codes are defined to specific custom market area naming by first 3 zips using these boundaries and mixing them [ sometimes to define 1 market area i have to use 3 zip code from multiple states ] : https://www.mapofzipcodes.com/zip-code-m...de-map.pdf
WHAT IS A KEY MARKET AREA?
A Key Market Area, or KMA, is a collection of 3-digit
ZIP code areas that are influenced by or “keyed” to a
major city or economic center.

My KMA aka DMA is different ( custom tailored ) from https://en.wikipedia.org/wiki/Media_market or others.
I hope it explains why im using hard coded instead of loading from supported file
Reply
#6
BSDevo ' Wrote: I hope it explains why im using hard coded instead of loading from supported file
You are missing the point. There is no reason why your custom codes (or any other supporting info/data) is not stored in external file or db and read from there
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Looks like you could use 2 dictionaries. code->market and market->code.
zip_areas= {"010": "Springfield", "011": "Springfield" , .... ,"997": "Alaska", "998": "Alaska", "999": "Alaska"}
And a reverse dictionary that would look like this:
area_zips = { "Springfield": ("010", "011, "012"), ..., "Alaska": ("997", "998", "999")}
code->market would look like what you posted already.
You can construct one from the other. Since area_zips will be more compact, I would hardcode that and build zip_areas:
zip_areas = {}
for area, zips in area_zips.items:
    for zip in zips:
        zip_areas[zip] = area
Reply
#8
(Oct-28-2023, 02:49 PM)buran Wrote:
BSDevo ' Wrote: I hope it explains why im using hard coded instead of loading from supported file
You are missing the point. There is no reason why your custom codes (or any other supporting info/data) is not stored in external file or db and read from there

You mean something like working with Map ( as an example folium map) where i define geojson data ?
choropleth = folium.Choropleth(
        geo_data='us-state.geojson',
        data=filtered_df,
        columns=('State', 'RPM'),
        key_on='feature.properties.stusab',
        legend_name="RPMile",
        fill_color='BuGn',
        highlight=True
    ).add_to(m)
But its for reading my custom file after i upload my csv file then python splits it by my needs and adds to pandas dataframe ?
Reply
#9
(Oct-28-2023, 04:44 PM)deanhystad Wrote: Looks like you could use 2 dictionaries. code->market and market->code.
zip_areas= {"010": "Springfield", "011": "Springfield" , .... ,"997": "Alaska", "998": "Alaska", "999": "Alaska"}
And a reverse dictionary that would look like this:
area_zips = { "Springfield": ("010", "011, "012"), ..., "Alaska": ("997", "998", "999")}
code->market would look like what you posted already.
You can construct one from the other. Since area_zips will be more compact, I would hardcode that and build zip_areas:
zip_areas = {}
for area, zips in area_zips.items:
    for zip in zips:
        zip_areas[zip] = area


So my code would always read zip_areas then converts to area_zip every time my app runs from top to bottom ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Clean Up Script rotw121 2 1,020 May-25-2022, 03:24 PM
Last Post: rotw121
  How to clean UART string Joni_Engr 4 2,503 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  How to clean session mqtt SayHiii 0 2,010 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  how to clean up unstarted processes? Skaperen 2 2,257 Aug-27-2019, 05:37 AM
Last Post: Skaperen
  Code Clean-up and Maya/Python Slicing issues mvvthology 1 2,443 Aug-05-2019, 10:43 PM
Last Post: Yoriz
  sched.scheduler -> clean denisit 1 2,887 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  clean script by code fen1c5 8 4,702 Oct-16-2018, 05:11 AM
Last Post: volcano63
  clean up list elements and replace metalray 7 4,244 Aug-30-2018, 08:13 AM
Last Post: metalray
  Clean Data using NLTK disruptfwd8 0 3,338 May-12-2018, 11:21 PM
Last Post: disruptfwd8
  Clean up Code liamwemyss 1 3,373 Jun-07-2017, 08:36 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020