[Tutor] Borrowing restricted code

Avi Gross avigross at verizon.net
Wed Dec 5 11:22:35 EST 2018


I don't want to start (and continue) another sideways discussion. Alan and
Steven (not Stephen) both made very good points and helped clarify my
understanding.

I am NOT advocating copying code. Not even "free" code.

I am saying there may be times you want to package the code for special
purposes.

Perhaps someone can enlighten me on a subtle aspect here.

What does it mean to include something?

If we return to the days of yesteryear, albeit much of it exists this year,
a major programming paradigm was to compile a language like C or PASCAL into
some machine language or a binary form. But you only wrote a part of the
final program and inserted parts from what had been written, mainly, by
others.

One inclusion in languages like C was variants of lines like this:

#include "stdio.h"

What would happen was when a phase of the compile process read your file and
saw a request to include, it would pause, try to find the file in the usual
places, and then copy the text and continue. This could be recursive if that
file included another. It is in one sense similar to how "import" works in
python or "library" in R and similar features in other languages.

What was different was that one large file was created which was a flattened
version of this tree of code. Another pass of the compiler would then
process it. Nothing was RUN until later and only if there were no
compilation errors. Along the way, another form of inclusion had to happen.
If your code (directly or indirectly) called functions not defined in your
own code, and not already compiled in, it had to search assorted libraries
of such code, find the segments needed, and link them in. Eventually, it got
even worse and shared libraries would be mounted in memory and when your
code ran, it would link in what was needed.

The above is not meant to be a precise description but indicates that your
code was rarely all your own. You could literally set it up so if someone
else left their code unguarded, you could arrange to grab text or library
code into your finished executable.

In python, you can generally have access to the python code of what you can
import or at least to the byte code. But there may be different rules
attached to several categories.

If you use "import" you don't so much copy as USE the code. I mean the
interpreter pauses evaluating the current file and opens the one you asked
for and reads it as if it were your code. Some actions are transient. A line
like "5+3" evaluates to 8 and you ignore it. But many and perhaps most lines
end up creating instantiations of objects (yes, classes are also objects)
which then sit in memory. Functions are also objects and pretty much contain
within them everything you need to reconstruct the function if you know
where to look. Is what is copied really different than the original text
used? 

If you import the base modules that come with python, can you assume it is
freely given with few strings other than the kind discussed? If it is
outside but available from a standard download location, perhaps you need to
see the documentation for each item. If it is sold to you, I suspect you
need to be very careful about including any.

Back to the main topic, for me. What kind of uses might be considered legal
or at least not worth prosecuting? The law in various countries likely
differs, Alan and I are definitely using different legal systems but
possibly not as different as where Asad is. (His phone number would be in
India.) Clearly if you make a product and sell it, then borrowing code can
be a serious thing. If you are a student doing a homework assignment and
will never use it again, few might care especially if you use small amounts
and don't claim it is your own work.

But the question I would like answered is DOES IT MAKE A DIFFERENCE how you
borrow the use of the code? 

We talked about the normal intended method:

Load the file into a place on your local machine.
Write one or more python files and one of them imports it.

A second method I suggested WHEN NEEDED AND ALLOWED is to find the code you
need, perhaps on another machine where you can download it, or in some other
public repository. Copy the minimum you need into your own code, with
perhaps enough attribution and explanation.

Now what about a third way? No edit needed. Simply use a program like "cat"
to concatenate multiple files including the one with your code into a bigger
lump. Feed that to the python interpreter.  Included would be the code you
want. True, the code would now all be in one namespace. But is this form of
copying all that much different than was intended? 

I am not going to continue in any further discussion. But I do think this
could be useful to people learning python as often the best way to find out
how to do something is to study the code of someone who did it. You might
want to play with it and see if you can improve it or vary some part ...

So it would be good to know what can be done legally and what to avoid.

As an aside, I note that in C, the included files had no concept of a
namespace so for efficiency, it could make sense to gather all your code
into a simple file. In python, there may be non-trivial overhead in
searching for all the files to import and opening and evaluating each. But I
can still see how putting every single function or class definition in
another file in some huge tree may be overkill and make for a slow runtime.
If you end up importing "*" all the time so it is all in your global
namespace, why not bring them into the main program. I am, of course,
talking about your own code.









More information about the Tutor mailing list