What could be causing my object to constantly transform into a NoneType?

I am currently developing a Youtube audio sampling program with an object named time_snip that stores the start and end times of a sample in milliseconds. I have encountered a situation where entering the time correctly gives the expected output, but typing the time in the wrong format (handled by a try/except exception) and then entering it correctly results in an

AttributeError: 'NoneType' object has no attribute 'start'
.

The time_snip class definition is as follows:

    class time_snip:
        def __init__(self,start_hour,start_min,start_sec,start_msec,end_hour,end_min,end_sec,end_msec):
            self.start = (start_hour*60*60*1000)+(start_min * 60 * 1000) + (start_sec * 1000)+(start_msec)
            self.end = (end_hour*60*60*1000)+(end_min * 60 * 1000) + (end_sec * 1000)+(end_msec)
            self.start_ind = (str(start_min)+':'+str(start_sec))
            self.end_ind =(str(end_min)+':'+str(end_sec))

Main code snippet:

    from pytube import YouTube
    from TimeSnip import time_snip
    time_options = input("Would you like to find by time(1) or by word(2)?: ")
    url ="https://www.youtube.com/watch?v=vGwNPeEXEDM&ab_channel=SilasLaspada"
    ytd = YouTube(URL)

    # Function to set time values
    def time_set(youtube_object):
            try:
                hr_length = int(ytd.length/3600)
                min_length = int((ytd.length%3600)/60)
                sec_length = ytd.length%60

                print("the video is " + str(hr_length)+':'+str(min_length)+':'+str(sec_length) +'long')
                start_time_srt_format = input("Enter start time in 'hr:min:sec,msec' format: ")
                end_time_srt_format = input("Enter end time in 'hr:min:sec,msec' format: ")
                start_parts = start_time_srt_format.split(':')
                end_parts = end_time_srt_format.split(':')
                start_secmsec = start_parts[2].split(',')
                end_secmsec = end_parts[2].split(',')
                start_hr = int(start_parts[0])
                start_min = int(start_parts[1])
                start_sec = int(start_secmsec[0])
                start_msec = int(start_secmsec[1])
                end_hr = int(end_parts[0])
                end_min = int(end_parts[1])
                end_sec = int(end_secmsec[0])
                end_msec = int(end_secmsec[1])

                word = (str(start_sec)+'-'+str(end_sec))
                timeSnip = time_snip(start_hr, start_min, start_sec, start_msec, end_hr, end_min,end_sec,end_msec)
                return timeSnip
            except IndexError:
                print("Invalid input! Try again!")
                time_set(youtube_object)
    
    if time_options =='1':
        timeSnip = time_set(ytd)
        word = str(timeSnip.start/1000)+' sec'
    elif time_options =='2':
        word = input("Enter word: ")

This issue is perplexing me, as I cannot understand why it occurs.

Answer №1

Within the except block, there is no return statement. This means that if an exception occurs, the function will be called again (an incorrect approach), and even if it does succeed and returns a value, it would not be utilized. To rectify this issue, modify the line to return time_set(youtube_object). However, it is advisable to convert this into a while loop instead of using recursion.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Power of Strings in Python

I have encountered a challenge - I need to develop a program that prompts the user for their first name, saves it, then asks for their last name and does the same. The final task is to display the first name and last name in two separate rows as shown belo ...

Error encountered in Python 2.7 with Selenium 2: 'NoneType' object does not possess the attribute 'group'

Having recently delved into the world of Python/Selenium, I've been encountering a persistent roadblock while attempting to adjust some code. Despite my efforts scouring the internet and experimenting with various tweaks for days on end, I find myself ...

Comparison of differences between Django and Google App Engine

I am currently working on a Django wiki project and I intend to deploy it on Google App Engine. Does anyone know if it is feasible to implement a text difference system, similar to textdiff, within App Engine? ...

What is the best way to insert key-value pairs from a Python dictionary into Excel without including the brackets?

Is there a way to append key value pairs in a Python dictionary without including the brackets? I've tried searching for similar questions but haven't found a solution that works for me. # Create a new workbook called 'difference' fil ...

Generate a personalized report for the Behave Python framework, requiring retrieval of both the Scenario Name and its corresponding status

Looking to generate a personalized report for the Behave and Python framework. The goal is to retrieve the Scenario Name and status. Any suggestions on how to accomplish this? Curious if there is an event listener class available in the behave framework t ...

other methods or enhancements for accelerating mpmath matrix inversion

Currently, I am developing Python code that involves frequent inversion of large square matrices containing 100-200 rows/columns. The precision limits of the machine are being reached, prompting me to experiment with using mpmath for arbitrary precision m ...

Capturing a series of values and storing them in a separate column using Python

I am dealing with a column ID that consists of either 10 numbers or 7 numbers. My goal is to create a new column that only includes the last 3 digits if the Column ID has a length of 10, and if it has a length of 7, then I want to keep the entire number. D ...

Issues encountered when attempting to replace a Python extension module while a Python script is in the process of

Currently, I am faced with the following issue: Suppose I have a Python script named Test.py that utilizes a C++ extension module (created using SWIG, referred to as "Example"). In my directory, I have Test.py, Example.py, and _Example.so. During the exec ...

Experiencing trouble with locating elements using partial link text in ASCII code

. Upon clicking on CloudApper™ within the Solutions section, I encounter an ASCII code error. I locate the element by using: driver.find_element_by_partial_link_text('CloudApper™') Is there a way to successfully click on this partial link ...

Importing an array into DynamoDB using a JSON string with boto3

When it comes to uploading a JSON file to an AWS DynamoDB table in Python, I have been using a script that I found on this particular page. However, I am facing a challenge in understanding whether Python can be instructed to split a single string from the ...

What are the steps for capturing video using openCV in a python program?

I am facing a challenge in figuring out how to capture a video using openCV. While I have managed to display the video on an html template with the existing codes, I am uncertain about which codes are needed to actually record the video. -camera.py- i ...

Unable to access Docker Flask App connected to Docker DB in browser

I currently have a Flask App running in one Docker container and a Postgres database in another Docker container. I am attempting to build and run these containers using 'docker-compose up --build'. However, when I attempt to open the 'Runni ...

What is causing the "property object is not callable" error when attempting to use bulk_create in Django for inserting data that includes foreign keys?

I am currently working with Django 2.17 and a SQLite 3.26 database, attempting to import data from a csv file. Initially, I used the get_or_create method for insertion, but found it to be too slow. As a result, I started testing out bulk_create for faster ...

Tips for integrating Tornado authentication with AngularJS

I have been experimenting with the authentication system outlined in the tornado documentation, and I am encountering a Cross-Origin Request issue when trying to integrate it with AngularJS. Is there a way to successfully combine Tornado's authentica ...

Is there a way to reset my Pong game without losing my score?

For my first fully developed Python project, I have created a Pong game using tkinter for most of the functionality. However, I encountered an issue with restarting the program using the restart button without affecting the score counter in the top left ...

Choosing Checkboxes with Selenium

I'm attempting to utilize Selenium to interact with a checkbox, as indicated in the HTML image. The issue is that all four options have the same checkbox input tag, and their unique identifier is only specified by the id="country_england" .. ...

Unable to locate element using XPath on specific webpage

In my pursuit, I aim to extract word definitions using Python. My initial focus is on retrieving the primary definition of the term "assist," which should be "to help." This information is sourced from dictionary.cambridge.org //Navigate web driver to des ...

Transform data from ceilometer into a pandas dataframe using Python

As a newcomer to Python and openstack ceilometer, I am currently working on reading ceilometer data. The code snippet below shows how I am achieving this: import ceilometerclient.client cclient = ceilometerclient.client.get_client(2, os_username="Ceilome ...

Can we enhance the optimization of curve fitting when one variable depends on another?

Is it feasible to perform curve fitting on a dual exponential function where one parameter is strictly greater than another? I'm unsure of how to enforce this constraint in my code. Below is a simple example: import numpy as np import matplotlib.pyp ...

Error with Selenium version 117.0.5938.150: When passing proxies, 'WebDriver.init() got multiple values for argument 'options'

Encountering an issue with the latest version of Selenium when attempting to pass proxies using SeleniumWire. The error message received is "WebDriver.init() got multiple values for argument 'options'". See below for the current code snippet: fro ...