473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python & Unicode decimal interpretation

In reading over the source for CPython's PyUnicode_EncodeDecimal,
I see a dance to handle characters which are neither dec-equiv nor
in Latin-1. Does anyone know about the intent of such a conversion?

As far as I can tell, error handling is one of:
strict, replace, ignore, xmlcharrefreplace, or something_else
What I don't understand is whether, in the ignore or something_else
cases, there is any chance that digits will show up anywhere that
they would not if these characters were treated as a character like '?'?

Can someone either give me definitive "why not" or (preferably) give
me a test case that shows where that interpretation does not hold.

--Scott David Daniels
sc***********@acm.org
Dec 3 '05 #1
3 1440
Scott David Daniels wrote:
In reading over the source for CPython's PyUnicode_EncodeDecimal,
I see a dance to handle characters which are neither dec-equiv nor
in Latin-1. Does anyone know about the intent of such a conversion?
To support this:
int(u"\N{DEVANAGARI DIGIT SEVEN}")

7

As far as I can tell, error handling is one of:
strict, replace, ignore, xmlcharrefreplace, or something_else
What I don't understand is whether, in the ignore or something_else
cases, there is any chance that digits will show up anywhere that
they would not if these characters were treated as a character like '?'?

Can someone either give me definitive "why not" or (preferably) give
me a test case that shows where that interpretation does not hold.


In the "ignore" case, no output is produced at all, for the unencodable
character; this is the same way that '?' would be treated (it is
also unencodable).

In the something_else case, a user-defined exception handler could
treat the error in any way it liked, e.g. encoding all letters
u'A' to digit '0'. This might be different from the way this error
handler would treat '?'.

Regards,
Martin
Dec 3 '05 #2
Martin v. Löwis wrote:
Scott David Daniels wrote:
In reading over the source for CPython's PyUnicode_EncodeDecimal,
I see a dance to handle characters which are neither dec-equiv nor
in Latin-1. Does anyone know about the intent of such a conversion?
To support this:
>>> int(u"\N{DEVANAGARI DIGIT SEVEN}") 7

OK, That much I have handled. I am fiddling with direct-to-number
conversions and wondering about cases like
int(u"\N{DEVANAGARI DIGIT SEVEN}" + XXX

+ u"\N{DEVANAGARI DIGIT SEVEN}")

Where XXX does not pass the digit test, but must either:
(A) be dropped, giving a result of 77
or (B) get translated (e.g. to u'234') giving 72347
or (C) get translated (to u'2' + YYY + u'4') where YYY will
require further handling ...

I don't really understand how the "ignore" or "something_else"
cases get caused by python source [where they come from]. Are they
only there for C-program access?
In the "ignore" case, no output is produced at all, for the unencodable
character; this is the same way that '?' would be treated (it is
also unencodable). If I understand you correctly -- I can consider the digit stream to stop
as soon as I hit a non-digit (except for handling bases 11-36).
In the something_else case, a user-defined exception handler could
treat the error in any way it liked, e.g. encoding all letters
u'A' to digit '0'. This might be different from the way this error
handler would treat '?'.


--Scott David Daniels
sc***********@acm.org
Dec 3 '05 #3
Scott David Daniels wrote:
>>> int(u"\N{DEVANAGARI DIGIT SEVEN}") 7


OK, That much I have handled. I am fiddling with direct-to-number
conversions and wondering about cases like
>>> int(u"\N{DEVANAGARI DIGIT SEVEN}" + XXX + u"\N{DEVANAGARI DIGIT SEVEN}")


int() passes NULL as error mode, equalling strict. So if you get an
unencodable character, you get the UnicodeError.
I don't really understand how the "ignore" or "something_else"
cases get caused by python source [where they come from]. Are they
only there for C-program access?


Neither, nor. This code is dead.
In the "ignore" case, no output is produced at all, for the unencodable
character; this is the same way that '?' would be treated (it is
also unencodable).


If I understand you correctly -- I can consider the digit stream to stop
as soon as I hit a non-digit (except for handling bases 11-36).


No. In "ignore" mode, a codec doesn't stop at the unencodable character.
Instead, it skips it, continuing with the next character.

I mistakenly said that this would happen to '?' (question mark) also;
this is incorrect: PyUnicode_EncodeDecimal copies all Latin-1 characters
to the output, latin-1-encoded. So '?' would appear in the output,
even in "ignore" mode.

Handling of bases is not done in the function at all. Instead, the
callers of PyUnicode_EncodeDecimal will deal with number formats
(base, prefix, exponent syntax, etc.) They will assume ASCII
bytes.

Regards,
Martin
Dec 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bsagert | last post by:
Some web feeds use decimal character entities that seem to confuse Python (or me). For example, the string "doesn't" may be coded as "doesn’t" which should produce a right leaning apostrophe....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.