Archive

Posts Tagged ‘Assignment’

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.

Suggestion: Initialize multiple fields to same value at constructor call in C#

When using http://github.com/zoomicon/ZUI I would like to write:

FloatingWindow window = new FloatingWindow()
{
  Content = display,
  Title = IconText = title
};

but I have to write:

FloatingWindow window = new FloatingWindow()
{
  Content = display,
  Title = title,
  IconText = title

};

instead. For consistency, I’d prefer that it supported such assignment. Now it thinks IconText is some external field or similar

 

you can vote for this suggestion at:

http://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/10904790-allow-to-initialize-multiple-fields-to-the-same-va

%d bloggers like this: