Archive

Posts Tagged ‘Python’

HowTo: Extract numeric suffix from a string in Python

I recently needed to extract a numeric suffix from a string value in Python. Initially I did the following:

import re

def extractNumSuffix(value):

    return (None if (search:=re.search(‘^\D*(\d+)$’, value, re.IGNORECASE)) is None else search.group(1))

Note that return has a single-line expression.

So

print(extractNumSuffix("test1030"))

prints 1030

Tried it online at:

https://repl.it/languages/python3

extractNumSuffix

However, then I found out that Assignment Expressions in Python only work from Python 3.8 and up, so I changed it to this one:

import re

def extractNumSuffix(value):
    search=re.search(r"^\D*(\d+)$", value, re.IGNORECASE)
    return (None if search is None else search.group(1))

which should work in Python 2.x too. Don’t forget to import the regular expressions (re) module.

HowTo: Install Python, PIP and cx_Oracle on MSYS2 / Windows

Here’s how to install Python, PIP package installer and cx_Oracle extension module on MSYS2:

$ pacman -S python
warning: python-3.8.3-1 is up to date — reinstalling
resolving dependencies…
looking for conflicting packages…

Packages (1) python-3.8.3-1

Total Installed Size:  108.66 MiB
Net Upgrade Size:        0.00 MiB

:: Proceed with installation? [Y/n] y

$ pacman -S cx_Oracle
error: target not found: cx_Oracle

$ pip install cx_Oracle
bash: pip: command not found

$ pacman -S python-pip
resolving dependencies…
looking for conflicting packages…

Packages (2) python-setuptools-47.1.1-1  python-pip-20.1.1-1

Total Download Size:    2.22 MiB
Total Installed Size:  10.91 MiB

:: Proceed with installation? [Y/n] y
:: Retrieving packages…

$ pip install cx_Oracle
Collecting cx_Oracle
  Downloading cx_Oracle-8.0.0.tar.gz (325 kB)
     |████████████████████████████████| 325 kB 693 kB/s
Using legacy setup.py install for cx-Oracle, since package ‘wheel’ is not installed.
Installing collected packages: cx-Oracle
    Running setup.py install for cx-Oracle … error
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3.exe -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-wqtzv2rv/cx-Oracle/setup.py’"’"’; __file__=’"’"’/tmp/pip-install-wqtzv2rv/cx-Oracle/setup.py’"’"’;f=getattr(tokenize, ‘"’"’open’"’"’, open)(__file__);code=f.read().replace(‘"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, __file__, ‘"’"’exec’"’"’))’ install –record /tmp/pip-record-mui2mjs9/install-record.txt –single-version-externally-managed –compile –install-headers /usr/include/python3.8/cx-Oracle
         cwd: /tmp/pip-install-wqtzv2rv/cx-Oracle/
    Complete output (17 lines):
    running install
    running build
    running build_ext
    building ‘cx_Oracle’ extension
    creating build
    creating build/temp.msys-3.1.5-x86_64-3.8
    creating build/temp.msys-3.1.5-x86_64-3.8/src
    creating build/temp.msys-3.1.5-x86_64-3.8/odpi
    creating build/temp.msys-3.1.5-x86_64-3.8/odpi/src
    x86_64-pc-msys-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -march=x86-64 -mtune=generic -O2 -pipe -march=x86-64 -mtune=generic -O2 -pipe -march=x86-64 -mtune=generic -O2 -pipe -DCXO_BUILD_VERSION=8.0.0 -Iodpi/include -Iodpi/src -I/usr/include/python3.8 -c src/cxoApiType.c -o build/temp.msys-3.1.5-x86_64-3.8/src/cxoApiType.o
    In file included from src/cxoModule.h:14,
                     from src/cxoApiType.c:11:
    /usr/include/python3.8/Python.h:44:10: fatal error: crypt.h: No such file or directory
       44 | #include <crypt.h>
          |          ^~~~~~~~~
    compilation terminated.
    error: command ‘x86_64-pc-msys-gcc’ failed with exit status 1
    —————————————-
ERROR: Command errored out with exit status 1: /usr/bin/python3.exe -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-wqtzv2rv/cx-Oracle/setup.py’"’"’; __file__=’"’"’/tmp/pip-install-wqtzv2rv/cx-Oracle/setup.py’"’"’;f=getattr(tokenize, ‘"’"’open’"’"’, open)(__file__);code=f.read().replace(‘"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, __file__, ‘"’"’exec’"’"’))’ install –record /tmp/pip-record-mui2mjs9/install-record.txt –single-version-externally-managed –compile –install-headers /usr/include/python3.8/cx-Oracle Check the logs for full command output.

Found a solution here for other Linuxes, mentioning

apt install python-dev clang libcrypt-dev

Via MSYS2 package manager (pacman), libcrypt and libcrypt-devel seem to be available. The second one should fetch the first one if needed, together with header files for development.

$ pacman -S libcrypt-devel
resolving dependencies…
looking for conflicting packages…

Packages (1) libcrypt-devel-2.1-2

Total Download Size:   0.04 MiB
Total Installed Size:  0.04 MiB

:: Proceed with installation? [Y/n] y

Now we can install cx_Oracle succesfully:

$ pip install cx_Oracle
Collecting cx_Oracle
  Using cached cx_Oracle-8.0.0.tar.gz (325 kB)
Using legacy setup.py install for cx-Oracle, since package ‘wheel’ is not installed.
Installing collected packages: cx-Oracle
    Running setup.py install for cx-Oracle … done
Successfully installed cx-Oracle-8.0.0

Python rstrip and whitespace

Was just reading

https://www.w3schools.com/python/ref_string_rstrip.asp

which says

The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.

Notes:

1) the chars param was added at Python 2.2.3, can’t use it at older versions as noted at

https://docs.python.org/2.6/library/string.html

string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

2) from the official doc and other docs too I read the default is to remove whitespace characters, not just the space char:

