Re: [Tutor] beginning to code

2017-09-23 Thread Bill

Steve D'Aprano wrote:

On Sun, 24 Sep 2017 08:18 am, Bill wrote:


All one has to do, I think, is consider (1) that passing objects by
"making copies" of them, would be prohibitively expensive


Swift passes certain values (but not others!) by value and makes a copy. That
includes many potentially large data types including strings, dictionaries and
arrays, but using copy-on-write so the data isn't physically copied until you
actually mutate it. From the Swift documentation:


 The description above refers to the “copying” of strings, arrays,
 and dictionaries. The behavior you see in your code will always
 be as if a copy took place. However, Swift only performs an actual
 copy behind the scenes when it is absolutely necessary to do so.
 Swift manages all value copying to ensure optimal performance, and
 you should not avoid assignment to try to preempt this optimization.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html


So I maintain that one could design a language similar to Python except that
objects are assigned and passed by value, making copies only when actually
needed using copy-on-write. Swift is *almost* that language: the difference is
that Swift distinguishes between "structs" that are copied, and "objects" which
are not.



and consider
that something else has to happen as an alternative, and (2) understand
that in Python, objects don't have names, they have references (which
have names).  The rest could be "implementation dependent" (no?)

No.

There are many different alternatives for "something else", and while some of
them may be behave the same in some circumstances, they do not behave the same
in all circumstances. Any interpreter calling itself Python would be expected
to match the behaviour of the reference implementation, CPython.

For example, if I made "Pass-By-Reference Python" where all argument passing was
done by reference, my language would differ from real Python:


function(x, y)  # allowed
function(namespace.x, module.y)  # allowed
function(x + 1, 2)  # FORBIDDEN: can't pass expressions or constants


This would be okay as long as x + 1 evaluates to an object, no?




Obviously that's not Python!

On the other hand, "Pass-By-Name Python" would allow passing expressions and
constants, but will differ in other ways.

Assignment by reference would mean that name binding was an *alias* operation:


module.y = 1
x = module.y  # x is an alias for the name "module.y"
x = 2  # binds 2 to module.y
assert module.y == 2






--
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Steve D'Aprano
On Sun, 24 Sep 2017 08:18 am, Bill wrote:

> All one has to do, I think, is consider (1) that passing objects by
> "making copies" of them, would be prohibitively expensive 


Swift passes certain values (but not others!) by value and makes a copy. That
includes many potentially large data types including strings, dictionaries and
arrays, but using copy-on-write so the data isn't physically copied until you
actually mutate it. From the Swift documentation:


The description above refers to the “copying” of strings, arrays,
and dictionaries. The behavior you see in your code will always
be as if a copy took place. However, Swift only performs an actual
copy behind the scenes when it is absolutely necessary to do so.
Swift manages all value copying to ensure optimal performance, and
you should not avoid assignment to try to preempt this optimization.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html


So I maintain that one could design a language similar to Python except that
objects are assigned and passed by value, making copies only when actually
needed using copy-on-write. Swift is *almost* that language: the difference is
that Swift distinguishes between "structs" that are copied, and "objects" which
are not.


> and consider 
> that something else has to happen as an alternative, and (2) understand
> that in Python, objects don't have names, they have references (which
> have names).  The rest could be "implementation dependent" (no?)

No.

There are many different alternatives for "something else", and while some of
them may be behave the same in some circumstances, they do not behave the same
in all circumstances. Any interpreter calling itself Python would be expected
to match the behaviour of the reference implementation, CPython.

For example, if I made "Pass-By-Reference Python" where all argument passing was
done by reference, my language would differ from real Python:


function(x, y)  # allowed
function(namespace.x, module.y)  # allowed
function(x + 1, 2)  # FORBIDDEN: can't pass expressions or constants


Obviously that's not Python!

On the other hand, "Pass-By-Name Python" would allow passing expressions and
constants, but will differ in other ways.

Assignment by reference would mean that name binding was an *alias* operation:


module.y = 1
x = module.y  # x is an alias for the name "module.y"
x = 2  # binds 2 to module.y
assert module.y == 2




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Steve D'Aprano
On Sun, 24 Sep 2017 07:03 am, ROGER GRAYDON CHRISTMAN wrote:

> I usually do not encourage people to optimize correctness out of their code.


+1 quote of the week :-)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Tim Chase
On 2017-09-23 19:14, Chris Angelico wrote:
> On Sat, Sep 23, 2017 at 7:07 PM, Kryptxy 
> wrote:
> > Thank you all! I opened a ticket about the same (on github).
> > I got response from most of them, and all are agreeing to the
> > change. However, one contributor did not respond at all. I tried
> > e-mailing, but no response.
> > Can I still proceed changing the licence? It has been more than a
> > week since the ticket was opened.  
> 
> Nope. Contributions made under the GPL have a guarantee that they
> will only and forever be used in open source projects. You're
> trying to weaken that guarantee, so you have to get clear
> permission from everyone involved.
> 
> Unless you can show that the contributions in question are so
> trivial that there's no code that can be pinpointed as that
> person's, or you replace all that person's code, you can't proceed
> to relicense it without permission.

Alternatively, you can rip out that contributor's code and re-code it
from scratch in a clean-room without consulting their code.  Then
their code is under their license while your re-implementation code
is under whatever license you like.

If their contributions were minor, this might be a nice route to go.
If they were a major contributor, you could be looking at a LOT of
work.

But those are your options:

- keep the project as GPL

- get *ALL* contributors to formally agree to the license change,

- confirm that the contributions of recalcitrant contributor(s)
  are limited to trivial changes, or

- recreate all GPL code in a clean-room under your own license


-tkc




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Bill

Chris Angelico wrote:

On Sun, Sep 24, 2017 at 8:18 AM, Bill  wrote:

Stephan Houben wrote:

Op 2017-09-23, Rick Johnson schreef :

These pissing contests over how values are passed in Python
are totally irrelevant. What does it matter? Nothing will be
gained or lost by arguing over which is true, or not. Unless
the distinction is preventing you from doing something that
you'd like to do, or unless you want to argue that one
"value passing method" would bring X, Y or Z benefits over
the other, what does it matter?

Amen.


All one has to do, I think, is consider (1) that passing objects by "making
copies" of them, would be prohibitively expensive and consider that
something else has to happen as an alternative, and (2) understand that in
Python, objects don't have names, they have references (which have names).
The rest could be "implementation dependent" (no?)   To be amusing, how did
the chicken pass an egg to the_other_side_of_the_road(e)?  Could the egg get
crushed (stay tuned)?

Actually they don't "have" references in any real sense of possession.
I agree (I was a bit hasty in my choice of words); but if they didn't 
"have" these references, it would be difficult, though not impossible, 
to refer to them.   Also keep in mind that the garbage collector keeps 
track, generally, of how many there are for each object!   So from the 
gc's point of view, objects (definitely) "have" references.  Next, 
what will Egg.__del__() do? : )





An object "has" things like its type, its attributes, etc etc; if you
have a reference to an object, you can query it for its type. But you
can't ask an object if there's a reference to it over here or there.
(Yes, I know that sys.getrefcount exists in CPython, but this isn't
part of the language's definition. Also, even that is just a counter -
you can't find specific references.) An object may have a reference to
other objects (eg a list's contents), but it's a one-way thing -
there's no way to find all the references to this object.

So more accurate would be to say that objects don't have names, but
names refer to objects. When you assign to a simple name, you cause
that name to refer to the object you gave it.

ChrisA


--
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Chris Angelico
On Sun, Sep 24, 2017 at 8:18 AM, Bill  wrote:
> Stephan Houben wrote:
>>
>> Op 2017-09-23, Rick Johnson schreef :
>>>
>>> These pissing contests over how values are passed in Python
>>> are totally irrelevant. What does it matter? Nothing will be
>>> gained or lost by arguing over which is true, or not. Unless
>>> the distinction is preventing you from doing something that
>>> you'd like to do, or unless you want to argue that one
>>> "value passing method" would bring X, Y or Z benefits over
>>> the other, what does it matter?
>>
>> Amen.
>>
>
> All one has to do, I think, is consider (1) that passing objects by "making
> copies" of them, would be prohibitively expensive and consider that
> something else has to happen as an alternative, and (2) understand that in
> Python, objects don't have names, they have references (which have names).
> The rest could be "implementation dependent" (no?)   To be amusing, how did
> the chicken pass an egg to the_other_side_of_the_road(e)?  Could the egg get
> crushed (stay tuned)?

Actually they don't "have" references in any real sense of possession.
An object "has" things like its type, its attributes, etc etc; if you
have a reference to an object, you can query it for its type. But you
can't ask an object if there's a reference to it over here or there.
(Yes, I know that sys.getrefcount exists in CPython, but this isn't
part of the language's definition. Also, even that is just a counter -
you can't find specific references.) An object may have a reference to
other objects (eg a list's contents), but it's a one-way thing -
there's no way to find all the references to this object.

So more accurate would be to say that objects don't have names, but
names refer to objects. When you assign to a simple name, you cause
that name to refer to the object you gave it.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Bill

Stephan Houben wrote:

Op 2017-09-23, Rick Johnson schreef :

These pissing contests over how values are passed in Python
are totally irrelevant. What does it matter? Nothing will be
gained or lost by arguing over which is true, or not. Unless
the distinction is preventing you from doing something that
you'd like to do, or unless you want to argue that one
"value passing method" would bring X, Y or Z benefits over
the other, what does it matter?

Amen.



All one has to do, I think, is consider (1) that passing objects by 
"making copies" of them, would be prohibitively expensive and consider 
that something else has to happen as an alternative, and (2) understand 
that in Python, objects don't have names, they have references (which 
have names).  The rest could be "implementation dependent" (no?)   To be 
amusing, how did the chicken pass an egg to 
the_other_side_of_the_road(e)?  Could the egg get crushed (stay tuned)?


--
https://mail.python.org/mailman/listinfo/python-list


Re: How to share class relationship representations?

2017-09-23 Thread Pavol Lisy
On 9/23/17, Stephan Houben  wrote:
> Op 2017-09-22, Pavol Lisy schreef :
>> On 9/19/17, leam hall  wrote:
>>> I'm working on designing the classes, sub-classes, and relationships in
>>> my
>>> code. What is a good visual way to represent it so it can be stored in
>>> git
>>> and shared on the list without large images or attachments?
>>>
>>> Thanks!
>>>
>>> Leam
>>
>> https://stackoverflow.com/questions/29586520/can-one-get-hierarchical-graphs-from-networkx-with-python-3#29597209
>
> For direct inclusion in source code, what about plain text with
> Unicode box drawing characters?
>
> ┏━━┓
> ┃object┃
> ┗━━━┳━━┛
> ┃
>  ┏━━┻━━┓
>  ┃float┃
>  ┗━┛

I am not big fan of UML. I don't really think it is helping. I think
that it often just enhance maintenance cost.

But if you really need or want it could be good to have some tool to
simplify work.

And you could probably look at -> http://www.plantuml.com/plantuml

To trying something like diagram above you could put there next text and submit:

@startuml
object object
object float
object int
object -- float
object -- int
@enduml

As I said I don't like UML so I don't propose this :)

But if you need "git-able" (text representation) of class diagrams
which could be used to generate quite nice images then maybe this tool
could be useful.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread ROGER GRAYDON CHRISTMAN
On Fri, Sep 22, 2017 12:03 PM, Dennis Lee Bier  wrote:>
On Fri, 22 Sep 2017 23:30:34 +1000, Steve D'Aprano
>
 declaimed the following:
>

>The exercise is to demonstrate pass by reference semantics. That requires
>
>demonstrating the same semantics as the Pascal swap procedure:
>
>
>
>procedure swap(var a, var b):
>
>  begin
>
>tmp := a;
>
>a := b;
>
>b := tmp;
>
>  end;
>
>
>
>(ignoring the type declarations for brevity).
>
>
>
Or FORTRAN (since call-by-reference has been the standard in that
>
language from the start -- no special keywords needed to enable reference
>
semantics)... It's been 20 years, and I'm using an F90 text, so this is
>
going to be less than perfect:
>
subroutine iswap(a, b)
integer :: a
>
integer :: b
>
a = IEOR(a, b)
>
b = IEOR(a, b)
>
a = IEOR(a, b)
>
return
>
end
>
>{That's a bitwise exclusive OR; no temp storage needed}
>
> a = 123
>
> b = 321
>
> a = a ^ b
>
> a
>
314
>
> b = a ^ b
>
> b
>
123
>
> a = a ^ b
>
> a
>
321
>
>
>


And then along comes someone who asks for
to swap array elements a[I] and a[j], where I and j
just happen to be equal.You don't preserve the
values, as would be anticipated, but instead clear
the argument(s) to zero.

Saving one temp storage is a very tiny gain for the potential cost
of a program malfunction.   And if you go so far as to bring in 
a compiled language like Fortran into the mix, where the
'temp storage' is just a machine register, and not RAM,
you really have saved nothing at all.


I usually do not encourage people to optimize correctness out of their code.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Beginners and experts (Batchelder blog post)

2017-09-23 Thread Ned Batchelder

On 9/23/17 2:52 PM, Leam Hall wrote:

On 09/23/2017 02:40 PM, Terry Reedy wrote:

https://nedbatchelder.com//blog/201709/beginners_and_experts.html

Great post.


Yup. Thanks for the link. I often have that "I bet Fred> doesn't get frustrated." thing going. Nice to know Ned bangs his 
head now and again.  :P




"Ow!" --me


--
https://mail.python.org/mailman/listinfo/python-list


Re: Beginners and experts (Batchelder blog post)

2017-09-23 Thread Terry Reedy

On 9/23/2017 2:52 PM, Leam Hall wrote:

On 09/23/2017 02:40 PM, Terry Reedy wrote:

https://nedbatchelder.com//blog/201709/beginners_and_experts.html

Great post.


Yup. Thanks for the link. I often have that "I bet Fred> doesn't get frustrated." thing going. Nice to know Ned bangs his 
head now and again.  :P


As do I ;-).


--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list


Re: what is happening in panda "where" clause

2017-09-23 Thread Pavol Lisy
On 9/22/17, Peter Otten <__pete...@web.de> wrote:
> Exposito, Pedro (RIS-MDW) wrote:
>
>> This code does a "where" clause on a panda data frame...
>>
>> Code:
>> import pandas as pd;
>> col_names = ['Name', 'Age', 'Weight', "Education"];
>> # create panda dataframe
>> x = pd.read_csv('test.dat', sep='|', header=None, names = col_names);
>> # apply "where" condition
>> z = x[ (x['Age'] == 55) ]
>> # prints row WHERE age == 55
>> print (z);
>>
>> What is happening in this statement:
>> z = x[ (x['Age'] == 55) ]
>>
>> Thanks,
>
> Let's take it apart into individual steps:
>
> Make up example data:
>
 import pandas as pd
 x = pd.DataFrame([["Jim", 44], ["Sue", 55], ["Alice", 66]],
> columns=["Name", "Age"])
 x
> Name  Age
> 0Jim   44
> 1Sue   55
> 2  Alice   66
>
> Have a look at the inner expression:
>
 x["Age"] == 55
> 0False
> 1 True
> 2False
>
> So this is a basically vector of boolean values. If you want more details:
> in numpy operations involving a a scalar and an array work via
> "broadcasting". In pure Python you would write something similar as
>
 [v == 55 for v in x["Age"]]
> [False, True, False]
>
> Use the result as an index:
>
 x[[False, True, True]]
> Name  Age
> 1Sue   55
> 2  Alice   66
>
> [2 rows x 2 columns]
>
> This is again in line with numpy arrays -- if you pass an array of boolean
> values as an index the values in the True positions are selected. In pure
> Python you could achieve that with
>
 index = [v == 55 for v in x["Age"]]
 index
> [False, True, False]
 [v for b, v in zip(index, x["Age"]) if b]
> [55]

You could also write:

>>> x[index]
x[index]
  Name  Age
1  Sue   55

As Peter told, understanding numpy or "numpy like" behavior behind
curtain could be key to understanding what is happening.

Look at this:

>>> def half_age(a):
>>> return a/2

we could use this function in where() ->

>>> x[half_age(x.Age)<30]
  Name  Age
0  Jim   44
1  Sue   55

You could think that any function could be used, but it is not true.
a/2 (where a is numpy array) is defined. (I am not sure that it is
really numpy under the hood or if it will be in future pandas versions
but it seems so).

But what if we have more universal function?

>>> def first_char(a):
>>> return a[0].lower()

>>> x[first_char(x.Name)=='a']
... ERROR ... (first_char could not work with pandas serie as a
argument. It means pandas Series doesn't have lower() function)

But you could create index like Peter described. It could be simpler
to remember if you are using pandas not often:

>>> x[[first_char(i)=='a' for i in x.Name]]
Name  Age
2  Alice   66

It is not (IMHO) best solution because you are creating list in memory
(which could be big) . Unfortunately my version of pandas (0.20.3)
doesn't support generator

>>> x[(first_char(i)=='a' for i in x.Name)]
... ERROR...

But you could apply more complex function to Series!

>>> x[x.Name.apply(first_char)=='a']
Name  Age
2  Alice   66

x.Name.apply(first_char) is Series where first_char is applied on each
value. And serie (and numpy.array) understand == operator (and return
value is Series. Which is acceptable as where function parameter)

Although I am not sure that actual version of pandas doesn't create
whole Series in memory :) I expect it is more optimized (and could be
even more in future).

Be aware that I am not expert and I am using pandas only very seldom
just for my little statistics!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Stephan Houben
Op 2017-09-23, Rick Johnson schreef :
> These pissing contests over how values are passed in Python
> are totally irrelevant. What does it matter? Nothing will be
> gained or lost by arguing over which is true, or not. Unless
> the distinction is preventing you from doing something that
> you'd like to do, or unless you want to argue that one
> "value passing method" would bring X, Y or Z benefits over
> the other, what does it matter?

Amen.

For what it's worth, this discussion lead me to do a bit of historical
research into how these terminologies came to be (also to see if there
is some sort of "official" definition of what they actually mean).

The earliest to which I can trace the discussion is on the context of
the definition of the Algol programming language. Algol had both
"call by value" and "call by name".

Then the whole "call by XXX" terminology took off and people started
talking about "call by copy in/out" and whatever. The "call by
reference" terminology was introduced to describe what Fortran had been
doing all along.

The CLU people (primary Barbara Liskov) introduced "call by object
sharing", to describe what CLU did. This matches pretty well what Python
does, and what Java does, and what basically every programming language
invented in the last 25 years does.

Now, as I said, what Java and what CLU do is pretty similar, but in
the Java community this very same thing is called "call by value".
As far as I can follow, this is for the following reason:

 * The Algol report invented "call by value" and "call by name" and was
   very influential.

 * In particular, the Report on the Scheme language was heavily
   influenced by the Algol report. In the Scheme report, Scheme is
   described as being "call by value", again probably because of
   influence of the Algol report, and Scheme is definitely NOT
   "call by name". Note that in our terminology, Scheme should
   properly be called "call by object [sharing]".

 * Guy Steele, who was involved in the Scheme standard, then went 
   on to work on the Java language definition.

So Java got its terminology from the Algol->Scheme->Java route.
Python got it from CLU.

As an aside, in the academic literature, "call by value" is almost
always contrasted with "call by name" (nobody seems to have ever
published a paper discussing "call by reference").
Typically, this comparison is done in calculi which even lack
assignment so that the difference between call by value and call by
reference would be unobservable anyway.

Stephan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Beginners and experts (Batchelder blog post)

2017-09-23 Thread Leam Hall

On 09/23/2017 02:40 PM, Terry Reedy wrote:

https://nedbatchelder.com//blog/201709/beginners_and_experts.html

Great post.


Yup. Thanks for the link. I often have that "I bet Fred> doesn't get frustrated." thing going. Nice to know Ned bangs his 
head now and again.  :P


Leam


--
https://mail.python.org/mailman/listinfo/python-list


Beginners and experts (Batchelder blog post)

2017-09-23 Thread Terry Reedy

https://nedbatchelder.com//blog/201709/beginners_and_experts.html

Great post.
--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Rick Johnson
Mark Lawrence wrote:
> [...] 
> I have no interest it what the C++ does, looks like or
> anything else. All I'm bothered about is that two highly
> respected members of the Python community have stated quite
> clearly that Python is call by object.  Many other people
> have stated the same in this thread or previously, yet
> still people who know squat about Python continue to debate
> the subject.  Why waste effort going over old ground?  Is
> it completely impossible for people to step outside of
> their world of pass by value or pass by reference?

This is one of those rare times when i agree with Mark (and
excuse me for a sec whilst i pinch myself...).

These pissing contests over how values are passed in Python
are totally irrelevant. What does it matter? Nothing will be
gained or lost by arguing over which is true, or not. Unless
the distinction is preventing you from doing something that
you'd like to do, or unless you want to argue that one
"value passing method" would bring X, Y or Z benefits over
the other, what does it matter?

It's like arguing that red lights should be green and green
lights should be red. What are the pros and cons of such
change? IOW, the argument lacks _substance_.

If you're going to argue. At least do it correctly. And try
to make the battle at least _slightly_ entertaining. Folks,
there is reason why flag football never became as popular as
its full-contact sister. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Py 3.6 tarfile

2017-09-23 Thread Klaus Jantzen
   Hi,

   if I understand the documentation of the tarfile module correctly writing

   TarfileObject.add(".../path/to/filename", recursive=False)

   means that the directory structure of the file object will not be included
   in the archive.

   In the following script only "testtext1.pdf" is stored outside a directory
   structure. The other files are always stored in a directory structure;
   using recursive=False does not have any effect.

   

   #!/usr/bin/env python3
   #
   import argparse
   import sys
   import tarfile
   import os

   arch = "Archive.tgz"
   os.unlink(arch)
   try:
   TO = tarfile.open(name=arch, mode='x:gz')  # Tarfile object
   TO.add("testtext1.pdf")
   TO.add("./testtext2.pdf")
   TO.add("./testtext3.pdf", recursive=False)
   TO.add("./files/testtext4.pdf")
   TO.add("./files/testtext5.pdf", recursive=False)
   TO.close()
   except FileExistsError as err:
   pass

   =

   Is my understanding correct or what do have to do so that the files are
   stored without any directory information?

   Thanks for any hints.

   --

   K.D.J.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get the webpage with socks5 proxy in python3?

2017-09-23 Thread Length Power
sudo lsof -i:1080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sslocal 1795 root4u  IPv4  16233  0t0  TCP localhost:socks (LISTEN)
sslocal 1795 root5u  IPv4  16234  0t0  UDP localhost:socks 

An app was listening on localhost:1080,it is ready for curl's socks5 proxy.
The app provided  socks5 proxy service is shadowsocks client on my pc.

curl can work with socks proxy in my pc.

target="target_url_youtube"
curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample  

The target url can be downloaded with scoks5 proxy in curl.   

shadowsocks client--->shadowsocks server--->target_url_youtube
127.0.0.1:1080  1xx.1xx.1xx.1xx:port   target_url_youtube

Notice:
All the packages from 127.0.0.1:1080  to 1xx.1xx.1xx.1xx:port  is sent and 
received by shadowsocks client and server.
curl just sent packages to  127.0.0.1:1080.


Now i want to get the target webpage with socks proxy in python3.

the first try :

import urllib.request
target="target_url_youtubr"
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
web = urllib.request.urlopen(target).read() 
print(web)

The error info:
sock.connect(sa)
OSError: [Errno 101] Network is unreachable

Notice:
It is no use to write {'sock5': 'localhost:1080'}  as {'sock5': 
'127.0.0.1:1080'},i have verified it.   

the second try:

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)
socket.socket = socks.socksocket
import urllib.request
target="target_url_youtubr"
print(urllib.request.urlopen('target').read())

error info:
raise BadStatusLine(line)
http.client.BadStatusLine: 

The third try:as Martijn Pieters say in web 

[Python3 - Requests with Sock5 proxy ][1]

import socks
import socket
from urllib import request
socks.set_default_proxy(socks.SOCKS5, "localhost", 1080)
socket.socket = socks.socksocket
target="target_url_youtube"
r = request.urlopen(url)
print(r.read()) 

ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:600)
urllib.error.URLError: 

