<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Oct 24, 2016 at 10:50 AM, Ryan Birmingham <span dir="ltr"><<a href="mailto:rainventions@gmail.com" target="_blank">rainventions@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr">I also believe that using a text file would not be the best solution; using a dictionary,</p></blockquote><div><br></div><div>actually, now that you mention it -- .translate() already takes a dict, so if youw ant to put your translation table in a text file, you can use a dict literal to do it:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0.8ex;border-left:1px solid rgb(204,204,204);border-right:1px solid rgb(204,204,204);padding-left:1ex;padding-right:1ex"></blockquote><span style="font-family:monospace,monospace"># contents of file:</span><br><blockquote class="gmail_quote" style="margin:0px 0.8ex;border-left:1px solid rgb(204,204,204);border-right:1px solid rgb(204,204,204);padding-left:1ex;padding-right:1ex"></blockquote><font face="monospace, monospace"><br></font><blockquote class="gmail_quote" style="margin:0px 0.8ex;border-left:1px solid rgb(204,204,204);border-right:1px solid rgb(204,204,204);padding-left:1ex;padding-right:1ex"></blockquote><span style="font-family:monospace,monospace">{</span></div><div class="gmail_quote"><span style="font-family:monospace,monospace">32: 95,</span><br><blockquote class="gmail_quote" style="margin:0px 0.8ex;border-left:1px solid rgb(204,204,204);border-right:1px solid rgb(204,204,204);padding-left:1ex;padding-right:1ex"></blockquote><span style="font-family:monospace,monospace">105: 64,</span></div><div class="gmail_quote"><span style="font-family:monospace,monospace">115: 36,</span></div><div class="gmail_quote"><span style="font-family:monospace,monospace">}</span></div><div class="gmail_quote"><font face="monospace, monospace"><br></font></div>then use it:<div class="gmail_quote">







<p class="gmail-p1"><font face="monospace, monospace"><span class="gmail-s1">s.translate(ast.literal_eval(</span><span class="gmail-s2">open</span><span class="gmail-s1">(</span><span class="gmail-s3">"trans_table.txt"</span><span class="gmail-s1">).read()))</span></font></p></div>now all you need is a tiny little utility function:<div class="gmail_quote"><font face="monospace, monospace"><br></font></div><div class="gmail_quote"><font face="monospace, monospace">def translate_from_file(s, filename):</font></div><div class="gmail_quote"><font face="monospace, monospace">    return </font><span class="gmail-s1" style="font-family:monospace,monospace">s.translate(ast.literal_eval(</span><span class="gmail-s2" style="font-family:monospace,monospace">open</span><span class="gmail-s1" style="font-family:monospace,monospace">(</span><span class="gmail-s3" style="font-family:monospace,monospace">filename</span><span class="gmail-s1" style="font-family:monospace,monospace">).read()))</span></div><div class="gmail_quote"><font face="monospace, monospace"><br></font></div><br>:-)<br><br>-Chris<br><div class="gmail_quote"><font face="monospace, monospace"><br></font><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace, monospace"><br><br> </font><p>other data structure, or anonomyous function would make more sense than having a specially formatted file.</p>
<div class="gmail_extra"><br><div class="gmail_quote">On Oct 24, 2016 13:45, "Chris Barker" <<a href="mailto:chris.barker@noaa.gov" target="_blank">chris.barker@noaa.gov</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>my thought on this:<div><br></div><div>If you need translate() you probably can write the code to parse a text file, and then you can use whatever format you want.</div><div><br></div><div>This seems a very special case to build into the stdlib.</div><div><br></div><div>-CHB</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 24, 2016 at 10:39 AM, Mikhail V <span dir="ltr"><<a href="mailto:mikhailwas@gmail.com" target="_blank">mikhailwas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello all,<br>
<br>
I would be happy to see a somewhat more general and user friendly<br>
version of string.translate function.<br>
It could work this way:<br>
string.newtranslate(file_with_<wbr>table, Drop=True, Dec=True)<br>
<br>
So the parameters:<br>
<br>
1. "file_with_table" : a text file with table in following format:<br>
<br>
#[In]    [Out]<br>
<br>
97    {65}<br>
98    {66}<br>
99    {67}<br>
100    {}<br>
...<br>
110    {110}<br>
<br>
<br>
Notes:<br>
All values are decimal or hex (to switch between parsing format use<br>
Dec parameter)<br>
As it turned out from my last discussion, majority prefers hex notation,<br>
so I am not in mainstream with my decimal notation here, but both<br>
should be supported.<br>
Empty [Out] value {} means that the character will be deleted.<br>
<br>
2. "Drop = True" this will set the default behavior for those values<br>
which are NOT in the table.<br>
<br>
For Drop = True: all values not defined in table set to [out] = {},<br>
and be deleted.<br>
<br>
For Drop=False: all values not defined in table set [out] = [in], so<br>
those remain as is.<br>
<br>
3. Dec= True : parsing format Decimal/hex. I use decimal everywhere.<br>
<br>
<br>
Further thoughts: for 8-bit strings this should be simple to implement<br>
I think. For 16-bit of course<br>
there is issue of memory usage for lookup tables, but the gurus could<br>
probably optimise it.<br>
E.g. at the parsing stage it is not necessary to build the lookup<br>
table  for whole 16-bit range of course,<br>
but take only values till the largest ordinal present in the table file.<br>
<br>
About the format of table file: I suppose many users would want also<br>
to define characters directly, I am not sure<br>
if it is really needed, but if so, additional brackets or escape char<br>
could be used, like this for example:<br>
<br>
a    {A}<br>
\98    {\66}<br>
\99    {\67}<br>
<br>
but as said I don't like very much the idea and would be OK for me to<br>
use numeric values only.<br>
<br>
So approximately I see it.<br>
Feel free to share thoughts or criticise.<br>
<br>
<br>
Mikhail<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail-m_-5977369141168560210m_37502283999232763gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959</a>   voice<br>7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329</a>   fax<br>Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317</a>   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br></blockquote></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>