Does torch.save() overwrite the previous saved model which was saved in the same path? - torch

If i use torch.save() every epoch, does it overwrite the previous one? The task_id is '0', so the path is always './0checkpoint.pth.jar'.
def save_checkpoint(state, is_best,task_id, filename='checkpoint.pth.tar'):
f = open('./' + task_id + filename, 'w')
torch.save(state, './' + task_id + filename)
if is_best:
shutil.copyfile('./' + task_id + filename, './' + task_id + 'model_best.pth.tar')
f.close()

Related

Obspy Issue: Acceleration Data from GeoNet

I am trying to create ASCII files of acceleration data from various New Zealand earthquakes, code attached below. I was previously able to generate ASCII files of the acceleration data, however, now running the same code I get the message:
UserWarning: The StationXML file has version 1, ObsPy can read versions (1.0, 1.1). Proceed with caution.
And the acceleration files are no longer being written. Please let me know if there is a way to fix this issue.
import os
from obspy import UTCDateTime
from obspy.clients.fdsn import Client as FDSN_Client
from obspy import read_inventory
from obspy.geodetics import kilometers2degrees
client = FDSN_Client("GEONET")
evid="2016p858000"
minrad=kilometers2degrees(0)
maxrad=kilometers2degrees(30)
cat = client.get_events(eventid=evid)
print(cat)
event = cat[0]
origin = event.origins[0]
otime = origin.time
print(otime)
otime = cat[0].origins[0].time
print(otime)
inventory = client.get_stations(latitude=cat[0].origins[0].latitude,
longitude=cat[0].origins[0].longitude,
minradius=minrad,
maxradius=maxrad,
channel="H??",
level="channel",
starttime = otime-60,
endtime = otime+4*60).remove(channel='HNZ')
print(inventory)
from obspy import Stream
st = Stream()
for network in inventory:
for station in network:
try:
st += client.get_waveforms(network.code, station.code, "*", "H??",
otime-60, otime + 4*60, attach_response=True).remove(channel='HNZ')
st_rem1=st.copy()
pre_filt = (0.025, 0.03, 70.0, 80.0)
acc = st_rem1.copy()
acc.remove_response(output='ACC', pre_filt=pre_filt)
print(acc[0])
acc.plot()
acc[0].write('MS_' + evid + '_' + network.code + '_' + station.code + '_' + station[0].code + '.ascii', format='SLIST')
acc[1].write('MS_' + evid + '_' + network.code + '_' + station.code + '_' + station[1].code + '.ascii', format='SLIST')
except:
pass

How to get a message from a Chat, after using a command and storing that message as a variable (Pyrogram)?

So I am kinda new with Pyrogram and I want to create my own Genshin Bot. After using the command redeem code, I want the message to be taken and stored as variable. so can anyone help me with that
after taking the code as input from user I would be able to use genshin.py api wrapper to redeem code. Just need help with getting message and storing it as variable.
import genshin
import os
from dotenv import load_dotenv
from pyrogram import Client, filters
load_dotenv()
global chatid
chatid = 842544591
global uid
uid = os.getenv("uid")
ltuid = os.getenv("ltuid")
ltoken = os.getenv("ltoken")
cookie_token = os.getenv("cookie_token")
api_id = os.getenv("api_id")
api_hash = os.getenv("api_hash")
bot_token = os.getenv("bot_token")
cookies = {"ltuid": ltuid,
"ltoken": ltoken,
"cookie_token": cookie_token,
"uid": uid}
client = genshin.Client(cookies)
bot = Client(
"Genshin Bot",
api_id=api_id,
api_hash=api_hash,
bot_token=bot_token
)
#bot.on_message(filters.command('start'))
def start_command(bot, message):
message.reply_text(
"Welcome to Genshin Auto Tasks Bot.\nFor Getting Started Use /help command.")
#bot.on_message(filters.command('help'))
def help_command(bot, message):
message.reply_text("This is Bot's Help Section")
#bot.on_message(filters.command('notes'))
async def get_notes(bot, message):
data = await client.get_full_genshin_user(uid)
notes = await client.get_notes(uid)
active_days = (data.stats.days_active)
total_characters = (data.stats.characters)
abyss_total_stars = (data.abyss.previous.total_stars)
resin_count = notes.current_resin
resin_recovery_time = notes.remaining_resin_recovery_time
await message.reply_text("Pranay Asia" + "\n" +
"uid : " + str(uid) + "\n" +
"-----------------------------------------------------------------" + "\n" +
"Resin Count: " + str(resin_count) + "/" + str(notes.max_resin) + "\n" +
"Countdown to next resin recovery: " + str(resin_recovery_time) + "\n" +
"Total No. of Active Days: " + str(active_days) + "\n" +
"Total No. of Characters: " + str(total_characters) + "\n" +
"Total Stars in Abyss: " + str(abyss_total_stars)
)
#bot.on_message(filters.command('redeemcode'))
def redeem_code(bot, message):
message.reply_text("Send the Code to Redeem")
bot.run()
try message.text
I use it as a userbot but nothing changes so much. This piece of code saves the sent message to a variable and filters out the command itself. For a bot it will be easier: answer = message.text
#app.on_message(filters.command("ns", prefixes=".") & filters.text)
async def EXAMPLE(_,msg):
orig_text = msg.text.split(".ns ", maxsplit=1)[1]
text = orig_text

