Determine the firefox version available for update using Python - linux

I am looking for snippet which will check which version is available to download for updates.
I use python 3.x. So it would be nice if anyone has a hint how i can check the version available on the server. The OUtput should generate a variable in which the version number of firefox is stored. for example 22.0
I am using linux as the operating system of my choice.
to be clear:
I don't want to know whhich version is already installed on my system. i want to know which version can be updated.
So far i got the following code:
def firefox_version_remote():
firefox_version_fresh = os.popen("curl -s -l ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-i686/de/").read()
# short name for firefox version num fresh
fvnf = " "
for i in firefox_version_fresh:
if i.isalpha() != True:
fvnf = fvnf + i
return fvnf.strip()
this returns -22.0..2 where it should return 22.0

Have you considered using a regular expression to match the numbers you're trying to extract. That would be a lot easier. Something like this:
matches = re.findall(r'\d+(?:\.\d+)+', firefox_version_fresh)
if matches:
fvnf = matches[0]
That's assuming the version is of the form x.y potentially followed by more sub versions (e.g. x.y.z).
\d+ is one or more digits
(?: )+ is one or more of everything in the parentheses. The ?: tells the compiler that it's a non-capturing group - i.e. you're not interesting in extracting the data inside the parentheses as a separate group.
\.\d+ matches a dot followed by one or more digits
So the whole expression can be described as one or more digits followed by one or more occurences of a dot and one or more digits.

Related

Python regular expressions with Foreign characters in python PyQT5

This problem might be very simple but I find it a bit confusing & that is why I need help.
With relevance to this question I posted that got solved, I got a new issue that I just noticed.
Source code:
from PyQt5 import QtCore,QtWidgets
app=QtWidgets.QApplication([])
def scroll():
#QtCore.QRegularExpression(r'\b'+'cat'+'\b')
item = listWidget.findItems(r'\bcat\b', QtCore.Qt.MatchRegularExpression)
for d in item:
print(d.text())
window = QtWidgets.QDialog()
window.setLayout(QtWidgets.QVBoxLayout())
listWidget = QtWidgets.QListWidget()
window.layout().addWidget(listWidget)
cats = ["love my cat","catirization","cat in the clouds","catść"]
for i,cat in enumerate(cats):
QtWidgets.QListWidgetItem(f"{i} {cat}", listWidget)
btn = QtWidgets.QPushButton('Scroll')
btn.clicked.connect(scroll)
window.layout().addWidget(btn)
window.show()
app.exec_()
Output GUI:
Now as you can see I am just trying to print out the text data based on the regex r"\bcat\b" when I press the "Scroll" button and it works fine!
Output:
0 love my cat
2 cat in the clouds
3 catść
However... as you can see on the #3, it should not be printed out cause it obviously does not match with the mentioned regular expression which is r"\bcat\b". However it does & I am thinking it has something to do with that special foreign character ść that makes it a match & prints it out (which it shouldn't right?).
I'm expecting an output like:
0 love my cat
2 cat in the clouds
Researches I have tried
I found this question and it says something about this \p{L} & based on the answer it means:
If all you want to match is letters (including "international"
letters) you can use \p{L}.
To be honest I'm not so sure how to apply that with PyQT5 also still I've made some tries & and I tried changing the regex to like this r'\b'+r'\p{cat}'+r'\b'. However I got this error.
QString::contains: invalid QRegularExpression object
QString::contains: invalid QRegularExpression object
QString::contains: invalid QRegularExpression object
QString::contains: invalid QRegularExpression object
Obviously the error says it's not a valid regex. Can someone educate me on how to solve this issue? Thank you!
In general, when you need to make your shorthand character classes and word boundaries Unicode-aware, you need to pass the QRegularExpression.UseUnicodePropertiesOption option to the regex compiler. See the QRegularExpression.UseUnicodePropertiesOption reference:
The meaning of the \w, \d, etc., character classes, as well as the meaning of their counterparts (\W, \D, etc.), is changed from matching ASCII characters only to matching any character with the corresponding Unicode property. For instance, \d is changed to match any character with the Unicode Nd (decimal digit) property; \w to match any character with either the Unicode L (letter) or N (digit) property, plus underscore, and so on. This option corresponds to the /u modifier in Perl regular expressions.
In Python, you could declare it as
rx = QtCore.QRegularExpression(r'\bcat\b', QtCore.QRegularExpression.UseUnicodePropertiesOption)
However, since the QListWidget.findItems does not support a QRegularExpression as argument and only allows the regex as a string object, you can only use the (*UCP) PCRE
verb as an alternative:
r'(*UCP)\bcat\b'
Make sure you define it at the regex beginning.

AQL numeric literal parse error without leading zero

I get a parse error with an AQL query that includes a numeric literal with a value 'x' where
-1 < x < 1 and x != 0, and where the leading zero is omitted, such as x < .5 or x > -.2.
I think this may be a bug, but the documentation doesn't clearly state whether a leading zero is required or not (seems odd that it would be required).
I'm only running version 3.4 rc4, so I can't verify if this behavior is present with any officially released versions. Can someone confirm? Or are there any ArangoDB devs on here that care to comment?
Thanks!
AQL does not support floating point number literals without leading digit.
The documentation shows examples of supported notations, and one like .5 is not among them.
If you want to propose this as feature, create an issue on GitHub.
You can find the code that defines the language tokens for numbers here:
https://github.com/arangodb/arangodb/blob/devel/arangod/Aql/tokens.ll#L447
(0|[1-9][0-9]*) {
/* a numeric integer value */
...
(0|[1-9][0-9]*)((\.[0-9]+)?([eE][\-\+]?[0-9]+)?) {
/* a numeric double value */
As can be seen from these regular expressions, literals like 00 and 00.123 are not supported either - there must not be more than one leading 0 in the integer part.
Ran this in 3.3.19 and runs fine:
let tmp = [0.2,3,4,0.5]
for t in tmp
filter t > 0.5
return t
This throws a parsing error
let tmp = [0.2,3,4,0.5]
for t in tmp
filter t > .5
return t
So, I would think it is safe to say that the 0 is mandatory
Update
We recently merged a pull request into the devel branch of ArangoDB that adds support for fractional numbers without leading zeros to AQL. This is available in the ArangoDB development version from next build onwards, but not available in any release yet. So far, target release would be 3.5.
If there is popular demand for the feature, it should be easy to backport the pull request to ArangoDB 3.4 as well.

AWK different versions behavior when using regex pattern

Background:
Recently I have tried to build libopencm3-examples on Ubuntu 14.04 and encountered a build error (while for Ubuntu 16.04.1 LTS it works ok). I started digging in order to find the reason. As I have discovered, libopencm3 uses specific linker script generator:
see libopencm3-examples/libopencm3/ld/README
the purpose of this tool is to pass target micro controller specific defines to linker script template. So it use preprocessor under template script and pass target specific parameters like so:
-D_FPU=hard-fpv5-sp-d16 -D_ROM_OFF=0x08000000 -D_RAM_OFF=0x20000000
to retrieve this parameters awk script is used.
./libopencm3/scripts/genlink.awk
for generating -D_XXX keys this script operates under device database ./libopencm3/ld/devices.data
like so:
awk -v PAT="$(DEVICE)" -v MODE="DEFS" -f $(OPENCM3_DIR)/scripts/genlink.awk $(OPENCM3_DIR)/ld/devices.data
Question:
The awk script part, extracting the defines info from database looks like this:
...
for (i = 3; i <= NF; i = i + 1) {
...
else if ($i ~ /[[:upper:]]*=/) {
if ("DEFS" == MODE)
printf "-D_%s ",$i;
}
}
the row in database, processed by the script:
stm32f3[01]3?c* stm32f3ccm ROM=256K RAM=40K CCM=8K
What confuse me, is that the proposed pattern (/[[:upper:]]*=/) should match [ROM]=256K f.e., but not ROM=256K (yes?). Anyhow, as i already mentioned /[[:upper:]]*=/ works for ubuntu 16.04 (GNU Awk 4.1.3) (Why?), while for 14.04 i needed to change /[[:upper:]]*=/ -> /[:upper:]*=/ to force it work (is this a bug or what?). Am I missing something?
No, the square bracket character is special in regex. If you want to match it literally, use \[. The expression [:upper:] inside square brackets refers to the character class consisting of upper-case characters. I'm guessing you want
/[][:upper:][]+=/
to form a bracket expression consisting of literal closing square bracket, uppercase characters, and literal opening square bracket. Notice also the switch to + instead of * to prevent matching on a lone equals sign (* means zero or more, so with zero repetitions, it would match on any equals sign).
Possibly the Awk you have doesn't support POSIX character classes at all. Then, you could replace [:upper:] with A-Z though it won't then match locale-sensitively.

util/Natural unexpected behavior in alloy

I tried the following snippet of Alloy4, and found myself confused by the behavior of the util/Natural module. The comments explain more in detail what was unexpected. I was hoping someone could explain why this happens.
module weirdNatural
private open util/natural as nat
//Somehow, using number two obtained from incrementing one works as I expect, (ie, there is no
//number greater than it in {0,1,2}. but using number two obtained from the natrual/add function
//seems to work differently. why is that?
let twoViaAdd = nat/add[nat/One, nat/One]
let twoViaInc = nat/inc[nat/One]
pred biggerAdd {
some x: nat/Natural | nat/gt[x, twoViaAdd]
}
pred biggerInc {
some y: nat/Natural | nat/gt[y, twoViaInc]
}
//run biggerAdd for 10 but 3 Natural //does not work well, it does find a number gt2 in {0,1,2}
run biggerInc for 10 but 3 Natural //works as expected, it finds a number gt2 in {0,1,2,3}, but not in {0,1,2}
Thanks for this bug report. You are absolutely right, that is a weird behavior.
Alloy4.2 introduced some changes to how integers are handled; namely, the + and - operators in Alloy4.2 are always interpreted as set union/difference, so built-in functions plus/minus have to be used to express arithmetic addition/subtraction. On the other side, the util/natural module (mistakenly) hasn't been updated to use the latest syntax, which was the root cause of the weird behavior you experienced (specifically, the nat/add function uses the old + operator instead of plus, whereas nat/inc doesn't).
To work around this issue, you can either
open the util/natural module (choose "File -> Open Sample Models" from the main menu)
edit the file and replace the two occurrences of <a> + <b> with plus[<a>, <b>]
save the new file in the same folder with your model als file (e.g., as "my_nat.als")
open it from your main module (e.g., open my_nat as nat)
or
download the latest unofficial version where this bug is fixed (you might need to manually delete the Alloy temp folder to make sure that the Alloy Analyzer is not using the old (cached) version of the util/natural library).

R - extract number from string

I used max(list.files(path, "my_files_v")) to read in the latest my_files_v version.
The directory had files of:
my_files_v1, my_files_v2, ... my_files_v9
My code seemed to work perfectly fine as the script read in always the my_file_v with the highest version until I came to version _v10. From that point onwards max(list.files(..)) always detected _v9 as the max and therefore skipped _v10 and _v11 etc.
One solution would be to extract the version number from the string using substr or something like that but is there a better way of doing that? If not, does anyone know how to extract that pattern from a string as I am very poor regarding that
You could use:
max(as.integer(gsub('my_files_v', '', list.files(path, "my_files_v")))
assuming your files do not have extensions like you've shown. However, so they sort properly in my file browsers, I usually name files with zero padding. In R you can do that with sprintf:
sprintf('%03d', 1:10)
For what its worth, to extract the number portion of the file name you can use regular expressions and grouping:
gsub('my_files_v([0-9]+)\\.Rdata', '\\1', list.files(path, 'my_files_v'))

Resources