I am building a python code to validate the email address and the phone number in a given CSV file using pandas and I want to write a separate CSV file with only the validated values. I am totally new to python and I have written a code for the functionality as follows:
from email_validator import validate_email, EmailNotValidError
import pandas as pd
import re
file = r'sample.csv'
filtered = r'filtered.csv'
valid = r'Valid.csv'
df=pd.read_csv(file)
def eVali(dataFrame):
try:
validate_email(dataFrame)
return True
except EmailNotValidError:
return False
def phoneValid(dataFrame):
if re.search("\w{3}-\w{3}-\w{4}",dataFrame):
return True
else:
return False
df["Email_validate"] = df['email'].apply(eVali)
df_fltrd = df[df['Email_validate']]
#del df_fltrd['Email_validate']
print(df_fltrd)
df_fltrd["Phone_validate"] =df_fltrd['phone'].apply(phoneValid)
df_valid = df_fltrd[df_fltrd["Phone_validate"]]
del df_valid["Phone_validate", "Email_validate"]
print(df_valid)
df_fltrd.to_csv(filtered)
df_valid.to_csv(valid)
This code is working fine and I could create a new CSV with validated values as I require. but when I tried to organize this code as a proper class with the proper method it gives an error saying,
Traceback (most recent call last):
File "E:\myTasks\validator.py", line 7, in <module>
class Validator:
File "E:\myTasks\validator.py", line 47, in Validator
validation(self.file)
AttributeError: module 'self' has no attribute 'file'
This is the class I created.
Validator.py
import self as self
from email_validator import validate_email, EmailNotValidError
import pandas as pd
import re
class Validator:
def __init__(self):
self.file = r'sample.csv'
self.outFile =r'filteredSample.csv'
def emailValid(dataframe):
try:
validate_email(dataframe)
return True
except EmailNotValidError:
return False
def phoneValid(dataframe):
if re.search("\w{3}-\w{3}-\w{4}", dataframe):
return True
else:
return False
def validation(self):
df = pd.read_csv(self.file)
df = df.copy();
df["Email_validate"] = df['email'].apply(Validator.emailValid)
df_filtered = df[df['Email_validate']]
print(df_filtered)
df_filtered["Phone_validate"] = df_filtered['phone'].apply(Validator.phoneValid)
df_valid = df_filtered[df_filtered["Phone_validate"]]
del df_valid["Email_validate"]
del df_valid["Phone_validate"]
print(df_valid)
df_valid.to_csv(self.outFile)
validation(self)
Can someone please help me with this. It will be really appreciated. Thanks in advance!
Well, you can't call an instance method from the class itself
validation(self)
This bit should be outside of your class, for example it could be called from your main function after having instantiated your Validator object.
my_validator = Validator()
my_validator.validation()
You do not import self.
self is the instance you are in at the time of code execution.
Your problem is that you did not understand classes yet. You tried to call a class method within the class which python does but toes not like.
I'd suggest you have a look at https://docs.python.org/3/tutorial/classes.html and/or https://www.w3schools.com/python/python_classes.asp.
You want to push the last line to the end and add
def main():
i = Validator()
i.validation()
if __name__ == "__main__":
main()
Related
I am new to learning Python and currently I am working on Classes. Below is a program that I am referring to. However, on calling the class, I am getting the following error:
from airtravel import *
a = Aircraft("G-EUPT", "Airbus A319", num_rows=22, num_seats_per_row=6)
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Aircraft' is not defined
Code:
class Flight:
def __init__(self, number):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid route number '{}'".format(number))
if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number '{}'".format(number))
self._number = number
def number(self):
return self._number
def airline(self):
return self._number[:2]
class Aircraft:
def __init__(self, registration, model, num_rows, num_seats_per_row):
self._registration = registration
self._model = model
self._num_rows = num_rows
self._num_seats_per_row = num_seats_per_row
def registration(self):
return self._registration
def model(self):
return self._model
def seating_plan(self):
return(range(1, self._num_rows + 1),
"ABCDEFGHJK"[:self._num_seats_per_row])
Where am I going wrong? Kindly help me understand. Why am I receiving this error?
I plan to execute the command a.Registration to get G-EUPT as the output.
this is a basic question in python for the new ,the python searchs for file path,
I guess the airtravel is that you create .PY(airtravel.py) file in your folder
like this:
and you will get the answer:
and if you learn more about import python file ,you can look here.
the import in python
from airtravel import (Aircraft, Flight)
First, I am using python 3.6.
I am trying import and use my own .py file in my project. I import my LinkedList.py file and create a Mylist class, which extends the imported file's class.
When I try the construct an instance of the Mylist class, which involves creating an instance of my inheritedLinkedList derived class, I get the following error:
Traceback (most recent call last):
File "*/PycharmProjects/Veri Yapilari/lists.py", line 65, in <module>
test = Mylist()
File "*/PycharmProjects/Veri Yapilari/lists.py", line 38, in __init__
self.linkedlist = inheritedLinkedList()
File "*/PycharmProjects/Veri Yapilari/lists.py", line 8, in __init__
super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument
Here's the section of the code where the problem occurs:
test = Mylist()
test.insertFirstM(incomingDataM=4) # <- Causes a TypeError.
Below is the main script in its entirety:
import LinkedList as myLinkedList
class inheritedLinkedList(myLinkedList.DoublyLinkedList):
def __init__(self):
super.__init__()
def raplaceElements(self, dataToBeChanged, incomingData):
position = self.find(dataToBeChanged)
position.data = incomingData
def swapElements(self, swap1, swap2):
position1 = self.find(swap1)
prev1 = position1.previous
next1 = position1.next
position2 = self.find(swap2)
prev2 = position2.previous
next2 = position2.next
prev1.next = position1
position1.previous = prev1
position1.next = next1
next1.previous = position1
prev2.next = position2
position2.previous = prev2
position2.next = next2
next2.previous = position2
def insertBefore(self, incomingData, previousNode=None):
self.insert(incomingData, self.find(previousNode).previous.data)
class Mylist:
def __init__(self):
# self.linkedlist = inheritedLinkedList;
self.linkedlist = inheritedLinkedList() # Per martineau's suggestion.
def replaceElements(self, dataToBeChanged, incomingData):
self.linkedlist.raplaceElements(dataToBeChanged, incomingData)
def swapElements(self, swap1, swap2):
self.linkedlist.swapElements(swap1, swap2)
def insertFirstM(self, incomingDataM):
self.linkedlist.insertFirst(incomingDataM)
def insertLast(self, incomingData):
self.linkedlist.insert(incomingData)
def insertAfter(self, incomingData, incomingNode):
self.linkedlist.insert(incomingData, incomingNode)
def insertBefore(self, incomingData, incomingNode):
self.linkedlist.insert(incomingData, incomingNode)
def remove(self, incomingData):
self.linkedlist.remove(incomingData)
def listprint(self):
self.linkedlist.listprint()
test = Mylist()
test.insertFirstM(4)
The code for the imported LinkedList module (LinkedList.py) can be obtained—if needed—by downloading it from my github repository.
As I said in a comment, you're not using the super built-in correctly. Try do things this way instead (so it's like the example in the linked documentation):
class inheritedLinkedList(myLinkedList.DoublyLinkedList):
def __init__(self):
super().__init__() # Change line to this.
Actually, since the derived class' __init__() is currently doing nothing but that, it's not even necessary because that's what would occur automatically if the subclass didn't define its own. In other words, the following would accomplish the same thing:
class inheritedLinkedList(myLinkedList.DoublyLinkedList):
# ** NOT REALLY NEEDED AT ALL **
# def __init__(self):
# super().__init__()
P.S. You also ought to change the very end of the LinkedList.py script so the last few lines that are there don't execute when it's imported as a module by lists.py:
...
nextNode.previous = previousNode
dataToBeDeleted.next = dataToBeDeleted.previous = None
if __name__ == '__main__': # Should add this, too.
list1 = SinglyLinkedList()
list2 = DoublyLinkedList()
list2.insertFirst(6)
I'd like to write class which reads the *.csv file and parse it using the pandas library. I'm wondering where I should initialize df.
#!/usr/bin/env python
import pandas as pd
import os
class ParseDataBase(object):
def __init__(self, name_file):
self.name_file = name_file
def read_file(self):
"""Read the file concent"""
try:
self.df = pd.read_csv(self.name_file)
except IndexError:
print ("Error: Wrong file name")
sys.exit(2)
return self.df
def dispaly_file(self):
print self.df
def main():
x = ParseDataBase('something.csv')
x.dispaly_file()
if __name__ == '__main__':
main()
The above code returns the following error: 'ParseDataBase' object has no attribute 'df'.
I don't want to pass to many variables while crating the object.
I'm new to object oriented programming, so any comments and hints are highly appreciated!
You aren't assigning self.df unless you run read_file(), which you aren't.
def main():
x = ParseDataBase('something.csv')
x.read_file()
x.dispaly_file()
the attribute df gets assigned in the read_file method. You are trying to access that attribute prior to it existing.
I'd do this:
#!/usr/bin/env python
import pandas as pd
import os
class ParseDataBase(object):
def __init__(self, name_file):
self.name_file = name_file
# Change I made to initiate in the init method.
self.df = self.read_file()
def read_file(self):
"""Read the file concent"""
try:
self.df = pd.read_csv(self.name_file)
except IndexError:
print ("Error: Wrong file name")
sys.exit(2)
return self.df
def dispaly_file(self):
print self.df
def main():
x = ParseDataBase('something.csv')
x.dispaly_file()
if __name__ == '__main__':
main()
I wrote a code which is going to store occurrences of words from a text file and store it to a dictionary:
class callDict(object):
def __init__(self):
self.invertedIndex = {}
then I write a method
def invertedIndex(self):
print self.invertedIndex.items()
and here is how I am calling:
if __name__ == "__main__":
c = callDict()
c.invertedIndex()
But it gives me the error:
Traceback (most recent call last):
File "E\Project\xyz.py", line 56, in <module>
c.invertedIndex()
TypeError: 'dict' object is not callable
How can I resolve this?
You are defining a method and an instance variable in your code, both with the same name. This will result in a name clash and hence the error.
Change the name of one or the other to resolve this.
So for example, this code should work for you:
class CallDict(object):
def __init__(self):
self.inverted_index = {}
def get_inverted_index_items(self):
print self.inverted_index.items()
And check it using:
>>> c = CallDict()
>>> c.get_inverted_index_items()
[]
Also check out ozgur's answer for doing this using #property decorator.
In addition to mu's answer,
#property
def invertedIndexItems(self):
print self.invertedIndex.items()
then here is how you'll cal it:
if __name__ == "__main__":
c = callDict()
print c.invertedIndexItems
Methods are attributes in Python, so you can't share the same name between them. Rename one of them.
I'm sending a variable value from programa1 for a new object using :
def send_price(self):
self.pricesend = float(self.text1.get()) #this take a value from a tkinker.Entry
print(self.pricesend)
objetoprograma1.Object(self.pricesend)
the object "objetoprograma1" return a new value using:
class Object():
def __init__(self, price):
self.price_recibe = float(price)
print(self.price_recibe)
self.new_price = self.price_recibe + 10
print(self.new_price)
programa1.Aplication.recibe_newprice(self, float(self.new_price))
now I want to update the value in the principal1 tkinter.Entry called self.text1:
def recibe_newprice(self, new_price):
self.new_price = new_price
print("price new recibe" , self.new_price)
## this don't work.. this don't update or change the value in the tkinter.Entry
self.text1.delete(0, len(self.text1.get()))
self.text1.insert(self.my_main, str(self.new_price))
I have the following exception:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "B:\MAESTRIA\PYTHON\trabajos\hello\programa1.py", line 38, in send_price
objetoprograma1.Object(self.pricesend)
File "B:\MAESTRIA\PYTHON\trabajos\hello\objetoprograma1.py", line 19, in __init__
programa1.Aplication.recibe_newprice(self, float(self.new_price))
File "B:\MAESTRIA\PYTHON\trabajos\hello\programa1.py", line 51, in recibe_newprice
self.text1.delete(self.my_main, len(self.text1.get()))
AttributeError: 'Object' object has no attribute 'text1'
the full programa1.py
# -*- coding: latin-1 -*-
import tkinter
import objetoprograma1
import time
class Aplication():
def __init__(self,my_main):
self.my_main = my_main
self.variables()
self.GUI()
def variables (self):
self.price = None
self.list = []
def GUI(self):
self.text1 = tkinter.Entry()
self.text1.insert(0, "1000")
self.text1.grid(column = 0, row = 0)
self.boton1 = tkinter.Button(self.my_main, text = "sendprice", command = self.send_price )
self.boton1.grid(column=1, row = 0)
def send_price(self):
self.pricesend = float(self.text1.get())
print(self.pricesend)
objetoprograma1.Object(self.pricesend)
def recibe_newprice(self, new_price):
self.new_price = new_price
print("price new recibe" , self.new_price)
## this don't work
self.text1.delete(0, len(self.text1.get()))
self.text1.insert(self.my_main, str(self.new_price))
if __name__ == "__main__":
root = tkinter.Tk()
#root.geometry("800x500+0+0")
root.title("titulo")
app = Aplication(my_main=root)
root.mainloop()
and objetoprograma1.py
# -*- coding: latin-1 -*-
import programa1
import tkinter
import time
class Object():
def __init__(self, price):
self.price_recibe = float(price)
print(self.price_recibe)
self.new_price = self.price_recibe + 10
print(self.new_price)
programa1.Aplication.recibe_newprice(self, float(self.new_price))
Look at your Object class and look at the exception message. You are calling the recibe_newprice method, but passing it the Object instance (Object has no text1 attribute). The recibe_newprice is written for the Aplication class and as such expects self to be an instance of the Aplication class. You seem to be mixing up what classes are for or how the self argument works.
My first tip is to name things with more descriptive names. Names like Object, Application, and Program1 don't tell the reader anything about what the purpose of those objects are.
Second, do you know the difference between classes and functions? Maybe this will help. I would code the send_price method this way:
def send_price(self, price_recibe):
pricesend = float(self.text1.get())
print(pricesend)
print(price_recibe)
new_price = price_recibe + 10
print(new_price)
self.recibe_newprice(new_price)
If this doesn't make sense why I'm doing things this way or why this might be considered better/easier than the way you did it then I suggest researching how python classes, attribute assignment, and argument passing works.