HarfBuzz language_from_string Python introspection method doesn't accept str - python

I'm just getting started with HarfBuzz, and have switched to using the GObject Introspection interface to explore its API. Everything has been easy so far, but I'm a bit confused with language_from_string, which has the following docstring:
language_from_string(str:list) -> HarfBuzz.language_t
i.e. in IPython, I do:
from gi.repository import HarfBuzz
?HarfBuzz.language_from_string
in vanilla Python, you can replace the last line with: print(HarfBuzz.language_from_string.__doc__) (or similar)
if I call this method with a string, e.g:
HarfBuzz.language_from_string('en')
I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Item 0: Must be number, not str
back, while if I convert to a list of code points first:
en = HarfBuzz.language_from_string(list(map(ord, 'en')))
the error goes away, and I get something useful back. e.g. I can do:
HarfBuzz.language_to_string(en)
and I get the expected en back, in a string.
HarfBuzz Issue #91 is about this method, but doesn't seem to be relevant.

You have to call it like HarfBuzz.language_from_string(b'en') (string but prefixed with b) in python3 as strings are not just sequence of bytes anymore in py3 unlike py2.
Do you know any gi API that gets an actual python representation of string in python3? If so let me know otherwise this is expected from HarfBuzz side.

Related

How to use self-docummenting equals (debugging) specifier with str.format()?

Python 3.8 introduced = specifier in f-strings (see this issue and pull request).
It allows to quickly represent both the value and the name of the variable:
from math import pi as π
f'{π=}'
# 'π=3.141592653589793'
I would like to use this feature on a pre-defined string with str.format():
'{π=}'.format(π=π)
However, it raises an exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'π='
Is there a way to make it work (e.g. with a special dunder method)?
Why it may be useful?
one could have a programmatic template for multiple values of the same variable (in a loop)
in contrasts, f-strings have to be hardcoded; think about internationalization
one could reference constants defined in a module in its docstring (module.__doc__.format(**vars(module));
workaround: define an f-string variable at the end of the module, overwrite the module.__doc__ at runtime.

Show elements of an array in TkMessageBox

The code i'm using is the following:
if len(areUnusedParams) > 0:
tkMessageBox.showinfo('Error!','The following parameters are unchanged throughout the C-files and are also not present in parameter.txt:\n')
Now, i want the contents of areUnusedParams (which is an array) to be posted when the MessageBox is displayed and every element should be on their own line.
I was contemplating something like:
'\n'.join(areUnusedParams)
But I don't know how to implement it, PyCharm keeps complaining when I try to do it.
when you add the list of areUnusedParams to showinfo as a third argument it will raise a TypeError much like the following:
Traceback (most recent call last):
File ".../test.py", line 3, in <module>
tkMessagebox.showinfo("title","message","thing")
TypeError: showinfo() takes from 0 to 2 positional arguments but 3 were given
Since you intend the '\n'.join(areUnusedParams) to be part of the message you need to add it to the message instead of passing as an additional argument:
tkMessageBox.showinfo('Error!',
'The following parameters are unchanged throughout the C-files and are also not present in parameter.txt:\n' \
+ '\n'.join(areUnusedParams))
I assume that all of areUnusedParams are all strings but just to be thorough, if they are not then it is an invalid thing to pass to str.join in which case you can cast all of them to strings using map:
'\n'.join(map(str,areUnusedParams))

How do I port MultiCheckComboBox to PySide?

I want the items in a combobox to be checkable. I have found a way to do this here.
This specific example uses PyQt and since I use PySide, it needs some modifications to work.
The first error I get is about QStringList which I replaced with [].
Then I get the following error:
Traceback (most recent call last):
File "MultiCheckComboBox.py", line 401, in data
if not value.isValid():
AttributeError: 'NoneType' object has no attribute 'isValid'
I am not sure what needs to be changed to fix the above error.
PyQt4 (by default) returns some values as instances of QVariant (although it can be configured to return a native Python type). PySide however, always converts to native Python types.
Your code is assuming that PyQt4 will be using QVariant. That line of code assumes value is a QVariant. You will need to change it to:
if value is not None:
in this case, however this is unlikely to be the only instance of a QVariant in this code and the modification you need to make to those will be dependent on the QVariant function that the code is trying to call.

Monkeypatch PEP 461 in Python 3.0-3.4

Is is possible to monkeypatch PEP 461 functionality in to Python 3.0-3.4?
(I'm working on porting dulwich to python 3, and as it has a lot of protocol and file format code, it relies heavily on byte formatting.)
To monkeypatch it in, you would need to assign a relevant function to bytes.format. Trying it with a dummy function does this:
>>> bytes.format = lambda *s: None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'bytes'
So, no, you can't do this. You would need to modify the C code that creates the bytes class - at which point, you might as well just apply the patch from the relevant bug.

Does a variable override method here

I am starting to play around with python a little, and as a novice i tried this:
>>> s="";str=""
>>> for x in [ 1,2,3,4,5,6 ] :
... s += str(x)
...
Traceback (most recent call last):
File "<console>", line 3, in <module>
TypeError: 'str' object is not callable
I accidentally declared a variable called str (str is supposed to be a function).
Assuming it would break the semantics too much, even in a dynamically
typed language, is there a namespace i can use to qualify methods
like str and ensure this does not happen or make it difficult?
This is what import <module> instead of from <module> import * is used for. As long as you use str in the only meaning of local variable value in <module>, you can use
module.str elswhere, without mangling namespace.
The only tokens that can't be clashed are keywords. This is intended functionality and there is no way to prevent this: everything is an object in Python
You might want to use some IDE tools, p.ex. Eclipse+PyDev, that checks your code and warn for possible errors.
As per your question you have already defined str=""
so when you will call str method which converts values into string it will not call actual method in place of that it will call str="".
that's why you are getting error because you can not call a str object to convert int to string.

Categories