Hi ! need help in choice of class with a customer - appointment table - python

I'm trying to build a "customer" menu textbased to make automatic appointments and manage conflicts. I'm using Python but so far I've been using procedural programming (I'm a newbie), but it's getting too complicated to maintain. I would like to use a CSV file to keep all the contact (up to a thousand) and calcurse to manage my calendar.
Basically, I would like to check if the contact is approved and then send him my availabilities. I try to use the datefinder module to get a date & time, check if available, and ask for the order.
I'd like to know what class I need to create:
import csv
import datetime
import time
class Contact:
def __init__(self, index, name, last_time, last_level='unreliable', appdate=None, apporder=None, appnote=None):
self.index = index
self.name = name
self.last_time = last_time #last time I received a message from this person to unlogg if it's
over 5min agoo
self.last_level = last_level # can be unreliable,main,date,order,note as
# unreliable= don't even send availibities
# main=the person is logged send availibities
# date=ask for date, order=ask for order, note=ask for note
self.appdate = datetime.datetime.strptime(appdate, '%Y-%m-%d %H:%M').date()
self.apporder = apporder
self.appnote = appnote
class Appointment:
# I'd like to create a class for all the appointments from the calcurse file listing all the appointments to be able to check for instances if any conflicts with any new appointments.
def __init__(self, name, appdate, apporder, appnote):
self.name = name
self.appdate = datetime.datetime.strptime(appdate, '%Y-%m-%d %H:%M')
self.apporder = apporder
self.appnote = appnote
def find_contact_in_file(name):
try:
with open("contact", 'r') as csvfile:
reader = csv.reader(csvfile)
index = 0
for line in reader:
index += 1
if line[0] == name:
print(index, line)
contact = Contact(index, line[0], line[1], line[2], line[3], line[4], line[5])
return contact
return contact.Contact(index + 1, name, int(time.time()), "unreliable", None, None, None)
except IOError:
print("I/O error")
Do I need a class Reply to make the reply from the message I get from the customer?

Related

Assigning worker names to groups based on the number of days they've worked

This will be hard to explain but please bare with me. I'm currently trying to complete a question for an online course and I have literally no idea what to do. The scenario of the question places me as a programmer in an office that needs to create a program assigning specific staff members that have attended a conference. The task gave me two text files; one text file titled "confPack.txt" that reads
Basic conference pack
Bonus conference pack
And another titled "employees.txt" that reads:
Williams,Mary,Y
Nguyen,Vinh,,Y
Kingsley,Margret
Kline,Bob,Y,Y
Mitchell,Frank,Y
Lowe,Elizabeth,Y,Y
Basically, I need to assign certain staff members to their appropriate group/"pack" based on how many days they have attended the conference. The "Y"'s in the employee.txt file represents the amount of days they have attend (one Y = one day of attendance).
The course question itself wants me to access the confpack.txt file and read the records into an array , access the employees.txt file and loop through the records (checking for the end of file) and use logical operators to select the appropriate conference attendees. They said it should be displayed like so:
Report date: [dd/mm/yyyy] *i've already displayed the time correctly
Attendee: [Surname, first name] Pack/s: [1 or 2 days pack], [both days pack]
And here is what my code looks like so far:
import datetime
dTime = datetime.datetime.now()
confFile = open("confPack.txt", "r+")
print("Report Date: "+ dTime.strftime("%d/%m/%Y"))
print(confFile.read())
with open("employees.txt", "r") as f:
data = f.readlines()
for line in data:
words = line.split(",")
print(words)
confFile.close()
Any help is appreciated. And if you're wondering why I can't contact my course teachers for help, believe me when I say that they are never online.
Edit: In regards #Adirio
I want the output to look like this:
Report Date: 7/9/2020
Attendee: [Williams, Mary] Pack/s: [Basic Conference Pack]
Attendee: [Nguyen, Vinh] Pack/s: [Basic Conference Pack]
Attendee: [Kingsley, Margret] Pack/s: [N/A]
Attendee: [Kline, Bob] Pack/s: [Bonus Conference Pack]
Attendee: [Mitchell, Frank] Pack/s: [Basic Conference Pack]
Attendee: [Lowe, Elizabeth] Pack/s: [Bonus Conference Pack]
Edit #2: Thanks again #Adirio for the answer. However, I actually needed to access the confPack.txt file which reads:
Basic Conference Pack
Bonus Conference Pack
and the print out either the Basic or Bonus conference Pack for its employee.
from datetime import datetime
class Employee:
def __init__(self, surname, name, *args):
self.name = name.strip()
self.surname = surname.strip()
self.days = 0
for arg in args:
if arg.strip() == 'Y':
self.days += 1
now = datetime.now()
print("Report Date: " + now.strftime("%d/%m/%Y"))
#Here i've tried making a .readlines variable to print out the specific conference pack
conf = open("confPack.txt")
all_lines = conf.readlines()
with open("employees.txt", "r") as f:
employees = []
for line in f.readlines():
if len(line.strip()) != 0:
employees.append(Employee(*line.split(",")))
for employee in employees:
print(f'Attendee: [{employee.surname}, {employee.name}]', end=' ')
if employee.days == 2:
print("Pack/s: [" + all_lines[2]+"]")
elif employee.days == 1:
print("Pack/s: [" + all_lines[0]+"]")
else:
print("Pack/s: [N/A]")
Output:
Report Date: 09/09/2020
Attendee: [Williams, Mary] Pack/s: [Basic conference pack
] #As you can see, it prints on a new line
Attendee: [Nguyen, Vinh] Pack/s: [Basic conference pack
]
Attendee: [Kingsley, Margret] Pack/s: [N/A]
Attendee: [Kline, Bob] Pack/s: [Bonus conference pack]
Attendee: [Mitchell, Frank] Pack/s: [Basic conference pack
]
Attendee: [Lowe, Elizabeth] Pack/s: [Bonus conference pack]
Process finished with exit code 0
First I will clean your original code a bit, removing the file you are opening and closing for nothing and using the a with clause for the other as it is a very healthy pattern.
from datetime import datetime
now = datetime.now()
print("Report Date: " + now.strftime("%d/%m/%Y"))
with open("confPack.txt", "r+") as confFile:
print(confFile.read())
with open("employees.txt", "r") as f:
for line in f.readlines():
words = line.split(",")
print(words)
Now let's get to work. We will create a class that represents each employee:
class Employee:
def __init__(self, surname, name, *args):
self.name = name
self.surname = surname
self.days = 0
for arg in args:
if arg.strip() == 'Y':
self.days += 1
The __init__ method accepts the arguments as they are read from the file (surname, name, and a sequence of 'Y'). The name and surname are assigned directly while the rest of the arguments are stored in a list called args. We loop through this list adding 1 day to the day counter if it is equal to 'Y'. The .strip() part removes leading and trailing whitespaces so that we can compare to 'Y' safely.
So all together:
from datetime import datetime
class Employee:
def __init__(self, surname, name, *args):
self.name = name.strip()
self.surname = surname.strip()
self.days = 0
for arg in args:
if arg.strip() == 'Y':
self.days += 1
print("Report Date: " + datetime.now().strftime("%d/%m/%Y"))
with open("confPack.txt", "r+") as f:
packs = ['N/A']
for line in f.readlines():
if len(line.strip()) != 0:
packs.append(line.strip())
with open("employees.txt", "r") as f:
employees = []
for line in f.readlines():
if len(line.strip()) != 0:
employees.append(Employee(*line.split(",")))
# Do whatever you need with the employee list
for employee in employees:
print(f"Attendee: [{employee.surname}, {employee.name}] Pack/s: [{packs[employee.days]}]")
We could also make the part where we open the files shorter by using list comprehensions:
with open("confPack.txt", "r+") as f:
packs = ['N/A'] + [line.strip() for line in f.readlines() if len(line.strip())]
with open("employees.txt", "r") as f:
employees = [Employee(line.split(",")) for line in f.readlines() if len(line.strip())]

How to use weatherstack.com api

