-
Notifications
You must be signed in to change notification settings - Fork 115
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
added a bullet dict class as #27 requested #30
base: master
Are you sure you want to change the base?
Conversation
the way this works is you define a dict the keys are shown to the user but the values are returned when selected. i think this would be nice to have added it to client file as class BulletDict, and set up in init, also added an example feel free to give me advice on how to improve this class
bullet/__init__.py
Outdated
from .client import ScrollBar | ||
from .client import BulletDic |
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.
Your class is named BulletDict, not BulletDic
examples/BulletDic.py
Outdated
@@ -0,0 +1,16 @@ | |||
from bullet import BulletDic |
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.
Class name again
bullet/client.py
Outdated
@@ -224,6 +224,127 @@ def launch(self, default = None): | |||
if ret is not None: | |||
return ret | |||
|
|||
|
|||
@keyhandler.init | |||
class BulletDict: |
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.
This is a lot of repeated code for what seems like it should just be a child class that overrides accept().
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.
ah okay that makes sense ill do that now thank you.
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.
what are your thoughts on it now? any improvements you would recommend me trying to do?
fixing what was suggested by rcfox on pull request bchao1#27
(I should point out that I have no authority on this project, I'm just a random guy commenting on someone else's repo.) After sleeping on this, it seems like it would be best if this were rolled into the main classes. (Sorry for the runaround) It's simple enough to detect the type of variable being passed into the constructor. It also seems like list choices would be the special case. If you get a list, you could just make a dict out of it: |
Actually, it would probably be better to store choices as a list of (key, value) pairs. That way duplicate list items won't be lost, and you avoid the weirdness of trying to index into a dictionary at a certain position. |
thats okay im just trying to improve my code, it was my new years resolution to contribute to things, also im still at University so learning better ways of doing things is always nice. also that two list idea seams pretty good so you would just have a list for displaying and a list for return values |
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.
Mostly looks good, just needs a bit of polish.
|
||
#test flag also return objects | ||
self.objects=[] | ||
if(type(choices) is dict): |
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.
isinstance(obj, cls)
is generally preferable to type(obj) is cls
since it supports subtypes. (For instance, OrderedDict
)
self.objects=[choices[x] for x in self.choices] | ||
else: | ||
self.choices = choices | ||
|
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.
If you set self.objects = choices
, you wouldn't need the extra check in accept(), you could also just return from self.objects
bullet/client.py
Outdated
self.objects=[] | ||
if(type(choices) is dict): | ||
self.choices = list(choices.keys()) | ||
self.objects=[choices[x] for x in self.choices] |
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.
How about list(choices.values())
for consistency?
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.
did not know that there was a .values() function
bullet/client.py
Outdated
@@ -197,7 +205,11 @@ def moveDown(self): | |||
def accept(self): | |||
utils.moveCursorDown(len(self.choices) - self.pos) | |||
cursor.show_cursor() | |||
ret = self.choices[self.pos] | |||
#flag check and return ether objects or just choices | |||
if(self.objects): |
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.
Nitpicking, but you generally leave off parentheses on if statements in Python for simple conditions like this.
Also, try to be consistent with whitespace to match the surrounding code.
removed flag statement from accept by making objects the same as choices if its a list
the way this works is you define a dict the keys are shown to the user but the values are returned when selected. i think this would be nice to have.
added it to client file as class BulletDict, and set up in init, also added an example feel free to give me advice on how to improve this class