Why data packet can't send via localhost:1080 and get the target_url_youtube's 
content,but curl can?   
How to fix my python3 code for socks5 proxy?

shadowsocks client-->shadowsocks server-->target_url_youtube
127.0.0.1:1080   1xx.1xx.1xx.1xx:port target_url_youtube
`curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample`  can  do the 
job.
why  all the three python codes can't do?
How to fix it?



  [1]: 
https://stackoverflow.com/questions/31777692/python3-requests-with-sock5-proxy
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get the webpage with socks5 proxy in python3?

2017-09-23 Thread Length Power
sudo lsof -i:1080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sslocal 1795 root4u  IPv4  16233  0t0  TCP localhost:socks
(LISTEN)
sslocal 1795 root5u  IPv4  16234  0t0  UDP localhost:socks

An app was listening on localhost:1080,it is ready for curl's socks5
proxy.
The app provided  socks5 proxy service is shadowsocks client on my pc.

curl can work with socks proxy in my pc.

target="target_url_youtube"
curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample

The target url can be downloaded with scoks5 proxy in curl.

shadowsocks client--->shadowsocks server--->target_url_youtube
127.0.0.1:1080  1xx.1xx.1xx.1xx:port   target_url_youtube

Notice:
All the packages from 127.0.0.1:1080  to 1xx.1xx.1xx.1xx:port  is sent and
received by shadowsocks client and server.
curl just sent packages to  127.0.0.1:1080.


Now i want to get the target webpage with socks proxy in python3.

the first try :

import urllib.request
target="target_url_youtubr"
proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
web = urllib.request.urlopen(target).read()
print(web)

The error info:
sock.connect(sa)
OSError: [Errno 101] Network is unreachable

Notice:
It is no use to write {'sock5': 'localhost:1080'}  as {'sock5':
'127.0.0.1:1080'},i have verified it.

the second try:

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 1080)
socket.socket = socks.socksocket
import urllib.request
target="target_url_youtubr"
print(urllib.request.urlopen('target').read())

error info:
raise BadStatusLine(line)
http.client.BadStatusLine:

The third try:

import socks
import socket
from urllib import request
socks.set_default_proxy(socks.SOCKS5, "localhost", 1080)
socket.socket = socks.socksocket
target="target_url_youtube"
r = request.urlopen(url)
print(r.read())

ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:600)
urllib.error.URLError: 

Why data packet can't send via localhost:1080 and get the
target_url_youtube's content,but curl can?
How to fix my python3 code for socks5 proxy?

shadowsocks client-->shadowsocks server-->target_url_youtube
127.0.0.1:1080   1xx.1xx.1xx.1xx:port target_url_youtube
`curl --socks5-hostname 127.0.0.1:1080 $target -o /tmp/sample`  can  do
the job.
why  all the three python codes can't do?
How to fix it?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] beginning to code

2017-09-23 Thread Mark Lawrence via Python-list

On 23/09/2017 04:06, Bill wrote:

Mark Lawrence wrote:

On 22/09/2017 08:01, Bill wrote:

Steve D'Aprano wrote:

On Fri, 22 Sep 2017 02:57 pm, Bill wrote:


I find Python to be more more
like Java, with regard to "passing objects by reference".
Which is not a surprise, since both Python and Java use the same 
value passing

style: pass by object reference, or pass by sharing if you prefer.

Java people don't call it that. They call it pass by value, and 
categorically
deny that it is pass by reference. (They're right about the second 
point.)


I figure that, internally, an address, a pointer, is being passed by 
value to implement pass by reference.  Why do you say "they are 
right" above? Are you saying it's not pass by reference?




Please see 
http://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ 
and http://effbot.org/zone/call-by-object.htm





I would would agree with the description provided for the C++ example 
provided


string some_guy = "fred";
  is replaced by
char* some_guy="fred";

To see that this is correct, note the some_guy may subsequently be 
assigned to a character string much longer then "fred".  An additional 
note: A character string literal, like "cat", never occurs more than 
once in compiled C++ program unit.  This also shows that the provided 
description can't be completely correct. One last thing,


string some_guy = "fred"

is really the same thing as

string some_guy("fred");

and both equivalently call the string constructor.

The data type of "fred" is const char*, not (class) string.



I have no interest it what the C++ does, looks like or anything else. 
All I'm bothered about is that two highly respected members of the 
Python community have stated quite clearly that Python is call by 
object.  Many other people have stated the same in this thread or 
previously, yet still people who know squat about Python continue to 
debate the subject.  Why waste effort going over old ground?  Is it 
completely impossible for people to step outside of their world of pass 
by value or pass by reference?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

---
This email has been checked for viruses by AVG.
http://www.avg.com


--
https://mail.python.org/mailman/listinfo/python-list


Re: Old Man Yells At Cloud

2017-09-23 Thread Rick Johnson
Steve D'Aprano wrote:
> Paul Rubin wrote:
> > Steve D'Aprano writes:
> > > Having to spend a few hours being paid to migrate code
> > > using "print x" to "print(x)", or even a few months, is
> > > not a life-changing experience.
> > 
> > Didn't someone further up the thread mention some company
> > that had spent 1.5 years porting a py2 codebase to py3?
> 
> Its possible, but if so I missed it.  And did I mention one
> of the coders at the company I work, who decided to port
> our Python 2 code base to Python 3? He stayed behind one
> night after hours and did it in three hours. We've been
> talking about it for about two years.  What are we to make
> of anecdotes like these? Apart from the possibility that
> one, or the other, or both, is an exaggeration or lie[1]?

Your personal experience at one (small) company is hardly
relevant. And do you really expect this fine community to
belief, that Steven D'Aprano, yes, the man who's had a
hard-on for Python3 long before it was ever publically
released, has not been writing Python2.x code using
backported future feature for years? So why should this fine
community be surprised that your migration took roughly the
same amount of time that it took for Gilligan and his
cohorts to be stranded on an island? 

Just sit right back and you'll hear a tale
a tale of a some fateful "schmit",
that began with feature request,
one that floated his tiny ship.

The code monkey was a mighty patchin' man,
the BDFL brave and sure,
a handful of sycophants set sail that day,
for a three hour tour,
a three hour tour...

The patching started getting rough,
and compatibility would be lost.
And if not for the courage of the fearless crew
the erection would be lost.
The erection would be lost...

So this is the tale of our castaways,
here for a long long time.
They'll have to make the best of things,
as evangelism is an uphill climb. :-(

The first mate and his Skipper too,
will do their very best,
to make the others comf'terble,
in their Python-ideas-island nest.

No print statement, no rights, no tolerance,
not a single luxury,
like a north korean prison state
it's as tyrannical as can be.

So join us here each week my friends,
you're sure to get a smile,
from all the perverted castaways
here on Python3's fantasy Isle!

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyQt: viewport vs window - how do you translate co-ordinates?

2017-09-23 Thread Michael Torrie
On 09/23/2017 05:38 AM, Veek M wrote:
> I didn't understand any of that - could someone expand on that para?
> Is there a reading resource that explains the Viewport and translations? I am 
> not a CS student so I did not study computer graphics.

I'm sure there are lots of things that might help. This is primarily
about OpenGL but the principles may apply:
https://www.cs.mtsu.edu/~jhankins/files/4250/notes/WinToView/WinToViewMap.html

Alternatively, you can think it through.  Imagine you're laying out a
fence around your house. You could use global coordinates for
everything, or you could just call the exact center of your yard 0,0 and
measure everything from there, in whatever units you wish. That's the
idea here. Rather than referring to pixels by their screen coordinates
(which can change if it moves around the screen), you refer to them by
an arbitrary coordinate system with it's own scaling relative to the
screen region.  You could have a screen region that is 100 px by 100 px,
and the you designate that the upper left corner of that region is
-500,500 and the lower right corner is 500,-500. Or it could be
something like -500,200 to 500,-200, which would make things stretched
out in the x axis compared to the y axis.

> (sorry for using google groups but my knode is broken and pan is horrible on 
> olvwm - my plan is to write myself a News client in PyQt :))

I don't think you need to know that much about graphics coordinate
systems to get started with Qt. Most GUI layout stuff does not depend on
pixels or coordinates.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Michael Torrie
On 09/23/2017 03:07 AM, Kryptxy via Python-list wrote:
> Thank you all! I opened a ticket about the same (on github).
> I got response from most of them, and all are agreeing to the change.
> However, one contributor did not respond at all. I tried e-mailing, but no 
> response.
> Can I still proceed changing the licence? It has been more than a week since 
> the ticket was opened.

If you remove the contributor's contributions from your project, then
you can proceed without his permission. Otherwise, Chris and Leam are
correct.

You could replace all the contributor's code with your own code.  This
is a bit tricky as you've seen the contributor's code.  One way around
this is to tell someone what the contributor's patch did, and have them
recreate it, without looking at that person's patch.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Old Man Yells At Cloud