xcom value are overwritten by else statement in if/else loop

I'm looping through a folder with some sql files. For each file I want to push them as xcom value with specific value for each queries.
The code below is kind of working however not when adding else statement. not set value is overwriting everything.
directory = r'airflow_home/dags/sql'
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as file:
sqlFile = file.read()
file.close()
if filename == 'api_params.sql':
query = sqlFile.format(partitioned_key,execution_date_second,partitioned_key,next_execution_date_second)
if filename == 'create_fact_table.sql':
query = sqlFile.format(fact_table_dest)
if filename == 'create_geo_table.sql':
query = sqlFile.format(fact_table_dest)
if filename == f'{geo_type}'+'.sql':
query = sqlFile.format(execution_date)
filename = 'geo_query'
if filename == 'schema_' + f'{schema}' + '.sql':
query = sqlFile.format(fact_table_dest,raw_table_dest,execution_date,next_execution_date)
filename = 'production_query'
if filename == 'insert_key.sql':
query = sqlFile.format(raw_table_dest,execution_date,next_execution_date)
else:
query = 'not set'
task_instance.xcom_push(key=filename, value=query)
can someone explain me what's happening here?
You are using multiple if statements, which are executed one after the other. The else just referring to the last if statement and therefore overwriting previous set query parameter. What you are actually looking for is elif - see Python Docs.
directory = r'airflow_home/dags/sql'
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r') as file:
sqlFile = file.read()
file.close()
if filename == 'api_params.sql':
query = sqlFile.format(partitioned_key,execution_date_second,partitioned_key,next_execution_date_second)
elif filename == 'create_fact_table.sql':
query = sqlFile.format(fact_table_dest)
elif filename == 'create_geo_table.sql':
query = sqlFile.format(fact_table_dest)
elif filename == f'{geo_type}'+'.sql':
query = sqlFile.format(execution_date)
filename = 'geo_query'
elif filename == 'schema_' + f'{schema}' + '.sql':
query = sqlFile.format(fact_table_dest,raw_table_dest,execution_date,next_execution_date)
filename = 'production_query'
elif filename == 'insert_key.sql':
query = sqlFile.format(raw_table_dest,execution_date,next_execution_date)
else:
query = 'not set'
task_instance.xcom_push(key=filename, value=query)

Writing CSV table to PDF Report

I have a script that pulls data from a .csv file and writes it to a newly-generated PDF using ReportLab. It works fine but the data itself is skewed all over the page when I open the document as it has been written as a string. Is there any way I can write this data from the .csv file to the PDF in the same format as the .csv file (Table Form) so it is organised and readable?
# Script to generate a PDF report after data has been parsed into simInfo.csv file
import csv
import datetime
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
now = datetime.datetime.now()
def import_data(pdf, data_file):
sim_card_info = csv.reader(open(data_file, "r"))
for row in sim_card_info:
_id = row[0]
icc_id = row[1]
sim_id = row[2]
display_name = row[3]
carrier_name = row[4]
number = row[5]
data_roaming = row[6]
mcc = row[7]
mnc = row[8]
pdf_filename = _id + icc_id + sim_id + display_name + carrier_name + number + data_roaming + mcc + mnc + '.pdf'
generate_report(pdf, _id, icc_id, sim_id, display_name, carrier_name, number, data_roaming, mcc, mnc, pdf_filename)
def generate_report(pdf, _id, icc_id, sim_id, display_name, carrier_name, number, data_roaming, mcc, mnc, pdf_filename):
sim_data = "" + _id + icc_id + sim_id + display_name + carrier_name + number + data_roaming + mcc + mnc
pdf.drawString(50, 100, sim_data)
pdf.showPage()
def front_page():
pdf = canvas.Canvas("H:\College Fourth Year\Development Project\Final Year Project 2018\Forensic Reports\Sim Card Report.pdf", pagesize=letter)
pdf.setLineWidth(.3)
pdf.setFont('Helvetica', 12)
pdf.drawString(30, 750, 'LYIT MOBILE FORENSICS DIVISION')
pdf.drawString(500, 750, "Date: " + now.strftime("%d-%m-%y")) # Prints date of the report(on the fly)
pdf.line(500, 747, 595, 747)
pdf.drawString(500, 725, 'Case Number:')
pdf.drawString(580, 725, "10")
pdf.line(500, 723, 595, 723)
line1 = 'This forensic report on sim card data has been compiled by the forensic'
line2 = 'examiner in conclusion to the investigation into the RTA'
line3 = 'case which occurred on the 23/01/2018.'
textObject = pdf.beginText(30, 700)
lines = [line1, line2, line3]
for line in lines:
textObject.textLine(line)
pdf.drawText(textObject)
return pdf
def main():
data_file = 'H:\College Fourth Year\Development Project\Final Year Project 2018\ExtractedEvidence\simCardInfo.csv'
pdf = front_page()
import_data(pdf, data_file)
pdf.save()
print(" Sim Card Forensic Report Generated!")
if __name__ == '__main__':
main()

Why python asyncio coroutine is pausing at the line socket.recv(1024) ? why the while loops are failed to cross yeild statements?

I have a small application where, one coroutine will send data and the other coroutine will receive data and logs whether it received the exact transmitted data or not.
Both coroutines are in while loops. Some how, the trans() coroutine and recv() coroutine are not proceeding ahead when they hit the line yield from XXXXXXX
data, server = yield from recv_sock.recvfrom(1024)
Here is the code
import asyncio
import socket
import time
import datetime
import logging
trans_addr = ('localhost', 5555)
recv_addr = ('localhost',6666)
#asyncio.coroutine
def trans():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(trans_addr)
i = 0
global sen_data
while True:
print("hi")
sen_data = "HELLO " + str(i)
sent = yield from sock.sendto(sen_data.encode(), recv_addr)
print(sent)
print("hi1")
yield from time.sleep(2)
i += 1
print("hi1")
#asyncio.coroutine
def recv():
recv_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
recv_sock.bind(recv_addr) # binding the receiving end to 1.241 and port 6666
#recv_sock.setblocking(0)
while True:
try:
print("hello")
data, server = yield from recv_sock.recvfrom(1024)
print("hello2")
if (data):
recv_data = data.decode()
if (sen_data == recv_data):
logging.info("transmitted data :" + sen_data + " is Received as :" + recv_data + " at :" + str(
datetime.datetime.now()) + '\n')
print("transmitted data :" + sen_data + " is Received as :" + recv_data + " at :" + str(
datetime.datetime.now()) + " from :" + str(server) + '\n')
else:
logging.critical("Data missed : ")
logging.critical("Transmitted data " + sen_data + " is != " + "received data : " + recv_data + '\n')
print("data is missing--->")
print("Transmitted data " + sen_data + " is != " + "received data : " + recv_data + '\n')
except:
pass
# print("not receiving data due to some fault in the receiving socket")
# time.sleep(1)
loop=asyncio.get_event_loop()
tasks = [loop.create_task(trans()), loop.create_task(recv())]
wait_tasks = asyncio.wait(tasks)
loop.run_forever()
loop.run_until_complete(wait_tasks)
Output is:
hello
hi
Can anyone let me know,why the coroutines are failed to cross the yield from commands? i am using python 3.3.2
yield from or await should be used with coroutines. recvfrom is not a coroutine. For example, you can use loop.sock_recv() instead:
reader, writer = socket.socketpair()
writer.setblocking(False)
reader.setblocking(False)
await loop.sock_recv(...)
await loop.sock_sendall(...)

Resources