, and most likely str. A good debugging tip is to use repr rather
than str to print out debugging messages.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
file.
If your modem is going at full speed for those 15 minutes, you'll have
around 6.3Mb of data. Even after decompressing, and unless the data is
in some quite difficult to parse format, it'll take seconds to
process.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Apr 19, 7:37 pm, Gabriel Genellina gagsl-...@yahoo.com.ar
wrote:
The threading module has such aliases, but there are no plans for mass
renaming all the stdlib that I know of. You'll have to live with this
inconsistency.
It's been fixed in Python 3.0!
--
Paul Hankin
--
http
the 'law of demeter'. If
nothing else, it makes the speaker and listener much easier to read.
If you later want to test these classes, you'll find it a ton easier,
since you can mock the client class.. but perhaps that's a little too
advanced.
HTH
--
Paul Hankin
--
http://mail.python.org/mailman
)
for x in IN:
d[x] += 1
SN = [x for (x, c) in d.iteritems() if c 1]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
IN.sort()
SN = [k for k, v in itertools.groupby(IN) if len(list(v)) 1]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
is
well written, and you've already reached the limits of the power of
regexps, and it's difficult to read.
Have a look at pyparsing for a simple solution to your problem.
http://pyparsing.wikispaces.com/
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Aug 3, 8:12 am, binaryjesus [EMAIL PROTECTED] wrote:
On Aug 3, 1:46 am, Paul Hankin [EMAIL PROTECTED] wrote:
On Aug 2, 10:35 pm, binaryjesus [EMAIL PROTECTED] wrote:
hi i am working on a S3 project and facing a really weird problem!
take a look at the following import statements
))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
preferred format.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
you already have a module
for getting the metadata out of one of your modules; perhaps it could
go in there?
def version(module):
return getattr(module, 'Version_Text')[0][0]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
: 'si_98udasgf'
For speed, you can use 'string.translate', but simplest is to use a
comprehension:
import string
def magic_function(s, keep=string.ascii_letters + string.digits +
'_'):
return ''.join(c for c in s if c in keep)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
):
...
def ITEM_TREE(self):
...
builder = Builder()
for word in parse_list:
item = getattr(builder, word)()
...
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
is actually a competitive advantage for your company?
Have you seen this?
http://www.paulgraham.com/pypar.html
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Jul 9, 8:23 pm, Robert [EMAIL PROTECTED] wrote:
given d:
d = [soep, reeds, ook]
I want it to print like
soep, reeds, ook
I've come up with :
print (%s+, %s*(len(d)-1)) % tuple(d)
but this fails for d = []
any (pythonic) options for this?
print ', '.join(d)
--
Paul Hankin
())
But if you don't need the string, just write straight to the file:
for part in dmntGenerator():
dmntFile.write(part)
This is likely to be a lot faster as no large string is produced.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
if length == 1:
yield [n]
return
for i in xrange(1, n + 2 - length):
for p in partitions(n - i, length - 1):
yield [i] + p
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
the builtin 'sum' function.
sum(a + b for a, b in zip(xrange(1, 51), xrange(50, 0, -1)))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
solution, as it's readable and short.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
class are allowed to replace attributes with their
own lists, you'll have to catch these and convert to NotifyingLists;
and it may be somewhat messy.
I hope this is useful to you.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
things simpler
since you don't have to worry about 'scale'.
Your examples convert easily:
from decimal import Decimal
qty = Decimal('12.5')
price = Decimal('123.45')
print price * qty
print qty * price
print (qty * price).quantize(Decimal('0.01'))
--
Paul Hankin
--
http://mail.python.org/mailman
http://www.python.org/dev/peps/pep-3136/
It contains exactly this idea, but using 'break letters' rather than
'raise letters.StopIteration()'. I think I like the PEP's syntax
better than yours, but anyway, it was rejected.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
, score_costs)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
]
def _setu(self, u): self.y[1] = u
u = property(_getu, _setu)
... and similarly for du, V
Using these two tricks, your code line would be:
s.du = s.a * (s.b * s.V - s.u)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On May 28, 11:25 am, James [EMAIL PROTECTED] wrote:
word = raw_input(Type a word:)
start = len(word)
for letter in range(start, 0, -1):
print letter
Hi James,
for letter in reversed(word):
print letter
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
should 'args' have to be iterable anyway? I don't understand
what's going on here? Could someone help me with this?
Did you actually write self,args = args?
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
)]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On May 20, 7:34 pm, [EMAIL PROTECTED] wrote:
i have a big file with sentences, the first file of each sentence
contains a colon(:) somewher eon that line
i want to jump past that sentence.
if all(x != ':' for x in line):
this way i can check but i dont want to check for every line in the
and any dashes between
strings. I could do it in access manually but want to do from python
script
'filter' returns a string if it's argument is a string, so works
nicely here.
def cleanup(s):
return filter(lambda x: x not in ' -', s)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo
(seen.add(c) or c for c in s if c not in seen)
I wouldn't write it this way though :)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
This is better written using takewhile...
itertools.takewhile(lambda x: x != value, iterable)
But if you really need to reinvent the wheel, perhaps this is simpler?
def test(iterable, value, op=operator.ne):
for x in iterable:
if not op(x, value):
return
yield x
--
Paul
):
print 'FizzBuzz'[4*(i%30):4+4*(i%51)] or i
for i in xrange(1, 101):
print 'Fizz'*(i%31)+'Buzz'*(i%51) or i
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
[i] for i in list2]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
be None if we're on the
first element, otherwise (previous_key, previous_value).
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
way
to write what you want.
import heapq
def mostfreq(message):
return heapq.nlargest(3, set(message), key=message.count)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
in topics:
print Next,topic,course starts,info[topic]
Better:
for topic, when in sorted(topics.iteritems(), reverse=True):
print 'Next %s course starts %s' % (topic, when)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(x.capitalize() for x in
header.split('-'))
setattr(self, header, value)
You may want to add some error checking though!
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
implementation defined what sign r has if
either a or b is negative.
This means python already has C-like behaviour... it's compatible with
standard C, although not with C99.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
that answer is set to the empty string when no match
is found.
for answer in l:
if str in answer: break
else:
answer = ''
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
module. What I want to know is how to
get the number of occurences of numeric element in an array. Say,
Something like this should do the job:
histogram = [0] * 256
for x in my_array:
histogram[x] += 1
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Mar 11, 10:35 am, royG [EMAIL PROTECTED] wrote:
i am checking if a directory exists and if it does i want to delete it
and its contents.then i want to create the directory before creating
files in it.
Have a look at shutil.rmtree
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo
On Mar 10, 3:12 am, George Sakkis [EMAIL PROTECTED] wrote:
On Mar 9, 7:37 pm, Paul Hankin [EMAIL PROTECTED] wrote:
On Mar 9, 8:58 pm, duccio [EMAIL PROTECTED] wrote:
Someone knows if it's possible to make this __iter__ function with just
one 'yield' intead of two?
...
def
():
yield nn #2
Only one yield and shorter (but not really any simpler):
from itertools import chain
class Node:
...
def __iter__(self):
for x in chain([self], *self.childs):
yield x
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
in range(0, width*height, width*2):
for j in range(0,width):
y.append(Y[i+j])
There's nothing really wrong with your code. Maybe it's a little nicer
written like this:
y = []
for i in range(0, height, 2):
y.extend(Y[i * width + j] for j in range(width))
--
Paul Hankin
--
http
()
#process x to get zero or more y's
#for each y:
q.put(y)
Why not just do it like that? With a few changes it'll work fine:
while q:
x = q.pop(0)
for y in process(x):
q.append(y)
Or (almost) equivalently...
while q:
x = q.pop(0)
q.extend(process(x))
--
Paul
is false, its value
is returned; otherwise, y is evaluated and the resulting value is
returned.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(element)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(list, args), len(args), [None] *
len(args)):
yield tuple(x)
print list(cartesian_product('ab', 'cd', xrange(3)))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Feb 12, 10:17 pm, Ben C [EMAIL PROTECTED] wrote:
On 2008-02-12, Paul Rubin wrote:
Paul Hankin [EMAIL PROTECTED] writes:
def genDescendants(self):
return chain([self], *[child.genDescendants()
for child in self.children])
That is scary. It generates an in-memory list
()
for child in self.children])
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
I need to search list a for the sequence of list b
def list_contains(a, b):
return any(a[i:i+len(b)] == b for i in range(len(a) - len(b) + 1))
list_contains(range(1, 7), [2, 3, 4])
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
as a text file or string, and parse it at program startup
to build the hashtable. That way anyone can look at or modify the
table, even if they know nothing about python or coding.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
at http://docs.python.org/lib/module-time.html
Untested:
time.strptime(my_date, '%d %b %y %H:%M:%S %Z')
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
for the last line would be 'apps = filter(apptitles.get,
apps)'
or 'apps = apptitles.keys()'.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
in reversed(bad_indices):
del a[bad_index]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
a much cleaner approach.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
not guaranteed to work.
From http://docs.python.org/lib/built-in-funcs.html
Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
library.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(*xs, **kw):
pad = kw.get('padding', None)
maxlen = max(len(x) for x in xs)
return islice(izip(*[chain(x, repeat(pad)) for x in xs]), maxlen)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Jan 17, 7:02 pm, George Sakkis [EMAIL PROTECTED] wrote:
On Jan 17, 12:25 pm, Paul Hankin [EMAIL PROTECTED] wrote:
On Jan 17, 4:38 pm, Bruno Desthuilliers bruno.
[EMAIL PROTECTED] wrote:
Now there are very certainly smart solutions using itertools, but the
one I cooked is way too
','blib',0.9]
Use regular expressions: see http://docs.python.org/lib/node49.html
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Jan 14, 12:56 pm, Neal Becker [EMAIL PROTECTED] wrote:
How do I tell if my python-2.5 is build with ucs2 or ucs4?
See if unichr(0x1) raises ValueError: if it does, you're ucs2.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
) = [
binwriter(fn) for fn in filenames]
This can be more cleanly written using locals()
for fn in filenames:
locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb')
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Jan 9, 10:02 am, Fredrik Lundh [EMAIL PROTECTED] wrote:
Paul Hankin wrote:
This can be more cleanly written using locals()
for fn in filenames:
locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb')
from the reference manual:
locals()
Update and return
itertools (just for a bit of fun). I'm trying this:
list(itertools.chain(testDict.itervalues())
Close! Try:
list(itertools.chain(*testDict.itervalues())
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
=True):
...
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
():
return (False, sthingelse failed)
domoreprocessing()
...
return (True, all validation OK)
Isn't that a lot more readable?
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
]
return x
# Nothing in the list passes the 'prop' test.
return None
# Example: pick 100 odd integers from 0 to 1000.
a = RandomPicker(xrange(1000), lambda x: x % 2 == 1)
print [a.pick() for i in xrange(100)]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo
]
x[r] = x[n - i - 1]
def shuffled(seq):
Generate the elements of seq in a random order
return (seq[i] for i in randxrange(len(seq)))
def pick_random(seq, prop):
return itertools.ifilter(prop, shuffled(seq)).next()
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo
If it were uniform, it should be around 111.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
]
random.shuffle(seq)
return itertools.ifilter(prop, seq).next()
I've used 5 'stabs' here. Perhaps it should be a function of L, but I
suppose you can tune it for your data.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Jan 5, 5:12 pm, Paul Hankin [EMAIL PROTECTED] wrote:
On Jan 5, 4:14 pm, [EMAIL PROTECTED] wrote:
On Jan 5, 5:07 pm, [EMAIL PROTECTED] wrote:
Hello, Paul and Arnaud.
While I think about your answers: do you think there is any way to
avoid shuffle?
It may take unnecessary long
On Jan 3, 1:31 pm, Simon Willison [EMAIL PROTECTED] wrote:
How can I tell Python I know this says it's a unicode string, but I
need you to treat it like a bytestring?
u'Bob\x92s Breakfast'.encode('latin-1')
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
... I use itertools regularly (and have a functional
background), but have never needed takewhile or dropwhile. I'd be
happy to see them deprecated.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
'.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
in range(c + 1, n + 1)
if cube(a) + cube(b) == cube(c) + cube(d)]
for n in (10, 12, 20):
print list(taxicab(n))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
On Dec 26, 1:09 am, Steven D'Aprano [EMAIL PROTECTED]
cybersource.com.au wrote:
On Mon, 24 Dec 2007 17:14:58 +0100, Martin P. Hellwig wrote:
As Dennis already pointed out I like to use dictionaries in these cases,
so I would use sand = dict() instead of sands = list() and would do
sand[i] =
you avoid trouble when your data
contains commas or control characters such as newlines.
import csv
help(csv)
Suggests this code:
import csv
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csvFile)
csv_writer.writerows(cursor.fetchall())
csv_file.close()
--
Paul Hankin
--
http
)? If so, use codecs.open to read it, and you
won't get the \x00's (you'll get a unicode string).
Or you can remove them using replace:
a = a.replace('\x00', '')
HTH
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
), and then any number of characters. If you're using this for path
splitting filenames under Windows, you should look at os.path.split
instead of writing your own.
HTH
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
divide_list(lst, n):
return zip(*[lst[i::n] for i in range(n)])
[It produces a list of tuples rather than a list of lists, but it
usually won't matter].
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
)
templine = ''
else:
templine += char
No need for all that code, because you're reimplementing the 'split'
method of strings:
x = 'blah\nblah\n'
lst = x.split('\n')[:-1]
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
, self.weights)
The 'uid' business makes me suspicious: what's it for? If you need it,
you probably need to initialise with an explicit test for None rather
than just 'if uid' which will be wrong if you use a uid of 0...
self.id = uid if uid is not None else id(self)
HTH
--
Paul Hankin
--
http
in ('access.log', 'error.log'):
logs += ['/home/%s/%s/log/%s' % (row[1], row[0], name)]
logs = '\n'.join(logs)
Or equivalently using a list comprehension...
logs = '\n'.join('/home/%s/%s/log/%s' % (row[1], row[0], name)
for row in resultSet
for name in ('access.log', 'error.log'))
--
Paul
():
def getpath(path=path):
return path
setattr(self, name, getpath)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
coroutines?
def parser(score):
for i in xrange(1, 6):
yield i
if score(i):
print %d passed! % i
def is_odd(n):
return n % 2
def m():
for i in parser(is_odd):
# Presumably do something here...
pass
--
Paul Hankin
--
http://mail.python.org/mailman
):
Or all the same?
for a in range(1, 10):
b = c = d = e = f = a
Whatever you're doing though, there's almost certainly a better way.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
list2tuple(source):
return transform(source, list, tuple)
def tuple2list(source):
return transform(source, tuple, list)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
like this is probably what you want...
for n in range(10 ** 5, 10 ** 6):
digits = map(int, str(n))
... test digits
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
come up with so far? Have you looked at the 're' module?
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
is
created (with a re.match call) to see why your expectation differs
from what happened. Perhaps you could add more print statements to the
loops to see what values 'files' and 'name' take.
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
, thanks, I'll have to file that one away, and use it now and then.
I'm intrigued - when would you want a callable module?
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
Smith', 'location': 'CA',},{'name': 'John
Smith', 'location': 'AZ',},]
I would want to sort by name first, then sub sort by location. Any
ideas? Thanks!
test.sort(key=lambda x:(x['name'], x['location']))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(words, key=lambda word: model.get(word, 1))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
-mindedly...
def conv(x, sep='.'):
x, y = str(x), []
while x:
y.append(x[-3:])
x = x[:-3]
return sep.join(reversed(y))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
(filename, 'r')))
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
% (i + 1, '*' * hundreds)
--
Paul Hankin
--
http://mail.python.org/mailman/listinfo/python-list
1 - 100 of 184 matches
Mail list logo