2017-09-23 Thread Paul Moore
On 23 September 2017 at 12:37, Steve D'Aprano
 wrote:
> 95% of Python is unchanged from Python 2 to 3. 95% of the remaining is a 
> trivial
> renaming or other change which can be mechanically translated using a tool 
> like
> 2to3. Only the remaining 5% of 5% is actually tricky to migrate. If your code
> base is full of things relying on that 5% of 5%, then you'll struggle.
> Otherwise, is probably much easier than people expect.

And in my experience, one of the worst difficulties is the transition
to "clean" Unicode handling. I've seen many Python 2 codebases that
mostly-work, either by assuming often-but-not-always-true things like
"Everyone uses UTF-8", or "subprocesses always use the same encoding
as my code" and then introduce "fixes" by tactical re-encoding rather
than redesigning the code to "decode at the boundaries" - because it's
quicker to do so, and everyone has deadlines.

In those cases, the Python 3 transition can be hard, not because
there's a lot of complexity to writing Python 3 compatible code, but
because Python 3 has a stricter separation between bytes and (Unicode)
strings, and doesn't support sloppy practices that Python 2 lets you
get away with. You *can* write Unicode-clean code in Python 2 (and
then the transition is easy) but many people don't, and that's when
things get difficult. The worst cases here are people who know how to
write good Unicode-safe code, and do so in Python 2, but using a
different approach than the one Python 3 takes. Those people put
effort into writing correct code, and then have to change that, *just*
for the transition - they don't get the correctness benefit that
others do.

(I should also point out that writing Unicode-safe code is hard, from
a *design* perspective, because an awful lot of data comes to you
without a known encoding - text files, for example, or the output of a
subprocess. Sometimes you *have* to guess, or make assumptions, and
Python 3 tends to force you to make those assumptions explicit.
Ironically, it's often the better coders that find this hard, as they
are the ones who worry about error handling, or configuration options,
rather than just picking a value and moving on).

Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Old Man Yells At Cloud

2017-09-23 Thread Steve D'Aprano
On Fri, 22 Sep 2017 04:05 pm, Paul Rubin wrote:

> Steve D'Aprano  writes:
>> Having to spend a few hours being paid to migrate code using "print x"
>> to "print(x)", or even a few months, is not a life-changing experience.
> 
> Didn't someone further up the thread mention some company that had spent
> 1.5 years porting a py2 codebase to py3?

Its possible, but if so I missed it.

And did I mention one of the coders at the company I work, who decided to port
our Python 2 code base to Python 3? He stayed behind one night after hours and
did it in three hours. We've been talking about it for about two years.

What are we to make of anecdotes like these? Apart from the possibility that
one, or the other, or both, is an exaggeration or lie[1]?

- perhaps one code base is bigger than the other;

- perhaps one is an unspeakable mess, and the other is nice clean code;

- perhaps one has masses of unit tests while the other barely even works;

- perhaps one company has good coders and the other has terrible coders;

- perhaps it was 1.5 years elapsed time, ten days effort (I've worked
  with people like that).

Who can say?

95% of Python is unchanged from Python 2 to 3. 95% of the remaining is a trivial
renaming or other change which can be mechanically translated using a tool like
2to3. Only the remaining 5% of 5% is actually tricky to migrate. If your code
base is full of things relying on that 5% of 5%, then you'll struggle.
Otherwise, is probably much easier than people expect.




[1] I admit it: mine was an exaggeration. It actually took him four hours.

-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


PyQt: viewport vs window - how do you translate co-ordinates?

2017-09-23 Thread Veek M
pg 329, Rapid GUI Programming

http://storage4.static.itmages.com/i/17/0923/h_1506165624_2588733_59fdfcd4cc.png

In PyQt terminology the physical coordinate system is called the “viewport”,
and confusingly, the logical coordinate system is called the “window”.

In Figure 11.4, we have a physical widget size of 800 × 600. By calling setWin-
dow(-60, -60, 120, 120) we can create a “window” with a top-left coordinate of
(-60, -60), a width of 120, a height of 120, and centered at point (0, 0). The
window’s coordinate system is a logical coordinate system that QPainter auto-
matically maps to the underlying physical device. After the setWindow() call,
all our painting takes place using the logical (window) coordinate system.
In this case, the widget is rectangular, but our window has the same width and
height. This means that the items we paint will be stretched out horizontally,
since coordinates in the y-axis will be scaled by QPainter in the ratio 120:600
(1:5), whereas those in the x-axis will be scaled in the ratio 120:800 (1:6 2
3
).



1. The physical (coordinate system) widget size is 800x600 = Viewport
2. Logical Coordinates = Window

When he does setWindow(-60, -60, 120, 120) he's moving along a diagonal from 
top-left to bottom-right. From that point he's then drawing a 120x120 rectangle.

A. So how does he get a (150,200) point and a -37.5 point??? 
B. Also, how is it stretched horizontally? If i use the inner 120x120 as a 
frame of reference and draw a circle - it'll still be a circle??? Just draw a 
square on a blackboard.. and then draw a circle in it..

I didn't understand any of that - could someone expand on that para?

C. Why does QPainter have to scale in a different ratio??
QPainter understands two coordinate systems - physical and logical and by 
default the two systems match each other.

Is there a reading resource that explains the Viewport and translations? I am 
not a CS student so I did not study computer graphics.


(sorry for using google groups but my knode is broken and pan is horrible on 
olvwm - my plan is to write myself a News client in PyQt :))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Kryptxy via Python-list
Yep. I will wait for a response. Thank you!

 Original Message 
On 23 Sep 2017, 16:57, Chris Angelico wrote:

> On Sat, Sep 23, 2017 at 7:44 PM, Leam Hall  wrote:
>> Like Chris said, evaluate the level of effort on the code. Wait, or replace.
>> You will be happier when you take the honorable path.
>
> Remembering that that, of course, is Plan B; plan A is to keep trying
> to contact that last contributor.. S/he hasn't refused, so there's
> still every possibility of getting that last okay.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list @gmail.com>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Boolean Logic

2017-09-23 Thread Steve D'Aprano
On Sat, 23 Sep 2017 03:01 pm, Bill wrote:

> if (20 - 10)  > 15 :
>  print("true")
> else:
>  print("false");

print(20 - 10  > 15)


will do the job.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Chris Angelico
On Sat, Sep 23, 2017 at 7:44 PM, Leam Hall  wrote:
> Like Chris said, evaluate the level of effort on the code. Wait, or replace.
> You will be happier when you take the honorable path.

Remembering that that, of course, is Plan B; plan A is to keep trying
to contact that last contributor.. S/he hasn't refused, so there's
still every possibility of getting that last okay.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Leam Hall

On 09/23/2017 05:14 AM, Chris Angelico wrote:

On Sat, Sep 23, 2017 at 7:07 PM, Kryptxy  wrote:

Thank you all! I opened a ticket about the same (on github).
I got response from most of them, and all are agreeing to the change.
However, one contributor did not respond at all. I tried e-mailing, but no
response.
Can I still proceed changing the licence? It has been more than a week since
the ticket was opened.


Nope. Contributions made under the GPL have a guarantee that they will
only and forever be used in open source projects. You're trying to
weaken that guarantee, so you have to get clear permission from
everyone involved.

Unless you can show that the contributions in question are so trivial
that there's no code that can be pinpointed as that person's, or you
replace all that person's code, you can't proceed to relicense it
without permission.


I'm with Chris on this one. You made a social, and in many places 
legally binding, agreement. Can't change it without everyone's agreement.


Like Chris said, evaluate the level of effort on the code. Wait, or 
replace. You will be happier when you take the honorable path.


Leam
--
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Chris Angelico
On Sat, Sep 23, 2017 at 7:07 PM, Kryptxy  wrote:
> Thank you all! I opened a ticket about the same (on github).
> I got response from most of them, and all are agreeing to the change.
> However, one contributor did not respond at all. I tried e-mailing, but no
> response.
> Can I still proceed changing the licence? It has been more than a week since
> the ticket was opened.

Nope. Contributions made under the GPL have a guarantee that they will
only and forever be used in open source projects. You're trying to
weaken that guarantee, so you have to get clear permission from
everyone involved.

Unless you can show that the contributions in question are so trivial
that there's no code that can be pinpointed as that person's, or you
replace all that person's code, you can't proceed to relicense it
without permission.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Change project licence?

2017-09-23 Thread Kryptxy via Python-list
Thank you all! I opened a ticket about the same (on github).
I got response from most of them, and all are agreeing to the change.
However, one contributor did not respond at all. I tried e-mailing, but no 
response.
Can I still proceed changing the licence? It has been more than a week since 
the ticket was opened.

