Re: Mathematica 7 compares to other languages

2008-12-10 Thread Roberto Bonvallet
[x/sum(x**2 for x in v)**0.5 for x in v] or, in order to avoid computing the norm for each element: def u(v): return (lambda norm: [x/norm for x in v])(sum(x**2 for x in v) **0.5) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Regarding coding style

2008-03-07 Thread Roberto Bonvallet
rnt about it anywhere. I started to do it spontaneously in order to tell apart end-of-sentence periods from abbreviation periods. Anyway, I don't think it's something people should be forced to do. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Elementary string-formatting

2008-01-12 Thread Roberto Bonvallet
ision This forces all divisions to yield floating points values: >>> 1/3 0 >>> from __future__ import division >>> 1/3 0.33331 >>> HTH, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Distinguishing attributes and methods

2007-12-08 Thread Roberto Bonvallet
On Dec 8, 4:19 am, [EMAIL PROTECTED] wrote: > With properties, attributes and methods seem very similar. I was > wondering what techniques people use to give clues to end users as to > which 'things' are methods and which are attributes. Methods are verbs, attributes are n

Re: Limit Guessing Algorithm

2007-12-02 Thread Roberto Bonvallet
l return 7.125. Is it close enough? Finding a better estimation is more a math problem than a Python one. Best regards, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: 3 number and dot..

2007-11-02 Thread Roberto Bonvallet
k with raise. I completely agree. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: 3 number and dot..

2007-11-02 Thread Roberto Bonvallet
On 31 oct, 22:21, Paul Rubin <http://[EMAIL PROTECTED]> wrote: > def convert(n): >assert type(n) in (int,long) I'd replace this line with n = int(n), more in the spirit of duck typing. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: 3 number and dot..

2007-10-31 Thread Roberto Bonvallet
oupby(enumerate(reversed(str(x))), lambda (n, i): n//3))[::-1] '12.332.321' >>> -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Normalize a polish L

2007-10-23 Thread Roberto Bonvallet
e URL is .../ValparaSoViADelMar. And if I wrote a blog entry about pingüinos and ñandúes, it would appear probably as .../ping-inos- and-and-es. Ugly and off-topic :) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Iteration for Factorials

2007-10-23 Thread Roberto Bonvallet
fact(x): r = re.compile(r"%d ! = (\d+)" % x) for line in urllib.urlopen("http://www.google.cl/search?q=%d%%21"; % x): m = r.search(line) if m: return int(m.group(1)) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Normalize a polish L

2007-10-16 Thread Roberto Bonvallet
; pinguino Frühstück -> Fruehstueck I'd like that web applications (e.g. blogs) took into account these conventions when creating URLs from the title of an article. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Nested For and While Statements

2007-09-24 Thread Roberto Bonvallet
the actual code. It is easier, less error prone and more useful to copy and paste them instead of writing some pseudocode. Maybe the repeated index i is a typo you made when writing this post and the original code is correct, but we don't have any way to know that. -- Roberto Bonvallet --

Re: subclass of integers

2007-09-15 Thread Roberto Bonvallet
rn the same result as integer addition. However if either x or y > is None, these operations return None. No need to create a new class: try: result = a * b except TypeError: result = None -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: "Variable variable name" or "variable lvalue"

2007-09-11 Thread Roberto Bonvallet
x) > period = Slab(accu) Better to use the `sum' builtin and a generator expression: period = Slab(sum(Material(x)) for x in numbers) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Speed of Python

2007-09-07 Thread Roberto Bonvallet
while j < 1000: ... I'm no expert on Python optimization either, so I cannot guarantee that both are the best way to write this algorithm. > > > [...] > > > m=j+1 This step also doesn't occur in the Matlab

Re: Speed of Python

2007-09-07 Thread Roberto Bonvallet
lists of 1000 elements, so you're not actually measuring only the numeric computations. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Problem with extremely small real number

2007-09-03 Thread Roberto Bonvallet
: n = int(n) k = int(k) if n <= 0: raise ValueError("Wrong value of n: %d" % n) Best regards, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: programmatically define a new variable on the fly

2007-08-09 Thread Roberto Bonvallet
On Aug 9, 6:11 pm, Lee Sander <[EMAIL PROTECTED]> wrote: > I would like to define a new variable which is not predefined by me. > For example, > I want to create an array called "X%s" where "%s" is to be determined > based on the data I am processing. Use a dictionary. -- http://mail.python.org/

Re: Sorting dict keys

2007-07-20 Thread Roberto Bonvallet
e new list. Try this: c = copy.copy(a.keys()) c.sort() -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting dict keys

2007-07-20 Thread Roberto Bonvallet
On 20 jul, 18:50, Alex Popescu <[EMAIL PROTECTED]> wrote: > If you just want to iterate over your dict in an ordered manner than all > you have to do is: > > for k in my_dict.keys().sort(): > # rest of the code sort() returns None, so this code won't work either. -- R

Re: running a random function

2007-06-07 Thread Roberto Bonvallet
x27;t see a > way of actually running the function. Try this: def f(x): print "Calling f with arg %s" % x def g(x): print "Calling g with arg %s" % x def h(x): print "Calling h with arg %s" % x import random functions = [f, g, h] for i in range(10):

Re: removing spaces between 2 names

2007-05-15 Thread Roberto Bonvallet
[EMAIL PROTECTED] wrote: > s = "jk hij ght" > print "".join(s.split(" ")) "".join(s.split()) is enough. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from unicode chars to HTML entities

2007-02-08 Thread Roberto Bonvallet
es included" solution that doesn't involve > reinventing the wheel? recode is good for this kind of things: $ recode latin1..html -d mytextfile It seems that there are recode bindings for Python: $ apt-cache search recode | grep python python-bibtex - Python inte

Re: C parsing fun

2007-02-08 Thread Roberto Bonvallet
OSE::#. This fails when the code already has the strings "#::OPEN::#" and "#::CLOSE::" in it. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: DOTALL not working as expected

2007-01-18 Thread Roberto Bonvallet
e pattern: > > import re > p = re.compile('X.*?Y', re.DOTALL) > print re.sub(p, 'Z', 'Xab\ncdY') > > Still the question - my fault or a bug? Your fault. According to the documentation [1], the re.sub function takes a count as a fourth

Re: One more regular expressions question

2007-01-18 Thread Roberto Bonvallet
Victor Polukcht wrote: > My actual problem is i can't get how to include space, comma, slash. Post here what you have written already, so we can tell you what the problem is. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Comparing a matrix (list[][]) ?

2007-01-13 Thread Roberto Bonvallet
3], ... [0, 0, 0, 5]] >>> min(min(x for x in row if x > 0) for row in matrix) 5 -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: parsing a file name

2007-01-12 Thread Roberto Bonvallet
7; >>> a.split("-")[-1][:-len(".src.rpm")] '2.3.4' >>> ".".join(map(str, range(2, 5))) '2.3.4' -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Restrictive APIs for Python

2006-12-15 Thread Roberto Bonvallet
n't document those private internals. Or document that they must not be accessed directly. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: AI library

2006-12-15 Thread Roberto Bonvallet
o): File "", line 1 def a-star(self, nodeFrom, nodeTo): ^ SyntaxError: invalid syntax -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: skip last line in loops

2006-12-15 Thread Roberto Bonvallet
e off the last item: >> >> for line in list(open("file"))[:-1]: >> print line >> >> > > hi > would it be a problem with these methods if the file is like 20Gb in > size...? The second one would be a problem, since

Re: tuple.index()

2006-12-14 Thread Roberto Bonvallet
) to be available. list(my_arg).index(...) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Routine for prefixing '>' before every line of a string

2006-12-14 Thread Roberto Bonvallet
> a, consequat ut, elementum ac, libero. Donec malesuada lacus vel quam. Ut a > massa vel velit fringilla rutrum. Maecenas massa sem, vulputate non, > lacinia eu, cursus ut, urna. Donec ultrices sollicitudin nunc. Sed vel arcu > in lacus posuere faucibus. Lorem ipsum

Re: speed of python vs matlab.

2006-12-14 Thread Roberto Bonvallet
Chao wrote: > My Bad, the time used by python is 0.46~0.49 sec, > I tried xrange, but it doesn't make things better. Actually it does: it doesn't waste time and space to create a big list. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Conditional iteration

2006-12-14 Thread Roberto Bonvallet
e the new conditional syntax because he felt it was needed or just to avoid typing a couple of extra lines. He did it just to avoid people keep using the ugly and error-prone "a and b or c" idiom. See the related PEP: http://www.python.org/dev/peps/pep-0308/ -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Conditional iteration

2006-12-13 Thread Roberto Bonvallet
ndant? This could be more convenient to you, but certainly not pythonic. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: newbie - HTML character codes

2006-12-13 Thread Roberto Bonvallet
;ofile.write(line) This should work (untested): infile = open('filename', 'r') outfile = open('otherfile', 'w') for line in infile: outfile.write(line.replace('—', '--')) But I think the best approach is to use a existing aplication or library that solves the problem. recode(1) can easily convert to and from HTML entities: recode html..utf-8 filename Best regards. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterating over several lists at once

2006-12-13 Thread Roberto Bonvallet
k) in cartesian_product(l1, l2, l3): print "do something with", i, j, k -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.has_key(x) versus 'x in dict'

2006-12-06 Thread Roberto Bonvallet
Andy Dingley wrote: > Out of interest, whats the Pythonic way to simply cast (sic) the set to > a list, assuming I don't need it sorted? The list comprehension? mySet = set(myList) myList = list(mySet) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.has_key(x) versus 'x in dict'

2006-12-06 Thread Roberto Bonvallet
I wrote: > In Python > 2.4: lastline.replace(">", ">=") -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.has_key(x) versus 'x in dict'

2006-12-06 Thread Roberto Bonvallet
Andy Dingley wrote: > I need to generate a set (lots of intersections involved), but then I > need to display it sorted > >lstBugsChanged = [ bugId for bugId in setBugsChanged ] >lstBugsChanged.sort() In Python > 2.4: sorted(setBugsChanged) -- Roberto

Re: dict.has_key(x) versus 'x in dict'

2006-12-06 Thread Roberto Bonvallet
Fredrik Lundh wrote: [...] > this is why e.g. > >string[:len(prefix)] == prefix > > is often a lot faster than > >string.startswith(prefix) This is interesting. In which cases does the former form perform better? [I won't stop using str.startswith anyway

Re: Python regular expression

2006-12-05 Thread Roberto Bonvallet
s.python.org/lib/module-re.html: This module provides regular expression matching operations *similar* to those found in Perl. Similar != the same. See http://docs.python.org/lib/re-syntax.html for details about valid syntax for regular expressions in Python. Cheers, -- Roberto Bo

Re: PythonTidy

2006-11-30 Thread Roberto Bonvallet
immediately noticed them and came up with an example to raise both issues is a indicator of how easy it would be to customize the script :) Cheers! -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: PythonTidy

2006-11-30 Thread Roberto Bonvallet
interpreter. > > See http://www.python.org/dev/peps/pep-0263/ The -*- syntax is emacs-style. The encoding directive is anything that matches r"coding[=:]\s*([-\w.]+)". The docs recommend to use either emacs-style or vim-style. See http://docs.python.org/ref/encodings.html Chee

Re: PythonTidy

2006-11-30 Thread Roberto Bonvallet
newsgroup, most people said they preferred #!/usr/bin/env python over #!/usb/bin/python for the shebang line. See http://tinyurl.com/yngmfr . Best regards. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: for i in range() anti-pattern [was Re: trouble writing results to files]

2006-11-30 Thread Roberto Bonvallet
ern" tends to imply that it is always > wrong. Right, I should have said: "iterating over range(len(a)) just to obtain the elements of a is not the pythonic way to do it". Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: How to detect what type a variable is?

2006-11-29 Thread Roberto Bonvallet
gt; but nothing.. type() doesn't return a string, it returns a type object. You should try this: if isinstance(artistList, list): ... Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Overloading "if object" unary operator

2006-11-29 Thread Roberto Bonvallet
way, if possible; I know that I could test for a.id. Define a method called __nonzero__ that returns True or False. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: trouble writing results to files

2006-11-29 Thread Roberto Bonvallet
Neil Cerutti wrote: > On 2006-11-29, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: >> BTW, iterating over range(len(a)) is an anti-pattern in Python. > > Unless you're modifying elements of a, surely? enumerate is your friend :) for n, item in enumerate(a):

Re: trouble writing results to files

2006-11-29 Thread Roberto Bonvallet
attern in Python. You should do it like this: for item in a: output.writerow([item]) > Second, there is a significant delay (5-10 minutes) between when the > program finishes running and when the text actually appears in the > file. Try closing the file explicitly. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Calling functions with dynamic arguments

2006-11-29 Thread Roberto Bonvallet
SeanDavis12 wrote: > I have a dictionary like: > > {"a":1, "b":2} > > and I want to call a function: > > def func1(a=3,b=4): >print a,b > > so that I get a=1,b=2, how can I go about that? func1(**yourdict) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple text parsing gets difficult when line continues to next line

2006-11-28 Thread Roberto Bonvallet
ESTED program = open(fileName) for line in program: while line.rstrip("\n").endswith("_"): line = line.rstrip("_ \n") + program.readline() do_the_magic() Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Modifying every alternate element of a sequence

2006-11-28 Thread Roberto Bonvallet
er (worse) way, just for fun: >>> from itertools import cycle >>> input = [1, 2, 3, 4, 5, 6] >>> wanted = [x * sign for x, sign in zip(input, cycle([1, -1]))] >>> wanted [1, -2, 3, -4, 5, -6] Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: sqlite3 views, if not exists clause

2006-11-14 Thread Roberto Bonvallet
.." should have supported this! <:o) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Programmatically finding "significant" data points

2006-11-14 Thread Roberto Bonvallet
iff) ['0.4', '0.1', '-0.2', '-0.01', '0.11', '0.5', '-0.2', '-0.2', '0.6', '-0.1', '0.2', '0.1', '0.1', '-0.45', '0.15', '-0.3', '-0.2&#x

Re: SyntaxError: Invalid Syntax.

2006-11-10 Thread Roberto Bonvallet
t;db = "homebase_zingers" > ); > > > but even when I have that, I still get the same error. Could you please copy and paste the exact code that is triggering the error, and the exact error message? (BTW, in Python you don't need to end your sta

Re: SyntaxError: Invalid Syntax.

2006-11-10 Thread Roberto Bonvallet
turn is a reserved keyword. You cannot have a variable with that name. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: range syntax

2006-11-10 Thread Roberto Bonvallet
rser know which colon terminates the 'for' in the following example: for i in 2:3:4: ... -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: extract text from a string

2006-11-09 Thread Roberto Bonvallet
le, it's tempting to suggest the best method > would be string_name[1:-1] and that you don't need a regex. ...or string_name.lstrip('+').rstrip('\n') I bet he doesn't need a regexp! Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Why can't you assign to a list in a loop without enumerate?

2006-11-01 Thread Roberto Bonvallet
ly only one -- obvious way to > do it? 8) The obvious one is to use enumerate. TOOWTDI allows less obvious ways to exist. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: To remove some lines from a file

2006-10-26 Thread Roberto Bonvallet
Sebastian Busch wrote: > The task is: > > "Remove the first two lines that don't begin with "@" from a file." awk 'BEGIN {c = 0} c < 2 && !/^@/ {c += 1; next} {print}' < mybeautifulfile -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: dict problem

2006-10-25 Thread Roberto Bonvallet
Roberto Bonvallet wrote: > Alistair King wrote: >> DS1v = {'C': 6, 'H': 10, 'O': 5} > > Try DS1v['C'] instead of DS1v[C]. > updateDS1v(FCas, C, XDS) > updateDS1v(FHas, H, XDS) > updateDS1v(FOas, O, XDS) > updateDS1v(F

Re: dict problem

2006-10-25 Thread Roberto Bonvallet
> NameError: name 'C' is not defined > > dictionary is: > > DS1v = {'C': 6, 'H': 10, 'O': 5} Try DS1v['C'] instead of DS1v[C]. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehension (searching for onliners)

2006-10-20 Thread Roberto Bonvallet
s damn onliner im looking for. I know, trying to put complex logic in one line makes you do all that. Go for the multiliner! -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: More Noob Questions

2006-10-19 Thread Roberto Bonvallet
think of us, non-native English speakers, that don't know slang words like "noob" that don't even appear in the dictionaries and don't add anything to your question. Cheers, -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: matrix Multiplication

2006-10-18 Thread Roberto Bonvallet
... [7, 8, 9, 10]] >>> >>> a = [[1, 2, 3], ... [4, 5, 6]] >>> >>> ab = [[sum(i*j for i, j in zip(row, col)) for col in zip(*b)] for row in a] >>> ab [[30, 36, 42, 48], [66, 81, 96, 111]] Straightforward from the definition of matrix multiplication. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Plotting histograms

2006-10-18 Thread Roberto Bonvallet
ange(-a, a+1) for i in xrange(100)] >>> >>> # print histogram ... for i in range(-a, a+1): ... print "%+d %s" % (i, '*' * v.count(i)) ... -5 * -4 * -3 * -2 ****** -1 ** +0 * +1 +2 *** +3 * +4 +5 :) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: naming objects from string

2006-09-21 Thread Roberto Bonvallet
: bob_apple = (1, 2, ..., 9), you don't need to initialize bob_apple with an empty tuple. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Replace single character at given position

2006-09-20 Thread Roberto Bonvallet
ableString class that would be a better approach: http://docs.python.org/lib/module-UserString.html Anyway, I bet that what Martin wants to do can be done by using only string methods :) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Are Python's reserved words reserved in places they dont need?tobe?

2006-09-14 Thread Roberto Bonvallet
st by appending a underscore to the word: pass_, return_, ... wink_ -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Request for tips on my first python script.

2006-09-08 Thread Roberto Bonvallet
build a 1-element tuple, you have to put a trailing comma: elif opt in ("--notfound", ): but it would be clearer if you just use: elif opt == "--notfound": -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: This seems to crash my program and gives me errors on the #include statements

2006-09-04 Thread Roberto Bonvallet
Please paste here the errors you get, and paste also the relevant code (not the whole program) that triggers that error. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: replace deepest level of nested list

2006-09-04 Thread Roberto Bonvallet
gt;>> l = >>> [[['r00','g00','b00'],['r01','g01','b01']],[['r10','g10','b10'],['r11','g11','b11']]] >>> def get_deepest(l, n): ... if isinstance(l[0], list): ... return [get_deepest(s, n) for s in l] ... else: ... return l[n] ... >>> get_deepest(l, 0) [['r00', 'r01'], ['r10', 'r11']] >>> get_deepest(l, 1) [['g00', 'g01'], ['g10', 'g11']] >>> n is the chosen index. HTH. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Syntax suggestion.

2006-08-31 Thread Roberto Bonvallet
bash-like syntax, you should really consider using bash :) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionary with object's method as thier items

2006-08-30 Thread Roberto Bonvallet
so i could access every method of instances of C Something like this? >>> class C: ... def f1(self): ... print "i'm one" ... def f2(self): ... print "i'm two" ... >>> obj = C() >>> d = {'one': obj.f1,

Re: refering to base classes

2006-08-29 Thread Roberto Bonvallet
k", you just have to omit the voice method for the dog class: it will be inherited from creature. If you want dog.voice() to do something else, you can call superclass' method like this: def voice(self): creature.voice(self) print "brace your self" any_other_magic() HTH -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Taking data from a text file to parse html page

2006-08-24 Thread Roberto Bonvallet
a good idea to try to reuse modules that are good at what they do. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: find, replace and save string in ascii file

2006-08-23 Thread Roberto Bonvallet
e in enumerate(lines): if "ALFA" in line: code = lines[i + 1].split("=")[1].strip("' \n") lines[i] = line.replace("ALFA", "BETA%s" % code) file(name).writelines(lines) -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: text editor suggestion?

2006-08-21 Thread Roberto Bonvallet
evim (easy vim). It is part of the standard vim distribution (actually it's the same program). Anyway, I suggest learning the classic modal vim, it's really worth it. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: mapping None values to ''

2006-06-18 Thread Roberto Bonvallet
imho <[EMAIL PROTECTED]>: > map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ]) You don't need map when using list comprehensions: ["" for i in [a, b, c] if i in ("None", None)] -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: code is data

2006-06-18 Thread Roberto Bonvallet
Ravi Teja <[EMAIL PROTECTED]> said: > I *like* 1..5 (ada, ruby) instead of range(5). If I had macros, I would > have done it myself for *my* code. You can write your own preprocessor to handle things like that. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Most elegant way to generate 3-char sequence

2006-06-10 Thread Roberto Bonvallet
> > aab > > Right. But that wasn't the question :p The question was about elegance, and elegance is when someone asks "do something 4 times for 5 strings of length 3" and you solve it for "do something n times for m strings

Re: Concatenating dictionary values and keys, and further operations

2006-06-07 Thread Roberto Bonvallet
d for j in d if i < j] >>> for i, j in pairs: ... cartesian_product = [(x, y) for x in d[i] for y in d[j]] ... print i + j, cartesian_product ... ac [(1, 6), (1, 7), (2, 6), (2, 7)] ab [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)] bc [(3, 6), (3, 7), (4, 6), (4, 7), (5, 6), (5, 7)] You can do whatever you want with this cartesian product inside the loop. > >Finally i want to check each pair if it is present in the file,whose > >format i had specified. I don't understand the semantics of the file format, so I leave this as an exercise to the reader :) Best regards. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Storing nothing in a dictionary and passing it to a function

2006-06-05 Thread Roberto Bonvallet
e 'sin' : (self.arbtrandef, (2, 3)), 'exp' : (self.arbtrandef, (2, 4)), 'pwl' : (self.pwldef, ()),# empty tuple represented by () 'sffm' : (self.arbtrandef, (5, 0)), } for (fname, (func, args)) in alldict.items(): # items unpacked directly func(fname, *args) Best regards. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: Concatenating dictionary values and keys, and further operations

2006-06-05 Thread Roberto Bonvallet
;a') and 2(type 'b') are connected,3 and 5 are connected > and so on. > I am not able to figure out how to do this.Any pointers would be helpful I don't understand very well what you want to do. Could you explain it more clearly, with an example? -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: reordering elements of a list

2006-06-04 Thread Roberto Bonvallet
s mainlist[i - 1]. If orderinglist is [3, 4, 2, 1], then [mainlist[i - 1] for i in orderinglist] is: [mainlist[3 - 1], mainlist[4 - 1], mainlist[2 - 1], mainlist[1 - 1]] Remember that indexing starts from 0. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list

Re: reordering elements of a list

2006-06-03 Thread Roberto Bonvallet
arguments and return the following list: > > ['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p'] >>> mainlist = list('qwertyuiop') >>> orderinglist = [3, 4, 2, 1] >