Passing parameters to a python function via pycall that need to be in a dict form

I’m trying to use the Date Parser package from Python in a Pluto notebook. The example from the package documentation I am trying to replicate is this:
parse(‘December 2015’, settings={‘STRICT_PARSING’: True})

I can get the basic command to work by changing single quotes to double quotes for it to recognise the date string. I don’t know how to change the kwarg command settings={‘STRICT_PARSING’: True}, which needs to be passed to the parse() command as a dict data type.

How do I convert this into something Python will understand?

My Pluto Notebook is below:

Conda.add("dateparser")
dparser = pyimport("dateparser")
#Command in Python: parse('December 2015', settings={'STRICT_PARSING': True})
output = dparser.parse("December 2015")
output
2015-12-23T00:00:00

None of the following attempts worked:

dparser.parse("December 2015", settings={"STRICT_PARSING": True})
dparser.parse("December 2015", settings=PyDict{"STRICT_PARSING": True})
dparser.parse("December 2015", settings=PyDict{"STRICT_PARSING": "True"})
py"""

"""
begin
	py"""
kwargs = {'STRICT_PARSING': True}
"""
	r = dparser.parse('December 2015', settings=py"kwargs"))
end

I am trying to process a long list of dates where sometimes the day, month and year is given, but other times just the year and the month and the year is given. I don’t think there is a similar option in Dates.jl to not parse incomplete dates - which is why I’ve turned to Python. In Python I will also be able to access the span command in the arrow package, which will help me to find the range of possible dates for when the day or month is not stated. Again, I haven’t found anything similar in Julia to do this.

The Date Parser function definition is:

Basic Usage

The most straightforward way is to use the dateparser.parse function, that wraps around most of the functionality in the module.

dateparser. parse (date_string, date_formats=None, languages=None, locales=None, region=None, settings=None, detect_languages_function=None )[source]

Parse date and time from given date string.

Parameters: * date_string (str) – A string representing date and/or time in a recognizably valid format.

  • date_formats (list) – A list of format strings using directives as given here. The parser applies formats one by one, taking into account the detected languages/locales.
  • languages (list) – A list of language codes, e.g. [‘en’, ‘es’, ‘zh-Hant’]. If locales are not given, languages and region are used to construct locales for translation.
  • locales (list) – A list of locale codes, e.g. [‘fr-PF’, ‘qu-EC’, ‘af-NA’]. The parser uses only these locales to translate date string.
  • region (str) – A region code, e.g. ‘IN’, ‘001’, ‘NE’. If locales are not given, languages and region are used to construct locales for translation.
  • settings (dict) – Configure customized behavior using settings defined in dateparser.conf.Settings.
  • detect_languages_function (function) – A function for language detection that takes as input a string (the date_string) and a confidence_threshold, and returns a list of detected language codes. Note: this function is only used if languages and locales are not provided.
    Returns: Returns datetime representing parsed date if successful, else returns None
    Return type: datetime.
    Raises: ValueError: Unknown Language, TypeError: Languages argument must be a list, SettingValidationError: A provided setting is not valid.

Thanks

I don’t think the examples you tried involving PyDict{<...>} are valid Julia syntax. I would just pass a Dict instead:

output = dparser.parse("December 2015"; settings=Dict("STRICT_PARSING" => true))

Great, this worked! Sorry I’m very much a beginner and have not used a Dict() structure before.

No worries! By the way, I had no idea that Dates.jl was so permissive, that’s certainly good to know. It looks like this is on the horizon to be addressed in 2.0

On the python end of things, you may also want to check out PythonCall.jl/CondaPkg.jl. It’s a newer alternative to PyCall.jl/Conda.jl that is pretty nice to work with, even in Pluto notebooks

One of its main selling points for me is the use of project specific environments to avoid polluting your global

1 Like