[FAQTS] Python Knowledge Base Update -- July 9th, 2000

Fiona Czuczman fiona at sitegnome.com
Sun Jul 9 08:26:08 EDT 2000


Hi Guys,

The latest entries into http://python.faqts.com

regards,

Fiona Czuczman


## Unanswered Questions ########################################


-------------------------------------------------------------
Is there a utility to convert module docstrings to HTML for nice documentation?
http://www.faqts.com/knowledge-base/view.phtml/aid/4610
-------------------------------------------------------------
Stu D



## New Entries #################################################


-------------------------------------------------------------
Formatted printing of numbers with significant digits
http://www.faqts.com/knowledge-base/view.phtml/aid/4596
-------------------------------------------------------------
Russell Wallace


# Number formatting functions by Russell Wallace

# Includes inserted commas, specifiable number of decimal places,
# and the ability to specify a number of significant digits before or
# after the decimal point.

# This code is in the public domain.

def format(x, sigdigits=0, decimals=0):
	if sigdigits == 0:
		sigdigits = 16

	sign = ""
	if x < 0:
		sign = "-"
		x = -x

	decpart, intpart = modf(x)
	s = sign + formatnat(intpart) + str(decpart)[1:]

	s = strtocharlist(s)
	count = 0
	counting = 0
	propflag = 0
	propindex = 0
	for i in range(len(s)):
		c = s[i]
		if c in digits:
			if c != "0":
				counting = 1
			if counting:
				count = count + 1
			if count > sigdigits:
				if not propindex:
					propflag = s[i] >= "5"
					propindex = i
				s[i] = "0"

	if propflag:
		prop(s, propindex)

	s = charlisttostr(s)
	while (s[len(s) - 1] == "0"):
		s = s[:-1]

	i = index(s, ".") + 1
	n = len(s) - i
	s = s + "0" * (decimals-n)

	if s[len(s) - 1] == ".":
		s = s[:-1]
	return s

def formatnat(x):
	s = ""
	count = 0
	while 1:
		c = fmod(x, 10)
		c = str(int(c))
		if not count and s != "":
			s = "," + s
		count = (count + 1) % 3
		s = c + s
		x = floor(x / 10)
		if x == 0:
			break
	return s

def prop(s, i):
	while i:
		i = i - 1
		while i >= 0 and not (s[i] in digits):
			i = i - 1
		if i < 0:
			break
		d = int(s[i]) + 1
		if d == 10:
			d = 0
		s[i] = str(d)
		if d:
			return
	i = 0
	if s[0] == "-":
		i = 1
	if countdigits(s) % 3 == 0:
		s.insert(i, ",")
	s.insert(i, "1")

def countdigits(s):
	n = 0
	for c in s:
		if c == ".":
			break
		if c in digits:
			n = n + 1
	return n

def formatmoney(x):
	return "$" + format(x, 0, 2)

def formatpercent(x, y):
	x = float(x) / y
	x = x * 100
	return format(x) + "%"


-------------------------------------------------------------
How can I remove a number of items from a list using a loop?
http://www.faqts.com/knowledge-base/view.phtml/aid/4623
-------------------------------------------------------------
Fiona Czuczman
Bjorn Pettersen

the obvious way:

  for x in list[:]:  # create a copy
    if f(x):
      list.remove(x)

perhaps faster:

  for i in range(len(list)-1, 0, -1):
    x = list[i]
    if f(x):
      list.remove(x)

that traverses the list backwards, so should be more resistant to the
list changing...


## Edited Entries ##############################################


-------------------------------------------------------------
How can I tokenize a string using more than 1 separator?
http://www.faqts.com/knowledge-base/view.phtml/aid/3140
-------------------------------------------------------------
Fiona Czuczman
David Goodger,Mikael Olofsson

use re.split:

>>> import re
>>> s = "one,two;three:four,five;six:seven"
>>> re.split("[,;:]", s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']
>>> sep = re.compile("[,;:]")
>>> sep.split(s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']

---- Alternately ----

seps = ':|/'
mystring = 'foo:bar|baz/zot'
newstring = string.join(map(lambda x, seps=seps:(x+seps[0])[x in 
seps],a),'')
result = string.split(newstring,seps[0])


-------------------------------------------------------------
Why do some strange characters appear instead of norwegian characters in GUI of pythonwin build 132?
http://www.faqts.com/knowledge-base/view.phtml/aid/4590
-------------------------------------------------------------
Nils Otto Johansen, Fiona Czuczman
Neil Hodgson

I'm not sure about 132 but 131 defaults to using Unicode for editing
Python source code and the interactive window. To stop this, open
Pythonwin\pywin\scintilla\view.py, find the OnInitialUpdate method and
comment out the two lines under "# Enable Unicode if we can". Save the 
file, shut down Pythonwin and restart it and see if this fixes the 
problem.







More information about the Python-list mailing list