# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Extracting Room Info
# Already added `room_info.split(' · ')`.

# +
import pandas as pd

df = pd.read_csv('alaska_entire_home_super_hosts_2021-01-09.csv')

df.room_info
# -

df.room_info[0]

type(df.room_info[0])

# +
room = df.room_info[0]

room
# -

room.split("'")

len(room.split("'"))

room.split("'")[1]

# +
split_info = room.split("'")

split_info
# -

split_info = [i for i in room.split("'") if i not in '[], ']
split_info

split_info = [i.split() for i in room.split("'") if i not in '[], ']
split_info

split_info = [i.split() for i in room.split("'") if i not in '[], ']
split_info = {i[1]:i[0] for i in split_info}
split_info

# On different rooms... (verification)

for room in df.room_info.sample(5): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    split_info = {i[1]:i[0] for i in split_info}
    print(split_info)

for room in df.room_info.sample(5): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
#     split_info = {i[1]:i[0] for i in split_info}
    print(split_info)

for room in df.room_info.sample(15): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
#     split_info = {i[1]:i[0] for i in split_info}
    print(split_info)

# add check for `['Studio']` (0 rooms).

for room in df.room_info.sample(15): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    # add check for 'Studio' (0 rooms)
    split_info = {i[1]:i[0] if i != ['Studio'] else i for i in split_info}
    print(split_info)

# +
new_list = []
for room in df.room_info.sample(10): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    for i in split_info:
        if i != ['Studio']:
            try:
                new_list.append(i)
            except:
                print(i)
                
                
new_list
# -

# Add check for `['Half-bath']`.

for room in df.room_info.sample(15): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    # add check for 'Studio' (0 rooms)
    split_info = {i[1]:i[0] if i not in [['Studio'], ['Half-bath']] else i for i in split_info}
    print(split_info)

# +
new_list = []
for room in df.room_info.sample(100): 
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    for i in split_info:
        if i not in [['Studio'], ['Half-bath']]:
            try:
                new_list.append(i)
            except:
                if len(i)==1:
                    print(i)
                else:
                    raise Exception('idk')
                
                
new_list
# -

# Add check for `['x', 'shared', 'bath']`...

# +
c = []

for room in df.room_info.sample(150):
    room_dict = {}
    split_info = [i.split() for i in room.split("'") if i not in '[], ']
    # add check for 'Studio' (0 rooms)
#     split_info = {i[1]:i[0] if i not in [['Studio'], ['Half-bath']] else i for i in split_info}
    for i in split_info:
        if i not in [['Studio'], ['Half-bath']]:
            if len(i) == 2:
                room_dict[i[1]] = i[0]
            # shared-baths
            elif len(i) == 3:
                i = [i[0], '-'.join([i[1], i[2]])]
                room_dict[i[1]] = i[0]
            else:
                raise Exception(f'unexpected room_info | unexpected split_info len(i)=={len(i)}!=2!=3')
        else:
            # Half-baths and Studios
            if i[0] == 'Studio':
                room_dict['is_studio'] = True
            room_dict[i[0]] = 0
    
    try:
        room_dict['bedrooms']
    except:
        try:
            room_dict['bedrooms'] = room_dict['bedroom']
        except:
            room_dict['bedrooms'] = room_dict['Studio']
         
    try:
        room_dict['baths']
    except:
        try:
            room_dict['baths'] = room_dict['bath']
        except:
            room_dict['baths'] = None
            
    room_dict['half_baths'] = room_dict.get('Half-bath')
    room_dict['shared_baths'] = room_dict.get('shared-baths')
    room_dict['is_studio'] = room_dict.get('is_studio', False)
            
    room_dict = {key:value for key,value in room_dict.items() if key in ['guests', 'bedrooms', 'beds', 'is_studio', 'baths', 'half_baths', 'shared_baths']}
            
    c.append(room_dict)
       
for d in c:
# #     if d['shared_baths'] != None:
# #         print(d)
#     if (d['half_baths'] != None) and (d['half_baths'] != 0):
#         print(d)
    if d['is_studio'] == True:
        print(d)
# c
# -

# ## Before

def get_room_info(listing):
    """
    returns room info of listing 
    """
    room_info = listing.find('div', {'class', '_kqh46o'}).text
    room_info = room_info.split(' · ')
    return room_info


# ## After

def get_room_info(listing):
    """
    returns room info of listing 
    """
    room_info = listing.find('div', {'class', '_kqh46o'}).text
    split_info = room_info.split(' · ')
    room_dict = {}
    for i in split_info:
        if i not in [['Studio'], ['Half-bath']]:
            if len(i) == 2:
                room_dict[i[1]] = i[0]
            # shared-baths
            elif len(i) == 3:
                i = [i[0], '-'.join([i[1], i[2]])]
                room_dict[i[1]] = i[0]
            else:
                raise Exception(f'unexpected room_info | unexpected split_info len(i)=={len(i)}!=2!=3')
        else:
            # Half-baths and Studios
            if i[0] == 'Studio':
                room_dict['is_studio'] = True
            room_dict[i[0]] = 0
    
    try:
        room_dict['bedrooms']
    except:
        try:
            room_dict['bedrooms'] = room_dict['bedroom']
        except:
            room_dict['bedrooms'] = room_dict['Studio']
         
    try:
        room_dict['baths']
    except:
        try:
            room_dict['baths'] = room_dict['bath']
        except:
            room_dict['baths'] = None
            
    room_dict['half_baths'] = room_dict.get('Half-bath')
    room_dict['shared_baths'] = room_dict.get('shared-baths')
    room_dict['is_studio'] = room_dict.get('is_studio', False)
            
    room_dict = {key:value for key,value in room_dict.items() if key in ['guests', 'bedrooms', 'beds', 'is_studio', 'baths', 'half_baths', 'shared_baths']}
            
    return room_dict

StarcoderdataJupyterScriptsDedupFiltered