hello,
I generate dynamically a sequence of values,
but this "sequence" could also have length 1 or even length 0.
So I get some line in the form of:
line = '(2,3,4)'
line = ''
line = '(2)'
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)
So in fact I get the value from an exec statement, like this
exec 'signals = ' + line
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something
As this meant for real-time signals, I want it fast, so (I think) I
can't afford extensive testing.
Any smart solution there ?
thanks,
Stef Mientki 13 2403
On Oct 19, 12:24 am, stef mientki <stef.mien...@g mail.comwrote:
I generate dynamically a sequence of values,
but this "sequence" could also have length 1 or even length 0.
So I get some line in the form of:
line = '(2,3,4)'
line = ''
line = '(2)'
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)
So in fact I get the value from an exec statement, like this
exec 'signals = ' + line
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something
As this meant for real-time signals, I want it fast, so (I think) I
can't afford extensive testing.
Any smart solution there ?
First: don't collect data into strings - python has many container
types which you can use.
Next, your strings look like they're supposed to contain tuples. In
fact, tuples are a bit awkward sometimes because you have to use
'(a,') for a tuple with one element - (2) isn't a tuple of length one,
it's the same as 2. Either cope with this special case, or use lists.
Either way, you'll have to use () or [] for an empty sequence.
--
Paul Hankin
On Fri, 19 Oct 2007 01:24:09 +0200, stef mientki wrote:
hello,
I generate dynamically a sequence of values, but this "sequence" could
also have length 1 or even length 0.
So I get some line in the form of:
line = '(2,3,4)'
line = ''
line = '(2)'
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)
So in fact I get the value from an exec statement, like this
exec 'signals = ' + line
And then, one day, somebody who doesn't like you will add the following
to your input data:
"0; import os; os.system('rm # -rf /')"
[ Kids: don't try this at home! Seriously, running that command will be
bad for your computer's health. Or at least it would, if I hadn't put a
spike in it. ]
Don't use exec in production code unless you know what you're doing. In
fact, don't use exec in production code.
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something
No, I would say it already failed before it even got there.
>>line = '' exec 'signals = ' + line
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
signals =
^
SyntaxError: unexpected EOF while parsing
This is the right way to deal with your data:
input_data = """ (2, 3 , 4)
(2)
(3,4,5)
( 1, 2,3)
"""
for line in input_data.spli t('\n'):
line = line.strip().st rip('()')
values = line.split(',')
for value in values:
value = value.strip()
if value:
print(value)
As this meant for real-time signals, I want it fast, so (I think) I
can't afford extensive testing.
Don't guess, test it and see if it is fast enough. Some speed ups:
If you're reading from a file, you can just say: "for line in file:"
instead of slurping the whole lot into one enormous string, then
splitting over newlines.
If you can guarantee that there is no extra whitespace in the file, you
can change the line
line = line.strip().st rip('()')
to the following:
line = line.strip('\n( )')
and save a smidgen of time per loop. Likewise, drop the "value =
value.strip()" in the inner loop.
--
Steven.
On Oct 19, 10:58 am, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
On Fri, 19 Oct 2007 01:24:09 +0200, stef mientki wrote:
hello,
I generate dynamically a sequence of values, but this "sequence" could
also have length 1 or even length 0.
So I get some line in the form of:
line = '(2,3,4)'
line = ''
line = '(2)'
(in fact these are not constant numbers, but all kind of integer
variables, coming from all over the program, selected from a tree, that
shows all "reachable" variables)
So in fact I get the value from an exec statement, like this
exec 'signals = ' + line
And then, one day, somebody who doesn't like you will add the following
to your input data:
"0; import os; os.system('rm # -rf /')"
[ Kids: don't try this at home! Seriously, running that command will be
bad for your computer's health. Or at least it would, if I hadn't put a
spike in it. ]
Don't use exec in production code unless you know what you're doing. In
fact, don't use exec in production code.
Now I want to iterate over "signals", which works perfect if there are 2
or more signals,
but it fails when I have none or just 1 signal.
for value in signals :
do something
No, I would say it already failed before it even got there.
>line = '' exec 'signals = ' + line
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
signals =
^
SyntaxError: unexpected EOF while parsing
This is the right way to deal with your data:
input_data = """ (2, 3 , 4)
(2)
(3,4,5)
( 1, 2,3)
"""
for line in input_data.spli t('\n'):
line = line.strip().st rip('()')
values = line.split(',')
for value in values:
value = value.strip()
if value:
print(value)
As this meant for real-time signals, I want it fast, so (I think) I
can't afford extensive testing.
Don't guess, test it and see if it is fast enough. Some speed ups:
If you're reading from a file, you can just say: "for line in file:"
instead of slurping the whole lot into one enormous string, then
splitting over newlines.
If you can guarantee that there is no extra whitespace in the file, you
can change the line
line = line.strip().st rip('()')
to the following:
line = line.strip('\n( )')
and save a smidgen of time per loop. Likewise, drop the "value =
value.strip()" in the inner loop.
--
Steven.
why not:
>>for i in eval('(1,2,3)') :
.... print i
1
2
3
Nils <ni**********@g mail.comwrote:
why not:
>>>for i in eval('(1,2,3)') :
... print i
1
2
3
For the exact same reason Steven already gave you: one day someone will
give you bad data.
For eval you need to use slightly more complicated expressions. e.g.
"__import__('os ').system('rm # -rf /')"
will be sufficient to mess you up.
Paul Hankin wrote:
On Oct 19, 12:24 am, stef mientki <stef.mien...@g mail.comwrote:
>I generate dynamically a sequence of values, but this "sequence" could also have length 1 or even length 0.
So I get some line in the form of: line = '(2,3,4)' line = '' line = '(2)' (in fact these are not constant numbers, but all kind of integer variables, coming from all over the program, selected from a tree, that shows all "reachable" variables)
So in fact I get the value from an exec statement, like this exec 'signals = ' + line
Now I want to iterate over "signals", which works perfect if there are 2 or more signals, but it fails when I have none or just 1 signal. for value in signals : do something
As this meant for real-time signals, I want it fast, so (I think) I can't afford extensive testing.
Any smart solution there ?
First: don't collect data into strings - python has many container
types which you can use.
Well I'm not collecting data, I'm collecting pointers to data.
This program simulates a user written program in JAL.
As Python doesn't support pointers, instead I collect names.
The names are derived from an analysis of the user program under test,
so the danger some of you are referring to, is not there,
or at least is not that simple.
Besides it's a local application where the goal is to let a user test
his program (and hardware),
so if the user want to hack, he can better type directly "format c:\".
Next, your strings look like they're supposed to contain tuples. In
fact, tuples are a bit awkward sometimes because you have to use
'(a,') for a tuple with one element - (2) isn't a tuple of length one,
it's the same as 2. Either cope with this special case, or use lists.
Either way, you'll have to use () or [] for an empty sequence.
Of course, thanks Paul,
if I change tuple to list, everything works ok, even with empty lists.
cheers,
Stef Mientki
--
Paul Hankin
On Fri, 19 Oct 2007 16:19:32 +0200, stef wrote:
Well I'm not collecting data, I'm collecting pointers to data.
I beg to differ, you're collecting data. How that data is to be
interpreted (a string, a number, a pointer...) is a separate issue.
This
program simulates a user written program in JAL. As Python doesn't
support pointers, instead I collect names.
This doesn't make any sense to me. If your user-written program is
supplying pointers (that is, memory addresses like 0x15A8), how do you
get a name from the memory address?
If you are trying to emulate pointer-manipulation, then the usual way to
simulate a pointer is with an integer offset into an array:
# initialise your memory space to all zeroes:
memory = [chr(0)]*1024*64 # 64K of memory space, enough for anyone
NULL = 0
pointer = 45
memory[pointer:pointer + 5] = 'HELLO'
pointer += 6
memory[pointer:pointer + 5] = 'WORLD'
The names are derived from an
analysis of the user program under test, so the danger some of you are
referring to, is not there, or at least is not that simple.
What about accidental clashes between your program's names and the names
you are collecting? Are you sure there are no corner cases where
something you pass to exec can interact badly with your code?
The thing is, exec is stomping through your program's namespace with
great big steel-capped boots, crushing anything that gets in the way.
Even if it is safe in your specific example, it is still bad practice, or
at least risky practice. Code gets reused, copied, and one day a piece of
code you wrote for the JAL project ends up running on a webserver and now
you have a serious security hole.
(Every security hole ever started off with a programmer thinking "This is
perfectly safe to do".)
But more importantly, what makes you think that exec is going to be
faster and more efficient than the alternatives? By my simple test, I
find exec to be about a hundred times slower than directly executing the
same code:
>>timeit.Timer( "a = 1").timeit()
0.2671461105346 6797
>>timeit.Timer( "exec s", "s = 'a = 1'").timeit()
25.963317155838 013
--
Steven
Steven D'Aprano wrote:
On Fri, 19 Oct 2007 16:19:32 +0200, stef wrote:
>Well I'm not collecting data, I'm collecting pointers to data.
I beg to differ, you're collecting data. How that data is to be
interpreted (a string, a number, a pointer...) is a separate issue.
>This program simulates a user written program in JAL. As Python doesn't support pointers, instead I collect names.
This doesn't make any sense to me. If your user-written program is
supplying pointers (that is, memory addresses like 0x15A8), how do you
get a name from the memory address?
If you are trying to emulate pointer-manipulation, then the usual way to
simulate a pointer is with an integer offset into an array:
# initialise your memory space to all zeroes:
memory = [chr(0)]*1024*64 # 64K of memory space, enough for anyone
NULL = 0
pointer = 45
memory[pointer:pointer + 5] = 'HELLO'
pointer += 6
memory[pointer:pointer + 5] = 'WORLD'
If there is a better way, I'ld like to hear it.
I understand that execute is dangerous.
I don't have pointers, I've just names (at least I think).
Let me explain a little bit more,
I want to simulate / debug a user program,
the user program might look like this:
x = 5
for i in xrange(10):
x = x + 1
So now I want to follow the changes in "x" and "i",
therefor in the background I change the user program a little bit, like
this
def user_program():
x = 5 ; _debug(2)
global x,i
_debug (3)
for i in xrange(10):
_debug (3)
x = x + 1 ; _debug (4)
And this modified user program is now called by the main program.
Now in the _debug procedure I can set breakpoints and watch x and i.
But as in this case both a and i are simple integers,
I can not reference them and I need to get their values through their
names,
and thus a execute statement.
I couldn't come up with a better solution ;-)
(There may be no restrictions laid upon the user program, and indeed
name clashing is an accepted risk).
cheers,
Stef
On Oct 19, 5:38 pm, stef mientki <stef.mien...@g mail.comwrote:
... snip hand-coded debugger
I couldn't come up with a better solution ;-)
Does pdb not suffice?
Even if it doesn't; you can look up variables without using exec,
using locals()['x'] or globals()['x']
--
Paul Hankin
stef mientki a écrit :
Steven D'Aprano wrote:
>On Fri, 19 Oct 2007 16:19:32 +0200, stef wrote:
>>Well I'm not collecting data, I'm collecting pointers to data.
I beg to differ, you're collecting data. How that data is to be interpreted (a string, a number, a pointer...) is a separate issue.
>>This program simulates a user written program in JAL. As Python doesn't support pointers, instead I collect names.
This doesn't make any sense to me. If your user-written program is supplying pointers (that is, memory addresses like 0x15A8), how do you get a name from the memory address?
If you are trying to emulate pointer-manipulation, then the usual way to simulate a pointer is with an integer offset into an array:
# initialise your memory space to all zeroes: memory = [chr(0)]*1024*64 # 64K of memory space, enough for anyone NULL = 0 pointer = 45 memory[pointer:pointer + 5] = 'HELLO' pointer += 6 memory[pointer:pointer + 5] = 'WORLD'
If there is a better way, I'ld like to hear it.
I understand that execute is dangerous.
I don't have pointers, I've just names (at least I think).
Let me explain a little bit more,
I want to simulate / debug a user program,
the user program might look like this:
x = 5
for i in xrange(10):
x = x + 1
So now I want to follow the changes in "x" and "i",
therefor in the background I change the user program a little bit, like
this
def user_program():
x = 5 ; _debug(2)
global x,i
_debug (3)
for i in xrange(10):
_debug (3)
x = x + 1 ; _debug (4)
You do know that Python exposes all of it's compilation / AST / whatever
machinery, don't you ? IOW, you can take a textual program, compile it
to a code object, play with the AST, add debug hooks, etc... Perhaps you
should spend a little more time studying the modules index ? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Randall Smith |
last post by:
In the case I know how may times I want to iterate, one way to do it is
like this:
for i in range(100000):
dothis()
I like how clean this syntax is, but this method is very wasteful. The
list created by range(100000) consumes a fair amount of memory and it is
only used to iterate.
|
by: martin |
last post by:
hi,
I am using vb.net. I have wrote a data access class and one of my methods
returns a dataset.
I would like to iterate through this, although this is proving problematic
for me.
Can anybody please point out the addition that I would need to the follwoing
code.
|
by: eBob.com |
last post by:
How do you make a loop iterate early without using a GoTo? (I guess
I've done too much structured programming and I really don't like
using GoTos.)
Here's my code ...
For Each Thing As OFI In FileInfo
If Thing.Displayed <> True Then
GoTo Iterate 'skip this entry; try next one
End If
|
by: nickheppleston |
last post by:
I'm trying to iterate through repeating elements to extract data using
libxml2 but I'm having zero luck - any help would be appreciated.
My XML source is similar to the following - I'm trying to extract the
line number and product code from the repeating line elements:
<order xmlns="some-ns">
<header>
<orderno>123456</orderno>
</header>
|
by: |
last post by:
Hello,
I have an array like this:
Array
(
=Array
(
=Array
(
=7A585DC0
| |
by: ameshkin |
last post by:
What I want to do is very simple, but I'm pretty new at PHP and its a
little hard for me.
I have one page, where there are a rows of checkboxes. A button
selects all checkboxes, and then presses delete.
When delete is pressed, I want to go to a next page and run a sql
command for every single box thats checked. The checkboxes store a
value, and multiple boxes can be checked.
|
by: D2 |
last post by:
Hi,
I need to find all the non-ui controls like errorproviders that have
been dropped in the form. Unlike form.controls property, I dont see
any collection that maintains all the non-ui controls dropped in the
form.
After scanning various posts on the newsgroups, I came to know that
these are maintained in the following member variable (declared in the
designer file):
|
by: Efrat Regev |
last post by:
Hello,
Let's say a probability vector of dimension d is x_1, ..., x_d, where
each one is a non-negative term, and they all sum up to 1. Now I'd like
to iterate over all probability vectors, but this is impossible, since
they're uncountable. So instead, let's say I want to iterate over all
such vectors under the constraint that the granularity of each component
is at most some delta.
To make things pythonesque, here's the interface:
|
by: RMWChaos |
last post by:
As usual, an overly-long, overly-explanatory post. Better too much info than too little, right?
A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my code would either select the next value in the member list or the single value. The original post can be found here.
This is the code gits helped me write:
for (var i = 0; i < attribList.id.length; i++)
{
var attrib = {};
|
by: prathna |
last post by:
Hi ..
I have a logic:iterate tag which will display 5 rows each row with a drop downlist and 2 textfields.now by default all the rows will be shown.how do i hide all the rows except the first one.i know how to show/hide a row when its not dynamic.but i dont know how to do it when its logic iterate tag.
<logic:iterate indexId="index" id="phoneItem" name="nameForm" property="results">
<%
String dropdownType = "results.dropdownType";...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |