Skip to content

Commit bc0424a

Browse files
authored
document settings (#722)
* document settings * remove deprecated * fix typo in * improve docs * add mention to SettingValidationError * improve some other docs * fix flake8 issue Co-authored-by: marc <Marc>
1 parent 2908d21 commit bc0424a

File tree

9 files changed

+261
-240
lines changed

9 files changed

+261
-240
lines changed

dateparser/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def parse(date_string, date_formats=None, languages=None, locales=None, region=N
2727
2828
:param locales:
2929
A list of locale codes, e.g. ['fr-PF', 'qu-EC', 'af-NA'].
30-
The parser uses locales to translate date string.
30+
The parser uses only these locales to translate date string.
3131
:type locales: list
3232
3333
:param region:
@@ -41,7 +41,9 @@ def parse(date_string, date_formats=None, languages=None, locales=None, region=N
4141
4242
:return: Returns :class:`datetime <datetime.datetime>` representing parsed date if successful, else returns None
4343
:rtype: :class:`datetime <datetime.datetime>`.
44-
:raises: ValueError - Unknown Language
44+
:raises:
45+
``ValueError``: Unknown Language, ``TypeError``: Languages argument must be a list,
46+
``SettingValidationError``: A provided setting is not valid.
4547
"""
4648
parser = _default_parser
4749

dateparser/conf.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ class Settings:
1111
"""Control and configure default parsing behavior of dateparser.
1212
Currently, supported settings are:
1313
14-
* `PREFER_DATES_FROM`: defaults to `current_period`. Options are `future` or `past`.
15-
* `PREFER_DAY_OF_MONTH`: defaults to `current`. Could be `first` and `last` day of month.
16-
* `SKIP_TOKENS`: defaults to `['t']`. Can be any string.
17-
* `TIMEZONE`: defaults to `UTC`. Can be timezone abbreviation or any of
18-
`tz database name as given here <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>`_.
19-
* `RETURN_AS_TIMEZONE_AWARE`: return tz aware datetime objects in case timezone is detected in the date string.
20-
* `RELATIVE_BASE`: count relative date from this base date. Should be datetime object.
21-
* `RETURN_TIME_AS_PERIOD`: returns period as `time` in case time component is detected in the date string.
22-
Default: False.
23-
* `PARSERS`: list of date parsers to use, in order of preference. Default:
24-
:attr:`dateparser.settings.default_parsers`.
14+
* `DATE_ORDER`
15+
* `PREFER_LOCALE_DATE_ORDER`
16+
* `TIMEZONE`
17+
* `TO_TIMEZONE`
18+
* `RETURN_AS_TIMEZONE_AWARE`
19+
* `PREFER_DAY_OF_MONTH`
20+
* `PREFER_DATES_FROM`
21+
* `RELATIVE_BASE`
22+
* `STRICT_PARSING`
23+
* `REQUIRE_PARTS`
24+
* `SKIP_TOKENS`
25+
* `NORMALIZE`
26+
* `RETURN_TIME_AS_PERIOD`
27+
* `PARSERS`
2528
"""
2629

2730
_default = True

dateparser/date.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class DateDataParser:
268268
269269
:param locales:
270270
A list of locale codes, e.g. ['fr-PF', 'qu-EC', 'af-NA'].
271-
The parser uses locales to translate date string.
271+
The parser uses only these locales to translate date string.
272272
:type locales: list
273273
274274
:param region:
@@ -293,7 +293,8 @@ class DateDataParser:
293293
:return: A parser instance
294294
295295
:raises:
296-
ValueError - Unknown Language, TypeError - Languages argument must be a list
296+
``ValueError``: Unknown Language, ``TypeError``: Languages argument must be a list,
297+
``SettingValidationError``: A provided setting is not valid.
297298
"""
298299

299300
locale_loader = None

dateparser_data/settings.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
]
77

88
settings = {
9-
'PREFER_DATES_FROM': 'current_period',
10-
'PREFER_DAY_OF_MONTH': 'current',
11-
'SKIP_TOKENS': ["t"],
9+
# Date order
10+
'DATE_ORDER': 'MDY',
11+
'PREFER_LOCALE_DATE_ORDER': True,
12+
13+
# Timezone related
1214
'TIMEZONE': 'local',
1315
'TO_TIMEZONE': False,
1416
'RETURN_AS_TIMEZONE_AWARE': 'default',
15-
'NORMALIZE': True,
17+
18+
# Incomplete dates
19+
'PREFER_DAY_OF_MONTH': 'current',
20+
'PREFER_DATES_FROM': 'current_period',
1621
'RELATIVE_BASE': False,
17-
'DATE_ORDER': 'MDY',
18-
'PREFER_LOCALE_DATE_ORDER': True,
1922
'STRICT_PARSING': False,
23+
'REQUIRE_PARTS': [],
24+
25+
# Language detection
26+
'SKIP_TOKENS': ['t'],
27+
'NORMALIZE': True,
28+
29+
# Other settings
2030
'RETURN_TIME_AS_PERIOD': False,
2131
'PARSERS': default_parsers,
22-
'REQUIRE_PARTS': [],
2332
}

docs/conf.rst

-5
This file was deleted.

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Contents:
4848
introduction
4949
installation
5050
usage
51+
settings
5152
supported_locales
5253
contributing
5354
authors

docs/introduction.rst

+29-22
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ Relative Dates
7474

7575
.. note:: Testing above code might return different values for you depending on your environment's current date and time.
7676

77-
.. note:: Support for relative dates in future needs a lot of improvement, we look forward to community's contribution to get better on that part. See ":ref:`contributing`".
78-
77+
.. note:: For `Finnish` language, please specify ``settings={'SKIP_TOKENS': []}`` to correctly parse relative dates.
7978

8079
OOTB Language Based Date Order Preference
8180
-----------------------------------------
@@ -86,13 +85,12 @@ OOTB Language Based Date Order Preference
8685
>>> parse('le 02-03-2016') # detects french, uses DMY date order
8786
datetime.datetime(2016, 3, 2, 0, 0)
8887

89-
.. note:: Ordering is not locale based, that's why do not expect `DMY` order for UK/Australia English. You can specify date order in that case as follows using `settings`:
88+
.. note:: Ordering is not locale based, that's why do not expect `DMY` order for UK/Australia English. You can specify date order in that case as follows using :ref:`settings`:
9089

9190
>>> parse('18-12-15 06:00', settings={'DATE_ORDER': 'DMY'})
9291
datetime.datetime(2015, 12, 18, 6, 0)
9392

94-
For more on date order, please look at Settings.
95-
93+
For more on date order, please look at :ref:`settings`.
9694

9795

9896
Timezone and UTC Offset
@@ -120,8 +118,8 @@ By default, `dateparser` returns tzaware `datetime` if timezone is present in da
120118
>>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': '+0500'})
121119
datetime.datetime(2012, 1, 12, 22, 0)
122120

123-
`TIMEZONE` option may not be useful alone as it only attaches given timezone to
124-
resultant `datetime` object. But can be useful in cases where you want conversions from and to different
121+
``TIMEZONE`` option may not be useful alone as it only attaches given timezone to
122+
resultant ``datetime`` object. But can be useful in cases where you want conversions from and to different
125123
timezones or when simply want a tzaware date with given timezone info attached.
126124

127125
>>> parse('January 12, 2012 10:00 PM', settings={'TIMEZONE': 'US/Eastern', 'RETURN_AS_TIMEZONE_AWARE': True})
@@ -139,8 +137,8 @@ Some more use cases for conversion of timezones.
139137
>>> parse('now EST', settings={'TO_TIMEZONE': 'UTC'}) # relative dates
140138
datetime.datetime(2017, 3, 10, 23, 24, 47, 371823, tzinfo=<StaticTzInfo 'UTC'>)
141139

142-
In case, no timezone is present in date string or defined in `settings`. You can still
143-
return tzaware `datetime`. It is especially useful in case of relative dates when uncertain
140+
In case, no timezone is present in date string or defined in :ref:`settings`. You can still
141+
return tzaware ``datetime``. It is especially useful in case of relative dates when uncertain
144142
what timezone is relative base.
145143

146144
>>> parse('2 minutes ago', settings={'RETURN_AS_TIMEZONE_AWARE': True})
@@ -151,15 +149,15 @@ In case, you want to compute relative dates in UTC instead of default system's l
151149
>>> parse('4 minutes ago', settings={'TIMEZONE': 'UTC'})
152150
datetime.datetime(2017, 3, 10, 23, 27, 59, 647248, tzinfo=<StaticTzInfo 'UTC'>)
153151

154-
.. note:: In case, when timezone is present both in string and also specified using `settings`, string is parsed into tzaware representation and then converted to timezone specified in `settings`.
152+
.. note:: In case, when timezone is present both in string and also specified using :ref:`settings`, string is parsed into tzaware representation and then converted to timezone specified in :ref:`settings`.
155153

156154
>>> parse('10:40 pm PKT', settings={'TIMEZONE': 'UTC'})
157155
datetime.datetime(2017, 3, 12, 17, 40, tzinfo=<StaticTzInfo 'UTC'>)
158156

159157
>>> parse('20 mins ago EST', settings={'TIMEZONE': 'UTC'})
160158
datetime.datetime(2017, 3, 12, 21, 16, 0, 885091, tzinfo=<StaticTzInfo 'UTC'>)
161159

162-
For more on timezones, please look at Settings.
160+
For more on timezones, please look at :ref:`settings`.
163161

164162

165163
Incomplete Dates
@@ -186,17 +184,27 @@ You can also ignore parsing incomplete dates altogether by setting `STRICT_PARSI
186184
>>> parse('December 2015', settings={'STRICT_PARSING': True})
187185
None
188186

189-
For more on handling incomplete dates, please look at Settings.
187+
For more on handling incomplete dates, please look at :ref:`settings`.
190188

191189

192190
Search for Dates in Longer Chunks of Text
193191
-----------------------------------------
194192

193+
.. warning:: Support for searching dates is really limited and needs a lot of improvement, we look forward to community's contribution to get better on that part. See ":ref:`contributing`".
194+
195+
195196
You can extract dates from longer strings of text. They are returned as list of tuples with text chunk containing the date and parsed datetime object.
196197

198+
197199
.. automodule:: dateparser.search
198200
:members: search_dates
199201

202+
203+
Advanced Usage
204+
==============
205+
If you need more control over what is being parser check the :ref:`settings` section as well as the :ref:`using-datedataparser` section.
206+
207+
200208
Dependencies
201209
============
202210

@@ -221,26 +229,25 @@ You can check the supported locales by visiting the ":ref:`supported-locales`" s
221229

222230
Supported Calendars
223231
===================
224-
* Gregorian calendar.
225232

226-
* Persian Jalali calendar. For more information, refer to `Persian Jalali Calendar <https://en.wikipedia.org/wiki/Iranian_calendars#Zoroastrian_calendar>`_.
233+
Apart from the Georgian calendar, `dateparser` supports the `Persian Jalali calendar` and the `Hijri/Islami calendar`
234+
235+
To be able to use them you need to install the `calendar` extra by typing:
236+
237+
pip install dateparser[calendars]
238+
239+
240+
* Example using the `Persian Jalali calendar`. For more information, refer to `Persian Jalali Calendar <https://en.wikipedia.org/wiki/Iranian_calendars#Zoroastrian_calendar>`_.
227241

228242
>>> from dateparser.calendars.jalali import JalaliCalendar
229243
>>> JalaliCalendar('جمعه سی ام اسفند ۱۳۸۷').get_date()
230244
{'date_obj': datetime.datetime(2009, 3, 20, 0, 0), 'period': 'day'}
231245

232246

233-
* Hijri/Islamic Calendar. For more information, refer to `Hijri Calendar <https://en.wikipedia.org/wiki/Islamic_calendar>`_.
247+
* Example using the `Hijri/Islamic Calendar`. For more information, refer to `Hijri Calendar <https://en.wikipedia.org/wiki/Islamic_calendar>`_.
234248

235249
>>> from dateparser.calendars.hijri import HijriCalendar
236250
>>> HijriCalendar('17-01-1437 هـ 08:30 مساءً').get_date()
237251
{'date_obj': datetime.datetime(2015, 10, 30, 20, 30), 'period': 'day'}
238252

239253
.. note:: `HijriCalendar` only works with Python ≥ 3.6.
240-
.. note:: For `Finnish` language, please specify `settings={'SKIP_TOKENS': []}` to correctly parse freshness dates.
241-
242-
243-
Install using following command to use calendars.
244-
245-
.. tip::
246-
pip install dateparser[calendars]

0 commit comments

Comments
 (0)