https://python-reference.readthedocs.io/en/latest/docs/str/rstrip.html

chars
Optional. String specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped.

Not sure what is considered whitespace in various Python versions though. At least in Python2 it wasn’t removing \r in both Solaris and MSYS2 (a POSIX environment on Windows) where I just tried.

For example, I was just debugging some program that was working in Python 3, but in Python 2 it was moving the cursor to the start of the line when printing a raw_input prompt with some string it had read before…
…the issue proved to be that it was opening a file with ‘r’ mode instead of ‘rU’ which is universal newlines mode – https://www.python.org/dev/peps/pep-0278/ – converts \r\n to \n – and it seems that rstrip was failing to remove the \r from the end of those strings.

In Python 3 it was either using the universal newlines mode by default and thus stripping the \r from strings while reading from the file, or the rstrip was removing \r too in Python3, but I guess it was the 1st case (didn’t do any more check since the universal newlines read file open mode fixed the issue in Python 2 for me).

Speaking of that, I wonder whether Python considers whitespace differently on Windows and on Unixes (aka having [l/r]strip commands remove \r on the 1st but not on the 2nd case), which would be an extra complexity when writing portable s/w.

HowTo: make raw_input & input work the same in both Python 2 and 3

Was just trying to make a Python 2 script work in Python 3 and at first it seemed I just needed a small change to add missing parentheses to the argument of a couple of print statements.

But then another issue came up, it wasn’t understanding the command raw_input(somePrompt) that was occuring at various places in that file to input text from the console.

Various solutions were proposed at

https://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3/62825723

but I think I came up with a cleaner looking solution that works in both Python2 and Python3, that should allow one to use either raw_input or input in both Python2 and Python3 with the semantics of Python2’s raw_input (aka the semantics of Python3’s input).

# raw_input isn't defined in Python3.x, whereas input wasn't behaving 
# like raw_input in Python 2.x. This should make both input and raw_input 
# work in Python 2.x/3.x like the raw_input from Python 2.x 
try: input = raw_input
except NameError: raw_input = input

In practice this came up from ideas at other answers on that SO thread. It tries to define input as raw_input which should fail in Python3.x since raw_input is undefined. In that case it will catch a NameError exception and do the reverse, aka define raw_input as input. However in Python2 the first command should execute fine, overriding the insecure input of Python2.x to work the same as raw_input (not trying to interpret input strings that is). Actually that is what Python 3.x input does.

Wonder why they didn’t declare it like that in the first place though, breaking compilation of Python2 programs. After all semantically only Python2 programs that were using the old insecure input would have an issue, not those that were using raw_input which is the semantics Python3 promotes with its newer input.

Twitter #bot or not? Posing as Automation/AI specialist & Python guru

I just came across this strange thing on twitter:

Received a notification that some “Virginia A. Osborn” liked a tweet of mine that was a) in Greek b) totally of a subset of Greek residents interest, highly unlikely it would have meant anything to a person with a foreign name. Then I saw that twitter account started following me immediately after that action.

image

It smelled bot to me, so I took a look at that account’s profile. She seems to have joined twitter long ago (back in 2010), so I should have probably checked her very first tweets to see what that account was about (wonder if there is an archive of what the profile details were saying back then in case that was a dormant bot).

image

It seems that account has quite some followers and several people that post AI-related stuff that I follow, plus it didn’t seem to post unrelated stuff:

image

However, I decided to look her name up on Google. I didn’t find much. So I used Google Image Search to look up her profile photo and bingo, it came up at a stock photo collection, together with other shots of that model posing on the same business-related stock photo theme: https://www.istockphoto.com/search/stack/694124043?assettype=image

image

So, is this a personna account of some real person or are bots ruling twitter (wonder what the ratio of real persons to bots is currently) and preparing for a future bot-a-gedon? (similar to infected zombie machines that is – wonder if such accounts are already pushing malware links or something).

Of course there’s always the chance that person did a deal with some photographer to pose for him and then used one of those photos in their profile, but I find it highly unlikely.

%d bloggers like this: