Skip to main content
added 3 characters in body
Source Link
FMc
  • 13.1k
  • 2
  • 29
  • 40

Python functions can have optional arguments with default values. If you adjust the signature of connect() to have a default, the create_database() function will be unnecessary.

def connect(database_name = ''):
    return mysql.connector.connect(
      host = 'localhost',
      user = 'root',
      passwd = '',
      database = database_name
    )

Contrary to your question text, you don't have a main function; you just have top-level code nested under an if-conditional. Top-level code (other than constants or imports) is generally a bad idea because it is inflexible and not easily tested or experimented with. Move that code into a proper main() function, and then just invoke it in the if-conditional. It's also not a bad idea to prepare for the future of your script by including the ability to handle simple command-line arguments -- often handy for debugging and experimentation even if they are never part of an intended use case of the script.

import sys

def main(args):
    ...

if __name__ == '__main__':
    main(sys.argv[1:])

After you make that change, the store_data() function will be broken, because it depends on having access to the global mycursor variable. That's another illustration of the problems with top-level code: it can camouflage dependencies and, in some cases, create situations that are difficult to debug and disentangle if the volume of such code grows large enough. Instead, the store_data() function should take the DB connection as an explicit argument. 

A rigorous approach of putting all code inside of functions will seem like a small hassle at first; but in my experience, it nearly always pays off in the form of fewer bugs, greater flexibility, improved code readability, and various other benefits.

Python functions can have optional arguments with default values. If you adjust the signature of connect() to have a default, the create_database() function will be unnecessary.

def connect(database_name = ''):
    return mysql.connector.connect(
      host = 'localhost',
      user = 'root',
      passwd = '',
      database = database_name
    )

Contrary to your question text, you don't have a main function; you just have top-level code nested under an if-conditional. Top-level code (other than constants or imports) is generally a bad idea because it is inflexible and not easily tested or experimented with. Move that code into a proper main() function, and then just invoke it in the if-conditional. It's also not a bad idea to prepare for the future of your script by including the ability to handle simple command-line arguments -- often handy for debugging and experimentation even if they are never part of an intended use case of the script.

import sys

def main(args):
    ...

if __name__ == '__main__':
    main(sys.argv[1:])

After you make that change, the store_data() function will be broken, because it depends on having access to the global mycursor variable. That's another illustration of the problems with top-level code: it can camouflage dependencies and, in some cases, create situations that are difficult to debug and disentangle if the volume of such code grows large enough. Instead, the store_data() function should take the DB connection as an explicit argument. A rigorous approach of putting all code inside of functions will seem like a small hassle at first; but in my experience, it nearly always pays off in the form of fewer bugs, greater flexibility, improved code readability, and various other benefits.

Python functions can have optional arguments with default values. If you adjust the signature of connect() to have a default, the create_database() function will be unnecessary.

def connect(database_name = ''):
    return mysql.connector.connect(
      host = 'localhost',
      user = 'root',
      passwd = '',
      database = database_name
    )

Contrary to your question text, you don't have a main function; you just have top-level code nested under an if-conditional. Top-level code (other than constants or imports) is generally a bad idea because it is inflexible and not easily tested or experimented with. Move that code into a proper main() function, and then just invoke it in the if-conditional. It's also not a bad idea to prepare for the future of your script by including the ability to handle simple command-line arguments -- often handy for debugging and experimentation even if they are never part of an intended use case of the script.

import sys

def main(args):
    ...

if __name__ == '__main__':
    main(sys.argv[1:])

After you make that change, the store_data() function will be broken, because it depends on having access to the global mycursor variable. That's another illustration of the problems with top-level code: it can camouflage dependencies and, in some cases, create situations that are difficult to debug and disentangle if the volume of such code grows large enough. Instead, the store_data() function should take the DB connection as an explicit argument. 

A rigorous approach of putting all code inside of functions will seem like a small hassle at first; but in my experience, it nearly always pays off in the form of fewer bugs, greater flexibility, improved code readability, and various other benefits.

Source Link
FMc
  • 13.1k
  • 2
  • 29
  • 40

Python functions can have optional arguments with default values. If you adjust the signature of connect() to have a default, the create_database() function will be unnecessary.

def connect(database_name = ''):
    return mysql.connector.connect(
      host = 'localhost',
      user = 'root',
      passwd = '',
      database = database_name
    )

Contrary to your question text, you don't have a main function; you just have top-level code nested under an if-conditional. Top-level code (other than constants or imports) is generally a bad idea because it is inflexible and not easily tested or experimented with. Move that code into a proper main() function, and then just invoke it in the if-conditional. It's also not a bad idea to prepare for the future of your script by including the ability to handle simple command-line arguments -- often handy for debugging and experimentation even if they are never part of an intended use case of the script.

import sys

def main(args):
    ...

if __name__ == '__main__':
    main(sys.argv[1:])

After you make that change, the store_data() function will be broken, because it depends on having access to the global mycursor variable. That's another illustration of the problems with top-level code: it can camouflage dependencies and, in some cases, create situations that are difficult to debug and disentangle if the volume of such code grows large enough. Instead, the store_data() function should take the DB connection as an explicit argument. A rigorous approach of putting all code inside of functions will seem like a small hassle at first; but in my experience, it nearly always pays off in the form of fewer bugs, greater flexibility, improved code readability, and various other benefits.