Skip to content
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

Optimized the print_with_magic_color() function. #84

Merged
merged 3 commits into from Oct 23, 2021

Conversation

Ingenious-c0der
Copy link
Member

@Ingenious-c0der Ingenious-c0der commented Oct 22, 2021

The function was originally generating a random number from 0,255 and then was checking if that number is from the excluded numbers (for color ints) and till it finds the number from accepted number range , it kept looping in a while loop. This was replaced by using random.choice([accepted int list]) instead.
Added annotations , doc string and removed the bool comparison (if bool_value==True:(bad practice) , if bool_value:(good practice). Removed unnecessary string formatting which makes the code un readable and %string formatting might be deprecated in the near future.

The function was originally generating a random number from 0,255 and then was checking if that number is form the excluded number and till it finds the number from accepted number range , it kept looping in a while loop. This was replaced by using random.choice([accepted int list]) instead.Added annotations , doc string and removed the bool comparison (if bool_value==True:(bad practice) , if bool_value:(good practice). Removed unnecessary string formatting which makes the code un readable and %string formatting might be deprecated in the near future.
@mmv-dev
Copy link
Collaborator

mmv-dev commented Oct 23, 2021

your function is having a bug,

image

Please modify the function, to print multicolor and single color based on user choice.
Please test the function with various inputs before PR.
Use try-except methodology. For reference check out our other functions.

I was calling random.choice on an int. Fixed that and also the imports
@Ingenious-c0der
Copy link
Member Author

Ingenious-c0der commented Oct 23, 2021

Sorry for that . I have fixed the bug now .
Magic = True for multicolor and False for single color.
The previous function did not have user choice for the color , hence wanted to make sure whether I should make this function have a color : int parameter which takes color according to user choice and applies to the string message to be printed.

To make sure every time the function is called the text is printed on a new line.
@mmv-dev mmv-dev merged commit dd41b95 into ClointFusion:master Oct 23, 2021
@mmv-dev
Copy link
Collaborator

mmv-dev commented Nov 1, 2021

We are getting an exception for the function you modified. Here are the exception details.

Its an "UnicodeEncodeError".
For time being, we restored the logic.
Please work on this and make a PR.

Error Details : ['Traceback (most recent call last):\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\ClointFusion\ClointFusion.py", line 203, in print_with_magic_color\n print(color+ch+res,end="")\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write\n self.__convertor.write(text)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write\n self.write_and_convert(text)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 187, in write_and_convert\n self.write_plain_text(text, cursor, start)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text\n self.wrapped.write(text[start:end])\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode\n return codecs.charmap_encode(input,self.errors,encoding_table)[0]\n', "UnicodeEncodeError: 'charmap' codec can't encode character '\u2764' in position 0: character maps to \n", '\nDuring handling of the above exception, another exception occurred:\n\n', 'Traceback (most recent call last):\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\ClointFusion\ClointFusion.py", line 206, in print_with_magic_color\n print(color+ch+res,end="")\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 41, in write\n self.__convertor.write(text)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 162, in write\n self.write_and_convert(text)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 187, in write_and_convert\n self.write_plain_text(text, cursor, start)\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\site-packages\colorama\ansitowin32.py", line 195, in write_plain_text\n self.wrapped.write(text[start:end])\n', ' File "C:\Users\USER NAME\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode\n return codecs.charmap_encode(input,self.errors,encoding_table)[0]\n', "UnicodeEncodeError: 'charmap' codec can't encode character '\u2764' in position 0: character maps to \n"]

@Ingenious-c0der
Copy link
Member Author

Ingenious-c0der commented Nov 2, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants