Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jupyter to improve time Management

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use Jupyter to improve time management". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python is incredibly extensible in exploring data. With Pandas or Dask, you can extend Jupyter to big data's domain. But what about small data, personal data, private data?

JupyterLab and Jupyter Notebook provide me with a great environment to look at my laptop life.

My exploration is based on the fact that almost every service I use has a Web API. I use a lot of these services: to-do lists, time trackers, habit trackers, and so on. There's another one that almost everyone uses: a calendar. The same idea can be applied to other services, but calendars have a cool feature: CalDAV, an open standard supported by almost all Web calendars.

Parsing Calendar with Python in Jupyter

Most calendars provide a way to export to CalDAV format. You may need some kind of authentication to access this private data. Just follow your service instructions. How to obtain credentials depends on your service, but in the end, you should be able to store these credentials in a file. I store my credentials in a file called .caldav in the root directory:

Import oswith open (os.path.expanduser ("~ / .caldav") as fpin: username, password = fpin.read () .split ()

Do not put your user name and password directly in Jupyter Notebook's notebook! They can easily be leaked due to git push errors.

The next step is to use the convenient PyPI caldav library. I found the CalDAV server for my email service (you may be different):

Import caldavclient = caldav.DAVClient (url= "https://caldav.fastmail.com/dav/", username=username, password=password)

CalDAV has a concept called principal (primary key). It doesn't matter what it is, just know that it's something you use to access the calendar:

Principal = client.principal () calendars = principal.calendars ()

Literally, a calendar is about time. Before you visit the event, you need to determine a time frame. The default is one week:

From dateutil import tzimport datetimenow = datetime.datetime.now (tz.tzutc ()) since = now-datetime.timedelta (days=7)

Most people use more than one calendar and want all events to appear together. The itertools.chain.from_iterable method makes this process simple:

Import itertools raw_events = list (itertools.chain.from_iterable (calendar.date_search (start=since, end=now, expand=True) for calendar in calendars))

It is important to read all events into memory, and it is an important practice to operate in API's native native format. This means that there is no need to return to the API service to refresh the data when adjusting the parsing, parsing, and display code.

But "primitive" is really primitive, and events appear as strings in a specific format:

Print (raw_events [12] .data) BEGIN:VCALENDAR VERSION:2.0 PRODID:-//CyrusIMAP.org/Cyrus 3.3.0-232-g4bdb081-fm-20200825.002-g4bdb081a//EN BEGIN:VEVENT DTEND:20200825T230000Z DTSTAMP:20200825T181915Z DTSTART:20200825T220000Z SUMMARY:Busy UID: 1302728i-040000008200E00074C5B7101A82E00800000000D939773EA578D601000000000 000000010000000CD71CC3393651B419E9458134FE840F5 END:VEVENT END:VCALENDAR

Fortunately, PyPI can once again use another helper library, vobject, to rescue:

Import ioimport vobject def parse_event (raw_event): data = raw_event.data parsed = vobject.readOne (io.StringIO (data)) contents = parsed.vevent.contents return contentsparse_event (raw_events [12]) {'dtend': [],' dtstamp': [], 'dtstart': [],' summary': [], 'uid': []}

Well, at least a little better.

There is still some work to be done to convert it into a reasonable Python object. The first step is to have a reasonable Python object. The attrs library provides a good start:

Import attrfrom _ future__ import annotations@attr.s (auto_attribs=True, frozen=True) class Event: start: datetime.datetime end: datetime.datetime timezone: Any summary: str

It's time to write conversion code!

The first abstraction takes the value from the parsed dictionary and does not require all the decoration:

Def get_piece (contents, name): return contents [name] [0] .valueget _ piece (_, "dtstart") datetime.datetime (2020, 8, 25, 22, 0, tzinfo=tzutc ())

Calendar events always have a "start", an "end", and a "duration". Some careful parsing logic can reconcile the two into the same Python object:

Def from_calendar_event_and_timezone (event, timezone): contents = parse_event (event) start= get_piece (contents, "dtstart") summary= get_piece (contents, "summary") try: end= get_piece (contents, "dtend") except KeyError: end= start + get_piece (contents, "duration") return Event (start=start, end=end, summary=summary, timezone=timezone)

It is useful to place events in the local time zone instead of UTC, so use the local time zone:

My_timezone = tz.gettz () from_calendar_event_and_timezone (raw_events [12], my_timezone) Event (start=datetime.datetime (2020, 8, 25, 22, 0, tzinfo=tzutc ()), end=datetime.datetime (2020, 8, 25, 23, 0, tzinfo=tzutc ()), timezone=tzfile ('/ etc/localtime'), summary='Busy')

Since events are real Python objects, they should actually have additional information. Fortunately, methods can be added to the class.

But it is not straightforward to find out which event occurred on which day. You need to choose a day in your local time zone:

Def day (self): offset = self.timezone.utcoffset (self.start) fixed = self.start + offset return fixed.date () Event.day = property (day) print (_ .day) 2020-08-25

Events are always represented internally as "start" / "end", but duration is a useful attribute. Duration can also be added to an existing class:

Def duration (self): return self.end-self.startEvent.duration = property (duration) print (_ .mom) 1:00:00

Now it's time to convert all events into useful Python objects:

All_events = [from_calendar_event_and_timezone (raw_event, my_timezone) for raw_event in raw_events]

The all-day event is a special case and may not be of much use in analyzing life. Now, you can ignore them:

# ignore all-day eventsall_events = [event for event in all_events if not type (event.start) = = datetime.date]

Events have a natural sequence-knowing which event occurs first may help with analysis:

All_events.sort (key=lambda ev: ev.start)

Now that the events are sorted, they can be loaded into each day:

Import collectionsevents_by_day = collections.defaultdict (list) for event in all_events: events_by_ day [event.day] .append (event)

With these, you have calendar events with dates, durations, and sequences as Python objects.

Report to your life with Python

Now it's time to write report code! It makes sense to have an eye-catching format with appropriate titles, lists, important content in bold, and so on.

These are some HTML and HTML templates. I like to use Chameleon:

Template_content = "Day Thing"

One of the cool features of Chameleon is to render objects using its html method. I will use it in two ways:

The summary will be displayed in bold

For most activities, I delete the summary (because this is my personal information)

Def _ _ html__ (self): offset = my_timezone.utcoffset (self.start) fixed = self.start + offset start_str = str (fixed) .split ("+") [0] summary = self.summary if summary! = "Busy": summary = "return f" {summary [: 30]}-{start_str} ({self.duration}) "Event.__html__ = _ html__"

For brevity, cut the report into daily:

Import chameleonfrom IPython.display import HTMLtemplate = chameleon.PageTemplate (template_content) html = template (items=itertools.islice (events_by_day.items (), 3,4)) HTML (html)

After rendering, it will look like this:

2020-08-25

-- 2020-08-25 08:30:00 (0:45:00)

-- 2020-08-25 10:00:00 (1:00:00)

-- 2020-08-25 11:30:00 (0:30:00)

-- 2020-08-25 13:00:00 (0:25:00)

Busy-2020-08-25 15:00:00 (1:00:00)

-- 2020-08-25 15:00:00 (1:00:00)

-- 2020-08-25 19:00:00 (1:00:00)

-- 2020-08-25 19:00:12 (1:00:00)

This is the end of "how to use Jupyter to improve time Management". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report