Skip to content

Instantly share code, notes, and snippets.

@taosx
Created January 6, 2020 07:43
Show Gist options
  • Select an option

  • Save taosx/01c189154181d7f2a8da9fd735aeb7f4 to your computer and use it in GitHub Desktop.

Select an option

Save taosx/01c189154181d7f2a8da9fd735aeb7f4 to your computer and use it in GitHub Desktop.

Revisions

  1. taosx created this gist Jan 6, 2020.
    83 changes: 83 additions & 0 deletions decoding into class.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    from typing import Optional, List, Any
    from marshmallow import Schema, fields, pprint

    class Structure(object):

    _fields = []

    def _init_arg(self, expected_type, value):
    if isinstance(value, expected_type):
    return value
    else:
    return expected_type(**value)

    def __init__(self, **kwargs):
    field_names, field_types = zip(*self._fields)
    for name, field_type in self._fields:
    setattr(self, name, self._init_arg(field_type, kwargs.pop(name)))

    # Check for any remaining unknown arguments
    if kwargs:
    raise TypeError(
    'Invalid arguments(s): {}'.format(','.join(kwargs)))


    class BiographyWithEntities:
    raw_text: Optional[str]
    entities: Optional[List[Any]]

    def __init__(self, raw_text: Optional[str], entities: Optional[List[Any]]) -> None:
    self.raw_text = raw_text
    self.entities = entities


    class HDProfilePicURLInfo:
    url: str
    width: int
    height: int

    def __init__(self, url: str, width: int, height: int) -> None:
    self.url = url
    self.width = width
    self.height = height


    class ProfileContextLinksWithUserID(Structure):

    _fields = [("start", int), ("end", int), ("username", str)]


    class User(Structure):
    _fields = [('pk', int),
    ('username', str),
    ('full_name', str),
    ('is_private', bool),
    ('profile_pic_url', str),
    ('profile_pic_id', str),
    ('is_verified', bool),
    ('has_anonymous_profile_picture', bool),
    ('media_count', int),
    ('geo_media_count', int),
    ('follower_count', int),
    ('following_count', int),
    ('following_tag_count', int),
    ('biography', str),
    ('biography_with_entities', BiographyWithEntities),
    ('external_url', str),
    ('total_igtv_videos', int),
    ('usertags_count', int),
    ('is_favorite', bool),
    ('is_favorite_for_stories', bool),
    ('live_subscription_status', str),
    ('has_recommend_accounts', bool),
    ('has_chaining', bool),
    ('hd_profile_pic_url_info', HDProfilePicURLInfo),
    ('mutual_followers_count', int),
    ('profile_context', str),
    ('profile_context_links_with_user_ids',
    list),
    ('profile_context_mutual_follow_ids', list),
    ('is_business', bool),
    ('account_type', int),
    ('is_call_to_action_enabled', object),
    ('can_see_primary_country_in_settings', bool)]