[Tutor] who makes FOR loop quicker

2015-08-05 Thread John Doe
To pass by reference or by copy of - that is the question from hamlet. 
("hamlet" - a community of people smaller than a village python3.4-linux64)


xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1

So, catch the output and help, PLEASE, me improve the answer:
Does it appropriate ALWAYS reevaluate the terms of the list on each 
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from 
an output) and reduce unreasonable reevaluation, what I must to do for that?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-06 Thread John Doe
Can You, please, elaborate this "..Passing in Python is different than 
in C or other languages..."


'Cause as far as I know - default major Python's implementation CPython 
is written in C.





Joel Goldstick 於 08/05/2015 03:44 PM 寫道:

On Wed, Aug 5, 2015 at 3:53 AM, John Doe  wrote:

To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village
python3.4-linux64)

xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
 print(xlist)
 print("\txlist[%d] = %d" % (i, x))
 if x%2 == 0 :
 xlist.remove(x)
 print(xlist, "\n\n")
 i = i + 1

So, catch the output and help, PLEASE, me improve the answer:
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen
from an
output) and reduce unreasonable reevaluation, what I must to do for
that?


You aren't passing anything.  the for statement is in the same
namespace as the rest of the code.  Passing in python is different
than in C or other languages.

A couple of comments:

setting i = 0, then incrementing at the end of the loop would more
pythonically be done with the enumerate function.
Its generally a bad idea to remove items from and iterable while
interating over it.  I'm guessing that this is what is confusing you.
One way to remove items from a list is to create a new list, and
append items you want to it, skipping the ones you don't.  You don't
really need the index at all since python interation protocol will
walk through the list for you without worrying about index values


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-06 Thread John Doe
Well, I think, both of us understands that any reference isn't about any 
sort of a language. It's about REGISTER = [ALU, FPU, ...]


That's why reference inevitable.

While You're talking about Python - You're talking ONLY about 
interpreter for a BYTEcode

Alas, CPU don't speak BYTEcode but BITcode.

So, Python can't allocate memory for CPU only for interpreter, which 
will ask allocation through underlying-C-language.


Do I wrong?
CPU have compiler for Python?
As well as multithreading, for instance, in Python goes to a single 
interpreter, but in C - to multiple cores of CPU. So Python doesn't have 
REAL multithreading, but C - does.


And in my case by means of C-rules Python allocates FOR-loop's list as a 
reference. And that mistake wastes each iteration of FOR-loop in 
unnecessary RE-evaluation of initial-list IN LOGIC STATEMENT, which must 
be created only once. Any INITIATIONS make once. 'Cause it sucks 
CPU-memory-allocation-cycle.


Does this point make sense for You?

Joel Goldstick 於 08/06/2015 03:57 PM 寫道:

On Thu, Aug 6, 2015 at 4:34 AM, John Doe  wrote:

Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."


I hesitate, because this question is usually the fuel of flaming wars.
So in short:

C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)
Python passes an object -- everything in python is an object.  If the
object is mutable, and the function mutates it, those results will be
seen outside the function.  If the object is immutable, and the
function tries to change its value, a new object is created with the
new value.  Its name is the name given in the parameter list -- not
the name that the function was called with.  When the function
completes, that object is lost since the outer scoped named object
wasn't changed.


'Cause as far as I know - default major Python's implementation CPython is
written in C.


What language is used for its implementation has nothing to do with
its own specification.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-06 Thread John Doe

Thank You, Steven.

I've already written to Your colleague, so You will can see about.

And when I'm saying 'ALLOCATION' I keep in mind the REGISTER, not a 
glossary or thesaurus. Language is created for us, not for CPU.

Do You agree?

Passing VALUE is a time-expensive procedure. Python can't reach 
processor, so any Python's memory allocations don't have sense.


Language always was, is and will be the ONE - compiled bitcode.
Others - just syntax + specially-sphere-applied variations for Your 
pleasure.


Isn't it?

Steven D'Aprano 於 08/06/2015 04:45 PM 寫道:

On Thu, Aug 06, 2015 at 11:34:51AM +0300, John Doe wrote:


Can You, please, elaborate this "..Passing in Python is different than
in C or other languages..."


Argument passing in Python is:

- different to Perl, C, Scala, Algol and Pascal;

- the same as Ruby, Lua, Applescript and Javascript;

- the same as Java boxed values (object);

- different to Java unboxed values (machine types).


In C, all values are passed by value. When you pass an argument to a
function, the C compiler makes a copy of that value and passes the
value.

In Pascal, values can be passed by value (like C), or by reference.

The simplest demonstration of pass-by-reference is to write a "swap"
procedure. In Python terms:


# This does not actually work in Python.
def swap(a, b):
 tmp = a
 a = b
 b = tmp

x = 23
y = 42
swap(x, y)
print x, y  # prints 42 23

z = 19
swap(x, z)
print x, z  # prints 19 42


You *cannot* write a swap procedure like this in Python. The closest you
can do is write a function that returns the two values, then assign
them:

def swap(a, b):
 return b, a

x, y = swap(x, y)  # this works

but that is not pass by reference.

In Pascal, you can write such a swap procedure.

Scala and Algol use pass by name, and pass by value. This page explains
pass by name in Scala, and how it differs from pass by value:

http://alvinalexander.com/source-code/scala/simple-scala-call-name-example

In Java, unboxed values (not objects, low-level machine ints and floats)
are passed by value, like C.

Python, Ruby, Javascript, Lua, Java boxed values (objects), and many
other languages, all use the same passing style. This has a number of
names:

- pass by object;
- pass by sharing;
- pass by object sharing;

Some people (especially Ruby programmers) call it "pass by reference"
but that is wrong. Others (especially Java programmers) call it "call by
value, where the value is a reference" which is technically correct but
too long. Another name is "call by value/pass by reference", which is
just confusing.

See also:

https://en.wikipedia.org/wiki/Evaluation_strategy

In pass by object sharing, the argument is evaluated but *not* copied.
Since the argument is not copied, it is not pass-by-value. Inside the
function, you can modify the object, and since it is not a copy, the
original sees the changes. But *assignment* to the local variable inside
the function does not affect the caller's variable, so it is not
pass-by-reference.

To summarise:

Pass by value:
- Argument is copied? YES
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? NO

Pass by reference:
- Argument is copied? NO
- Assignment inside function affects original? YES
- Mutation of argument inside function affects original? YES

Pass by object sharing:
- Argument is copied? NO
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? YES




'Cause as far as I know - default major Python's implementation CPython
is written in C.


That is irrelevent. The argument passing strategy of a language is part
of the language itself, not the implementation language.

C does not allow variables to change type. But Python does. You cannot
do this in C:

x = 23  # x is an integer
x = "foo"  # and now it is a string

so clearly the behaviour of a programming language is not always the
same as that of the implementation language.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-06 Thread John Doe



Well...
Try this look. But I'm just a human and can make mistakes.:))

Passing value - allocates stack and creates NEW memory position.
Passing reference - makes stack pointer pointing to any position.
Dereference - makes stack pointer pointing to any position AND TAKES VALUE.

So, You can count how much in every case does CPU make steps.
And now add to this BIG ARRAY as input for calculation.

Always must keep in mind, that the NAME of variable exists for SCOPE of
You code, but VALUE - for CPU.

So, reference will be always, unless CPU have reached quantum-mechanics



Steven D'Aprano 於 08/06/2015 05:21 PM 寫道:

On Thu, Aug 06, 2015 at 08:57:34AM -0400, Joel Goldstick wrote:

On Thu, Aug 6, 2015 at 4:34 AM, John Doe  wrote:

Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."


I hesitate, because this question is usually the fuel of flaming wars.


Very wise :-)

But since I'm not so wise, here are some more comments.



So in short:

C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)


You are correct that C can pass a reference to a value, namely a
pointer. But from the perspective of the C compiler, that pointer *is*
the value, not the thing being pointed at. So passing a pointer as
argument is no different from passing an int or a float or a bool, it's
just a value, and the C compiler will use pass by value on the pointer
itself.

In C, one can use pointers to *simulate* pass by reference. But this is
not the same thing as actual pass by reference. In pass by reference,
you don't pass (a pointer to the variable you want), you pass (the
variable you want), and the compiler does all the magic needed to make
it work.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-06 Thread John Doe

Thank You, Alan.

This is THE FIRST time, when I've got a pleasure from the opponent.
You're maintain status of a thinking human and, as a humble DAOist, I 
always say THANK YOU, when I talk to such a Man.

'Cause wisdom bring us the beauty.

So, what else I can add.
Just a little bit.

It would be great to work with You.
You know, life is so shot..
And any talks don't make it better, alas.

Just we can do.



Alan Gauld 於 08/06/2015 09:54 PM 寫道:

On 06/08/15 14:28, John Doe wrote:

Well, I think, both of us understands that any reference isn't about any
sort of a language. It's about REGISTER = [ALU, FPU, ...]



No thats about the implementation.
The language and implemewntation are completely searate.
There can be many different implementations of a single
language and they all have to follow the semantics defined
by the language but are free to build those semantics
any way they like. (And indeed the different versions
of Python do just that!)


While You're talking about Python - You're talking ONLY about
interpreter for a BYTEcode
Alas, CPU don't speak BYTEcode but BITcode.


Some do. Some speak higher level things. For example
some CPUs speak Forth. And still others don't use binary
at all but use tri-state values. There werte even some
in the early days that used 4-state values. But these
are all implementation details that the programmer
doesn't need to care about.


So, Python can't allocate memory for CPU only for interpreter, which
will ask allocation through underlying-C-language.


Not necessarily. The interpreter could get C to allocate
a huge pool of memory at startup and then use that for
its own allocation/deallocation purposes.


CPU have compiler for Python?


Not yet but it is theoretically possible.
And python would not change if someone built one.


As well as multithreading, for instance, in Python goes to a single
interpreter, but in C - to multiple cores of CPU.


That depends on the implementation. I have a C compiler
that does not do multi-core/thread working. It's an
implementation detail and the C language does not
specify that it must. It all depends on the code that
the compiler generates.


 So Python doesn't have
REAL multithreading, but C - does.


Python does. But not all of its implementations do.
The emulate it instead.  But so far as the programmer is
concerned his code is using threading/concurrency.
He may need to be aware that the implementation is not
honoring his request fully but that doesn't change his
code.


And in my case by means of C-rules Python allocates FOR-loop's list as a
reference.


No, the C implementation might do that. Python as a language
does not. High level languages exist to stop us thinking about
the low level details. The implementation may change, that's not our
problem. Python specifies how the Python execution model works
not how the CPU or the assembler, or the compiler or the
implementer interprets that.

Even C has many different implementations. Some are more efficient
than others. Should we be worrying about which C compiler was used
to build our interpreter? Should we care about whether the CPU
implements multiplication in hardware or in microcode? Or whether it
caches local variables on on-chip cache or uses main memory?

And what about the I/O routines. Do we need to worry about
whether our chosen C compiler is using it's own I/O library,
or calling the BIOS directly?  or using the OS system calls?
These are all implementation details that regular
programmers can, and should, ignore.


And that mistake wastes each iteration of FOR-loop in
unnecessary RE-evaluation of initial-list IN LOGIC STATEMENT, which must
be created only once. Any INITIATIONS make once. 'Cause it sucks
CPU-memory-allocation-cycle.


In the modern world of fast CPUs and memory and where the vast majority
of applications run in virtual machines(JVM, .Net) and the vast majority
of servers run inside virtualized environments (VMWare etc)
none of that is of the slightest concern to me.

If I was writing code for an embedded system it might be more worrisome,
but then I'd probably not be using Python for that.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread John Doe
Hello List,
I am in need of your assistance. I have a text file with random words
in it. I want to write all the lines to a new file. Additionally, I am
using Python 2.7 on Ubuntu 12.04:

Here is my code:

def loop_extract():
with open('words.txt', 'r') as f:
for lines in f:
#print lines (I confirmed that each line is successfully printed)
with open('export.txt', 'w') as outf:
outf.write(lines)
#outf.write(lines)
#outf.write('{}\n'.format(lines))
#outf.write('{}\n'.format(line for line in lines))


For some reason, the second file only contains the last line from the
original file -- I have tried multiple variations (.read, .readlines,
.writelines, other examples preceded by comment from above and many
more) and tried to use the module, fileinput, but I still get the same
results.

I do understand there is another way to copy the file over, but to
provide additional background information on my purpose -- I want to
read a file and save successful regex matches to a file; exporting
specific data. There doesn't appear to be anything wrong with my
expression as it prints the expected results without failure. I then
decided to just write the export function by itself in its basic form,
per the code above, which the same behavior occurred; only copying the
last line. I've googled for hours and, unfortunately, at loss.

Thank you in advance for your help!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing all text that begins with "25"

2014-10-02 Thread John Doe
Hello,

If you want to accomplish what you are looking for within linux
(perhaps a bash script, instead?):

$ hamachi list | grep -oP '25\.\d+\.\d+\.\d+'
25.0.0.0
25.255.255.255

