We're making an exercise with pickle, and this code doesn't work as it is supposed to. Please help me, this is the code (some words are in Spanish because I'm from America):
import pickle
class persona:
def __init__(self, nombre, genero, edad):
self.nombre = nombre
self.genero = genero
self.edad = edad
print("se ha creado una persona nueva con el nombre de: ", self.nombre)
def __str__(self):
return "{} {} {}".format(self.nombre, self.genero, self.edad)
class listaPersonas:
personas = []
def __init__(self):
listaDePersonas = open("ficheroExterno", "ab+")
listaDePersonas.seek(0)
try:
self.personas = pickle.load(listaDePersonas)
print("Se cargaron {} personas del fichero externo".format(len(self.personas)))
except:
print("El fichero está vacío")
finally:
listaDePersonas.close()
del(listaDePersonas)
def agregarPersonas(self, p):
self.personas.append(p)
self.guardarPersonasEnFicheroExterno()
def mostrarPersonas(self):
for p in self.personas:
print(p)
def guardarPersonasEnFicheroExterno(self):
listaDePersonas = open("ficheroExterno", "wb")
pickle.dump(self.personas, listaDePersonas)
listaDePersonas.close()
del(listaDePersonas)
def mostrarInfoFicheroExterno(self):
print("La información sle fichero externo es la siguiente: ")
for p in self.personas:
print(p)
miLista = listaPersonas()
persona = persona("Sandra", "Femenino", 29)
miLista.agregarPersonas(persona)
miLista.mostrarInfoFicheroExterno()
and it throws that:
El fichero está vacío
se ha creado una persona nueva con el nombre de: Sandra
Traceback (most recent call last):
File "Guardado_permanente.py", line 54, in <module>
miLista.agregarPersonas(persona)
File "Guardado_permanente.py", line 34, in agregarPersonas
self.guardarPersonasEnFicheroExterno()
File "Guardado_permanente.py", line 42, in guardarPersonasEnFicheroExterno
pickle.dump(self.personas, listaDePersonas)
_pickle.PicklingError: Can't pickle <class '__main__.persona'>: it's not the same object as __main__.persona
***Repl Closed***
I have like 1 1/2 hour that I'm seeing this code and I'm trying to guess which is the problem, but the code is the same as my teacher's code. please, help me. I'm using Sublime text to code.
In this line, you have replaced your class persona with an instance of the class:
persona = persona("Sandra", "Femenino", 29)
pickle is trying to find the class definition for persona but can't, because it doesn't have a name anymore.
Don't try to use the same name for two things; only the last assignment counts. Standard style is to use CamelCase names for clasess, so you could name your class Persona instead.
Related
I have following function. So I would like to take 2 fields and concatenate them but when my pipeline finishes it doesn´t work
the pipeline finishes as correct but when I see in bigquery the fields have not been concatenated
It will be great if u can help me.
it´s the code used in the function:
import apache_beam as beam
from ..tools import ProcessLogger
_logger = ProcessLogger()
class ConcatFieldsFn(beam.DoFn):
"""Concatena los valores de los campos especificados en una pCollection por un valor especificado"""
def __init__(self, process: str, data_name: str, parameters: dict):
# Configuracion del logger
self.logger_data_name = data_name
self.logger_process = process
self.logger_subprocess = "Concatenar valores"
_logger.data_name = self.logger_data_name
_logger.process = self.logger_process
_logger.subprocess = self.logger_subprocess
# Parametros del proceso
self._fields = [field.get("name") for field in parameters.get("fields", None)]
_logger.info(
f"Se aplica regla: {_logger.subprocess} con los parametros: {parameters}"
)
def process(self, element):
# Configuracion del logger
_logger.data_name = self.logger_data_name
_logger.process = self.logger_process
_logger.subprocess = self.logger_subprocess
for field in self._fields:
if element[field] != None:
try:
element[field] = "|".join(element[field])
except Exception as ex:
_logger.error(
f"No se pueden eliminar las letras de los campos seleccionados: {ex}"
)
return [element]
I've been messing around with this code for some hours, and it still give me errors. I want to do an 'automark' for tests, where you introduce the good answers and the bad ones, and it calculates the final mark.
Code:
class puntuacion:
def __init__(self, aciertos, errores):
self.aciertos = int(input("Introduce el numero de aciertos"))
if self.aciertos > 40:
print("Numero maximo de aciertos: 40")
exit()
self.errores = int(input("Introduce el numero de errores: "))
def calculo(self):
totalac = self.aciertos * 0.25
print(totalac)
while self.errores > 3:
totalerr += -0.25
self.errores - 3
print'Nota final: ', totalac-totalerr
calculo({})
Error:
C:\Users\Admin>python C:\Users\Admin\Documents\python\test2.py
Traceback (most recent call last):
File "C:\Users\Admin\Documents\python\test2.py", line 1, in <module>
class puntuacion:
File "C:\Users\Admin\Documents\python\test2.py", line 17, in puntuacion
calculo({})
File "C:\Users\Admin\Documents\python\test2.py", line 10, in calculo
totalac = self.aciertos * 0.25
AttributeError: 'dict' object has no attribute 'aciertos'
You can't call an instance method directly: you have to instantiate the class first:
p = puntuacion(something, something_else)
p.calculo()
The calculo() method does not take any arguments: the first argument, selfis implicit, and corresponds to the class instance. You don't pass it.
You should also remove the input() functions from your __init__; you're already passing aciertos and errores as arguments, and generally you don't want to do "complicated" stuff (like user interaction) in the initialization of a class:
...
def __init__(self, aciertos, errores):
self.aciertos = aciertos
self.errores = errores
...
and then:
aciertos = int(input("Introduce el numero de aciertos"))
if aciertos > 40:
sys.exit("Numero maximo de aciertos: 40")
errores = int(input("Introduce el numero de errores: "))
p = puntuacion(aciertos, errores)
p.calculo()
I'm currently working on a little side project yet having multiple issues. I'm reading a file within the folder where the project is that holds data for 10 users.
As for the code itself...it's pretty big.
def ouvrir_fichier(nomFichier):
""" Ne pas oublier les docstring
"""
try:
fp = open(nomFichier, 'r')
return fp
except:
return print("Le fichier n'existe pas. Veuillez réessayer.")
def lire_fichier(fp):
""" Ne pas oublier les docstring"""
# Lis n et initialise une liste vide --
# où il y a une liste vide pour chaque usager du réseau
# ensuite lis le reste du fichier et ajouter l'information à reseau
liste1 = fp.readlines()
n = int(liste1[0])
liste2 = liste1[1:]
reseau = [[] for i in range(n)]
for i in liste2:
i = i.replace("\n", "")
data = i.split(" ")
valeur1 = int(data[0])
valeur2 = int(data[1])
reseau[valeur1].append(valeur2)
reseau[valeur2].append(valeur1)
fp.close()
return reseau
def trouver_nombre_elements_communs_entre_listes(liste1, liste2):
""" Ne pas oublier les docstring"""
compteur_amis_commun = 0
for element in liste1:
if element in liste2:
compteur_amis_commun = compteur_amis_commun + 1
return compteur_amis_commun
def initialiser_matrice(n):
"""
Crée une matrice nxn, initialisée avec des zéros et retourne la matrice.
Args:
n (int): dimension de la matrice nxn
Returns:
matrice (list): matrice initialisée
"""
matrice = []
for ligne in range(n): # pour chacune des lignes dans n
matrice.append([]) # créer une ligne (liste) et l'initialiser à 0
for colonne in range(n):
matrice[ligne].append(0) # ajouter un 0 pour chaque n colonne
return matrice
def calculer_scores_similarite(reseau):
""" Ne pas oublier les docstring"""
n = len(reseau)
matrice_similarite = initialiser_matrice(n)
liste1 = []
liste2 = []
compteur_liste1 = 0
compteur_liste2 = 0
for element_liste1 in reseau:
liste1 = element_liste1
for element_liste2 in reseau:
liste2 = element_liste2
compteur_amis_commun = trouver_nombre_elements_communs_entre_listes(liste1, liste2)
matrice_similarite[compteur_liste1][compteur_liste2] = compteur_amis_commun
compteur_liste2 = compteur_liste2 + 1
compteur_liste1 = compteur_liste1 + 1
compteur_liste2 = 0
return matrice_similarite
def recommander(id_usager,reseau,matrice_similarite):
""" Ne pas oublier les docstring"""
usager_matrice = matrice_similarite.index(id_usager)
ami_recommande = matrice_similarite.index(max(usager_matrice))
max_value = max(matrice_similarite.index(usager_matrice))
if ami_recommande == id_usager:
max_value = max_value - 1
ami_recommande = matrice_similarite.index(max_value)
while True:
if ami_recommande == reseau.index(ami_recommande):
ami_recommande = reseau.index(max_value, ami_recommande + 1)
return True
return ami_recommande
def main():
nomFichier = input("Nom du fichier contenant le réseau: ")
reseau = lire_fichier(ouvrir_fichier(nomFichier))
n = len(reseau)
matrice_similarite = calculer_scores_similarite(reseau)
while True:
while True:
id_usager = int(input("Entrer l'ID de l'usager pour lequel vous voulez une recommandation (entre 0 et {}):".format(n)))
if 0 <= id_usager and id_usager < n:
calculer_scores_similarite(reseau)
print("Pour la personne" , id_usager , ", nous recommandons l'ami" , recommander(id_usager, reseau, matrice_similarite))
continue
else:
print("Erreur: l'usager doit être un nombre entier entre ", 0, "et", n - 1, "inclusivement.\n")
autreRecommandation = input("Voulez-vous une autre recommandation (oui/non)?")
if autreRecommandation.lower() == "oui":
return True
else:
print("Merci d'avoir utiliser le programme de recommandation d'amis.")
break
if __name__ == "__main__":
main()
Most of the content seems to be working fine until I get to part where I need to recommend a user identification. I'll try to work on the doc string as well in the meantime but I could totally use a little bit of help as to debug this. I tested most of the code on another .py project until I hit the function recommander
First Edit:
I did forget to apply the return. I changed it and it is now in the def. Now however...I seem to be having this error.
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1668, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1662, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\pydevd.py", line 1072, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/101136/PycharmProjects/tp2/TP2.py", line 132, in <module>
main()
File "C:/Users/101136/PycharmProjects/tp2/TP2.py", line 119, in main
print("Pour la personne" , id_usager , ", nous recommandons l'ami" , recommander(id_usager, reseau, matrice_similarite))
File "C:/Users/101136/PycharmProjects/tp2/TP2.py", line 89, in recommander
usager_matrice = matrice_similarite.index(id_usager)
ValueError: 0 is not in list
Also I'm wondering about a little something. The part where I ask for an input to define id_usager in the def main(), I was wondering if there was a way to also treat characters as well as integers. Integers, I got it covered with my code but if somebody wants to write boy instead of a number, it'll fail.
accessing element of list is done by list_name[index], list_name.index(element) returns the index of that element,if they are two in the list it returns the index of the first one, if the element is not there,it throws a ValueError: "element" is not in the list.
in your case
change
usager_matrice = matrice_similarite.index(id_usager)
to
usager_matrice =matrice_similarite[id_usager]
and other places you want to access element of a list using index
in the scond question from the comment, check if its numeric like this
id_usager =input("Entrer l'ID de l'usager pour lequel vous voulez une recommandation (entre 0 et {}):".format(n))
if not id_usager.isdigit():
continue
id_user=int(id_usager)
note that this will convert also floats into int
I'm testing a query with python and sqlite3. First method works fine, but
second is not working. It is about the defined type of variable containing the resgisters in DB:
import sqlite3
def insertar():
db1=sqlite3.connect('tabla.db')
print("Estas en la funcion insertar")
nombre1=raw_input("Escribe el titulo de la novela: ")
autor1=raw_input("Escribe el autor de la novela: ")
year1=str(input("Digita el any de la novela: "))
consulta=db1.cursor()
strConsulta = "insert into tabla(nombre, autor, year) values\
('"+nombre1+"','"+autor1+"','"+year1+"')"
print(strConsulta)
consulta.execute(strConsulta)
consulta.close()
db1.commit()
db1.close()
def consultar():
db2 = sqlite3.connect("tabla.db")
print("Estas en la funcion insertar")
db2row_factory = sqlite3.Row
consulta = db2.cursor()
consulta.execute("select * from tabla")
filas = consulta.fetchall()
lista = []
for fila in filas:
s = {}
s['nombre'] = fila['nombre']
s['autor'] = fila['autor']
s['year'] = str(fila['year'])
lista.append(s)
consulta.close()
db2.close()
return(lista)
#consultar()
def menu():
Opcion= input("\nIngresa la opcion deseada\n1.Inserta un valor en la tabla\n2.Consultar los valores de la tabla\n")
if Opcion==1:
insertar()
menu()
elif Opcion==2:
ListaNovelas = consultar()
for novela in ListaNovelas:
print(novela['nombre'],novela['autor'],novela['year'])
menu()
menu()
I get this error while testing the second method consultar().
$ python file.py
Ingresa la opcion deseada
1.Inserta un valor en la tabla
2.Consultar los valores de la tabla
2
Estas en la funcion insertar
Traceback (most recent call last):
File "insertar.py", line 56, in <module>
menu()
File "insertar.py", line 51, in menu
ListaNovelas = consultar()
File "insertar.py", line 33, in consultar
s['nombre'] = fila['nombre']
TypeError: tuple indices must be integers, not str
db2row_factory = sqlite3.Row
This is the problematic line. Instead you meant to set the row_factory factory on the db2 connection instance:
db2.row_factory = sqlite3.Row
Then, all the fetched rows would be now sqlite3.Row instances having dictionary-like access to field values.
Just discovered this forum and it's unbelievable how helpful its community is. Well, I have an issue trying to make a "while" loop on Python. I want the program's menu to repeat until you select Option 6 - Salir (Exit). Everything is fine at first, but after I select an option and the program prints x thing, I press enter to return to the menu or continue (just like when you use a pause on C) and get an error.
Juan es un empleado mexicano comun con poco salario minimo. Que quiere saber de el? MENU
1.Salario
2.Sombrero
3.Playera
4.Pantalones
5.Tenis
6.Salir
Seleccione una opcion 1 El salario de Juan es 7.5
MENU
1.Salario
2.Sombrero
3.Playera
4.Pantalones
5.Tenis
6.Salir
Seleccione una opcion Traceback (most recent call last): File "C:\Users\joguzman\Documents\Proyectos Eclipse\Clases\src\main.py", line 22, in <module>
opcion=int(input("\nSeleccione una opcion\n")) ValueError: invalid literal for int() with base 10: ''
I also want it to clear the screen, which doesn't happen at all. Here's my code:
import os class empleadoClass: #las propiedades que tendra cada empleado
salario=7.5
sombrero='nike'
playera='polo'
pantalones='patito'
tenis='adidas'
juanObject = empleadoClass() #'juanObjeto' esta "heredando" propiedades de empleadoClass
print ("Juan es un empleado mexicano comun con poco salario minimo. Que quiere saber de el?") opcion=1
while (opcion!=6):
print("MENU \n1.Salario \n2.Sombrero \n3.Playera \n4.Pantalones \n5.Tenis \n6.Salir")
opcion=int(input("\nSeleccione una opcion\n"))
if (opcion==1):
print ("El salario de Juan es ",juanObject.salario)
os.system('pause>nul')
os.system('cls')
elif (opcion==2):
print ("La marca del sombrero de Juan es ",juanObject.sombrero)
os.system('pause>nul')
os.system('cls')
elif (opcion==3):
print ("La marca de la playera de Juan es ",juanObject.playera)
os.system('pause>nul')
os.system('cls')
elif (opcion==4):
print ("La marca de los pantalones de Juan es ",juanObject.pantalones)
os.system('pause>nul')
os.system('cls')
elif (opcion==5):
print ("La marca de los tenis de Juan es ",juanObject.tenis)
os.system('pause>nul')
os.system('cls')
elif(opcion==6):
print ("Gracias por usar nuestro programa!")
else:
print ("Ingrese una opcion correcta")
os.system('pause>nul')
os.system('cls')
Thanks in advance! :D And sorry for any grammar mistakes, as you can see I'm not a native english speaker.
EDIT: It seems the code's structure got a messy when posting... Does anyone know how to solve this? :/
I think a simpler way to do a menu like this is to:
def print_menu():
# print stuff here, 6 exits
while True:
print_menu()
try:
choice = int(input('>>'))
if choice == 1:
blah
elif choice == 2:
more blah
elif choice == 6:
break
except ValueError:
handle_error()
As for clearing the screen that depends on what OS you are using and how you are running the program. See this question - how to clear the screen in python