Categories
Python Answers

How to return dictionary keys as a list in Python?

Sometimes, we want to return dictionary keys as a list in Python.

In this article, we’ll look at how to return dictionary keys as a list in Python.

How to return dictionary keys as a list in Python?

To return dictionary keys as a list in Python, we can use the dict’s keys method.

For instance, we write

for key in newdict.keys():
  print(key)

to call newdict.keys to return an iterator with the keys of the newdict dictionary.

Then we use a for loop to loop through the keys.

Conclusion

To return dictionary keys as a list in Python, we can use the dict’s keys method.

Categories
Python Answers

How to send 100,000 HTTP requests quickly in Python?

Sometimes, we want to send 100,000 HTTP requests quickly in Python.

In this article, we’ll look at how to send 100,000 HTTP requests quickly in Python.

How to send 100,000 HTTP requests quickly in Python?

To send 100,000 HTTP requests quickly in Python, we can use the tornado library.

To install it we run

pip install tornado

Then we use it by writing

from tornado import ioloop, httpclient

i = 0

def handle_request(response):
    print(response.code)
    global i
    i -= 1
    if i == 0:
        ioloop.IOLoop.instance().stop()

http_client = httpclient.AsyncHTTPClient()
for url in open('urls.txt'):
    i += 1
    http_client.fetch(url.strip(), handle_request, method='HEAD')
ioloop.IOLoop.instance().start()

to create the handle_request function that gets the response and prints our the code.

Once i is 0, we have

ioloop.IOLoop.instance().stop()

to stop the async requests.

Then we create an HTTP client object with

http_client = httpclient.AsyncHTTPClient()

Then we loop through the URLs returned from urls.txt.

In the loop, we call fetch to make a GET request with each url and increment i.

Then we have

ioloop.IOLoop.instance().start()

to start making the requests asynchronously.

Conclusion

To send 100,000 HTTP requests quickly in Python, we can use the tornado library.

Categories
Python Answers

How to get the full path of the current file’s directory with Python?

Sometimes, we want to get the full path of the current file’s directory with Python.

In this article, we’ll look at how to get the full path of the current file’s directory with Python.

How to get the full path of the current file’s directory with Python?

To get the full path of the current file’s directory with Python, we can use pathlib.

For instance, we write

import pathlib
p = pathlib.Path(__file__).parent.resolve()

to get the full path of directory that the current script is in with

p = pathlib.Path(__file__).parent.resolve()

We can get the current working directory with

import pathlib

p = pathlib.Path().resolve()

Conclusion

To get the full path of the current file’s directory with Python, we can use pathlib.

Categories
Python Answers

How to convert a string to a number if it has commas in it as thousands separators with Python?

Sometimes, we want to convert a string to a number if it has commas in it as thousands separators with Python.

In this article, we’ll look at how to convert a string to a number if it has commas in it as thousands separators with Python.

How to convert a string to a number if it has commas in it as thousands separators with Python?

To convert a string to a number if it has commas in it as thousands separators with Python, we can use the locale module.

For instance, we write

import locale

locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) 
locale.atoi('1,000,000')

to call setlocale to set the locale.

Then we call locale.atoi to convert the string to an integer.

We can also use atof to convert the number string to float by writing

locale.atof('1,000,000.53')

Conclusion

To convert a string to a number if it has commas in it as thousands separators with Python, we can use the locale module.

Categories
Python Answers

How to convert pyspark string to date format with Python?

Sometimes, we want to convert pyspark string to date format with Python.

In this article, we’ll look at how to convert pyspark string to date format with Python.

How to convert pyspark string to date format with Python?

To convert pyspark string to date format with Python, we can use the to_timestamp method.

For instance, we write

from pyspark.sql.functions import to_timestamp

df = spark.createDataFrame([("1997-02-28 10:30:00",)], ["t"])
r = df.select(to_timestamp(df.t, "yyyy-MM-dd HH:mm:ss").alias("dt")).collect()

to create a data frame with spark.createDataFrame.

Then we call select to convert the datetime string in the data frame to a timestamp with to_timestamp.

Conclusion

To convert pyspark string to date format with Python, we can use the to_timestamp method.