What is the best way to embed binary value in String - stanza

I would like to use escape to represent control code to build a message with terminal color.
Can I use something like...
"\u001b[93m" or "\x1b[93m"
?

Currently I am using this method:
val newargs = append-all([to-string(to-char(27)) CGREEN msg to-string(to-char(27)) CEND])
to-string(to-char(27)) is too clumsy.
I am sure there is a better method.

You can see how this library implements terminal colors as a reference

Related

Can I format variables like strings in python?

I want to use printing command bellow in many places of my script. But I need to keep replacing "Survived" with some other string.
print(df.Survived.value_counts())
Can I automate the process by formating variable the same way as string? So if I want to replace "Survived" with "different" can I use something like:
var = 'different'
text = 'df.{}.value_counts()'.format(var)
print(text)
unfortunately this prints out "df.different.value_counts()" as as a string, while I need to print the value of df.different.value_counts()
I'm pretty sure alot of IDEs, have this option that is called refactoring, and it allows you to change a similar line of code/string on every line of code to what you need it to be.
I'm aware of VSCode's way of refactoring, is by selecting a part of the code and right click to select the option called change all occurances. This will replace the exact code on every line if it exists.
But if you want to do what you proposed, then eval('df.{}.value_counts()'.format(var)) is an option, but this is very unsecured and dangerous, so a more safer approach would be importing the ast module and using it's literal_eval function which is safer. ast.literal_eval('df.{}.value_counts()'.format(var)).
if ast.literal_eval() doesn't work then try this final solution that works.
def cat():
return 1
text = locals()['df.{}.value_counts'.format(var)]()
Found the way: print(df[var].value_counts())

Is there a better way to handle double click in pygobject

I've been using the following for handling double click:
def do_button_press_event(self, eb: Gdk.EventButton):
if eb.type == Gdk.EventType._2BUTTON_PRESS:
# todo: code double click
pass
Accessing the private property _2BUTTON_PRESS feels a bit dirty. Is there a better way to handle this?
It's not a private property: it's an artefact of the C enumeration member being GDK_2BUTTON_PRESS. Python does not allow identifiers to start with a number, so when translating the symbol GDK_2BUTTON_PRESS in the GdkEventType C enumeration into a field in Gdk.EventType Python class, PyGOBject needs to escape the 2BUTTON_PRESS part.
To avoid this, GTK introduced a GDK_DOUBLE_BUTTON_PRESS, which is correctly translated as Gdk.EventType.DOUBLE_BUTTON_PRESS.
The same explanation also applies to the GDK_3BUTTON_PRESS/GDK_TRIPLE_BUTTON_PRESS enumeration fields.

How do I change the type of a string to a bytestring?

For example, I have '\x87' and I want b'\x87'.
I know, that there exists .encode(), but when I execute ('\x87').encode(), I get b'\xc2\x87' and not b'\x87'.Is there any way to tell python that it should interpret the given string as a bytestring without possibly changing it in any way?
Try this.
my_str = '\x87'
my_str_as_bytes = my_str.encode(encoding='latin')
One possible solution is:
bytes(ord(x) for x in '\x87')

How to convert TextView to int?

I know that there are a lot of posts like this but I read them and my application does not work yet.
Im trying to convert TextView parameter into int.
I use this:
int MyScore;
TextView score = (TextView) findViewById(R.id.highscore);
MyScore = Integer.parseInt(score.toString());
When im launching the program and rich to the place this should work my program crushing becasue of the last line: MyScore = Integer.parseInt(score.toString());
How can I solve this?
Your last line should read
MyScore = Integer.parseInt(score.getText().toString());
The toString() method called on your score object describes the score object, it does not return the string entered into the score object. Please refer to the Oracle Java tutorials. It is a good idea to read through most of them.
Because you didn't specify the language used I cannot answer fully, but I can guess what the problem is. Most input fields have a value or text property, try parsing that.

Use STL string with unicode

I am coding a plugin for autodesk 3dsmax and they recommend to use the _T(x) macro for every string literal to make it work with unicode as well. I am using the stl string class a lot in this code. So do I have to rewrite the code: string("foo") to: string(_T("foo")) ? Actually the stl string class doesnt have a constructor for wchars, so it doesnt make sense, does it?
Thx
Look at the definition of "T" macro - it expands to "L" in "Unicode" builds or nothing in "non-Unicode" builds. If you want to keep using the string calss and follow the recommendation for your plugin, your best bet is to use something like tstring which would follow the same rules.
But the truth is - all this "T" business made a lot of sense 10 years ago - all modern Windows versions are Unicode-only and you can just use wstring.
You could create an own string class say xstring and use the _T for constants and then internally, depending on unicode or not switch to string or wstring. either that or instantiate xstring<yourchartype>

Resources