Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Problems with "re.fullmatch"
#1
Hi programmers! I am new in the forum and in Python.
I am programming a library whose function is to store books and comics and display data from them by screen.
Since I added some restrictions for some methods, the "Comic" class does not work for me, and the error it gives me I can not understand.
I'm programming with Atom and my terminal to run it.

The error in Terminal:

Error:
File "USO_BIBLIOTECA.py", line 4, in <module> from BIBLIOTECA import * File ... line 134, in <module> comic = Comic() File ... line 128, in __init__ libro.setAutor(self) File ... line 67, in setAutor if re.fullmatch(regex, new_autor): File ... line 168, in fullmatch return _compile(pattern, flags).fullmatch(string) TypeError: expected string or bytes-like object
The error in Atom:

Error:
File ... line 134, in <module> comic = Comic() File ... line 128, in __init__ libro.setAutor(self) File ... line 67, in setAutor if re.fullmatch(regex, new_autor): AttributeError: 'module' object has no attribute 'fullmatch'
The code of my program is:
import re
    class Biblioteca():  
        def __init__(self, tituloLibro, tituloComic):
            self.tituloLibro = tituloLibro
            self.tituloComic = tituloComic

        def setLibro(self, new_libro):
            self.tituloLibro = new_libro
        def setComic(self, new_comic):
            self.tituloComic = new_comic

        def getLibro(self):
            return self.tituloLibro
        def getComic(self):
            return self.tituloComic

    class Libro():
        def __init__(self):
            self.autor = ""
            self.editorial = ""
            self.idioma = ""
            self.paginasLeidas = 0
            self.paginasTotales = 0

        def setAutor(self, new_autor):
            regex = "[^\d\s]+( [^\d\s]+)*"
            if re.fullmatch(regex, new_autor):
               self.autor = new_autor
            else:
               print("\n\n ****** ERROR ******")
               self.autor = ""

        def setEditorial(self, new_editorial):
               regex = "[^\d\s]+( [^\d\s]+)*"
            if re.fullmatch(regex, new_editorial):
               self.editorial = new_editorial
            else:
               print("\n\n ****** ERROR ******")
               self.editorial = ""

        def setIdioma(self, new_idioma):
            regex = "[^\d\s]+( [^\d\s]+)*"
            if re.fullmatch(regex, new_idioma):
               self.idioma = new_idioma
            else:
               print("\n\n ****** ERROR ******")
               self.idioma = ""

        def setPaginasLeidas(self, new_paginasLeidas):
            try:
               self.paginasLeidas = int(new_paginasLeidas)
            except ValueError:
               print("\n\n ****** ERROR ******")
               self.paginasLeidas = 0
        def setPaginasTotales(self, new_paginasTotales):
            try:
               self.paginasTotales = int(new_paginasTotales)
            except ValueError:
               print("\n\n ****** ERROR ******")
               self.paginasLeidas = 0

        def getAutor(self):
            return self.autor

        def getEditorial(self):
            return self.editorial

        def getIdioma(self):
            return self.idioma

        def getPaginasLeidas(self):
            return self.paginasLeidas

        def getPaginasTotales(self):
            return self.paginasTotales

  libro  = Libro()

  class Comic(Libro):
       def __init__(self):
            Libro.__init__(self)
            libro.setAutor(self)
            libro.setEditorial(self)
            libro.setIdioma(self)
            libro.setPaginasLeidas(self)
            libro.setPaginasTotales(self)

  comic = Comic()
Thanks in advance.
Quote
#2
You don't have a file named re.py, do you? It looks like you might. If you can import something in Python, you generally speaking don't want to re-use those names (e.g. os, sys, json).

As for the rest - when you call setAuthor, you're passing a Comic object, not a string.
buran likes this post
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#3
Hi @micseydel! First, thanks for your time.
If I'm not mistaken, "re" is a library of python and "fullmatch" it's inside to "re"
But I'm not sure about this...
Your other answer I don't understand... I think I'm passing a Comic but this is a string.
I am going to paste the other part of my code:
elif (opcion == 2):
        print("Escriba el título del Comic: ")
        tituloComic = str(input("Título: "))
        biblioteca = Biblioteca(None, tituloComic)
         *
         *
         *
