Re: lxml - minor problem appending new element
Frank Millman wrote:
> Hi all
>
> I usually send lxml queries to the lxml mailing list, but it appears to
> be not working, so I thought I would try here.
>
> This is a minor issue, and I have found an ugly workaround, but I
> thought I would mention it.
Like this?
children = list(xml)
for y in children:
print(etree.tostring(y))
if y.get('z') == 'c':
child = etree.Element('y', attrib={'z': 'd'})
xml.append(child)
children.append(child)
It doesn't look /that/ ugly to me.
> In Python I can iterate through a list, and on a certain condition
> append a new item to the list, which is then included in the iteration.
Personally I follow the rule "never mutate a list you are iterating over",
even for appends, where the likelihood of problems is small:
items = ["a"]
for item in items:
if item == "a": items.append("a")
> >>> x = ['a', 'b', 'c']
> >>> for y in x:
> ... print(y)
> ... if y == 'b':
> ... x.append('d')
> ...
> a
> b
> c
> d
> >>> x
> ['a', 'b', 'c', 'd']
> >>>
>
> The same thing works in lxml -
>
> >>> lmx = ''
> >>> xml = etree.fromstring(lmx)
> >>> for y in xml:
> ... print(etree.tostring(y))
> ... if y.get('z') == 'b':
> ... xml.append(etree.Element('y', attrib={'z': 'd'}))
> ...
> b''
> b''
> b''
> b''
> >>> etree.tostring(xml)
> b''
>
> However, if it happens that the condition is met on the last item in the
> list, Python still works, but lxml does not include the appended item in
> the iteration. In the following, the only change is checking for 'c'
> instead of 'b'.
>
> >>> x = ['a', 'b', 'c']
> >>> for y in x:
> ... print(y)
> ... if y == 'c':
> ... x.append('d')
> ...
> a
> b
> c
> d
> >>> x
> ['a', 'b', 'c', 'd']
> >>>
>
> >>> lmx = ''
> >>> xml = etree.fromstring(lmx)
> >>> for y in xml:
> ... print(etree.tostring(y))
> ... if y.get('z') == 'c':
> ... xml.append(etree.Element('y', attrib={'z': 'd'}))
> ...
> b''
> b''
> b''
> >>> etree.tostring(xml)
> b''
>
> As you can see, the last element is correctly appended, but is not
> included in the iteration.
>
> Is there any chance that this can be looked at, or is it just the way it
> works?
File a bug report and see if the author is willing to emulate the list
behaviour.
> BTW, I see that ElementTree in the standard library does not have this
> problem.
Maybe uses a list under the hood.
--
https://mail.python.org/mailman/listinfo/python-list
Re: lxml - minor problem appending new element
On 2020-02-03 10:39 AM, Peter Otten wrote:
Frank Millman wrote:
This is a minor issue, and I have found an ugly workaround, but I
thought I would mention it.
Like this?
children = list(xml)
for y in children:
print(etree.tostring(y))
if y.get('z') == 'c':
child = etree.Element('y', attrib={'z': 'd'})
xml.append(child)
children.append(child)
It doesn't look /that/ ugly to me.
That is not bad. My actual solution was to revert to the non-pythonic
method of iterating over a list -
pos = 0
while pos < len(xml):
y = xml[pos]
print(etree.tostring(y))
if y.get('z') == 'c':
xml.append(etree.Element('y', attrib={'z': 'd'}))
pos += 1
That way, if I append to xml, I automatically pick up the appended element.
In Python I can iterate through a list, and on a certain condition
append a new item to the list, which is then included in the iteration.
Personally I follow the rule "never mutate a list you are iterating over",
even for appends, where the likelihood of problems is small:
items = ["a"]
for item in items:
if item == "a": items.append("a")
I did feel a bit uneasy doing it, but once I had got it working it did
not feel too bad. I did not test for appending from the last item, so
that bug has just bitten me now, but I will run with my workaround
unless/until lxml is fixed.
Is there any chance that this can be looked at, or is it just the way it
works?
File a bug report and see if the author is willing to emulate the list
behaviour.
Will do.
BTW, I see that ElementTree in the standard library does not have this
problem.
Maybe uses a list under the hood.
Thanks for the advice.
Frank
--
https://mail.python.org/mailman/listinfo/python-list
when uses time.clock,there are always mistakes
Hello ! The problem like this: RESTART: Shell >>> import time >>> t = time.clock() Traceback (most recent call last): File "", line 1, in t = time.clock() AttributeError: module 'time' has no attribute 'clock' >>> Thanks! Best wishes! -- https://mail.python.org/mailman/listinfo/python-list
no explanation towards not installing my application//
Sent from Mail for Windows 10 -- https://mail.python.org/mailman/listinfo/python-list
Re: no explanation towards not installing my application//
On Feb 3, 2020 8:18 AM, "the python app i had downloaded is not opening!" < [email protected]> wrote: I'm assuming you're using Windows 10. Correct? You're telling us you downloaded the program installer. Did you run the installer? Where did it tell you it was putting the program? Exactly what did you do to try to open python? Exactly what results did you get? Bob Gailer -- https://mail.python.org/mailman/listinfo/python-list
Re: when uses time.clock,there are always mistakes
On 03/02/2020 08:04, 石盼 wrote: Hello ! The problem like this: RESTART: Shell >>> import time >>> t = time.clock() Traceback (most recent call last): File "", line 1, in t = time.clock() AttributeError: module 'time' has no attribute 'clock' It is correct, there is no time.clock() function. The documentation is here: https://docs.python.org/3/library/time.html -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
Re: when uses time.clock,there are always mistakes
石盼 wrote: > The problem like this: > t = time.clock() > AttributeError: module 'time' has no attribute 'clock' Quoting https://docs.python.org/3/whatsnew/3.8.html#api-and-feature-removals """ The function time.clock() has been removed, after having been deprecated since Python 3.3: use time.perf_counter() or time.process_time() instead, depending on your requirements, to have well-defined behavior. (Contributed by Matthias Bussonnier in bpo-36895.) """ -- https://mail.python.org/mailman/listinfo/python-list
braket an matrix in python
x=np.linspace(0,2*math.pi,n) n=len(x) r=int(math.ceil(f*n)) h=[np.sort(np.abs(x-x[i]))[r] for i in range(n)] this is part of a python code. the last line is confusing? x is a matrix and x[i] is a number. how calculate x-x[i] for each i? why the put r in [] ? -- https://mail.python.org/mailman/listinfo/python-list
Re: braket an matrix in python
Parisa Ch wrote: > x=np.linspace(0,2*math.pi,n) > n=len(x) > r=int(math.ceil(f*n)) > h=[np.sort(np.abs(x-x[i]))[r] for i in range(n)] > > this is part of a python code. the last line is confusing? x is a matrix > and x[i] is a number. how calculate x-x[i] for each i? > why the put r in [] ? Brake it appart: y = x - x[i] # the numpy feature that makes that possible is called # "broadcasting" subtracts the value x[i] from every entry in the vector x z = np.abs(y) calculates the absolute value of every entry in y. t = np.sort(z) sorts the values in z and finally, assuming r is an integer, u = t[r] looks up a single scalar in the vector. The enclosing "list comprehension" [expr for item in iterable] builds a list of n values calculated as sketched above. -- https://mail.python.org/mailman/listinfo/python-list
