Categories
Python Answers

How to imploding a list for use in a Python MySQLDB IN clause?

Sometimes, we want to imploding a list for use in a Python MySQLDB IN clause

In this article, we’ll look at how to imploding a list for use in a Python MySQLDB IN clause.

How to imploding a list for use in a Python MySQLDB IN clause?

To imploding a list for use in a Python MySQLDB IN clause, we call cursor execute with a tuple.

For instance, we write

format_strings = ",".join(["%s"] * len(list_of_ids))
cursor.execute(
    "DELETE FROM foo.bar WHERE baz IN (%s)" % format_strings, tuple(list_of_ids)
)

to call cursor.execute with SQL string that has the the format_strings interpolated into it.

And then we use tuple(list_of_ids) to inmplode a list of items in the tuple into the placeholder in the format_strings list.

Conclusion

To imploding a list for use in a Python MySQLDB IN clause, we call cursor execute with a tuple.

Categories
Python Answers

How to convert number words to integers with Python?

Sometimes, we want to convert number words to integers with Python.

In this article, we’ll look at how to convert number words to integers with Python.

How to convert number words to integers with Python?

To convert number words to integers with Python, we can use the word2number package.

To install it, we run

pip install word2number

Then we use it by writing

from word2number import w2n

print w2n.word_to_num("two million three thousand eight hundred and ninety four")

to call word_to_num with the string that we want to convert to an integer.

Conclusion

To convert number words to integers with Python, we can use the word2number package.

Categories
Python Answers

How to run script with elevated privilege on Windows?

Sometimes, we want to run script with elevated privilege on Windows.

In this article, we’ll look at how to run script with elevated privilege on Windows.

How to run script with elevated privilege on Windows?

To run script with elevated privilege on Windows, we call shell.ShellExecuteEx to run the runas command.

For instance, we write

import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'

if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    sys.exit(0)
with open("somefilename.txt", "w") as out:
    print(out)

to call shell.ShellExecuteEx with the lpVerb argument set to 'runas' to run the runas command.

We run runas with the program name and 'asadmin' to run our command as admin.

Once we’re done we call sys.exit to exit the program.

Conclusion

To run script with elevated privilege on Windows, we call shell.ShellExecuteEx to run the runas command.

Categories
Python Answers

How to determine whether integer is between two other integers with Python?

Sometimes, we want to determine whether integer is between two other integers with Python.

In this article, we’ll look at how to determine whether integer is between two other integers with Python.

How to determine whether integer is between two other integers with Python?

To determine whether integer is between two other integers with Python, we can use comparison operators.

For instance, we write

if 10000 <= number <= 30000:
    pass

to check if `numbers is between 10000 and 30000 inclusive.

Conclusion

To determine whether integer is between two other integers with Python, we can use comparison operators.

Categories
Python Answers

How to sort a list of lists by a specific index of the inner list with Python?

Sometimes, we want to sort a list of lists by a specific index of the inner list with Python.

In this article, we’ll look at how to sort a list of lists by a specific index of the inner list with Python.

How to sort a list of lists by a specific index of the inner list with Python?

To sort a list of lists by a specific index of the inner list with Python, we can use the itemgetter function.

For instance, we write

from operator import itemgetter

l = [[0, 1, "f"], [4, 2, "t"], [9, 4, "afsd"]]
sorted_l = sorted(l, key=itemgetter(2))

to call sorted with list l and the key argument set to itemgetter with index 2 to sort list l by the 3rd item in the list and return the sorted list.

Conclusion

To sort a list of lists by a specific index of the inner list with Python, we can use the itemgetter function.