5

In my test code, I want to assert that a string ends with a number. Say the number is between [0,3):

assert_equals('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/number') #valid

assert_equals('/api_vod_asset/v0/assets/1', '/api_vod_asset/v0/assets/number') #valid

assert_equals('/api_vod_asset/v0/assets/5', '/api_vod_asset/v0/assets/number') #invalid

How to use regular expression or some other technique for number ?

8 Answers 8

3

You would probably want to use assertRegex:

test_case.assertRegex('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/[012]')

The one above works in the case of [0,3) range. If you do not want that restriction, you would likely want to have:

test_case.assertRegex('/api_vod_asset/v0/assets/0', '/api_vod_asset/v0/assets/[\d]')

All the code above works after the following lines have been added to your snippet:

import unittest as ut
test_case = ut.TestCase()
Sign up to request clarification or add additional context in comments.

Comments

2

If it will always be at the same place in the string, you can use string.split

something like

def check_range(to_test, valid_range):
    return int(to_test.split('/')[-1]) in range(valid_range[0], valid_range[1] + 1)

then you can do

check_range('/api_vod_asset/v0/assets/0', [0,3]) #True
check_range('/api_vod_asset/v0/assets/1', [0,3]) #True
check_range('/api_vod_asset/v0/assets/5', [0,3]) #False

Comments

2

First, use a regex like \d+$ to get any number (\d+) at the end of the string ($)...

>>> m = re.search(r"\d+$", s)

... then check whether you have a match and whether the number is in the required range:

>>> m is not None and 0 <= int(m.group()) < 3

Or use a range, if you prefer this notation (assuming upper bound of [0,3) is exclusive):

>>> m is not None and int(m.group()) in range(0, 3)

2 Comments

Does Python's re support \D? Then use \D[0-3]$ :)
@RadLexus You mean, as in "not a number and then any of 0 to 3"? Yes, that would work, provided that we are dealing only with single-digit numbers... My solution is more generic, relying neither on number of digits nor delimiter characters but just position in the string, as it was asked in the question.
1

I want to assert that a string ends with a number

if int(myString[-1]) in [0,1,2]:
     do something...

1 Comment

Of course, because myString[-1] gives you only the last char of the string. To get 10 you need myString[-2:]. However, if you want to check 'any number of chars after the last '/' you can use myString.split('/')[-1] instead. Assuming that your strings will have the number after the last slash.
0
>>> import re
>>> match = re.search(r'/api_vod_asset/v0/assets/(\d+)', '/api_vod_asset/v0/assets/5')
>>> match
<_sre.SRE_Match object at 0x10ff4e738>
>>> match.groups()
('5',)
>>> match.group(1)
'5'
>>> x = int(match.group(1))
>>> 0 <= x < 3
False
>>> 0 <= 2 < 3
True

Comments

0

Regex:

import re
reg_func = lambda x: bool(re.match(r'.*?[0123]$', x))

Python:

py_func = lambda x: x.endswith(tuple(map(str, range(4))))

Comments

0

I'm not sure that this is the most elegant way to do this but here is what i would do:

def is_finished_correctly(path):
# The code seems self-explaining, ask if you need more detailed explanations
    return (int(path.split("/")[-1]) in range(3))

assert(is_finished_correctly('/api_vod_asset/v0/assets/0'))

Comments

0

If you don't necessarily want to use regex, try this:

a = "hello"
a[-1] = "o" # gives you the last character of a string
try: 
    int(a[-1])
    print "last position is int"
except: 
    print "last position is no int"

int() raises a ValueError when you pass a parameter in a wrong base, which is 10 per default.

You could now do something like this:

path = '/api_vod_asset/v0/assets/0'
try: 
    int(path[-1])
    # do all your stuff here
except: 
    print "wrong path"
    continue # (if you are in a loop)

(If you loop over many paths, some of which might not be want you want to access)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.