Hi I'm pretty new to python. I try to make a chatbot with rasa for personal use. Would like to add weather api now. I use weatherstack.com. If I use their example this works, but if I adjust it to my own liking I always end up with the same error.
condition = api_response['current']['weather_descriptions'] TypeError: list indices must be integers or slices, not str
This is the code I am using.
class Actionweatherapi(Action):
def name(self):
return "actions.weatherapi"
def run(self, dispatcher, tracker, domain):
import requests
location = tracker.get_slot('location')
if location == 'None':
location = 'fetch:ip'
api_result = requests.get('http://api.weatherstack.com/current?access_key=0000000&query={}'.format(location))
api_response = api_result.json()
country = api_response['request']['query']
condition = api_response['current']['weather_descriptions']
temperature_c = api_response['current']['temperature']
humidity = api_response['current']['humidity']
wind_mph = api_response['current']['wind_speed']
response = """It is currently {} in {} at the moment. The temperature is {} degrees, the humidity is {}% and the wind speed is {} mph.""".format(condition, country, temperature_c, humidity, wind_mph)
dispatcher.utter_message(response)
return [AllSlotsReset()]
I have also reviewed the printf-style String Formatting on the python website, but I cannot figure out how to get it working.
The json response is like this example
http://api.weatherstack.com/current?access_key=1c80e8b9fe4fcef4f8d6fd7514a8e9e9&query=New%20York

How to speed up writing in a database?

I have a function which search for json files in a directory, parse the file and write data in the database. My problem is writing in database, because it take around 30 minutes. Any idea how can I speed up writting in a database? I have few quite big files to parse, but parsing the file is not a problem it take around 3 minutes. Currently I am using sqlite but in the future I will change it to PostgreSQL.
Here is my function:
def create_database():
with transaction.atomic():
directory = os.fsencode('data/web_files/unzip')
for file in os.listdir(directory):
filename = os.fsdecode(file)
with open('data/web_files/unzip/{}'.format(filename.strip()), encoding="utf8") as f:
data = json.load(f)
cve_items = data['CVE_Items']
for i in range(len(cve_items)):
database_object = DataNist()
try:
impact = cve_items[i]['impact']['baseMetricV2']
database_object.severity = impact['severity']
database_object.exp_score = impact['exploitabilityScore']
database_object.impact_score = impact['impactScore']
database_object.cvss_score = impact['cvssV2']['baseScore']
except KeyError:
database_object.severity = ''
database_object.exp_score = ''
database_object.impact_score = ''
database_object.cvss_score = ''
for vendor_data in cve_items[i]['cve']['affects']['vendor']['vendor_data']:
database_object.vendor_name = vendor_data['vendor_name']
for description_data in cve_items[i]['cve']['description']['description_data']:
database_object.description = description_data['value']
for product_data in vendor_data['product']['product_data']:
database_object.product_name = product_data['product_name']
database_object.save()
for version_data in product_data['version']['version_data']:
if version_data['version_value'] != '-':
database_object.versions_set.create(version=version_data['version_value'])
My models.py:
class DataNist(models.Model):
vendor_name = models.CharField(max_length=100)
product_name = models.CharField(max_length=100)
description = models.TextField()
date = models.DateTimeField(default=timezone.now)
severity = models.CharField(max_length=10)
exp_score = models.IntegerField()
impact_score = models.IntegerField()
cvss_score = models.IntegerField()
def __str__(self):
return self.vendor_name + "-" + self.product_name
class Versions(models.Model):
data = models.ForeignKey(DataNist, on_delete=models.CASCADE)
version = models.CharField(max_length=50)
def __str__(self):
return self.version
I will appreciate if you can give me any advice how can I improve my code.
Okay, given the structure of the data, something like this might work for you.
This is standalone code aside from that .objects.bulk_create() call; as commented in the code, the two classes defined would actually be models within your Django app.
(By the way, you probably want to save the CVE ID as an unique field too.)
Your original code had the misassumption that every "leaf entry" in the affected version data would have the same vendor, which may not be true. That's why the model structure here has a separate product-version model that has vendor, product and version fields. (If you wanted to optimize things a little, you might deduplicate the AffectedProductVersions even across DataNists (which, as an aside, is not a perfect name for a model)).
And of course, as you had already done in your original code, the importing should be run within a transaction (transaction.atomic()).
Hope this helps.
import json
import os
import types
class DataNist(types.SimpleNamespace): # this would actually be a model
severity = ""
exp_score = ""
impact_score = ""
cvss_score = ""
def save(self):
pass
class AffectedProductVersion(types.SimpleNamespace): # this too
# (foreign key to DataNist here)
vendor_name = ""
product_name = ""
version_value = ""
def import_item(item):
database_object = DataNist()
try:
impact = item["impact"]["baseMetricV2"]
except KeyError: # no impact object available
pass
else:
database_object.severity = impact.get("severity", "")
database_object.exp_score = impact.get("exploitabilityScore", "")
database_object.impact_score = impact.get("impactScore", "")
if "cvssV2" in impact:
database_object.cvss_score = impact["cvssV2"]["baseScore"]
for description_data in item["cve"]["description"]["description_data"]:
database_object.description = description_data["value"]
break # only grab the first description
database_object.save() # save the base object
affected_versions = []
for vendor_data in item["cve"]["affects"]["vendor"]["vendor_data"]:
for product_data in vendor_data["product"]["product_data"]:
for version_data in product_data["version"]["version_data"]:
affected_versions.append(
AffectedProductVersion(
data_nist=database_object,
vendor_name=vendor_data["vendor_name"],
product_name=product_data["product_name"],
version_name=version_data["version_value"],
)
)
AffectedProductVersion.objects.bulk_create(
affected_versions
) # save all the version information
return database_object # in case the caller needs it
with open("nvdcve-1.0-2019.json") as infp:
data = json.load(infp)
for item in data["CVE_Items"]:
import_item(item)

