Why is QLocale.system() showing a different locale than one constructed from language code? - pyqt5

My system locale is Portuguese (Brazil):
>>> sys_locale = QLocale.system()
>>> QLocale.languageToString(sys_locale.language())
'Portuguese'
>>> QLocale.countryToString(sys_locale.country())
'Brazil'
When comparing it to a QLocale constructed with the language code that represents it, the result is not equal:
>>> sys_locale == QLocale('pt_BR')
False
Constructing a QLocale with parameters from system locale works, though:
>>> pt_br = QLocale(sys_locale.language(), sys_locale.country())
>>> pt_br == QLocale('pt_BR')
True
Why is that?

Related

making numpy binary file data to two decimal [duplicate]

I have a numpy array, something like below:
data = np.array([ 1.60130719e-01, 9.93827160e-01, 3.63108206e-04])
and I want to round each element to two decimal places.
How can I do so?
Numpy provides two identical methods to do this. Either use
np.round(data, 2)
or
np.around(data, 2)
as they are equivalent.
See the documentation for more information.
Examples:
>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])
If you want the output to be
array([1.6e-01, 9.9e-01, 3.6e-04])
the problem is not really a missing feature of NumPy, but rather that this sort of rounding is not a standard thing to do. You can make your own rounding function which achieves this like so:
def my_round(value, N):
exponent = np.ceil(np.log10(value))
return 10**exponent*np.round(value*10**(-exponent), N)
For a general solution handling 0 and negative values as well, you can do something like this:
def my_round(value, N):
value = np.asarray(value).copy()
zero_mask = (value == 0)
value[zero_mask] = 1.0
sign_mask = (value < 0)
value[sign_mask] *= -1
exponent = np.ceil(np.log10(value))
result = 10**exponent*np.round(value*10**(-exponent), N)
result[sign_mask] *= -1
result[zero_mask] = 0.0
return result
It is worth noting that the accepted answer will round small floats down to zero as demonstrated below:
>>> import numpy as np
>>> arr = np.asarray([2.92290007e+00, -1.57376965e-03, 4.82011728e-08, 1.92896977e-12])
>>> print(arr)
[ 2.92290007e+00 -1.57376965e-03 4.82011728e-08 1.92896977e-12]
>>> np.round(arr, 2)
array([ 2.92, -0. , 0. , 0. ])
You can use set_printoptions and a custom formatter to fix this and get a more numpy-esque printout with fewer decimal places:
>>> np.set_printoptions(formatter={'float': "{0:0.2e}".format})
>>> print(arr)
[2.92e+00 -1.57e-03 4.82e-08 1.93e-12]
This way, you get the full versatility of format and maintain the precision of numpy's datatypes.
Also note that this only affects printing, not the actual precision of the stored values used for computation.

Pandas: Fast way to get cols/rows containing na

In Pandas we can drop cols/rows by .dropna(how = ..., axis = ...) but is there a way to get an array-like of True/False indicators for each col/row, which would indicate whether a col/row contains na according to how and axis arguments?
I.e. is there a way to convert .dropna(how = ..., axis = ...) to a method, which would instead of actual removal just tell us, which cols/rows would be removed if we called .dropna(...) with specific how and axis.
Thank you for your time!
You can use isna() to replicate the behaviour of dropna without actually removing data. To mimic the 'how' and 'axis' parameter, you can add any() or all() and set the axis accordingly.
Here is a simple example:
import pandas as pd
df = pd.DataFrame([[pd.NA, pd.NA, 1], [pd.NA, pd.NA, pd.NA]])
df.isna()
Output:
0 1 2
0 True True False
1 True True True
Eq. to dropna(how='any', axis=0)
df.isna().any(axis=0)
Output:
0 True
1 True
2 True
dtype: bool
Eq. to dropna(how='any', axis=1)
df.isna().any(axis=1)
Output:
0 True
1 True
dtype: bool

Search for repeating word in text

I haven't found any straight answers.
I need to find the words in text / string that is being repeated the most.
E.g.
String that has following values:
000587\local_users
000587\local_users
4444\et-4444
et\pmostowiak
et\pmostowiak
et\pmostowiak
Then the results needs to be et\pmostowiak
How should I accomplish this?
EDIT:
I'm using older version of jython so I can't use the collections library with Counter function
This prints all values that are found more than ones:
d = {}
for x in users:
d[x] = x in d
_result = [x for x in d if d[x]] # [1]
If I could reuse this further?
Once you have some iterable container of words, collections does exactly what you need.
>>> import collections
>>> words = ['000587\local_users', '000587\local_users', '4444\et-4444', 'et\pmostowiak', 'et\pmostowiak', 'et\pmostowiak']
>>> print collections.Counter(words).most_common(1)
[('et\\pmostowiak', 3)]
This begs the question of how to split a string.
This works:
>>> str = """000587\local_users
... 000587\local_users
... 4444\et-4444
... et\pmostowiak
... et\pmostowiak
... et\pmostowiak"""
>>> str.split('\n')
['000587\\local_users', '000587\\local_users', '4444\\et-4444', 'et\\pmostowiak', 'et\\pmostowiak', 'et\\pmostowiak']
>>> words = str.split('\n')

How To Compare The Date From Two Numpy.datetime64

What is the proper method to compare the date portion of two numpy.datetime64's?
A: 2011-01-10 Type: <type 'numpy.datetime64'>
B: 2011-01-10T09:00:00.000000-0700 Type: <type 'numpy.datetime64'>
The above example would return false by comparing (A == B)
You'll want to strip your datetime64 of time information before comparison by specifying the 'datetime64[D]' data type, like this:
>>> a = numpy.datetime64('2011-01-10')
>>> b = numpy.datetime64('2011-01-10T09:00:00.000000-0700')
>>> a == b
False
>>> a.astype('datetime64[D]') == b.astype('datetime64[D]')
True
I couldn't get numpy to create an array of datetime64[D] values from the string you gave for b above, by the way. I got this error:
>>> b = numpy.array(['2011-01-10T09:00:00.000000-0700'], dtype='datetime64[D]')
TypeError: Cannot parse "2011-01-10T09:00:00.000000-0700" as unit 'D' using casting rule 'same_kind'

Changing how object appears in interpreter

Is there a way to change how an object appears when displayed at the Python interpreter? For example:
>>> test = myobject(2)
>>> test
'I am 2'
OR
>>> test = myobject(2)
>>> test
myobject(2)
Yes, you can provide a definition for the special __repr__ method:
class Test:
def __repr__(self):
return "I am a Test"
>>> a = Test()
>>> a
I am a Test
In a real example, of course, you would print out some values from object data members.
The __repr__ method is described in the Python documentation here.