elif (opcion == 4):
        while True:
                print("""\n\n\n OPCIONES A MODIFICAR EN COMIC \n
                1 - Autor/es
                2 - Editorial
                3 - Idioma
                4 - Nº de páginas leidas
                5 - Nº de páginas
                6 - Regresar al menu anterior
                """)
                opcion4 = int(input("Seleccione una opción: "))
                print("Comic: " + str(tituloComic))

                if (opcion4 == 1):
                    autor = str(input("Autor/es: "))
                    comic.setAutor(autor)
                elif (opcion4 == 2):
                    editorial = str(input("Editorial: "))
                    comic.setEditorial(editorial)
                elif (opcion4 == 3):
                    idioma = str(input("Idioma: "))
                    comic.setIdioma(idioma)
                elif (opcion4 == 4):
                    paginasLeidas = str(input("Nº de páginas leidas: "))
                    comic.setPaginasLeidas(paginasLeidas)
                elif (opcion4 == 5):
                    paginasTotales = str(input("Nº de páginas: "))
                    comic.setPaginasTotales(paginasTotales)
          *
          *
          *
elif (opcion == 6):
         print(" \n\n DATOS DEL COMIC \n ")
         print("Título: " + str(biblioteca.getComic()))
         print("Autor/es: " + str(comic.getAutor()))
         print("Editorial: " + str(comic.getEditorial()))
         print("Idioma: " + str(comic.getIdioma()))
         print("Nº de páginas: " + str(comic.getPaginasLeidas()))
         print("Nº de páginas leidas: " + str(comic.getPaginasTotales()))
I'm sorry if I'm not clear enough
Quote
#4
Yes, re.fullmatch exists in Python. What I'm saying is that the error you're getting in Atom looks like it's caused by you having your own re.py file. You should rename that file.

I don't know what your most recent code is about, you haven't associated an error message with it or anything. In the original code you provided, you passed a Comic object instead of a string to re.fullmatch so that error made perefect sense.

For subsequent posts, please most minimal, runnable code that reproduces whatever error condition you're hitting. If you're posting more than 5-10 lines of code, you should probably be simplifying it further. I say this not just for our benefit (though I'm much more willing to debug short code), but rather because the process of doing so often helps the question asker figure things out for their self, which is a valuable skill to have.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#5
Hi @micseydel! Sorry if I didn't explain well and sorry for my ignorance.
I don't know how I can change the file name and I don't know if I should do that because the error is still, when running it in the terminal.

The second code that I wrote is the menu and the use of the functions/methods of the first code. In that code I don't have any mistakes. The important mistake is:

File "USO_BIBLIOTECA.py", line 4, in <module>
from BIBLIOTECA import *
File BIBLIOTECA.py line 134, in <module>
comic = Comic()
File BIBLIOTECA.py line 128, in __init__
libro.setAutor(self)
File BIBLIOTECA.py line 67, in setAutor
if re.fullmatch(regex, new_autor):
File BIBLIOTECA.py line 168, in fullmatch
return _compile(pattern, flags).fullmatch(string)
TypeError: expected string or bytes-like object.

BIBLIOTECA.py is the first code and USO_BIBLIOTECA.py is the second.

In relation to all the code that I have published, I will consider your advice for future publications.I am learning to simplify code

I'm trying to explain it to you as best I can. Thanks for your understanding
Quote
#6
Please summarize what your current question is as per my previous reply.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#7
My question is, how could I correct the code? I don't know how to fix the error that it gives me when executing the program. I don't understand the problem with "re.fullmatch".
You told me that Atom seems to have a file of its own called "re.py". But I would like to understand the other error (written in the previous post) since I usually run the codes in the console.
I sent you another part of the code so you could see that when I request a name for Comic, it is converted to string.

I appreciate your patience with me. :(

Thank you so much!
Quote
#8
(May-17-2019, 09:11 PM)micseydel Wrote: Please summarize what your current question is as per my previous reply.
I don't know what code you're talking about right now, you've posted conflicting things. This should be easy to resolve.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#9
I'm sorry, I'll start again.
I'm doing a program that has the function of a library, which saves, displays and deletes data about books and comics that the user writes.

The program is divided into two: the first (BIBLIOTECA.py) are the Library, Book and Comic classes (it is the first code that posted), the second (USE_BIBLIOTECA.py) imports to BIBLIOTECA.py and there is the menu with the different options (the second code that posted).
The important code is the first.

When I run the program from my Terminal (Mac OS) I get the error: "" TypeError: expected string or bytes-like object. "

You told me: In the original code you provided, you passed a Comic object instead of a string to re.fullmatch so that error made perefect sense.
But I don't know how to solve that error.

Sorry for all the above, I thought that I was explaining myself well.
Thanks for your patience.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)