Myashka/SO-Python_QA-filtered-2023-tanh_score-after_2023_02 huggingface dataset:
Title: Get values of DataFrame index column
Tags: python,pandas,dataframe

Question: I have a DataFrame with an index column that uses pandas TimeStamps.
How can I get the values of the index as if it were a normal column (df["index"])?

Answer: You can convert the index to a regular column by using the reset_index() method.
Title: Parsing data with unusual format in python
Tags: python,parsing

Question: I have this ASCII file that is formatted somewhat oddly where the headers are organized by rows followed the corresponding values and their assoicated error also organized by rows.  A snippet of the file is shown below.
f     7523
  10001  10002  10003  10004  10005  10006  10007  10008  10009  10010  10011
  10012  10013  10014  10015  10016  10017  10018  10019  10020  10021  10022
vals
  0.00000E+00 0.0000  7.65079E-12 0.7071  0.00000E+00 0.0000  3.87977E-14 0.5513
  0.00000E+00 0.0000  0.00000E+00 0.0000  0.00000E+00 0.0000  0.00000E+00 0.0000
  0.00000E+00 0.0000  1.92698E-14 0.7071  4.47277E-14 0.7071  0.00000E+00 0.0000
  0.00000E+00 0.0000  0.00000E+00 0.0000  0.00000E+00 0.0000  1.10023E-11 0.7053
  1.74410E-11 0.5005  0.00000E+00 0.0000  1.69181E-13 0.7071  0.00000E+00 0.0000
  0.00000E+00 0.0000  0.00000E+00 0.0000
vals
  1.34313E-06 0.0104  1.32163E-06 0.0102  1.30039E-06 0.0105  1.37575E-06 0.0106
  1.30792E-06 0.0102  1.28351E-06 0.0104  1.32164E-06 0.0102  1.32969E-06 0.0104
  1.31707E-06 0.0104  1.27281E-06 0.0103  1.28429E-06 0.0106  1.27027E-06 0.0105
  1.29623E-06 0.0105  1.32037E-06 0.0101  1.28948E-06 0.0105  1.33163E-06 0.0106
  1.36073E-06 0.0102  1.35462E-06 0.0102  1.38641E-06 0.0102  1.33099E-06 0.0102
  1.35307E-06 0.0100  1.33882E-06 0.0105

I want to parse this file so that the data is organized in five separate columns so that it looks something like
10001      0.00000E+00 0.0000   1.34313E-06 0.0104
10002      7.65079E-12 0.7071   1.32163E-06 0.0102
10003      0.00000E+00 0.0000   1.30039E-06 0.0105
....
10022      0.00000E+00 0.0000   1.33882E-06 0.0105

I am unsure of how to cycle through this data to have the above format however beyond the simple start of it with
# Open file
f = open('data.txt', 'r')

# Loop over lines and extract variables of interest
for line in f:
    line = line.strip()
    columns = line.split()