A log file which contains information like < timestamp , customer-id , page-id ,list of titles>

Write a code to print all the unique customers visited in last hour
My try:
import datetime
def find_repeated_customer():
file_obj = open(" my file path","r")
customer_last_visit = {}
repeat_customer = set()
while line in file_obj:
timestamp,customer_id,page_id = line.split(" : ")
last_visit = customer_last_vist.get(customer_id,None)
if not last_visit:
customer_last_visit[customer_id] = last_visit
else:
# assuming time stamp looks like 2016-10-29 01:03:26.947000
year,month,date = timestamp.split(" ")[0].split("-")
current_visit = datetime.date(year,month,date)
day_diff = current_visit - last_visit
if day_diff >=1:
repeat_customer.add(customer_id)
customer_last_visit[customer_id] = current_visit
I am completely failing over in order to get my desired output. By doing this I am able to get repeated customers in last one day but how to get unique users?
You can't do this kind of manipulation in one pass. You have to pass once through lines to get customers, and only then You can check who came once. In another pass, You check if current customer is in list on once-customers and do something with him.

Using Python Classes and Lists to print reports from a csv

I have a homework assignment that I have been stuck on for several days.
Basic problem description:
Incident class has properties: ID, time, type, location, narrative and status
methods: init, brief, isMorning, resolve
script takes one argument, the full path of crime report csv.
First few lines of CSV:
ID Time Type Location Narrative
1271 11:54 AM Drug Violation Wolf Ridge Report of possible drug violation. Student was referred to the university.
My code so far:
import sys
class Incident:
def __init__(self, ID, time, type, location, narrative, status):
self.ID = id
self.time = time
self.type = type
self.location = location
self.narrative = narrative
self.status = status
def brief(self):
print '''{0}: {1}, {2}
{3}
'''.format(self.ID, self.type, self.status, self.narrative)
def isMorning(self):
if 'AM' in self.time:
return True
else:
return False
def resolve(self):
if self.status == 'Pending':
self.status = 'Resolved'
try:
dataset = sys.argv[1] except IndexError:
print 'Usage: Requires full path input file name.'
sys.exit()
# Create an empty list to contain the Incident objects. crimeList = []
# Read the crime report. with open(dataset, 'r') as f:
# Read the header.
headers = f.readline().split(',')
# Read each record and parse the attributes.
for line in f:
lineList = line.strip().split(',')
reportNumber = lineList[0]
timeReported = lineList[1]
incidentType = lineList[2]
location = lineList[3]
narrative = lineList[4]
status = lineList[5].strip()
### Create initialize an Incident object instance and store it in a variable
crime = Incident(reportNumber, timeReported, incidentType, location, narrative, status)
### Append the new Incident object to the crimeList.
crimeList.append(crime)
What i'm stuck on:
I need to access the "nth" Incident in the crimeList and run various methods. I can't seem to find a way to access the item and have it functional to run methods on.
I've tried enumerating and splicing but just can't get anything to work?
Anyone have any suggestions?
Look up the nth crime from your crimeList like so: x=crimeList[n], and then call the methods on that instance: x.brief(), x.resolve(), etc.

Categories