-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: getattr and iter dunders #42
Open
BobTheBuidler
wants to merge
7
commits into
ApeWorX:main
Choose a base branch
from
BobTheBuidler:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7224216
feat: getattr and iter dunders
BobTheBuidler bc36578
fix: Mapping api and iter
BobTheBuidler 32a0bd1
Merge branch 'main' into patch-1
antazoey f647fbe
docs: update readme
antazoey da602a4
fix: correct kwarg
antazoey 13b583c
fix: token not in tokenlist
BobTheBuidler b13e9ae
fix: raise from current
BobTheBuidler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Iterator, Mapping | ||
|
||
from ape.contracts import ContractInstance | ||
from ape.exceptions import ContractNotFoundError | ||
from ape.types import ContractType | ||
|
@@ -106,22 +108,33 @@ | |
) | ||
|
||
|
||
class TokenManager(ManagerAccessMixin, dict): | ||
class TokenManager(ManagerAccessMixin, Mapping[str, ContractInstance]): | ||
@cached_property | ||
def _manager(self) -> TokenListManager: | ||
return TokenListManager() | ||
|
||
def __repr__(self) -> str: | ||
return f"<ape_tokens.TokenManager default='{self._manager.default_tokenlist}'>" | ||
|
||
def __getitem__(self, symbol: str) -> ContractInstance: | ||
try: | ||
token_info = self._manager.get_token_info( | ||
symbol, chain_id=self.network_manager.network.chain_id | ||
) | ||
def __len__(self) -> int: | ||
return len(list(self._manager.get_tokens(chain_id=self.provider.chain_id))) | ||
|
||
except ValueError as err: | ||
raise KeyError(f"Symbol '{symbol}' is not a known token symbol") from err | ||
def __iter__(self) -> Iterator[ContractInstance]: | ||
for token in self._manager.get_tokens(chain_id=self.provider.chain_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am getting the
|
||
yield self[token.symbol] | ||
|
||
def __getitem__(self, symbol: str) -> ContractInstance: | ||
token_info = None | ||
for tokenlist in self._manager.available_tokenlists(): | ||
try: | ||
token_info = self._manager.get_token_info( | ||
symbol, chain_id=self.network_manager.network.chain_id, token_listname=tokenlist | ||
) | ||
except ValueError: | ||
BobTheBuidler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
|
||
if token_info is None: | ||
raise KeyError(f"Symbol '{symbol}' is not a known token symbol") | ||
|
||
checksummed_address = to_checksum_address(token_info.address) | ||
try: | ||
|
@@ -130,3 +143,9 @@ def __getitem__(self, symbol: str) -> ContractInstance: | |
return self.chain_manager.contracts.instance_at( | ||
checksummed_address, contract_type=ERC20 | ||
) | ||
|
||
def __getattr__(self, symbol: str) -> ContractInstance: | ||
try: | ||
return self[symbol] | ||
except KeyError as e: | ||
raise AttributeError(str(e)) from None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried running:
but got this:
kinda weird. I do have multiple token lists installed though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if something like:
would work for this situation.
I tried it again, except changed it to:
I see the first like hundred get printed out super fast, and then it suddenly is very slow at outputting them.
hmmmmm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible the slowness is from downloading the last of the tokenlists? Looking at this code again I'm not sure where else a slowdown might come from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be fixed now with my latest commit.