For your python script, you want to group your regex:
reg = re.compile(r'(25\.\d+\.\d+\.\d+)', re.MULTILINE)

So when you call group(1) or group(0), it'll grab just the addresses.


On Thu, Oct 2, 2014 at 12:33 PM, David Rock  wrote:
> * Bo Morris  [2014-10-02 11:41]:
>> Hello all, hope everyone is doing well.
>>
>> When I run the linux command "hamachi list" i get something along the lines
>> of the following output
>>
>>087-888-279   Pandora25.x.x.xxx   alias: not 
>> set
>>096-779-867   AM1LaptopBD-PC25.x.x.xxx   alias: not set
>>097-552-220   OWS-Desktop 125.0.0.0  alias: not set
>>099-213-641   DESKTOP 25.0.0.0  alias: not set
>>
>> I am trying to write a python script that will run the above command and
>> only print out the IP's that begin with 25. How do I strip out all other
>> text except for the IP's that begin with "25?"
>
> There are a few assumptions that need to be made, for starters.
>
> Is the format always the same (ie, is the IP address always in column 3
> separated by whitespace)?  Looking at the line with "OWS-Desktop 1", the
> answer is no.  That complicates things a bit.  If it was true, you could
> use the string split method to get column 3.  Maybe the fields are
> separated by a tab?
>
> A regex may be possible, but you will have similar issues to using
> split.
>
> I'm also assuming it's possible for there to be IP addresses that do not
> start with 25. Are you looking to isolate those?
>
> It's not necessary to write out to a file first.  You can get the output
> from commands and work on it directly.
>
> Another approach would be to change the command you are running.  I've
> never heard of hamachi list before; does it have any commandline options
> to display only IP addresses?
>
> --
> David Rock
> da...@graniteweb.com
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread John Doe
Alan, Peter, et al:

Thank you all very much! Staring at this problem for hours was driving
me crazy and I am very appreciative for your guys' time in looking
into my silly error -- I have thoroughly reviewed both the responses
and it makes perfect sense (*sigh of relief*).



On Thu, Oct 2, 2014 at 6:08 PM, Peter Otten <__pete...@web.de> wrote:
> John Doe wrote:
>
>> Hello List,
>> I am in need of your assistance. I have a text file with random words
>> in it. I want to write all the lines to a new file. Additionally, I am
>> using Python 2.7 on Ubuntu 12.04:
>>
>> Here is my code:
>>
>> def loop_extract():
>> with open('words.txt', 'r') as f:
>> for lines in f:
>
> The name `lines` is misleading, you are reading one line at a time.
>
>> #print lines (I confirmed that each line is successfully
>> #printed)
>> with open('export.txt', 'w') as outf:
>> outf.write(lines)
>> #outf.write(lines)
>> #outf.write('{}\n'.format(lines))
>> #outf.write('{}\n'.format(line for line in lines))
>>
>>
>> For some reason, the second file only contains the last line from the
>> original file -- I have tried multiple variations (.read, .readlines,
>> .writelines, other examples preceded by comment from above and many
>> more) and tried to use the module, fileinput, but I still get the same
>> results.
>
> Every time the line
>
>> with open('export.txt', 'w') as outf:
>
> is executed the file "export.txt" is truncated:
>
> https://docs.python.org/dev/library/functions.html#open
>
> To avoid the loss of data open the file once, outside the loop:
>
> with open("words.txt") as infile, open("export.txt", "w") as outfile:
> for line in infile:
> outfile.write(line)
>
>
>> I do understand there is another way to copy the file over, but to
>> provide additional background information on my purpose -- I want to
>> read a file and save successful regex matches to a file; exporting
>> specific data. There doesn't appear to be anything wrong with my
>> expression as it prints the expected results without failure. I then
>> decided to just write the export function by itself in its basic form,
>> per the code above, which the same behavior occurred;
>
> That is a good approach! Reduce the code until only the source of the
> problem is left.
>
>> only copying the
>> last line. I've googled for hours and, unfortunately, at loss.
>
> I do that too, but not "for hours" ;)
>
>> I want to read a file and save successful regex matches to a file;
>> exporting specific data.
>
> An experienced user of Python might approach this scenario with a generator:
>
> def process_lines(infile):
> for line in infile:
> line = process(line) # your line processing
> if meets_condition(line): # your filter condition
> yield line
>
> with open("words.txt") as infile:
> with open("export.txt", "w") as outfile:
> outfile.writelines(
> process_lines(infile))
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor