Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Two questions

Reply
Thread Tools

Two questions

 
 
qscomputing@gmail.com
Guest
Posts: n/a
 
      06-02-2005
Hi,

I've developed in several other languages and have recently found
Python and I'm trying to use it in the shape of the PythonCard
application development tool.

My two questions:

1. What is the easiest way to create a for loop in the style I'm used
to from Delphi ie:
for I:=0 to 2 do begin
//code
end;

2. Philospohy(sp?) aside, I could potentially want to create a
binary-only distribution of my finished apps. I noticed the
documentation on .pyc files: how do I create these and, aside from
being basically read-only, are they used just like ordinary .py source
files? And can they be easily reverse-engineered?

Thanks,
- QS Computing.

 
Reply With Quote
 
 
 
 
Richard Lewis
Guest
Posts: n/a
 
      06-02-2005

On 2 Jun 2005 06:45:18 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) said:
> Hi,
>
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
>
> My two questions:
>
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
> //code
> end;
>

for i in range(0, 2):
do stuff

The range([start], stop, [step]) function generates a sequence of
numbers which the the for loop iterates over.

(You can also use xrange() for a more memory efficient solution for very
large ranges).

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?
>

To create binary only distributions for Windows you can use py2exe. Its
distributions files can be fairly easily reverse engineered.

Cheers,
Richard
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
>
> My two questions:
>
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
> //code
> end;


for i in xrange(0, 3):
# code

Please read the tutorial. I'm fairly sure this and many more things
you'll want to know are covered adequately.

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?


They are compiled versions of the .py files, so definitely not the same.
They are created automatically and transparently when you import .py
modules, so normally you don't pay any attention to them. They can
easily be reverse-engineered, if by that you mean turned back into
source code. See "decompyle" for example. Using the "compileall"
module you can manually compile .py to .pyc but, again, that's generally
not needed. Use of tools like py2exe is generally advised for packaging
and distibution if you don't want to distribute source, though few of
the existing tools do much more than package up .pyc files inside
archives, bundle the runtime library, and add wrapper code to make the
execution transparent.

Philosophy not entirely aside, you should note that object code in any
language can "easily" be reverse-engineered in the same way, with the
only difference being the degree of ease involved. If the code is worth
enough to someone that they are willing to risk violating your license
terms, they *will* be able to recover enough source code (whether it was
Python, C, or assembly) to do what they need. The only certain
protection is to keep the valuable code on a server and use some kind of
web service (or whatever) to control access to its execution. (There
have been *many* past discussions of all this in the forum -- it's a
very tired topic by now -- so please feel free to peruse the archives
via Google Groups before asking lots of the same questions over again.
You'll be doing yourself a favour.)

-Peter
 
Reply With Quote
 
Joal Heagney
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
> Hi,
>
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
>
> My two questions:
>
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
> //code
> end;


Um, assuming that this loops through the numbers 0 to 2 and assigns them
to the variable I, and then does something in code with I after it's
been assigned, the python equivalent is:

for I in range(0,3):
//code

(Note the whitespace after opening the for loop?)
And then break the indenting to finish the for loop. So you're next
piece of code (Whatever you had after end would go here:

//morecode.

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?


As long as you have write access to the directory that you're .py files
are in, when you run python, it will generate the .pyc files for you as
they are loaded.
There is also a utility script in the main distribution called
py_compile.py.
E.g. compiling a whole directory of .py files:

python /path/to/main/install/py_compile.py *.py

And to compile them as optimised binary files (.pyo):
python -O /path/to/main/install/py_compile.py *.py

They are used like ordinary .py source files. (Python actually executes
from the .pyc files it builds from your .py files.)
They can be reverse-engineered, but then so can Java/C++/Assembler. Have
a look through the group for something about being able to distribute
your modules.pyc as a zipfile - I remember something about being able to
do a -tiny- bit of extra protection by having them as a passworded zip file.

> Thanks,
> - QS Computing.
>


Welcome.

Joal
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      06-02-2005
Peter Hansen wrote:
> Philosophy not entirely aside, you should note that object code in any
> language can "easily" be reverse-engineered in the same way, with the
> only difference being the degree of ease involved. If the code is worth
> enough to someone that they are willing to risk violating your license
> terms, they *will* be able to recover enough source code (whether it was
> Python, C, or assembly) to do what they need.


Don't intend to hijack this thread, but this bit interests me. I know
several accomplished C/assembly programmers who have told me that
reverse engineering object code from either of these two languages is
anything but trivial. Yet, I *hear* and *read* the opposite all of the
time. Can anyone actually demonstrate a decompile that mirrors the
original source?

Also, I'd venture to say that the number of people in the world who can
consistently reverse engineer object code is almost statistically
insignificant... sure, they are out there, but you'll win the lottery
before you meet one of them and most of them work for big, bad
government agencies

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-02-2005
rbt wrote:
> Peter Hansen wrote:
>
>> Philosophy not entirely aside, you should note that object code in any
>> language can "easily" be reverse-engineered in the same way, with the
>> only difference being the degree of ease involved. If the code is
>> worth enough to someone that they are willing to risk violating your
>> license terms, they *will* be able to recover enough source code
>> (whether it was Python, C, or assembly) to do what they need.

>
>
> Don't intend to hijack this thread, but this bit interests me. I know
> several accomplished C/assembly programmers who have told me that
> reverse engineering object code from either of these two languages is
> anything but trivial. Yet, I *hear* and *read* the opposite all of the
> time. Can anyone actually demonstrate a decompile that mirrors the
> original source?


I give you one example: Online/Multiplayer GTA 3 or 4
(http://gta3mta.tk/)

A C-App never intended to work that way - but skillfully patched so that
it works! And that even only as OSS - no commercial interest (and thus
funding). So I day Peter's statement has full validity - it's a question
of interest.

Diez
 
Reply With Quote
 
Reinhold Birkenfeld
Guest
Posts: n/a
 
      06-02-2005
Richard Lewis wrote:
> On 2 Jun 2005 06:45:18 -0700, (E-Mail Removed) said:
>> Hi,
>>
>> I've developed in several other languages and have recently found
>> Python and I'm trying to use it in the shape of the PythonCard
>> application development tool.
>>
>> My two questions:
>>
>> 1. What is the easiest way to create a for loop in the style I'm used
>> to from Delphi ie:
>> for I:=0 to 2 do begin
>> //code
>> end;
>>

> for i in range(0, 2):
> do stuff


Eh, no. range(0, 3) would be correct, since the Python range function
generates a list from start to stop-1.

> The range([start], stop, [step]) function generates a sequence of
> numbers which the the for loop iterates over.
>
> (You can also use xrange() for a more memory efficient solution for very
> large ranges).
>
>> 2. Philospohy(sp?) aside, I could potentially want to create a
>> binary-only distribution of my finished apps. I noticed the
>> documentation on .pyc files: how do I create these and, aside from
>> being basically read-only, are they used just like ordinary .py source
>> files? And can they be easily reverse-engineered?


They are a binary representation of bytecode, just like in Java. They can be
reverse-engineered more easily than machine code, but it still is no no-brainer.
Btw, they are created automatically...

Reinhold
 
Reply With Quote
 
qscomputing@gmail.com
Guest
Posts: n/a
 
      06-02-2005
Thanks to you all for the quick response.

I've noticed that when I do
$ python myprog.py
the file myprog.pyc file is not created, but the .pyc files for files I
import *are* created. Is this intentional and, if so, how do I get the
myprog.pyc file?

Thanks,
- QS Computing.

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
> Thanks to you all for the quick response.
>
> I've noticed that when I do
> $ python myprog.py
> the file myprog.pyc file is not created, but the .pyc files for files I
> import *are* created. Is this intentional and, if so, how do I get the
> myprog.pyc file?


I thought the docs covered this, so I left it out. The
"main" .py file is not converted to a .pyc file for reasons I can't
remember (and don't care... after it, that's just the way it is). If
you really need a .pyc for it, the simplest thing to do is "import
myprog" from the interactive prompt. The compileall module I mentioned
would also be able to do this.

-Peter
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-02-2005
rbt wrote:
> Peter Hansen wrote:
>> Philosophy not entirely aside, you should note that object code in any
>> language can "easily" be reverse-engineered in the same way, with the
>> only difference being the degree of ease involved. If the code is
>> worth enough to someone that they are willing to risk violating your
>> license terms, they *will* be able to recover enough source code
>> (whether it was Python, C, or assembly) to do what they need.

>
> Don't intend to hijack this thread, but this bit interests me. I know
> several accomplished C/assembly programmers who have told me that
> reverse engineering object code from either of these two languages is
> anything but trivial. Yet, I *hear* and *read* the opposite all of the
> time. Can anyone actually demonstrate a decompile that mirrors the
> original source?


It all depends on the definition of "reverse engineering".

In my opinion and experience, very little code in the world is so
sophisticated that it is not roughly as easy to write equivalent code
from scratch (with the original, working program as a guide of what the
code actually does) as it would be to convert the object code back into
source. (Exceptions such as "decompyle" which may make the job near
trivial aside.)

If that's true, it leaves us with a very small subset of the code in any
given program, that might actually be worth the effort of converting
back to source. That bit of code will generally turn out to be so small
that once again an automated conversion to source is not really
necessary, since analysis of the object code would with relatively
little effort allow one to "reverse engineer" some equivalent source, in
whatever language (or pseudo-code) one chose.

So, for languages like C, where the compilation process is fairly
"destructive" to things like the variable names and control structures
used, "reverse engineering" in the sense of "automated conversion back
to equivalent source code" is rather difficult, probably infeasibly so
for most non-trivial programs. I personally don't understand the need
for this, other than in the case of "I lost my source", and the correct
answer there is a session of pummelling, followed by tarring and
feathering with the pages of the Subversion Book.

On the other hand, "reverse engineering" in the sense of "creating
source code capable of reproducing the effects of the valuable and
interesting parts of the object code" is not that difficult, and in the
sense of "understanding how this code works" is even easier, being just
the first part of that step.

Software development is far more about choosing the right problems to
solve and the right ways to solve them than it is about writing source
code for the program that will do the job.

And if I had an automated tool to reproduce source code for a given
program, I'd still be very concerned that I didn't end up with any of
its automated test cases.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Two ISP -Two Routers - 1 PIX James Parks Cisco 5 12-11-2003 08:55 PM
Cisco 2611 with Two T1 from two ISPs Adam Embrey Cisco 3 07-24-2003 10:22 PM



Advertisments