Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Spans is a pure Python implementation of PostgreSQL's range types.

License

NotificationsYou must be signed in to change notification settings

runfalk/spans

Repository files navigation

PyPI version statusPython versionMIT License

Spans is a pure Python implementation of PostgreSQL'srange types.Range types are convenient when working with intervals of any kind. Every timeyou've found yourself working with date_start and date_end, an interval may havebeen what you were actually looking for.

Spans has successfully been used in production since its first release30th August, 2013.

Installation

Spans exists on PyPI.

$ pip install Spans

Documentation is hosted on Read theDocs.

Example

Imagine you are building a calendar and want to display all weeks that overlapsthe current month. Normally you have to do some date trickery to achieve this,since the month's bounds may be any day of the week. With Spans' set-likeoperations and shortcuts the problem becomes a breeze.

We start by importingdate anddaterange

>>>fromdatetimeimportdate>>>fromspansimportdaterange

Usingdaterange.from_month we can get range representing January in the year2000

>>>month=daterange.from_month(2000,1)>>>monthdaterange(datetime.date(2000,1,1),datetime.date(2000,2,1))

Now we can calculate the ranges for the weeks where the first and last day ofmonth are

>>>start_week=daterange.from_date(month.lower,period="week")>>>end_week=daterange.from_date(month.last,period="week")>>>start_weekdaterange(datetime.date(1999,12,27),datetime.date(2000,1,3))>>>end_weekdaterange(datetime.date(2000,1,31),datetime.date(2000,2,7))

Using a union we can express the calendar view.

>>>start_week.union(month).union(end_week)daterange(datetime.date(1999,12,27),datetime.date(2000,2,7))

Do you want to know more? Head over to thedocumentation.

Use with Psycopg2

To use these range types with Psycopg2 thePsycoSpans.

Motivation

For a project of mine I started using PostgreSQL'stsrange type and neededan equivalent in Python. These range types attempt to mimick PostgreSQL'sbehavior in every way. Deviating from it is considered as a bug and should bereported.

Contribute

I appreciate all the help I can get! Some things to think about:

  • If it's a simple fix, such as documentation or trivial bug fix, please filean issue or submit a pull request. Make sure to only touch lines relevant tothe issue. I don't accept pull requests that simply reformat the code to bePEP8-compliant. To me the history of the repository is more important.
  • If it's a feature request or a non-trivial bug, always open an issue first todiscuss the matter. It would be a shame if good work went to waste because apull request doesn't fit the scope of this project.

Pull requests are credited in the change log which is displayed on PyPI and thedocumentaion on Read the Docs.

About

Spans is a pure Python implementation of PostgreSQL's range types.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp