Skip to content

Instantly share code, notes, and snippets.

@victorusachev
Last active October 13, 2019 23:03
Show Gist options
  • Select an option

  • Save victorusachev/aca29a2502e530c9a38267b444a288e2 to your computer and use it in GitHub Desktop.

Select an option

Save victorusachev/aca29a2502e530c9a38267b444a288e2 to your computer and use it in GitHub Desktop.
# convert to dict
# fiasHouseGuidByAddress = addressObjects
# .Select(
# ao => new KeyValuePair<string, Guid>(ao.AddressObjectId, ao.FiasHouseGuid)
# )
# .ToDictionary(t => t.Key, t => t.Value);
import uuid
from dataclasses import dataclass, field
from pprint import pprint
from typing import Dict, Any, Iterable
@dataclass
class AddressObject:
id: int
guid: uuid.UUID = field(default_factory=uuid.uuid4)
def to_dict(self, *fields) -> Dict[str, Any]:
if not fields:
fields = 'id', 'guid'
return {k: getattr(self, k) for k in fields}
def __iter__(self) -> Iterable:
yield from self.to_dict().items()
def main():
# create list of address objects
address_objects = [
AddressObject(ao_id) for ao_id in range(10)
]
# convert to dict
fias_house_guid_by_address = [dict(ao) for ao in address_objects]
pprint(fias_house_guid_by_address)
# or iterate
for item in address_objects:
print(item)
for first, second in address_objects:
print(first, second)
for item in address_objects:
(first_name, first_value), (second_name, second_value) = item
print(f'{first_name}: {first_value}, {second_name}: {second_value}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment