|
From: Waylan L. <wa...@gm...> - 2013-06-23 13:34:49
|
Actually, the way the Markdown class calls extensions, None will never
be passed as a value for configs. Markdown always sets a default value
to pass in, so technically, your extension doesn't need to worry about
setting a default value.
Otherwise you would be correct. `foo = dict(arg) or {}` would throw an
error if arg was None.
----
\X/ /-\ `/ |_ /-\ |\|
Waylan Limberg
On Sun, Jun 23, 2013 at 1:19 AM, Hendra <hen...@gm...> wrote:
>
> Ah, I see.. Pretty nasty gotcha..
> But then, in that case, in the wikilinks example, shouldn't the assignment
> be this way:
>
> if configs is None:
> configs = {}
> else:
> configs = dict(configs)
>
> Wouldn't dict(None) throw a TypeError as well? And since the constructor of
> the WikiLinkExtension doesn't have default value for the `configs`
> parameter, does that mean it must always receive an argument?
> Or did I misunderstood the whole extension API itself?
>
>
> On Sun, Jun 23, 2013 at 12:34 PM, Waylan Limberg <wa...@gm...> wrote:
>>
>> A dict is a mutable object, so you should never assign it as a default
>> to an argument. See this [1] for one of the many explanations out
>> there.
>>
>> We avoid the TypeError with a line like this inside a function/method:
>>
>> foo = arg or {}
>>
>> The above is shorthand for:
>>
>> if arg: # If arg is None this evaluates to false
>> foo = arg # only use arg when a value was passed in
>> else:
>> foo = {} # avoid a TypeError here
>>
>> Now the only way to get a TypeError is if the user passes in a bad
>> type -- unless that type would evaluate to false -- in which case the
>> default would be used.
>>
>> This is basic Python. Eventually you'll get it. In fact, I couldn't
>> remember what the reason was offhand. I knew it was wrong to assign a
>> dict as a default, but had to do a search to remember why.
>>
>> [1]: http://effbot.org/zone/default-values.htm
>> ----
>> \X/ /-\ `/ |_ /-\ |\|
>> Waylan Limberg
>>
>>
>> On Sat, Jun 22, 2013 at 11:15 PM, Hendra <hen...@gm...> wrote:
>> > I see,, thanks, that helps a lot. Should've checked out that one.
>> >
>> > Another question, hope this doesn't sound stupid. Why is the default
>> > argument value for `configs` that is passed to the `makeExtension` is
>> > `None`, instead of `{}`? wouldn't that cause a TypeError?
>> >
>> >
>> > On Sat, Jun 22, 2013 at 10:29 PM, Waylan Limberg <wa...@gm...>
>> > wrote:
>> >>
>> >> Hendra,
>> >>
>> >> Take a look at the wikilinks extension [1]. It makes extensive use of
>> >> configs. Note that some defaults are set first (line 95) in a dict.
>> >> Then the user provided settings are forced to a dict (line 101)
>> >> (`dict()` will convert a list of tuples to a dict or if passed a dict,
>> >> will simply return it). Finally, the settings are iterated over (line
>> >> 103), overriding the already set defaults (104).
>> >>
>> >> So, to answer your question, either a list of tuples or a dict can be
>> >> passed in as settings. Either way, it will be stored as a dict if you
>> >> use the config methods on the `markdown.extensions.Extension` class.
>> >> Of course, as you are subclassing, you can override config handling if
>> >> you want to make it work however you would like.
>> >>
>> >> Hope that helps.
>> >>
>> >> [1]:
>> >>
>> >> https://github.com/waylan/Python-Markdown/blob/master/markdown/extensions/wikilinks.py
>> >> ----
>> >> \X/ /-\ `/ |_ /-\ |\|
>> >> Waylan Limberg
>> >>
>> >>
>> >> On Sat, Jun 22, 2013 at 9:03 AM, Hendra <hen...@gm...> wrote:
>> >> > Hi,
>> >> > I am trying to write a simple extension for the python markdown API,
>> >> > but
>> >> > I
>> >> > am having some problem with setting the config. From the doc:
>> >> >
>> >> > import markdown
>> >> > import myextension
>> >> > configs = {...}
>> >> > myext = myextension.MyExtension(configs=configs)
>> >> > md = markdown.Markdown(extensions=[myext])
>> >> >
>> >> > It seems like I would have to pass the config in a dict, but when I
>> >> > try
>> >> > to
>> >> > initialize the extension with the name of the extension, it receives
>> >> > the
>> >> > config as a list (I am guessing from config.items() ).
>> >> >
>> >> > Looking around the source of the extensions here:
>> >> >
>> >> >
>> >> > https://github.com/waylan/Python-Markdown/tree/master/markdown/extensions,
>> >> > it seems like in most of them, configs is a list. So, which is the
>> >> > right
>> >> > one?
>> >> >
>> >> >
>> >> > --
>> >> > Best Regards, and Thank you,
>> >> > Hendra
>> >> >
>> >> >
>> >> >
>> >> > ------------------------------------------------------------------------------
>> >> > This SF.net email is sponsored by Windows:
>> >> >
>> >> > Build for Windows Store.
>> >> >
>> >> > http://p.sf.net/sfu/windows-dev2dev
>> >> > _______________________________________________
>> >> > Python-markdown-discuss mailing list
>> >> > Pyt...@li...
>> >> > https://lists.sourceforge.net/lists/listinfo/python-markdown-discuss
>> >> >
>> >
>> >
>> >
>> >
>> > --
>> > Best Regards, and Thank you,
>> > Hendra
>
>
>
>
> --
> Best Regards, and Thank you,
> Hendra
|