I need to build a program that parse the logs and gives a date
The log only has hours, no dates, So what am I supposed to do?
I tried using datetime but it just enters the today date
import math
import os
import random
import re
import sys
import logging
import datetime
def main():
file = open("Desktop/a1.log", "r")
lines = file.readlines()
file.close()
today = datetime.date.today()
for line in lines:
print (today, line)
main()
The logs are like this:
10:47:01> Screen 1: 2560x1080 at (0,1080) work area 2327x1080 at (233,1080)
10:47:01> Screen 2: 2560x1080 at (0,0) work area 2304x1080 at (256,0) primary
10:47:01> Screen 3: 1440x2560 at (-1440,0) work area 1440x2560 at (-1440,0)
and I need to set a first date and last date. How can I do this?
When you use today = datetime.date.today() it is only grabbing the current time.
Instead you should use today = datetime.datetime.now()
Then you can use today.year, today.month, today.day, today.hour, today.minute, today.second to get the individual fields you want or today.strftime("%Y-%m-%d %H:%M") to get several values at once.
def main():
file = open("Desktop/a1.log", "r")
lines = file.readlines()
file.close()
today = datetime.datetime.now()
for line in lines:
print (today.strftime("%Y-%m-%d %H:%M"), line)
More details can be found here: https://docs.python.org/3.0/library/datetime.html
Related
My aim is to send an image file in whatsapp to various persons who are not on my contact list. Try with pywhatkit. Store the number in an excel sheet and read it by pandas. Loop the number to send the image file. The first number gets the image file successfully, and the remaining numbers WhatsApp will open but the message will not send. Also tell, me how to avoid WhatsApp open in a new tab(chrome)every time.
Here's my code
import pandas as pd
file_name = r'd:\\Recipients data.xlsx'
df = pd.read_excel(file_name)
contact = df['Contact'].tolist()
import pywhatkit
import time
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M : %S")
h,m,s = current_time.split(':')
print(now)
h1 = int(h)
m1 = int(m)
s1 = int(s)
file_path = r'C:/Users/Asus/Downloads/subadev.jpeg'
for send_number in contact:
new_format = (f'"{str("+91")+str(send_number)}"')
print(new_format)
pywhatkit.sendwhats_image(new_format,file_path)
time.sleep(15)
expecting : send images to every number without open whats app every time
Here is the code (only this):
import pytz
from time import sleep
from datetime import datetime
dt_format = "%H:%M"
tz = pytz.timezone('Asia/Riyadh')
jt = datetime.now(tz)
time_now = (jt.strftime(dt_format))
time = time_now.replace(":","")
timed1 = (int("1530")) #the time in 24h format
while True:
#print('azan on')
if timed1 == time_now:
print(time_now)
print(timed1)
print ("its the time")
sleep (90)
I tried to keep the format normal (15:30) but still the same.
(replace) not required you can delete if so.
You just have to update the time and put it in the loop and it will work , thanks to #MatsLindh (check comments)
Ubuntu restricts what you can do with timezones - most applications have a hard dependency on /etc/localtime, including the Clock applet in mate-panel. I am trying to write a python applet that shows the time in a timezone of the user's choice, but I can't get it to auto refresh - I'd like to display the current time every 1s.
#!/usr/bin/env python
from datetime import datetime
from time import gmtime, strftime, sleep
import pytz
from os.path import expanduser
from os.path import exists
import gi
TIMEZONE = 'Australia/Sydney'
DATE_FMT = '%Y-%m-%d %H:%M:%S'
gi.require_version("Gtk", "2.0")
gi.require_version("MatePanelApplet", "4.0")
from gi.repository import GObject, Gtk, MatePanelApplet
# I moved this code out of applet_fill() and into its own function
# so that I can call it with Gtk.timeout_add or GObject.timeout_add
# ...but I get the dreaded white dot when reloading the app.
def calc_datetime(applet, timezone):
dt_xxx = pytz.timezone(strftime("%Z", gmtime())).localize(datetime.now()).astimezone(pytz.timezone(timezone)).strftime(DATE_FMT)
DateLabel = Gtk.Label(timezone + ':- ' + dt_xxx)
applet.add(DateLabel)
applet.show_all()
# DateLabel.set_text() works, but not when looped.
#while True:
# DateLabel.set_text('Hello')
# sleep(1)
return DateLabel
def applet_fill(applet):
# define custom timezone in ~/.config/company/timezone
cfg_file = expanduser('~') + '/.config/company/timezone'
if exists(cfg_file):
with open(expanduser('~') + '/.config/company/timezone', 'r') as file:
timezone = file.read().replace('\n', '')
else:
timezone = TIMEZONE
DateLabel = calc_datetime(applet, timezone)
# I atempted different things here, but again, white dot in the panel.
#i = 1
#while True:
# sleep(1)
# DateLabel.set_text('test again')
# #i = i + 1
# GObject.idle_add(calc_datetime, applet, timezone)
#Gtk.timeout_add('100', calc_datetime, applet, timezone)
#DateLabel.set_text('test')
#return True
# this is called by mate-panel on applet creation
def applet_factory(applet, iid, data):
if iid != "MyClockApplet":
return False
applet_fill(applet)
return True
MatePanelApplet.Applet.factory_main("MyClockAppletFactory", True, MatePanelApplet.Applet.__gtype__, applet_factory, None)
I put notes in the code comments.
The problem is in calc_datetime
Functions you add with idle_add return True or False, which determines whether this function should be called again.
This idle_added function is called in mainloop. There you can update all the labels.
Iv only just started python but have learned a lot over the last few month, now I have hit a wall about updating objects on a model at a good speed.
I have a model called Products and this is populated from a csv file, every day this file get updated with changes like cost, and quantity, I can compare each line of the file with the Products Model but having 120k lines this takes 3-4hours.
What process can I take to make this process this file faster. I only want to modify the objects if cost and quantity have changed
Any suggestions how I tackle this?
Ver3 of what i have tried.
from django.core.management import BaseCommand
from multiprocessing import Pool
from django.contrib.auth.models import User
from pprint import pprint
from CentralControl.models import Product, Supplier
from CentralControl.management.helpers.map_ingram import *
from CentralControl.management.helpers.helper_generic import *
from tqdm import tqdm
from CentralControl.management.helpers.config import get_ingram
import os, sys, csv, zipfile, CentralControl
# Run Script as 'SYSTEM'
user = User.objects.get(id=1)
# Get Connection config.
SUPPLIER_CODE, FILE_LOCATION, FILE_NAME = get_ingram()
class Command(BaseCommand):
def handle(self, *args, **options):
list_in = get_file()
list_current = get_current_list()
pool = Pool(6)
pool.map(compare_lists(list_in, list_current))
pool.close()
def compare_lists(list_in, list_current):
for row_current in tqdm(list_current):
for row_in in list_in:
if row_in['order_code'] == row_current['order_code']:
#do more stuff here.
pass
def get_current_list():
try:
supplier = Supplier.objects.get(code='440040')
current_list = Product.objects.filter(supplier=supplier).values()
return current_list
except:
print('Error no products with supplier')
exit()
def get_file():
with zipfile.ZipFile(FILE_LOCATION + 'incoming/' + FILE_NAME, 'r') as zip:
with zip.open('228688 .csv') as csvfile:
reader = csv.DictReader(csvfile)
list_in = (list(reader))
for row in tqdm(list_in):
row['order_code'] = row.pop('Ingram Part Number')
row['order_code'] = (row['order_code']).lstrip("0")
row['name'] = row.pop('Ingram Part Description')
row['description'] = row.pop('Material Long Description')
row['mpn'] = row.pop('Vendor Part Number')
row['gtin'] = row.pop('EANUPC Code')
row['nett_cost'] = row.pop('Customer Price')
row['retail_price'] = row.pop('Retail Price')
row['qty_at_supplier'] = row.pop('Available Quantity')
row['backorder_date'] = row.pop('Backlog ETA')
row['backorder_date'] = (row['backorder_date'])
row['backorder_qty'] = row.pop('Backlog Information')
zip.close()
#commented out for dev precess.
#os.rename(FILE_LOCATION + 'incoming/' + FILE_NAME, FILE_LOCATION + 'processed/' + FILE_NAME)
return list_in
I have once faced a problem of slow load of data, I can tell you what i did maybe it can help you somehow, I passed the execution to debug mode and tried to find out wich colomn is causing the slow loading, and everytime i see that a colomn is causing the problem I add an index on it (in the SGBD --> postgreSQL in my case), and it worked. I hope that you are facing the same problem so my answer can help you.
Here it's rough idea:
1, when reading csv, use pandas as suggest by #BearBrow into array_csv
2, convert the obj data from Django into Numpy Arrary array_obj
3, don't compare them one by one , using numpy substraction
compare_index = (array_csv[['cost',['quantity']]] - array[['cost',['quantity']]] == 0)
4, find the updated column
obj_need_updated = array_obj[np.logic_any(compare_index['cost'], compare['quantity'])]
then use Django bulk update https://github.com/aykut/django-bulk-update to bulk update
Hope this will give you hints to speed up your code
I'm trying to make a script which will change every day date in pivot table to yesterday.
Unfortunately, every time when I try this code, Excel automatically calculates date and so from 2014-05-08 makes 41766. How to avoid this issue?
When I write it to 'normal' cell it works fine. Problem is only with pivot tables...
import win32com.client as win32
import datetime
import time
from datetime import date, timedelta
now = datetime.datetime.now()
yesterday = date.today() - timedelta(1)
wczoraj = yesterday.strftime("%Y-%m-07")
#----------------------------------------------------------------------
def excel():
""""""
xl = win32.gencache.EnsureDispatch('Excel.Application')
ss = xl.Workbooks.Open(r'D:\python\skrypty\test.xlsx')
sh = ss.Worksheets("Arkusz5")
xl.Visible = True
time.sleep(1)
#kolumn,
sh.Cells(2,2).Value = wczoraj
ss.Close(True)
xl.Application.Quit()
if __name__ == "__main__":
excel()
try changing "07" to "%d" in strftime