Sent with [ProtonMail](https://protonmail.com) Secure Email.

>  Original Message 
> Subject: Re: Change project licence?
> Local Time: 15 September 2017 5:54 AM
> UTC Time: 15 September 2017 00:24
> From: ros...@gmail.com
> To: python-list@python.org 
>
> On Fri, Sep 15, 2017 at 10:17 AM, Ben Finney  
> wrote:
>>> I know how to change the licence, but I want to know is it fine and
>>> wise to change the licence at this point? (The project already has 19
>>> clones, 250+ GitHub stars. Here: https://github.com/kryptxy/torrench)
>>
>> Those represent a whole lot of recipients, who received GPLv3-or-later
>> license grant, guaranteeing the freedom of the work for all recipients
>> in all modified forms. I would consider it poor form to allow non-free
>> redistribution at this point.
>
> Those recipients can still do everything they previously could,
> *including redistributing the code under the GPLv3*. They have lost
> nothing.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Boolean Logic

2017-09-23 Thread Bill

Chris Warrick wrote:


This outputs "False is false", because you used the variable in your
expression. You can just do this:


print("s is", s)

This will print "s is False".

Ah, good point!   But I do like "self-documenting" output (so I don't 
mind seeing s)---if you had 5 or more statements a in a row like that, 
you would "miss" seeing the string s!   : )


Bill

--
https://mail.python.org/mailman/listinfo/python-list


Re: search and replace first amount of strings instances with one thing and a second amount of instances with another thing-

2017-09-23 Thread Bill

Bill wrote:

validationma...@gmail.com wrote:
i have a code in python to search and replace what i need though is 
to replace the first say 10 instances of the number 1 with 2 and the 
second 10 instances with the number 3. anybody knows how to do that?
Do you mean the (integer) number 1 or the character '1'? For instance, 
if the first line of the file is:

"There were 100 cats in the yard."
Do you want to change this to
"There were 200 cats in the yard."?
Remember that string objects are "immutable" so your code probably 
wouldn't work exactly like that.


I now realize that my last line does not apply. I suggest you start by 
just thinking about changing the first ten ones to twos.  Once you do 
that, you should have little trouble finishing the job. Being a Python 
newbie myself, your example helped motivate me to try my own example.  I 
used "type" to learn the data type of line (in your example), about 
which I was curious. My first concern was that is might only be 
"interable" object, but I learned that more is true.







fin = open(r'F:\1\xxx.txt')
fout = open(r'F:\1\xxx2.txt', "wt")
for line in fin:
 fout.write( line.replace('1', '2') )
fin.close()
fout.close()




--
https://mail.python.org/mailman/listinfo/python-list


Re: Python Boolean Logic

2017-09-23 Thread Chris Warrick
On 23 September 2017 at 06:46, Cai Gengyang  wrote:
> Output :
>
> ('bool_one = ', False)
> ('bool_two = ', False)
> ('bool_three = ', False)
> ('bool_four = ', True)
> ('bool_five = ', False)

You’re using Python 2 with Python 3-style print statements. To make it
look good, start your code with:

from __future__ import print_function

Or use the Python 2 form (without parentheses), or even better: switch
to Python 3.

Now, Codecademy is a peculiar place. They still teach Python 2 (that
sucks!) and have a specific teaching style. The way you’re supposed to
solve this is to just assign your answers to bool_one through
bool_five. You should just type True or False in each blank, using
Python to evaluate this expression is kinda cheating. Codecademy will
then tell you if you got the right answer. Don’t print stuff you
aren’t asked to print.

However, you should not depend only on Codecademy’s interpreter.
Install Python on your computer and work with that. I also recommend
learning using different resources, and learning Python 3. It’s the
future.

~~~

On 23 September 2017 at 07:01, Bill  wrote:
> Your answers appear correct, but you could write Python statements to test
> them (or any you encounter in the future). For instance,
>
> if (20 - 10)  > 15 :
> print("true")
> else:
> print("false");
>
> Or,
>
> s='(20 - 10)  > 15'
> b=(20 - 10)  > 15
> print(s, " is ", ("true" if b else "false") );  ## inside parentheses may be
> removed.

This outputs "False is false", because you used the variable in your
expression. You can just do this:

>>> print("s is", s)

This will print "s is False".

You can also replace your earlier `if` with:

>>> print((20 - 10) > 15)

(False will appear upper-case, of course.)

PS. don’t use semicolons with Python. Avoid eval() as well.

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: search and replace first amount of strings instances with one thing and a second amount of instances with another thing-

2017-09-23 Thread Peter Otten
Peter Otten wrote:

> validationma...@gmail.com wrote:
> 
>> i have a code in python to search and replace what i need though is to
>> replace the first say 10 instances of the number 1 with 2 and the second
>> 10 instances with the number 3. anybody knows how to do that?
>> 
>> fin = open(r'F:\1\xxx.txt')
>> fout = open(r'F:\1\xxx2.txt', "wt")
>> for line in fin:
>> fout.write( line.replace('1', '2') )
>> fin.close()
>> fout.close()
> 
> If the search token can occur more than once in a line you cannot use
> str.replace(). Here's a solution without regular expressions. I
> implemented the "repeat something" and the "replace token with something
> different every time" separately.
> 
> $ cat replace_increasing.py
> #!/usr/bin/env python3

Should also work with python2 once you add

try:
from future_builtins import zip, map
except ImportError:
pass

to make map() lazy.

> from itertools import chain, count, repeat

Lest you die of an itertools overdose here's a slightly simpler version:

#!/usr/bin/env python3


def numberstrings(repeat, start=1):
"""
>>> [v for i, v in zip(range(10), numberstrings(3, 1))]
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4']
"""
value = start
while True:
for _i in range(repeat):
yield str(value)
value += 1


def make_replace(token, replace):
"""
>>> r = make_replace("x", "ABCDEFGH")
>>> r("onextwoxthreex")
'oneAtwoBthreeC'
>>> r("no match")
'no match'
>>> r("x at start")
'D at start'
>>> r("somexsomewhere")
'someEsomewhere'
>>> r("xxx")
'FGH'
>>> try: r("x")
... except StopIteration: "OK"
'OK'
"""
replaceiter = iter(replace)

def replace(line):
parts = line.split(token)
return "".join(
[p + next(replaceiter) for p in parts[:-1]]
+ parts[-1:]
)
return replace


def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("infile")
parser.add_argument("outfile")
parser.add_argument("token")
parser.add_argument("-r", "--repeat", type=int, default=10)
args = parser.parse_args()

replace = make_replace(args.token, numberstrings(args.repeat))
with open(args.infile) as instream:
with open(args.outfile, "w") as outstream:
outstream.writelines(replace(line) for line in instream)


if __name__ == "__main__":
main()


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: search and replace first amount of strings instances with one thing and a second amount of instances with another thing-

2017-09-23 Thread Bill

validationma...@gmail.com wrote:

i have a code in python to search and replace what i need though is to replace 
the first say 10 instances of the number 1 with 2 and the second 10 instances 
with the number 3. anybody knows how to do that?
Do you mean the (integer) number 1 or the character '1'? For instance, 
if the first line of the file is:

"There were 100 cats in the yard."
Do you want to change this to
"There were 200 cats in the yard."?
Remember that string objects are "immutable" so your code probably 
wouldn't work exactly like that.





fin = open(r'F:\1\xxx.txt')
fout = open(r'F:\1\xxx2.txt', "wt")
for line in fin:
 fout.write( line.replace('1', '2') )
fin.close()
fout.close()


--
https://mail.python.org/mailman/listinfo/python-list


Re: search and replace first amount of strings instances with one thing and a second amount of instances with another thing-

2017-09-23 Thread Peter Otten
validationma...@gmail.com wrote:

> i have a code in python to search and replace what i need though is to
> replace the first say 10 instances of the number 1 with 2 and the second
> 10 instances with the number 3. anybody knows how to do that?
> 
> fin = open(r'F:\1\xxx.txt')
> fout = open(r'F:\1\xxx2.txt', "wt")
> for line in fin:
> fout.write( line.replace('1', '2') )
> fin.close()
> fout.close()

If the search token can occur more than once in a line you cannot use 
str.replace(). Here's a solution without regular expressions. I implemented 
the "repeat something" and the "replace token with something different every 
time" separately.

$ cat replace_increasing.py
#!/usr/bin/env python3
from itertools import chain, count, repeat


def repeater(values, repeatcount):
"""
>>> list(repeater("abc", 3))
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
>>> list(repeater(range(3), 2))
[0, 0, 1, 1, 2, 2]
"""
return chain.from_iterable(repeat(value, repeatcount) for value in 
values)


def _ran_out_of_values():
if 0:
yield
raise ValueError("ran out of values")


class Replace:
"""
>>> r = Replace("x", "ABCDEFGH")
>>> r("onextwoxthreex")
'oneAtwoBthreeC'
>>> r("no match")
'no match'
>>> r("x at start")
'D at start'
>>> r("somexsomewhere")
'someEsomewhere'
>>> r("xxx")
'FGH'
>>> try: r("x")
... except ValueError as err: print(err)
ran out of values
"""
def __init__(self, token, replace):
self.token = token
self.replaceiter = chain(replace, _ran_out_of_values())

def __call__(self, line):
parts = line.split(self.token)
return "".join(
chain(
(p + q for p, q in zip(parts[:-1], self.replaceiter)),
parts[-1:]
)
)


def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("infile")
parser.add_argument("outfile")
parser.add_argument("token")
parser.add_argument("-r", "--repeat", type=int, default=10)
args = parser.parse_args()

numberstrings = map(str, count(1))
replace = Replace(args.token, repeater(numberstrings, args.repeat))
with open(args.infile) as instream:
with open(args.outfile, "w") as outstream:
outstream.writelines(map(replace, instream))


if __name__ == "__main__":
main()

As an example let's replace the 'a's in the script above with numbers, 
repeated three times:

$ ./replace_increasing.py replace_increasing.py tmp.py a -r3
$ head tmp.py
#!/usr/bin/env python3
from itertools import ch1in, count, repe1t


def repe1ter(v2lues, repe2tcount):
"""
>>> list(repe2ter("3bc", 3))
['3', '3', '4', 'b', 'b', 'b', 'c', 'c', 'c']
>>> list(repe4ter(r4nge(3), 2))
[0, 0, 1, 1, 2, 2]
"""
return ch5in.from_iter5ble(repe5t(v6lue, repe6tcount) for v6lue in 
v7lues)


def _r7n_out_of_v7lues():
if 0:
yield
r8ise V8lueError("r8n out of v9lues")


$


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Boolean Logic

2017-09-23 Thread Bill

Steve D'Aprano wrote:

On Sat, 23 Sep 2017 03:01 pm, Bill wrote:


s='(20 - 10)  > 15'
b=(20 - 10)  > 15
print(s, " is ", ("true" if b else "false") );  ## inside parentheses
may be removed.

I am new to Python.  Maybe someone here is familiar with an elegant way
to get the the value of b directly from the string s?  Hmm... It appears
that eval() would work


Indeed it will, but don't get into the habit of using eval willy-nilly. While it
is absolutely fine to use it with data you provide yourself, it is a HUGE
security risk to eval strings that came from an untrusted user.


eval("__import__('os').system('echo """rm-rf /"""')")


Thank you. Studying that was a nice little lesson in itself!   I 
recognize that this technique can be used for 'good' as well as 'evil'!  : )


Bill

--
https://mail.python.org/mailman/listinfo/python-list