rhelmstedter / Zettelkasten

My personal zettelkasten.
36 stars 6 forks source link

"plot_note_writing.py", line 17, in <module> AttributeError: module 'plotext' has no attribute 'string_to_time' #1

Closed sebastianbock17 closed 2 years ago

sebastianbock17 commented 2 years ago

Dear mister Helmstedter,

First of all, thank you very much for this repository. The concrete issue: running the script "plot_note_writing.py", has an issue line 17, in AttributeError: module 'plotext' has no attribute 'string_to_time'.

So it means, that I somehow need to add an attribute named 'string_to_time' to the script?

Or how could I solve this issue?

Here is how I run the script (archlinux):

# Adding the required python module 'plotext':
$ pip3 install --user plotext

# Running the Script:

$ python3 plot_note_writing.py
#Traceback (most recent call last):
#  File "~/Zettelkasten/plot_note_writing.py", line 17, in <module>
#  x_axis.append(plt.string_to_time(date)) 
# AttributeError: module 'plotext' has no attribute 'string_to_time'

Some thankful feedback: This idea for commandline plotting a Kasten is astonishingly great, and you solved exactly the problem when for the function of using #tags that I had for weeks without the proper time of diving into vimscript or other code while having to study a lot. And thank you very much for your worked out example!!

It would be very nice if you could explain what could be the problem here, as a total python newby.

Many kind regards from Germany :)

RainFlyWave commented 2 years ago

There is no method such as 'string_to_time' in that module. List of all of the methods is below: builtins cached doc file loader name package path spec version _core _datetime _default _figure _matrices _subplot _utility axes_color bar build canvas_color clc cld clear_color clear_data clear_figure clear_plot clear_terminal clf clp cls clt colorize colorless colors datetime doc figure file frame grid hist image_plot limit_size limitsize linspace markers matrix_plot multiple_bar platform plot plot_date plot_size plotsize save_fig savefig scatter scatter_date show sin sleep span stacked_bar subplot subplots terminal_size ticks_color time title uncolorize version xaxis xfrequency xlabel xlim xscale xticks yaxis yfrequency ylabel ylim yscale yticks

sebastianbock17 commented 2 years ago

Thank you Mr. Kustra,

Around the mentioned line 17 using the method string_to_time is the following:

x_axis = []
y_axis = []
for date, count in count_by_month.items():
    x_axis.append(plt.string_to_time(date))
    y_axis.append(count)

The function 'plt.string_to_time(date)' in the script suggests its coming from the plotext package. While not included in the list for modules, reviewing the datetime module from plotext it has this function:

plt.datetime.string_to_datetime() # turns a string based datetime object into a datetime object.“

I tried to rename it in line 17:

x_axis.append(plt.datetime.string_to_datetime(date))

…but then came another error:

  File "~/Zettelkasten/plot_note_writing.py", line 17, in <module>
    x_axis.append(plt.datetime.string_to_datetime(date))

  File "~/.local/lib/python3.9/site-packages/plotext/_datetime.py", line 66, in string_to_datetime
    return _dt.strptime(string, self._datetime_form)

  File "/usr/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)

  File "/usr/lib/python3.9/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '01/12/2020' does not match format '%d/%m/%Y %H:%M:%S'
RainFlyWave commented 2 years ago

Okay now you're using wrong notation to the mentioned object. Method string_to_datetime takes argument in this '%d/%m/%Y %H:%M:%S' notation. But you only provide date without time. Maybe consider using something else that doesnt need actual time.

rhelmstedter commented 2 years ago

Hi @yetipowers I am glad you have found it useful. I am more than happy to help, but I am far from an expert. Seems like @RainFlyWave is on the right track. I just pulled up the docs for plotext. They are currently on version 4.1.0, but my local verison is 3.1.3. Seems like string_to_time no longer works. I will try to upgrade and fix it today.

In the meantime it might be useful to know how are your notes saved. All of my notes are markdown files named as YYYYmmddHHMM.md. So I wrote a regex that finds that pattern without the file extension. Then I convert those to datetime objects. Finally, for the graph I don't care about the day, only the month. So the count_by_month variable only stores notes as if they were all written on the first day of the month and the count. Hopefully that is helpful until I can update the code.

rhelmstedter commented 2 years ago

Ok, I updated the script. If your notes are saved in the same format as mine are and saved in the same relative location, it should plot them. But it looks like plotext has been completely rewritten and deals with datetime objects differently. I will have rewrite my script to match the new API.

sebastianbock17 commented 2 years ago

Dear mister Helmstedter,

Thank you a lot for the fast response! Seems like the issue was justified as plotex got upgraded! After the fix the tool finally works! I also decided for the same format yymmddmm beforehand, so I am very glad to now be able to use this visualization script - all in the commandline which is really great. I would also be interested in being able to plot the rate per day and per month, as I am expecting a big growing of my notes.

My next question will be to use the mermaid graph function, and printing the titlte in the YAML headers as the filenames in the visualization script next to the ids, if I find the solution I can post it :)

Wishing a nice evening and kind regards from Germany :)

rhelmstedter commented 2 years ago

Glad it worked out! Feel free to ask for anything in the future!