A symbol in a #comment

Hi everybody!!
I don’t know if such post are for this forum, but I will write it anyway.
I created a hook that calculates the time difference from when a Revit document was opened until when it was closed. Everything went fine until I started to writing #comments for description.
One comment had the “Δ” symbol and I don’t know why but the script stopped working.
This code works:

import os
import math
from pyrevit import script,revit
from datetime import datetime

doc = revit.doc
time = datetime.now()
dt_f2 = time.strftime("%H-%M-%S")

# getting the project info

project_name = doc.Title

# getting the time for the doc-opened file
try:
    dt_f1 = script.load_data("Project opened", this_project=True)
 
except:
    script.exit()

before = dt_f1.split("-")
after = dt_f2.split("-")

# function converting time to seconds
def get_seconds(lst):
    h = int(lst[0]) * 60 * 60
    m = int(lst[1]) * 60
    s = int(lst[2])
    return h + m + s

# function time to string
def time_to_string(s):
    unpadded = str(s).split(".")[0]
    return unpadded.rjust(2,"0")
   
# formating the time    
project_start = dt_f1
project_closed = dt_f2

# calculating the time difference
try:
    elapsed = get_seconds(after) - get_seconds(before)
    mins = math.floor(elapsed/60)
    secs = elapsed % 60
    msg = time_to_string(mins) + "mins" + time_to_string(secs) + "sec"    
except:
    script.exit()

data = [[project_name],[project_start],[project_closed],[msg]] 

# creating the file name
init_path = r'C:\Users\vasil\Desktop'
result = "resultat_" + project_name + "_" + dt_f2 + ".csv" 
csv_path = os.path.join(init_path,result)
 
# export the CSV
script.dump_csv(data,csv_path)de here

This doesn’t

import os
import math
from pyrevit import script,revit
from datetime import datetime

doc = revit.doc
time = datetime.now()
dt_f2 = time.strftime("%H-%M-%S")

# getting the project info

project_name = doc.Title

# getting the time for the doc-opened file
try:
    dt_f1 = script.load_data("Project opened", this_project=True)
 
except:
    script.exit()

before = dt_f1.split("-")
after = dt_f2.split("-")

# function converting time to seconds
def get_seconds(lst):
    h = int(lst[0]) * 60 * 60
    m = int(lst[1]) * 60
    s = int(lst[2])
    return h + m + s

# function time to string
def time_to_string(s):
    unpadded = str(s).split(".")[0]
    return unpadded.rjust(2,"0")
   
# formating the time    
project_start = dt_f1
project_closed = dt_f2

# calculating the Δ time
try:
    elapsed = get_seconds(after) - get_seconds(before)
    mins = math.floor(elapsed/60)
    secs = elapsed % 60
    msg = time_to_string(mins) + "mins" + time_to_string(secs) + "sec"    
except:
    script.exit()

data = [[project_name],[project_start],[project_closed],[msg]] 

# creating the file name
init_path = r'C:\Users\vasil\Desktop'
result = "resultat_" + project_name + "_" + dt_f2 + ".csv" 
csv_path = os.path.join(init_path,result)
 
# export the CSV
script.dump_csv(data,csv_path)

Have you ever encountered such things? Does " Δ" usually interfere with the script? Are there other such kind of symbols that prevent the script to run properly?

Hi :wave: @Vasile_Corjan

  • What kind of hook? Hooked to which event?
  • I noticed you are not using the hook / events arguments
    see: hook script anatomy
  • I ran a hook with just my code + your # calculating the Δ time comment and it works fine.
  • once in a comment prefixed line ‘#’, you can type pretty much anything

Hey @Jean-Marc
I used the extension hooks, from here. The first script I named doc-opened.py and the second script doc-closing.py .
I know that in #comment you can write almost anything and finding out that “Δ” can mess up with the script was a bit weird.

No, not really.
I tried it.
Nothing to do with the Delta sign


The code you are using code is quite messy.

  • use utf-8 encoding, that helps with special characters and foreign languages (French, russian, …)
# -*- coding: utf-8 -*-

# then your code below
  • you should not write try except statement without at least using this in the except statement:
except Exception as e:
    print(e)
    pass

because you won’t see the error, and you also escape from the script everytime there is an error.

  • load_data is not designed to create a file

  • You should print the result of your variables to see what is happening

  • you conversion of time is overcomplicated.

  • in the pyrevit coreutils module you may find ready made tools to make your code work nicely. Time in pyRevit module documentation

If you check the ‘modelchecker_check.py’ file will give you a hint on where to head modelchecker_chek.py

The following is to illustrate the principles, it will hopefully get you going:

# -*- coding: utf-8 -*-
# paste this in a doc-opened.py hook to try it out
"""
This measures the time taken to open a document in pyRevit.
"""
from datetime import datetime
from time import sleep
from pyrevit.script import store_data, load_data

start_time = datetime.now()
# Store the current time anyway you want
store_data('doc_open_time', start_time)

# Wait for 1 second to simulate opening and closing the document
sleep(1)

### the code below would happen in the doc-closed hook file ###

# Retrieve the stored start time anyway you want
stored_start_time = load_data('doc_open_time')

# Calculate the time difference
closed_time = datetime.now()
time_diff = closed_time - stored_start_time

# Print the time difference in seconds
print("Doc opened for {} seconds".format(time_diff.total_seconds()))
1 Like

Hey @Jean-Marc
The solution was # -*- coding: utf-8 -*- on the first line. Now it works with any special symbol.
I didn’t know it was necessary this comment line in order to add special characters.
PEP 263 helped to understand it as well.
Thank you for your help and time!