2 comments

  • djoldman3 hours ago
    &gt; _canonicalize_table = str.maketrans( &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ_.&quot;, &quot;abcdefghijklmnopqrstuvwxyz--&quot;, )<p>&gt; ...<p>&gt; value = name.translate(_canonicalize_table)<p>&gt; while &quot;--&quot; in value:<p>&gt; value = value.replace(&quot;--&quot;, &quot;-&quot;)<p>translate can be wildly fast compared to some commonly used regexes or replacements.
    • est2 hours ago
      I am curious, why not .lower().translate(&#x27;_.&#x27;, &#x27;--&#x27;)
      • fwip1 hour ago
        .lower() has to handle Unicode, right? I imagine the giant tables slow it down a bit.
    • teaearlgraycold3 hours ago
      I would expect however that a regex replacement would be much faster than your N^2 while loop.
      • notpushkin2 hours ago
        It would be, if it was a common situation.<p>This loop handles cases like `eggtools._spam` → `eggtools-spam`, which is probably rare (I guess it’s for packages that export namespaced modules, and you probably don’t want to export _private modules; sorry in advance for non-pythonic terminology). Having more than two separator characters in a row is even more unusual.
  • zahlman3 days ago
    Previously: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46557542">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=46557542</a>