Answer: Read the file line by line.
If the current line starts with f, you have started a new header section.  (It's not clear what you want to do with the 7523 value that is on the same line as the f in your example)
Otherwise if the current line starts with vals, you have started a new values section.
Otherwise you're in the middle of a header or values section that has already started.  Read the values from the line into a list and store them appropriately.
When the file is exhausted, print all the headers and values.
Title: How do i bind "mouse is pressed" in Python's tkinter?
Tags: python,tkinter,mouseclick-event

Question: I'm new to tkinter and I'm coding a simple "Paint" program, that draws a pixel on screen when I press my mouse.
So i tried to bind my function "is_pressed" like this:
#is pressed
def is_pressed(event):
    print('Mouse is pressed!')

#creating canvas
my_canvas = tkinter.Canvas(height=400, width=600)
my_canvas.pack()

#binding
my_canvas.bind('<Button-1>', is_pressed)

Now, I want this function to run when mouse is pressed, not when it's just clicked. How do i do it?

Answer: The event you need to bind to is <ButtonPress-1>, if you want to call a function when the button is pressed.
If you want to continuously call a function when the mouse is moved while the button is pressed, then you can bind to <B1-Motion>. That would allow you to draw as the user moves the mouse around.
Title: Why are rvecs and tvecs different for every view in OpenCV's calibratecamera?
Tags: python,opencv

Question: I'm struggling to understand the real meaning of revecs and tvecs in the output of cv2.calibrateCamera. As I understand them, together they define the pose of the camera, meaning position and orientation of the camera.
However, if I pass multiple views shot from the same camera to cv2.calibrateCamera I get rvecs and tvecs for every view. Shouldn't they be the same if the camera didn't move?
My homography matrix for the first image is defined by
K = mtx
R = cv2.Rodrigues(rvecs[0])[0]
T = tvecs[0]
RT = np.concatenate((R,T),axis=1)
H = np.dot(K, RT)

And I can see that it projects correctly. The way I understand homography it should stay the same for every image as the camera is not moving. However if R, T are changing H will change too.
It seems like I'm missing something very important about the way rvecs and tvecs are defined in OpenCV.

Answer: rvec and tvec allows to map points in WORLD coordinate system to CAMERA coordinate system. If you change your world coordinate system origin or orientation (which is probably the case if your world coordinate system is aligned with a chessboard and the chessboard is changing orientation/position in each frame), then it will change  rvec and tvec too even if the camera did not physically change position.
Title: SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed : self signed certificate in certificate chain (_ssl.c:992)
Tags: python,ssl,ssl-certificate,jira-rest-api

Question: I am using python to make a get request to jira cloud rest api to get details of an issue, but getting this SSL verification failed error message, I am using this script
import requests
import json

url = "https://your-domain.atlassian.net/rest/agile/1.0/issue/{issueIdOrKey}"

headers = {
  "Accept": "application/json",
  "Authorization": "Bearer <access_token>"
}

response = requests.request(
   "GET",
   url,
   headers=headers
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

error message-

requests.exceptions.SSLError:
HTTPSConnectionPool('host=your-domain.atlasian.net', port=443): Max
retries exceeded with url: /rest/agile/1.0/issue/{issueIdOrKey}
(caused by SSLError(SSLCertVerificationError(1, '[SSL:
CERTIFICATE_VERIFY_FAILED] certificate verify failed : self signed
certificate in certificate chain (_ssl.c:992)')))

Suggest me possible ways to resolve this issue.
Thank you!

Answer: Are you using YOUR domain name in the request?
From the errors, it looks like you're trying to use the fictitious domains my-domain.atlassian.net and your-domain.atlassian.net in your request.
Title: How can I improve the speed and performance of database queries?
Tags: python,django

Question: I'm currently working on a Django project where I'm encountering performance issues, especially as my application scales. To provide better context, I've included a realistic code snippet below that represents a performance-intensive section of my application:
from django.db import models

class Order(models.Model):
    # Fields and relationships...

class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    product = models.ForeignKey('Product', on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()

class Product(models.Model):
    # Fields and relationships...

def calculate_total_price(order_id):
    order_items = OrderItem.objects.filter(order_id=order_id).select_related('product')
    total_price = 0

    for item in order_items:
        product_price = item.product.price
        quantity = item.quantity
        total_price += product_price * quantity

    return total_price

In the above code snippet, I have an Order model that is associated with multiple OrderItems, and each OrderItem references a Product. The calculate_total_price function is used to calculate the total price for a given order by iterating over its associated OrderItems. However, as the number of OrderItems increases, the calculation becomes significantly slower.
I would greatly appreciate any insights and suggestions on how to optimize this code snippet or any other general Django performance tips. Are there any specific techniques, database query optimizations, caching mechanisms, or Django features I should consider to enhance the performance of this code? Additionally, if there are any profiling tools or libraries you recommend for identifying and resolving performance issues, please let me know.
I'm keen to learn best practices and advanced techniques for optimizing Django performance, as I want to ensure my application performs efficiently as it grows. Thank you in advance for your valuable assistance!
I'm not sure what to do about the performance issues so I don't know where to begin optimisations.
Edit
Including example view code:
class OrderDetailView(View):
    template_name = 'order_detail.html'

    def get(self, request, order_id):
        order = Order.objects.get(id=order_id)
        total_price = calculate_total_price(order_id)
        context = {
            'order': order,
            'total_price': total_price
        }
        return render(request, self.template_name, context)

Answer: Switch from SQLite to PostgreSQL as your database to improved performance and efficiency
Title: Can I prevent a method called by another method from being called without changing the definition of the original method?
Tags: python

Question: In OpenAI's Gymnasium environments, there's a render() method that at the end calls pygame.display.flip() to draw on the screen. I'm trying to prevent the render() method from calling the pygame.display.flip() method without changing the definition of the render() method. Is that possible in any way?

Answer: If this is for testing environment, then you might be able to use unittest.mock.patch to disable the call.
If you want to do this is a production code things gets a bit more tricky, but since it's open source, what you can do is just build your own version of the library.
It is possible to use unittest.mock for monkey patching outside the context of unit tests, it's not very recommendable, but if you know what you're doing, we're all consenting adults here.
Title: Is it possible to integrate python with CSS, Javascript, and HTML?
Tags: javascript,python,html,django,web

Question: I have a frontend web application built with JavaScript, HTML, and CSS. I would like to incorporate the output of a Python script into my frontend without using a server or a full backend solution.
The Python script is a simple neural network, and I want to display its output in the frontend, such as showing the result as a header or plotting a graph generated by the script.
I have already tried printing the output to the console from the Python script, but I need assistance with transferring that output to the frontend.
Is it possible to achieve this without a server? I am open to suggestions and starting with a simpler solution, like plotting a graph generated by the Python script in the frontend.
I appreciate any guidance or code examples on how to accomplish this. Thank you!
I have looked into Django but I feel as if that seems too complex and more server focused for the simple task I require.
! ANSWERED ! It only lets me accept one answer but I would say that both the streamlit and the Wasm Answers are acceptable.

Answer: looks like you are trying to make a web application with no backend.
javascript is your only hope, if there is no backend.
it is not possible to integrate python with CSS, Javascript, and HTML without a backend.
Title: I wrote a code in two ways with just one line different but I am getting different results
Tags: python

Question: I wrote a code in two ways with just one line different but I am getting different results. In one code I used else statement while in one I did not can someone please explain to me the difference between the two. I am a beginner at coding.
THE FIRST CODE
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        numMap = {}
        n = len(nums)

        # Build the hash table
        for i in range(n):
            numMap[nums[i]] = i

        # Find the complement
        for i in range(n):
            complement = target - nums[i]
            if complement in numMap and numMap[complement] != i:
                return [i, numMap[complement]]

        return []  # No solution found

THE SECOND ONE
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        numMap = {}
        n = len(nums)
        
    for i in range(n):
        numMap[nums[i]] = i

    for i in range (n):
        complement = target - nums[i] 
        if complement in numMap and numMap[complement] != i:
            return[i,numMap[complement]]
        else:
             return []

Answer: In the first implementation the case of no solution (return []) will only be hit after the completion of the iteration (the second for loop).
In the second implementation, instead, the function will always return after checking the first element.
Title: I wrote a code in two ways with just one line different but I am getting different results
Tags: python

Question: I wrote a code in two ways with just one line different but I am getting different results. In one code I used else statement while in one I did not can someone please explain to me the difference between the two. I am a beginner at coding.
THE FIRST CODE
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        numMap = {}
        n = len(nums)

        # Build the hash table
        for i in range(n):
            numMap[nums[i]] = i

        # Find the complement
        for i in range(n):
            complement = target - nums[i]
            if complement in numMap and numMap[complement] != i:
                return [i, numMap[complement]]

        return []  # No solution found

THE SECOND ONE
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        numMap = {}
        n = len(nums)
        
    for i in range(n):
        numMap[nums[i]] = i

    for i in range (n):
        complement = target - nums[i] 
        if complement in numMap and numMap[complement] != i:
            return[i,numMap[complement]]
        else:
             return []

Answer: Your second code does return from inside the for loop.
Thus it only executes the very first iteration of your loop and then returns from either the if or the else branch. It will never reach the second iteration of the loop.
In contrast, you first first code either returns from inside the loop (once the condition of the if statement is met) or it returns after iterating through the whole loop (if the condition of the if statement is never met while running through the loop).
Title: Fastapi service deployed on AWS keeps restarting every 10 minutes
Tags: python,amazon-web-services,fastapi,openai-api

Question: I have a fastapi service deployed on AWS, the API sends request to OpenAI API to generate text.
This API had been running fine for 2-3 days but now it keeps restarting on it's own after every 10 minutes. There are no errors or any helpful information in the AWS logs.
I also set request_timeout to 30 seconds in case this was happening because OpenAI API took too long to respond.
Any info about this is appreciated.
edit:
start.sh file contains this line: python3 main.py
and main.py is as follows:
import uvicorn

if __name__ ="__main__":
   uvicorn.run("app.main:app, host="0.0.0.0", port=9026, reload=True)

Note: When the service is not being used the container does not restart, it only happens when requests are being made to the service that the container restarts every 10-20 minutes depending on the number of requests.

Answer: you need to try uvicorn app.main:app --reload --host 0.0.0.0 --port 9026
in bash script to run, It is just another way to run.
Title: To print contents of linked list in an array using a def
Tags: python,arrays,data-structures,linked-list

Question: This is Python Code to create a linked list from scratch. Inside def printList(self): I want to print all the values from linked list in the form of an array. But it shows an error which you can see in the end.
class LinkedList:
    def __init__(self, value):
        self.head = {'value': value, 'next': None}
        self.tail = self.head
        self.length = 1
      
    def Append(self, value):
        newNode = {'value': value, 'next': None}

        self.tail['next'] = newNode
        self.tail = newNode
        self.length += 1
       
    def prepend(self, value):
        newNode = {'value': value, 'next': None}
        newNode.next: self.head
        self.head = newNode
        self.length += 1
    
        print("Head: ", f"{self.head}")
        print("Tail: ", f"{self.tail}")
        print("Length: ", f"{self.length}")

    def printList(self):
        array = []
        currentNode = self.head
        while currentNode:
            array.append(currentNode.value)
            currentNode = currentNode.next
        print(array)

# I want it to print: [2,5,12]

myLinkedList = LinkedList(5)
myLinkedList.Append(12)
myLinkedList.prepend(2)
myLinkedList.printList()

Output Error:
Traceback (most recent call last):
  File "/Users/asmitagharat/Desktop/projects/python/ds.py", line 213, in <module>
    myLinkedList.printList()
  File "/Users/asmitagharat/Desktop/projects/python/ds.py", line 205, in printList
    array.append(currentNode.value)
AttributeError: 'dict' object has no attribute 'value'. Did you mean: 'values'?

Answer: If you have a dictionary currentNode containing a key 'value', then the value corresponding to that key is not obtained as currentNode.value, but instead as currentNode['value'].
Title: Space of using range(n)
Tags: python,range

Question: Could you tell me please, if I use for i in range(n), does python creates a range of 0 .. n-1, and iterates elements in this container (O(n) extra space), or it use only 1 vatiable (O(1)) space)
From one side, I thought, if we can convert range to list, then using range function creates a container (O(n)).but from the other side, instead of using for i in range(n), we can use while i < n ... O(1).

Answer: In Python 3, range does not create all the elements, but only returns the current element when you request it. So yes, it's using O(1) space.
Title: Space of using range(n)
Tags: python,range

Question: Could you tell me please, if I use for i in range(n), does python creates a range of 0 .. n-1, and iterates elements in this container (O(n) extra space), or it use only 1 vatiable (O(1)) space)
From one side, I thought, if we can convert range to list, then using range function creates a container (O(n)).but from the other side, instead of using for i in range(n), we can use while i < n ... O(1).

Answer: This was one of the big changes between Python 2 and Python 3. In Python 2, range created a full list, but you could avoid creating a list by instead using xrange.  So in Python 2, you'd do for i in xrange(...) to efficiently iterate over a range without creating a temporary list.
In Python 3, this was changed so that range behaves like the Python 2 xrange. To get the Python 2 range behavior in Python 3, i.e. to create the full list, you can do list(range(...)), but of course you want to avoid that unless you truly need the list.
Title: How do I map ports inside the Dockerfile?
Tags: python,docker,flask,dockerfile,port

Question: I have a Docker image webshop (containing a Python Flask web application) that only runs when executed with the additional option:
docker run -p 80:80 webshop

-p 80:80 maps port 80 in the host to port 80 inside the docker container. Without this mapping, the image doesn't execute correctly.
Is there a way to specify this mapping inside the Dockerfile, so I don't need to include this additional option every time I try to run the image? In particular, I want others to be able to execute this image without additional commands.

Answer: No, and it wouldn't make any sense.
Your Dockerfile is a blueprint for how your environment should look like. It can't define things that are outside of its environment, for example, mapping ports on the host machine.
You can use EXPOSE as metadata in your Dockerfile. EXPOSE is for readability and tells the reader, what ports are being exposed from running your image. Again, it's strictly for readability purposes and doesn't actually do anything, but it's still a really good idea to have it.
But for host port mapping, that's done exclusively outside of the image itself and durning the runtime of your container.
Title: How to submit a form embedded within Folium HTML Popup in python
Tags: python,django,iframe,folium

Question: I am developing a web app in Django/Folium/Python to add markers to the map and I want to click on the marker and a popup appears with a form to be filled out and submitted. I have successfully developed that app but the submission does not work. If I add the URL inside the code it gives me this error: Encountered unknown tag 'url'
This is my code in Python:
def monitoring(request, pk):
map = folium.Map(location=[lat, lng], zoom_start=15)

html = f"""<form action="{% url 'index' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="note heading">Note heading</label>
<input type="text" name='note_heading' class="form-control">
</div>
    
<input type="hidden" name="lat" value="${lat}">
<input type="hidden" name="lng" value ="${lng}">
    
<div class="form-group">
<label for="note">Note</label>
<textarea class='form-control' name="note_des" >Enter note here</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>`
"""
iframe = folium.IFrame(html, width=200, height=200)
popup = folium.Popup(iframe)  # , max_width=100)
marker = folium.Marker([lat, lng], popup, draggable=True).add_to(map)
map = map._repr_html_()

context = {"lat": lat, "lng": lng, "map": map}
return render(request, "maps/monitoring.html", context)

Answer: in the third line of your python function, you have to write url_for('index') instead of url 'index'
This is the reason for the error
Title: getting error as 'list' object cannot be interpreted as an integer when one list is not showing any error
Tags: python,list,typeerror

Question: I am new to Python. I separated lists into two other lists with one having starting n elements and the other list having the ending n elements.
list = [1,2,3,4,5,6,7,8]
n=3
res = list[-n:]
list_i=[]
list_j=[]
for i in range (0,n):
    list_i.append(list[i])
    print(list_i)
for j in range(0,res):
    list_j.append(res[j])
    print(list_j)

I tried to use the same for loop for the list_j but it gives TypeError: 'list' object cannot be interpreted as an integer. I am not clear as the type for list and res both are <class 'list'>

Answer: In your first loop, you iterate from 0 to 3.
In the second loop, you are iterating from 0 to res. res is a list with values [6, 7, 8].
The error is saying the following: A list is not an integer. Your stop value MUST be a integer.
Hope this helps!
Title: Print is printing elements of linked list without loop
Tags: python,object,linked-list,f-string

Question: I am creating linked list. In my linked list i have str method in both classes Node and LinkedList. What i was trying to do is, print object in readable format whenever i print object to console. So i used str method in Node class, but i have no idea how this is printing all elements in the linked list without any loop whenever i use print() function.
class LinkedList:
    def __init__(self):
        self.head = None
    
    def __str__(self):
        if self.head is not None:
            return str(self.head)
        return 'empty linked list'
    
    def insert(self, data):
        if self.head is None:
            self.head = Node(data)
        else:
            head = self.head
            while head.next is not None:
                head = head.next
            head.next = Node(data)
        

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
    
    def __str__(self):
        return f"{self.data}\n{self.next}"
        
        
elems1 = [1,2,3,4]

llist1 = LinkedList()

for elem in elems1:
    llist1.insert(elem)
 
print(llist1)
    

I figured out that using {self.next} in str method in Node class is causing such behaviour but i have now idea how this is working.

Answer: Lets go down (or up if you prefer) the call stack.

print(llist1) this calls the method LinkedList.__str__
that LinkedList.__str__ then calls str(self.head) which then calls the method Node.__str__
that Node.__str__ then returns the expression f"{self.data}\n{self.next}"
in evaluating that expression, you then must call the self.next and then stringifyit. (putting that expression self.next in that f-string calls the __str__ method)
so now you've called Node.__str__ on self.next which as we know will then call its self.next and so on and so on until self.next == None in which case there's no more __str__ method to call
self.next will return the empty string  and stop the recursive call stack

You've created a recursive program without realizing it.
Title: zipfile.badzipfile even when I'm not reading zip file using pandas
Tags: python,pandas,dataframe

Question: In the current path there are multiple folders, each folder has multiple folders or xlsx files inside,I want to iterate through each folder and read the xlsx files until there are no more folders or until all xlsx files are read. There are 50+ folders and 2000+ excel files. Below is my code:
import os
import pandas as pd

current_path=os.getcwd()
dfs = []

def process_folder(path):
    for item in os.listdir(path):
        item_path=os.path.join(path, item)

        if os.path.isdir(item_path):
            process_folder(item_path)
        
        elif item.endswith('.xlsx'):
            df = pd.read_excel(item_path)
            dfs.append(df)

process_folder(current_path)
result_df = pd.concat(dfs, ignore_index=True)
result_df.to_excel('result.xlsx')

when I run the code, it shows error:"Excel file cannot be determined, you must specify an engine manually". So I modified read_excel: df = pd.read_excel(item_path, engine='openpyxl').
Then there is the error: "zipfile.BadZipFile: File is not a zip file."
However, I didn't read any zipfile. Not sure why this error shows up.

Answer: That error happens because a XLSX file is, essentially, a zipfile with a bunch of XML files inside it.
I would recommend looking which of the XLSX files are causing the error, then checking if the affected ones don't happen to be just renamed XLS files; open those on Excel, them use Save as... to save them as proper XLSX files.
Title: Unable to list tag bindings for a GCP resource
Tags: python,google-cloud-platform,cloud

Question: I'm trying to list tag bindings for a compute instance using python client libraries resourcemanager_v3.TagBindingsClient
but it is asking for a location parameter, which is not there in the request parameters provided in the docs. I thought that It will take the location of the instance as the location parameter if I provide the full URL of the instance but it results in the same error. Here's my code
client = resourcemanager_v3.TagBindingsClient()
request = resourcemanager_v3.ListEffectiveTagsRequest(
    parent = "//compute.googleapis.com/projects/{projectId}/zones/{zone}/instances/{instanceId}"
)
page_result = client.list_effective_tags(request = request)

for response in page_result:
    print(response)

I checked REST API for list tag bindings but it doesn't have location argument as well. Is there any solution for this?

Answer: Unfortunately there is no location parameter for list_effective_tags. This was an issue in terraform and they solved it but not on python client libraries
Title: distribute randomly a value to a fixed size list of values
Tags: python,numpy

Question: I have a value (a sum) (in the implementation it is n) that I want to distribute "randomly" to become a list of 12 elements summing up to that value (sum), I wrote the following method to achieve that:
def create_list_summing_up_to(n): 
    values = []
    for i in range(1,13):
        value = random.randint(0, int(n / i))
        values.append(value)
        n -= value
        if n<=0 and i<12:
            values.extend(np.zeros(12-i).tolist())
            break
    if n>0:
        for idx,el in enumerate(values):
            values[idx] = values[idx]+int (n/12)
    return values

My question is: is the method efficient even for large sums (of the order of millions)?
How could it be more efficient?

Answer: Your question is not well formed.  You do not "pick a random number".  You pick a random number from a given distribution.  Such a distribution might be uniform between 0 and 1, or Gaussian, or Poisson distribution.
You're asking us to pick 12 random numbers, and then ensure that they have a specific total.  That is not really possible.  (The probability is 0 that the total is correct.) The best you can hope for is to pick 12 random numbers and then scale them in some way to reach your total.  Again, there are many possibilities from linear scaling to more exotic scalings.  And it depends on your original distribution and scaling method whether the resulting 12 numbers are still members of the original distribution.
So what you think is an easy question really isn't.

From your response, it is clear you didn't get the point I was making: there is no such thing as "pick a random number".
Suppose I asked you to pick a random integer.  Whatever number you pick is going to be impossibly small.  The vast majority of integers are unbelievably large, and the fact you can even think it in your head  means you've limited yourself.  I can ask you "pick a number between 1 and 100".  No matter what, I have to explicitly or implicitly give you a probability for each integer, and those probabilities have to add up to 1.  I might even say: pick 0 with probability 1/2, pick 1 with probability 1/2, pick 2 with probability 1/4, .... that's legal.
Likewise for picking a floating point number.  I have to give you a non-negative curve, f and the area under the curve has to total 1.  The probability of your picking a number in the "neighborhood" of x is the value of f(x) times the width of the neighborhood. (Yeah, calculus.)
There is no such thing as "just a random number".
Title: Counting 31.12.'s within a Date Range
Tags: python,pandas

Question: I got 2 columns stating the start and end dates of an event in my data frame. I need to calculate the number of 31.12.XXXX's within that range (while treating it as an open interval). Any help is appreciated, thank you.

Answer: Assuming you mean "open interval" in the ordinary mathematical sense, being that the end dates themselves are excluded, then...
If you just want to work with the textual form, split each date into DD.MM and YYYY components j1=d1[0:5]; y1=int(d1[6:]); y2=int(d2[6:]).
Subtract the "start" year from the "finish" year, subtract one more if the first date is 31.12: n = y2 - y1 - int(j1 == "31.12")
Take the maximum of this and zero (because the answer cannot be negative): if (n<0): n=0
Alternatively, if you have dates represented in a computable form (e.g. Julian day numbers, Unix Epoch seconds, etc) start by adding one day to the "start" date; then take the year of each date, subtract one from the other, and you have your answer.
Sanity checking both approaches, consider:

any two dates in the same year: answer is 0.
31.12.2020 to 31.12.2021: answer is 0.
30.12.2020 to 01.01.2021: answer is 1.
Title: URLencoded shows as broken internal link
Tags: javascript,python,html,flask,urlencode

Question: I have a Flask web-application with lots of products. Some of the product names have a "/" in them. For the URL of the products with a slash in them, I am using URLencoding to solve this issue. So, an example of a URL is:
<a href="https://www.example.com/indProduct/productdb&#47;new">click</a>
And in developer tools, if you inspect that button, it renders:
<a href="/indProduct/productdb%26%2347%3Bnew">click/a>
This works perfectly. I can click the button and it takes me to the product. However, I am using SEMrush to audit my website, and it is raising the issue that this button is a "Broken internal link". The URL that it gives that shows it as broken is:
https://www.example.com/indProduct/productdb&#47;new
How do I fix this? The website works completely fine. There are no broken links anywhere. But for some reason, SEMrush logs that as a broken internal link...

Answer: Some servers will actually decode URLs thus making the URL encoding pointless in the main URL. You can pass your product name in a GET parameter or you can replace the forward slash with a different character and then replace it back when you read it.
Title: Skip lines when reading from csv in pandas
Tags: python,pandas,csv

Question: I have a large csv file containing weather observations from the Bureau of Meteorology. When reading from this file, I need to skip over some lines at the start.
The file looks like this:
"Daily Weather Observations..."
"Prepared..."

,"Date","Minimum temperature (�C)", ...
# data here

When reading from the file, I want to skip over the lines at the start which are not the data. However, the number of these lines vary from file to file. (I have many files.)
My current approach is:
import pandas as pd
actuals = pd.read_csv(path_here, skiprows=5, encoding='latin1')

However, this only works when I hardcode in the rows, and I need it to be able to dynamically skip over the rows.
How can I skip over these 'commentary' lines when reading from the csv file?

Answer: If the first character in the line is data when you read it, you can have your read_csv function read the data from the beginning of the line with #.
Title: How can I get a Timestamp of my OPC UA server?
Tags: python,opc-ua

Question: I need to get a timestamp, with date, of my OPC UA Server, preferably in the right timezone.
print(opcua.ua.DataValue())

I've tried doing this, but I won't get a timestamp.
DataValue(Value:Variant(val:None,type:VariantType.Null), StatusCode:StatusCode(Good))

If possible I would like to get it at the same time as this.
print(self.client.get_values(nodes))

Answer: All servers contain a CurrentTime Node that is part of the ServerStatus Object.
It can be found by navigating Server -> ServerStatus -> CurrentTime.
It always has the same NodeId: i=2258
Title: "500 internal servor error" when using a Flask application deployed by AWS Elastic Beanstalk
Tags: python,flask,deployment,amazon-elastic-beanstalk

Question: Homepage of my Flask application is rendered correctly, but the issue arises when I click a button (submit) which takes me to another endpoint where instead of rendering that page, I get an error saying, "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."
I tried to referring to various sources, but couldn't come up with a solution

Answer: After several hours, I resolved it and there were 2 issues:

The path specified in my python file (predict_pipeline.py): Used backslashes (‘\’) to separate the dirs, which is the convention for file paths in Windows. However, when deploying application on AWS Elastic Beanstalk, the underlying operating system in Linux which uses forward slashes (‘/’).

Sol: modify the file paths using os.path.join(), which is a platform independent way of joining directory and file names. Change “artifacts/model.pkl” to os.path.join(“artifacts”, “model.pkl”)

The version of scikit-learn==1.3.0 used in our flask application doesn’t match with the scikit-learn==1.2.2 version used by Elastic Beanstalk.

Sol: change scikit-learn version in our virtual environment by specifying scikit-learn==1.2.2 in the requirements.txt file in our project directory and install it using pip install -r requirements.txt
Title: Cannot find key in imported dataframe despite the key being present
Tags: python,pandas,keyerror

Question: I have a panel dataset about the Internet Penetration Rate of 11 countries and I want to encode the Country values.
    Country   Year  Int Pen Rate %  GDP Per Capita  GDP Growth %
0  Australia  2014       84.000000     76864.54828      2.579017
1  Australia  2015       84.560515     77397.27020      2.152736
2  Australia  2016       86.540000     78278.37956      2.730548
3  Australia  2017       86.545049     78751.93511      2.282184
4  Australia  2018       90.000000     79813.73387      2.883045

Why do I get KeyError on Country when I use the following function?
series['Country'] = label_encoder.fit_transform(series['Country'])
I used sep=',' when I read the dataset.

Answer: I looks like you have some excess whitespaces (the "Country " is not right aligned).
For cleaning use df.columns = list(map(str.strip, df.columns)) and replace df with the name of your dataframe.
Title: What is the safest way to click on an element in Playwright?
Tags: python,automation,playwright

Question: I need to click on an object using Playwright for Python! Sometimes the element's CSS changes, or it doesn't have an ID. I usually use
page.click(XPATH), But I know there are many other ways like:
1 - page.get_by_text("Get Started").click()
2 - page.get_by_role("button", name="Submit").click()
3 - page.get_by_selector("#login-button").click()
4 - page.get_by_selector("//div[@class='user-agent']").click()
5 - locator = page.locator(".user-agent")
    locator.click()

Which has the highest probability of success?

Answer: If Id is not available, I would tend to favor second option as its:

Unique : Usually there is only one submit button on a page.

combination: Two properties are less likely to change at the same time.


I think the most desirable quality in a locator is - rigid and flexible at the same time, which second option has it.
Title: Python- Unable to write to a csv-‘Type Error: “Delimiter” must be a 1-character string
Tags: python,pandas

Question: I have a dataframe-df which contains the below values
Key Summary Area****Priority
1   Verify if site is launching                     Homepage    High
2   Verify if Home page is launching correctly      Homepage    High
3   Verify if able to add fruits,veggies to cart      Cart     Medium
4   Verify if able to place an order                 Order      High
I am trying to load data to csv/tsv and I used the below line & it works fine
df.to_csv(“C\users\xxx\Documents\Output\FinalData.csv”,index=False)
df.to_tsv(“C\users\xxx\Documents\Output\FinalData.csv”,sep=’\t’,index=False)

When I send the above file to my downstream team, some times their EDW script is failing if there is a comma/ tab in any of the fields so they asked me to send the file with delimiter as ‘~|~’ or ‘~|’.
When I use the below code, I am getting error as ‘Type Error: “Delimiter” must be a 1-character string
a)  df.to_csv(“C\users\xxx\Documents\Output\FinalData.csv”, sep=’~|~’,index=False)   or
b)
separator=’,’
df.to_csv(“C\users\xxx\Documents\Output\FinalData.csv”, sep=’~|~’,index=False)  

or
c)
separator=’,’
df.to_csv(“C\users\xxx\Documents\Output\FinalData.csv”, sep=’~|’,index=False)  

Could you please suggest if anyways of writing data to a file with multiple delimiters or if there is anyother way of handling this scenario? Thanks!

Answer: Pandas to_csv() method supports only a single-character string as a delimeter, the same is true for Python standart csv library. So you should choose some single-digit delimiter and than write a script which will read the csv file (as a plain text) and replace chosen delimiters to ‘~|~’ or ‘~|’
Title: How to wrap content in sections divided by h4 elements at varying depths with Beautifulsoup
Tags: python,html,beautifulsoup

Question: What can I do to surround the content between each h4 element within the main element with section tags, when the depth of the h4 elements varies? In the example below, lines 3 to 5, lines 7 to 11, and lines 12 to 14 should each be wrapped with section tags.
<main class="main">
  <div class="styling-div"><h3>Main Heading 1</h3></div>
  <h4>Subheading</h4>
  <p>Text</p>
  <img src="src">
  <div class="styling-div"><div class="another-div"><h3>Main Heading 2</h3></div></div>
  <h4>Subheading</h4>
  <p>Text</p>
  <div class="styling-div-for-h5"><h5>Sub-subheading</h5></div>
  <p>Text</p>
  <p>Text</p>
  <div class="styling-div-for-h4"><h4>Subheading</h4></div>
  <p>Text</p>
  <figure>Image</figure>
</main>

What would be a suitable algorithm to find and place the section tags.

Answer: What you need to do to wrap parts of main in sections that are started by h4 tags at varying depths is iterate through the children of the main tag.
By checking if each child either is or contains an h4 tag you know you need to insert a section element before it and insert the child into it. If the child does not meet the condition you just insert it into the section last created or just leave it be if no section has been created yet.
Title: why is prev the return variable solution?
Tags: python,function,class,while-loop,linked-list

Question: so i found this code solution for leetcode #206 (reverse linked list) and i kept wondering why variable prev is the solution?
the code is:
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # setting pointers
        prev, curr = None, head

        while curr is not None:
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt
        return prev

lets set a [1, 2, 3, 4, 5] linked list
at the end of the iterations prev points to the last node (5). then why returning prev is the solution if prev is just a node and not the entire linked list.

Answer: Because in a linked list, each node is a list in and of itself. An empty list is denoted by the None value, and any non-None value is considered the first element of the list. But more importantly, that head node will have a link to the next value, and so on recursively, so you have all the information you need to iterate over the entire list.
You can also think of the link as a subslice: head.next is roughly equivalent to list[1:], i.e., a slice starting at the second element. And head.next.next would be list[2:]. So just head would be list[0:], i.e., the entire list.