I have a problem with conversion from string to an integer.
I have function which return variable a = 00007fff`90492630. This variable has type Unicode. This is the address of the function that I will use to install breakpoints via pykd (plugin for windbg).
At the next function, I would like to convert this variable to int, using int(a,16).
But i have this error:
ValueError: invalid literal for int() with base 16: '00007fff`90492630'
Thanks for your help!
If you need to remove the tick in the code, you can do :
a = '00007fff`90492630'
b = int(a.translate(str.maketrans({'`':""})), 16)
print(b)
You got a single quote\tick inside your string.
When deleting it, you got the following output :
>>> int("00007fff90492630", 16)
140735614101040
Try to use pykd.expr it converts any windbg constant / symbols / expression to 64 bit long value
If you want to get offset of a function, use pykd.getOffset(func_name) instead of dbgCommand('x ' + func_name )
Related
I have been trying to change an object to string, so i first did the below code
total_deliveries_data.Order_ID = map(lambda x : x.str.split('-').str[1].str.split(',').str[0], total_deliveries_data.Order_ID)
total_deliveries_data
when i try the below code to convert the column to in.
total_deliveries_data["Order_ID"] = total_deliveries_data["Order_ID"].astype(str).astype(int)
i get
ValueError: invalid literal for int() with base 10: '<map object at 0x7f9f1f678f10>'
How can i sort this out
I am trying to format my validator message with the min/max values in the actual validator.
Here's my Flask Form:
class MyForm(FlaskForm):
example = IntegerField(label=('Integer 0-10'),
validators=[InputRequired(), NumberRange(min=0, max=10, message="must be between %(min)s and %(max)s!")])
Using message="must be between %(min)s and %(max)s!" gives me the expected output:
must be between 0 and 10!
Whereas using message=f"must be between {min} and {max}!" gives me the output:
must be between <built-in function min> and <built-in function max>!
How can I use f-string formatting for my validator message? Is this something related to f-string evaluating at run-time? I don't fully understand the concept behind it, I just know it's the preferred way to string format.
The f-string literal is evaluated immediately, before being passed to IntegerField.
>>> foo = 3
>>> print(f'{foo}')
3
The other string contains literal %(...) substrings which are
used later with the % operator.
>>> print("%(foo)s")
%(foo)s
>>> print("%(foo)s" % {'foo': 3})
3
"must be between %(min)s and %(max)s!" is a string literal that Flask will later perform a search-and-replace on, while f"must be between {min} and {max}!" is a simpler and more efficient way to say "must be between " + str(min) + " and " + str(max) + "!". That evaluates to the string you described.
You must declare such variables, like
min = 1
max = 2
print(f"must be between {min} and {max}!")
But please consider to use somewhat different variable names to not shadow builtin functions.
Ok, I see it now, you wanted to use that as a kind of string template.
I'm communicating with a modem via COM port to recieve CSQ values.
response = ser.readline()
csq = response[6:8]
print type(csq)
returns the following:
<type 'str'> and csq is a string with a value from 10-20
For further calculation I try to convert "csq" into an integer, but
i=int(csq)
returns following error:
invalid literal for int() with base 10: ''
A slightly more pythonic way:
i = int(csq) if csq else None
Your error message shows that you are trying to convert an empty string into an int which would cause problems.
Wrap your code in an if statement to check for empty strings:
if csq:
i = int(csq)
else:
i = None
Note that empty objects (empty lists, tuples, sets, strings etc) evaluate to False in Python.
As alternative you can put your code inside an try-except-block:
try:
i = int(csq)
except:
# some magic e.g.
i = False
I am trying to break out of a for loop, but for some reason the following doesn't work as expected:
for out in dbOutPut:
case_id = out['case_id']
string = out['subject']
vectorspace = create_vector_space_model(case_id, string, tfidf_dict)
vectorspace_list.append(vectorspace)
case_id_list.append(case_id)
print len(case_id_list)
if len(case_id_list) >= kcount:
print "true"
break
It just keeps iterating untill the end of dbOutput. What am I doing wrong?
I'm guessing, based on your previous question, that kcount is a string, not an int. Note that when you compare an int with a string, (in CPython version 2) the int is always less than the string because 'int' comes before 'str' in alphabetic order:
In [12]: 100 >= '2'
Out[12]: False
If kcount is a string, then the solution is add a type to the argparse argument:
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-k', type = int, help = 'number of clusters')
args=parser.parse_args()
print(type(args.k))
print(args.k)
running
% test.py -k 2
yields
<type 'int'>
2
This confusing error would not arise in Python3. There, comparing an int and a str raises a TypeError.
Could it happen that kcount is actually a string, not an integer and, therefore, could never become less than any integer?
See string to int comparison in python question for more details.
Let's say I have a string
string = '1234567890'
and I want a slice of that string defined by another string
slice = '5:8'
This is easy to do with
>>>string[5:8]
'678'
However the slice is passed in through a file and changes on user input. Is their a way of doing something such as
>>>string[eval(slice)]
'678'
When I do this I get
5:8
^
SyntaxError: invalid syntax
I have a function that accounts for all four cases, I was just wondering if their was a more elegant way of doing this.
Thanks for your answers.
You are getting the syntax error since 5:8 isn't a valid Python statement on its own; eval expects normal Python code, not just fragments.
If you really want to use eval, you can say:
string = '1234567890'
sliceInput = '5:8'
result = eval('string[' + sliceInput + ']')
However this is not at all secure if you're allowing user input. A safer way would be:
string = '1234567890'
sliceInput = '5:8'
sliceParts = sliceInput.split(':')
if len(sliceParts) != 2:
# Invalid input -- either no ':' or too many
else:
try:
start, end = [ int(x) for x in sliceParts ]
except ValueError:
# Invalid input, not a number
else:
result = string[start : end]
Note that slice() is a built-in Python function, so it isn't considered good practice to use it as a variable name.
How about:
string = '1234567890'
slice = '5:8'
sliceP = slice.split(':')
string[int(sliceP[0]):int(sliceP[1])]
The slice syntax isn't permitted outside of brackets, so it will break if you try to eval it on its own. If you really want to eval input from a file, you can construct the complete call as a string, then eval it:
eval("string[" + slice + "]")
The usual caveats about eval apply: a malicious user can get your program to execute arbitrary code this way, so you might be better off trying to parse out the bounds rather than evaling them.