Re: Overriding True and False ?

2017-01-30 Thread Chris Angelico
On Mon, Jan 30, 2017 at 6:50 PM, Deborah Swanson
 wrote:
> Looks like the moral of the story is that in Python 2.7 you can redefine
> keywords, so long as you don't get any syntax errors after (or during)
> redefinition.

The moral is actually that "True" and "False" aren't keywords in Py2.
They're just built-ins. Compare:

rosuav@sikorsky:~$ python3
Python 3.7.0a0 (default:cebc9c7ad195, Jan 24 2017, 06:55:19)
[GCC 6.2.0 20161027] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1   0 LOAD_CONST   1 (True)
  2 RETURN_VALUE
>>>
rosuav@sikorsky:~$ python
Python 2.7.12+ (default, Sep  1 2016, 20:27:38)
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1   0 LOAD_GLOBAL  0 (True)
  3 RETURN_VALUE
>>>

In Python 2, returning "True" involves a name lookup; in Python 3,
it's simply a constant - a literal.

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


Re: Overriding True and False ?

2017-01-30 Thread Chris Angelico
On Mon, Jan 30, 2017 at 4:03 PM, Irv Kalb  wrote:
> It seems very odd that Python allows you to override the values of True and 
> False.  In the code, True and False were clearly recognized as keywords as 
> they were colored purple.  But there was no error message.
>
> You cannot assign new values to other keywords.  Simple tests of things like:
>
> for = 5
>
> while = 2
>
> not = 3
>
> As expected, all result in SyntaxError: invalid syntax.  Why would Python 
> allow you to override the values of True and False?  I wonder if this is some 
> sort of historical thing as these are the only keywords besides None that are 
> uppercased.  This line:
>
> None = 5
>
> Even gives a special SyntaxError: cannot assign to None

There are slightly different things going on here. Trying to assign to
a piece of syntax like "while" makes absolutely no sense, but trying
to assign to "None" is structurally sane, yet disallowed. IIRC there's
only one non-assignable name in Python 2 (None), but as mentioned,
Python 3 adds True and False to that. (Interestingly, Ellipsis is not
included in that.)

> I teach intro to programming using Python.

May I please request that you consider teaching Python 3? Python 2
isn't going anywhere (for better or for worse), and Py3 is a superior
language in many ways, not least of which is that it keeps text and
bytes separate, giving text the full power that it should have. For a
beginning programmer, this is very helpful; there's nothing to
un-learn when going international. (There will be new nuances to be
learned, such as RTL text, but nothing to unlearn.) Python 3 also
fixes a number of other problems that Python 2 inherited from C,
making it altogether a better language for teaching with.

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Chris Angelico
On Mon, Jan 30, 2017 at 6:25 PM, Joseph L. Casale
 wrote:
> .Net Core is fundamentally different and much like Python in the way that
> a compiler and runtime for a common language syntax specification has been
> written for multiple platforms. So in general, the same Python script could
> run on both platforms, and most certainly the same .NET Core source could
> compile and run on its supported platforms.

What do you mean by "both platforms"? Python scripts already run on
three major operating systems (Win/Lin/Mac) and a good number of
less-popular OSes; a well-written Python script will run in four major
Pythons (CPython, PyPy, Jython, IronPython) and a number of others;
and all manner of CPU architectures and so on.

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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Peter Otten
hmmeeranrizv...@gmail.com wrote:

> Hello Guys,
> Here i am creating a GUI which will act as a search engine that will find
> the results from the browser and save the results as a xls file. When i
> typed something in my search box and click the (GO)button.It should
> display search in progress.when the file is saved it should display done.
> How to do that? My button gets hung for a seconds.We should give any
> timeout for that?

Move the actual processing into another thread and have it communicate with 
your GUI through a Queue. Here's a sketch:

import Queue
import threading

from Tkinter import *
import mechanize

def clear_search(event):
search.delete(0, END)

def start_search():
thread = threading.Thread(
target=fun,
args=(search.get(),)
)
thread.setDaemon(True)
thread.start()
root.after(100, check_state)

def fun(new):
queue.put("searching")
url = "http://duckduckgo.com/html";
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(name="x")
br["q"] = str(new)
res = br.submit()
queue.put("writing result")
content = res.read()
with open("result1.xls", "w") as f:
f.write(content)
queue.put("done")

def check_state():
while True:
try:
message = queue.get_nowait()
except Queue.Empty:
break
state_info["text"] = message
if message == "done":
return
root.after(100, check_state)

queue = Queue.Queue()
root = Tk()

top_findings = Label(root, text="Top Findings:", font="-weight bold")
top_findings.pack()

search = Entry(root, width=100)
search.insert(0, "Enter the value to search")

# Did you see Christian Gollwitzer's post?
search.bind("", clear_search)

search.pack()

go_button = Button(root, text="GO", width=5, command=start_search)
go_button.pack()

state_info = Label(root)
state_info.pack()

root.mainloop()


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


Re: Rename file without overwriting existing files

2017-01-30 Thread Steve D'Aprano
On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote:

> On 30Jan2017 13:49, Steve D'Aprano  wrote:
>>This code contains a Time Of Check to Time Of Use bug:
>>
>>if os.path.exists(destination)
>>raise ValueError('destination already exists')
>>os.rename(oldname, destination)
>>
>>
>>In the microsecond between checking for the existence of the destination
>>and actually doing the rename, it is possible that another process may
>>create the destination, resulting in data loss.
>>
>>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug?
> 
> For files this is a problem at the Python level. At the UNIX level you can
> play neat games with open(2) and the various O_* modes.
> 
> however, with directories things are more cut and dry. Do you have much
> freedom here? What's the wider context of the question?

The wider context is that I'm taking from 1 to 
path names to existing files as arguments, and for each path name I
transfer the file name part (but not the directory part) and then rename
the file. For example:

foo/bar/baz/spam.txt

may be renamed to:

foo/bar/baz/ham.txt

but only provided ham.txt doesn't already exist.




-- 
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: Is shutil.get_terminal_size useless?

2017-01-30 Thread Paul Moore
On Monday, 30 January 2017 05:37:32 UTC, Steven D'Aprano  wrote:
> On Monday 30 January 2017 08:12, Serhiy Storchaka wrote:
> 
> > On 28.01.17 10:03, Steve D'Aprano wrote:
> >> Is shutil.get_terminal_size useless? When, if ever, should I use it in
> >> preference to the os version? If the shutil version is broken, can it be
> >> fixed?
> > 
> > Read the history of shutil.get_terminal_size(). All this was discussed.
> 
> 
> Yes, it was discussed, but not resolved: Antoine Pitrou just closed the task 
> and declared it done, without resolving the failures I am talking about here.
> 
> http://bugs.python.org/issue13609
> 
> (Thanks Eryk Sun for the link.)


Looks like it was closed with the comment "If you want further features, please 
open a new issue". It sounds to me like os.get_terminal_size() does what you 
want, and others find the shutil version useful. But if you have a use case for 
the shutil version that needs a change in its behaviour, I guess you should 
open an issue for that.

The docs on shutil.get_terminal_size seem pretty clear on what it does (whether 
you agree with the behaviour or not), so I don't think there's much benefit to 
proposing changes to the docs alone.

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Steve D'Aprano wrote:

> On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote:
> 
>> On 30Jan2017 13:49, Steve D'Aprano  wrote:
>>>This code contains a Time Of Check to Time Of Use bug:
>>>
>>>if os.path.exists(destination)
>>>raise ValueError('destination already exists')
>>>os.rename(oldname, destination)
>>>
>>>
>>>In the microsecond between checking for the existence of the destination
>>>and actually doing the rename, it is possible that another process may
>>>create the destination, resulting in data loss.
>>>
>>>Apart from keeping my fingers crossed, how should I fix this TOCTOU bug?
>> 
>> For files this is a problem at the Python level. At the UNIX level you
>> can play neat games with open(2) and the various O_* modes.
>> 
>> however, with directories things are more cut and dry. Do you have much
>> freedom here? What's the wider context of the question?
> 
> The wider context is that I'm taking from 1 to 
> path names to existing files as arguments, and for each path name I
> transfer the file name part (but not the directory part) and then rename
> the file. For example:
> 
> foo/bar/baz/spam.txt
> 
> may be renamed to:
> 
> foo/bar/baz/ham.txt
> 
> but only provided ham.txt doesn't already exist.

Google finds

http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions

and from a quick test it appears to work on Linux:

$ echo foo > foo
$ echo bar > bar
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> def rename(source, dest):
... os.link(source, dest)
... os.unlink(source)
... 
>>> rename("foo", "baz")
>>> os.listdir()
['bar', 'baz']
>>> rename("bar", "baz")
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in rename
FileExistsError: [Errno 17] File exists: 'bar' -> 'baz'


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


Re: Rename file without overwriting existing files

2017-01-30 Thread Jussi Piitulainen
Peter Otten writes:

> Steve D'Aprano wrote:
>
>> On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote:
>> 
>>> On 30Jan2017 13:49, Steve D'Aprano  wrote:
This code contains a Time Of Check to Time Of Use bug:

if os.path.exists(destination)
raise ValueError('destination already exists')
os.rename(oldname, destination)


In the microsecond between checking for the existence of the destination
and actually doing the rename, it is possible that another process may
create the destination, resulting in data loss.

Apart from keeping my fingers crossed, how should I fix this TOCTOU bug?
>>> 
>>> For files this is a problem at the Python level. At the UNIX level you
>>> can play neat games with open(2) and the various O_* modes.
>>> 
>>> however, with directories things are more cut and dry. Do you have much
>>> freedom here? What's the wider context of the question?
>> 
>> The wider context is that I'm taking from 1 to 
>> path names to existing files as arguments, and for each path name I
>> transfer the file name part (but not the directory part) and then rename
>> the file. For example:
>> 
>> foo/bar/baz/spam.txt
>> 
>> may be renamed to:
>> 
>> foo/bar/baz/ham.txt
>> 
>> but only provided ham.txt doesn't already exist.
>
> Google finds
>
> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions
>
> and from a quick test it appears to work on Linux:

It doesn't seem to be documented. I looked at help(os.link) on Python
3.4 and the corresponding current library documentation on the web. I
saw no mention of what happens when dst exists already.

Also, creating a hard link doesn't seem to work between different file
systems, which may well be relevant to Steve's case. I get:

OSError: [Errno 18] Invalid cross-device link: [snip]

And that also is not mentioned in the docs.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Wolfgang Maier

On 01/30/2017 03:49 AM, Steve D'Aprano wrote:

This code contains a Time Of Check to Time Of Use bug:

if os.path.exists(destination)
raise ValueError('destination already exists')
os.rename(oldname, destination)


In the microsecond between checking for the existence of the destination and
actually doing the rename, it is possible that another process may create
the destination, resulting in data loss.

Apart from keeping my fingers crossed, how should I fix this TOCTOU bug?



There is a rather extensive discussion of this problem (with no good 
cross-platform solution if I remember correctly):


https://mail.python.org/pipermail/python-ideas/2011-August/011131.html

which is related to http://bugs.python.org/issue12741

Wolfgang

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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Meeran Rizvi
On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
> Hello Guys,
> Here i am creating a GUI which will act as a search engine that will find the 
> results from the browser and save the results as a xls file.
> When i typed something in my search box and click the (GO)button.It should 
> display search in progress.when the file is saved it should display done.
> How to do that?
> My button gets hung for a seconds.We should give any timeout for that?
> 
> Here is my script
> 
> from Tkinter import *
> import mechanize
> def clear_search(event):
> search.delete(0,END)
> obj = Tk()
> Label = Label(obj,text="Top Findings:",font="-weight bold")
> search = Entry(obj,width=100)
> search.insert(0, "Enter the value to search")
> search.bind("", clear_search)
> def fun():
> new = search.get()
> url = "http://duckduckgo.com/html";
> br = mechanize.Browser()
> br.set_handle_robots(False)
> br.open(url)
> br.select_form(name="x")
> br["q"] = str(new)
> res = br.submit()
> content = res.read()
> with open("result1.xls", "w") as f:
> f.write(content)
> fun()
> Go = Button(obj,text="GO",width=5,command=fun)
> Label.pack()
> search.pack()
> Go.pack()
> mainloop()
> 

Hi Peter,
can u explain about these functions 
def start_search():
thread = threading.Thread(
target=fun,
args=(search.get(),)
)
thread.setDaemon(True)
thread.start()
root.after(100, check_state)

def check_state():
while True:
try:
message = queue.get_nowait()
except Queue.Empty:
break
state_info["text"] = message
if message == "done":
return
root.after(100, check_state)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Peter Otten
Meeran Rizvi wrote:

> On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
>> Hello Guys,
>> Here i am creating a GUI which will act as a search engine that will find
>> the results from the browser and save the results as a xls file. When i
>> typed something in my search box and click the (GO)button.It should
>> display search in progress.when the file is saved it should display done.
>> How to do that? My button gets hung for a seconds.We should give any
>> timeout for that?
>> 
>> Here is my script
>> 
>> from Tkinter import *
>> import mechanize
>> def clear_search(event):
>> search.delete(0,END)
>> obj = Tk()
>> Label = Label(obj,text="Top Findings:",font="-weight bold")
>> search = Entry(obj,width=100)
>> search.insert(0, "Enter the value to search")
>> search.bind("", clear_search)
>> def fun():
>> new = search.get()
>> url = "http://duckduckgo.com/html";
>> br = mechanize.Browser()
>> br.set_handle_robots(False)
>> br.open(url)
>> br.select_form(name="x")
>> br["q"] = str(new)
>> res = br.submit()
>> content = res.read()
>> with open("result1.xls", "w") as f:
>> f.write(content)
>> fun()
>> Go = Button(obj,text="GO",width=5,command=fun)
>> Label.pack()
>> search.pack()
>> Go.pack()
>> mainloop()
>> 
> 
> Hi Peter,
> can u explain about these functions
> def start_search():

This creates a new thread; target and args specify that in that thread
fun will be invoked with the current contents of the search Entry.

> thread = threading.Thread(
> target=fun,
> args=(search.get(),)
> )
> thread.setDaemon(True)
> thread.start()

This tells Tkinter to run the check_state function 100 milliseconds later.

> root.after(100, check_state)

As the -- aptly named ;) -- start_search() only starts the search without 
having to wait for the result it will only block the GUI for a moment that 
should normally be too short for the user to note.

You can think of the resulting script as two mini-programs that -- at least 
conceptually -- run indepently. If they access each other's data you can get 
an inconsistent state. The Queue class is written with precautions to avoid 
such problems. check_state() and Tkinter run in the main thread and only 
receive data from the thread executing fun() through the queue.

> def check_state():
> while True:

  Look into the queue if there is a message from fun(). If there is
  return it, otherwise instead of waiting for the next message to
  arrive raise an exception immediately. Remember, if we stay here
  too long the GUI will lag.

> try:
> message = queue.get_nowait()
> except Queue.Empty:

  Case (1) No pending messages, break out of the loop.

> break

  Case (2) We have a message. Update the text of the state_info
  Label accordingly,

> state_info["text"] = message

  then check if it's the last message we expect.

> if message == "done":

  Yes it's the last message; leave check_state() without
  rescheduling it.

> return

  We got here from the break statement. There are currently no messages, 
  but there may be later, so we ask Tkinter to run check_state again in
  100 milliseconds.

> root.after(100, check_state)

PS: You can verify that you understood this by answering the question: what 
happens if the user hits the go-button while the second thread is still 
running. Hint: as written the script does not handle this correctly.

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Jussi Piitulainen  wrote:
> It doesn't seem to be documented. I looked at help(os.link) on Python
> 3.4 and the corresponding current library documentation on the web. I
> saw no mention of what happens when dst exists already.
>
> Also, creating a hard link doesn't seem to work between different file
> systems, which may well be relevant to Steve's case. I get:
>
> OSError: [Errno 18] Invalid cross-device link: [snip]
>
> And that also is not mentioned in the docs.

Nor *should* either of those things be mentioned in the Python docs.

A lot of the functions of the 'os' module do nothing but call the
underlying OS system call with the same name. It would not only be
redundant to copy the OS documentation into the Python documentation,
it would be misleading and wrong, because of course the behaviour may
vary slightly from OS to OS.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Peter Otten
Peter Otten wrote:

> PS: You can verify that you understood this by answering the question:
> what happens if the user hits the go-button while the second thread is
> still running. Hint: as written the script does not handle this correctly.

On second thought it's probably not as bad as I thought when I wrote the 
above. I assumed that the messages after the first "done" would remain in 
the queue forever, but there is another independent check_state() invocation 
that should read the rest.
I hope ;)


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


Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jon Ribbens wrote:

> On 2017-01-30, Jussi Piitulainen  wrote:
>> It doesn't seem to be documented. I looked at help(os.link) on Python
>> 3.4 and the corresponding current library documentation on the web. I
>> saw no mention of what happens when dst exists already.
>>
>> Also, creating a hard link doesn't seem to work between different file
>> systems, which may well be relevant to Steve's case. I get:
>>
>> OSError: [Errno 18] Invalid cross-device link: [snip]
>>
>> And that also is not mentioned in the docs.
> 
> Nor *should* either of those things be mentioned in the Python docs.
> 
> A lot of the functions of the 'os' module do nothing but call the
> underlying OS system call with the same name. It would not only be
> redundant to copy the OS documentation into the Python documentation,
> it would be misleading and wrong, because of course the behaviour may
> vary slightly from OS to OS.

However, the current Python version of link() is sufficiently different from 
, say, to warrant its own documentation.

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jussi Piitulainen wrote:

> Peter Otten writes:
> 
>> Steve D'Aprano wrote:

>>> The wider context is that I'm taking from 1 to 
>>> path names to existing files as arguments, and for each path name I
>>> transfer the file name part (but not the directory part) and then rename
>>> the file. For example:
>>> 
>>> foo/bar/baz/spam.txt
>>>
>>> may be renamed to:
>>>
>>> foo/bar/baz/ham.txt
>>> 
>>> but only provided ham.txt doesn't already exist.
>>
>> Google finds
>>
>> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions
>>
>> and from a quick test it appears to work on Linux:
> 
> It doesn't seem to be documented. 

For functions with a C equivalent a look into the man page is usually 
helpful.

> I looked at help(os.link) on Python
> 3.4 and the corresponding current library documentation on the web. I
> saw no mention of what happens when dst exists already.
> 
> Also, creating a hard link doesn't seem to work between different file
> systems, which may well be relevant to Steve's case.

In his example above he operates inside a single directory. Can one 
directory spread across multiple file systems?

> I get:
> 
> OSError: [Errno 18] Invalid cross-device link: [snip]
> 
> And that also is not mentioned in the docs.


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


Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Peter Otten <__pete...@web.de> wrote:
> Jon Ribbens wrote:
>> A lot of the functions of the 'os' module do nothing but call the
>> underlying OS system call with the same name. It would not only be
>> redundant to copy the OS documentation into the Python documentation,
>> it would be misleading and wrong, because of course the behaviour may
>> vary slightly from OS to OS.
>
> However, the current Python version of link() is sufficiently different from 
>, say, to warrant its own documentation.

What are you referring to here? As far as I can see, the current
Python implementation of link() just calls the underlying OS call
directly.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Meeran Rizvi
On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
> Hello Guys,
> Here i am creating a GUI which will act as a search engine that will find the 
> results from the browser and save the results as a xls file.
> When i typed something in my search box and click the (GO)button.It should 
> display search in progress.when the file is saved it should display done.
> How to do that?
> My button gets hung for a seconds.We should give any timeout for that?
> 
> Here is my script
> 
> from Tkinter import *
> import mechanize
> def clear_search(event):
> search.delete(0,END)
> obj = Tk()
> Label = Label(obj,text="Top Findings:",font="-weight bold")
> search = Entry(obj,width=100)
> search.insert(0, "Enter the value to search")
> search.bind("", clear_search)
> def fun():
> new = search.get()
> url = "http://duckduckgo.com/html";
> br = mechanize.Browser()
> br.set_handle_robots(False)
> br.open(url)
> br.select_form(name="x")
> br["q"] = str(new)
> res = br.submit()
> content = res.read()
> with open("result1.xls", "w") as f:
> f.write(content)
> fun()
> Go = Button(obj,text="GO",width=5,command=fun)
> Label.pack()
> search.pack()
> Go.pack()
> mainloop()
> 

It is possible to display the output in my GUI itself?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Meeran Rizvi
On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
> Hello Guys,
> Here i am creating a GUI which will act as a search engine that will find the 
> results from the browser and save the results as a xls file.
> When i typed something in my search box and click the (GO)button.It should 
> display search in progress.when the file is saved it should display done.
> How to do that?
> My button gets hung for a seconds.We should give any timeout for that?
> 
> Here is my script
> 
> from Tkinter import *
> import mechanize
> def clear_search(event):
> search.delete(0,END)
> obj = Tk()
> Label = Label(obj,text="Top Findings:",font="-weight bold")
> search = Entry(obj,width=100)
> search.insert(0, "Enter the value to search")
> search.bind("", clear_search)
> def fun():
> new = search.get()
> url = "http://duckduckgo.com/html";
> br = mechanize.Browser()
> br.set_handle_robots(False)
> br.open(url)
> br.select_form(name="x")
> br["q"] = str(new)
> res = br.submit()
> content = res.read()
> with open("result1.xls", "w") as f:
> f.write(content)
> fun()
> Go = Button(obj,text="GO",width=5,command=fun)
> Label.pack()
> search.pack()
> Go.pack()
> mainloop()
> 

Thanks for the explanation Peter
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Chris Angelico
On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__pete...@web.de> wrote:
>> I looked at help(os.link) on Python
>> 3.4 and the corresponding current library documentation on the web. I
>> saw no mention of what happens when dst exists already.
>>
>> Also, creating a hard link doesn't seem to work between different file
>> systems, which may well be relevant to Steve's case.
>
> In his example above he operates inside a single directory. Can one
> directory spread across multiple file systems?

Yep. Try unionfs.

... make ourselves some scratch space ...
$ mkdir space modifier
$ dd if=/dev/zero of=space.img bs=4096 count=65536
$ mkfs space.img
$ sudo mount space.img space
$ dd if=/dev/zero of=modifier.img bs=4096 count=1024\
$ mkfs modifier.img
$ sudo mount modifier.img modifier

... put some content into the base directory ...
$ sudo -e space/demo.txt

... and now the magic:
$ unionfs modifier=RW:space=RO joiner/
$ cd joiner

At this point, you're in a directory that is the union of the two
directories. One of them is read-only, the other is read/write. It is
thus possible to view a file that you can't hard-link to a new name,
because the new name would have to be created in the 'modifier' file
system, but the old file exists on the 'space' one.

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jon Ribbens wrote:

> On 2017-01-30, Peter Otten <__pete...@web.de> wrote:
>> Jon Ribbens wrote:
>>> A lot of the functions of the 'os' module do nothing but call the
>>> underlying OS system call with the same name. It would not only be
>>> redundant to copy the OS documentation into the Python documentation,
>>> it would be misleading and wrong, because of course the behaviour may
>>> vary slightly from OS to OS.
>>
>> However, the current Python version of link() is sufficiently different
>> from
>>, say, to warrant its own documentation.
> 
> What are you referring to here? As far as I can see, the current
> Python implementation of link() just calls the underlying OS call
> directly.

The current signature differs from that of link()

os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)


but it looks like you are right in so far as link() is still called by 
default:

if ((src_dir_fd != DEFAULT_DIR_FD) ||
(dst_dir_fd != DEFAULT_DIR_FD) ||
(!follow_symlinks))
result = linkat(src_dir_fd, src->narrow,
dst_dir_fd, dst->narrow,
follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
else
#endif /* HAVE_LINKAT */
result = link(src->narrow, dst->narrow);


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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Peter Otten
Meeran Rizvi wrote:

> On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
>> Hello Guys,
>> Here i am creating a GUI which will act as a search engine that will find
>> the results from the browser and save the results as a xls file. When i
>> typed something in my search box and click the (GO)button.It should
>> display search in progress.when the file is saved it should display done.
>> How to do that? My button gets hung for a seconds.We should give any
>> timeout for that?
>> 
>> Here is my script
>> 
>> from Tkinter import *
>> import mechanize
>> def clear_search(event):
>> search.delete(0,END)
>> obj = Tk()
>> Label = Label(obj,text="Top Findings:",font="-weight bold")
>> search = Entry(obj,width=100)
>> search.insert(0, "Enter the value to search")
>> search.bind("", clear_search)
>> def fun():
>> new = search.get()
>> url = "http://duckduckgo.com/html";
>> br = mechanize.Browser()
>> br.set_handle_robots(False)
>> br.open(url)
>> br.select_form(name="x")
>> br["q"] = str(new)
>> res = br.submit()
>> content = res.read()
>> with open("result1.xls", "w") as f:
>> f.write(content)
>> fun()
>> Go = Button(obj,text="GO",width=5,command=fun)
>> Label.pack()
>> search.pack()
>> Go.pack()
>> mainloop()
>> 
> 
> It is possible to display the output in my GUI itself?

Instead of the strings "searching", "writing result", and "done" you could 
put search results into the queue and display them in a Text widget. To 
signal when you're done you can use an out-of-band value, typically None.

See also .


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


Re: Rename file without overwriting existing files

2017-01-30 Thread Jussi Piitulainen
Peter Otten writes:

> Jussi Piitulainen wrote:
>
>> Peter Otten writes:
>> 
>>> Steve D'Aprano wrote:
>
 The wider context is that I'm taking from 1 to 
 path names to existing files as arguments, and for each path name I
 transfer the file name part (but not the directory part) and then rename
 the file. For example:
 
 foo/bar/baz/spam.txt

 may be renamed to:

 foo/bar/baz/ham.txt
 
 but only provided ham.txt doesn't already exist.
>>>
>>> Google finds
>>>
>>> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions
>>>
>>> and from a quick test it appears to work on Linux:
>> 
>> It doesn't seem to be documented. 
>
> For functions with a C equivalent a look into the man page is usually
> helpful.

Followed by a few test cases to see what Python actually does, at least
in those particular test cases, I suppose. Yes.

But is it a bug in Python if a Python function *doesn't* do what the
relevant man page in the user's operating system says? Or whatever the
user's documentation entry is called. For me, yes, it's a man page.

>> I looked at help(os.link) on Python
>> 3.4 and the corresponding current library documentation on the web. I
>> saw no mention of what happens when dst exists already.
>> 
>> Also, creating a hard link doesn't seem to work between different file
>> systems, which may well be relevant to Steve's case.
>
> In his example above he operates inside a single directory. Can one
> directory spread across multiple file systems?

Hm, you are right, he does say he's working in a single directory.

But *I'm* currently working on processes where results from a batch
system are eventually moved to another directory, and I have no control
over the file systems. So while it was interesting to learn about
os.link, I cannot use os.link here; on the other hand, I can use
shutil.move, and in my present case it will only accidentally overwrite
a file if I've made a programming mistake myself, or if the underlying
platform is not working as advertised, so I'm in a different situation.

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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Meeran Rizvi
On Monday, January 30, 2017 at 8:19:47 PM UTC+5:30, Peter Otten wrote:
> Meeran Rizvi wrote:
> 
> > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
> >> Hello Guys,
> >> Here i am creating a GUI which will act as a search engine that will find
> >> the results from the browser and save the results as a xls file. When i
> >> typed something in my search box and click the (GO)button.It should
> >> display search in progress.when the file is saved it should display done.
> >> How to do that? My button gets hung for a seconds.We should give any
> >> timeout for that?
> >> 
> >> Here is my script
> >> 
> >> from Tkinter import *
> >> import mechanize
> >> def clear_search(event):
> >> search.delete(0,END)
> >> obj = Tk()
> >> Label = Label(obj,text="Top Findings:",font="-weight bold")
> >> search = Entry(obj,width=100)
> >> search.insert(0, "Enter the value to search")
> >> search.bind("", clear_search)
> >> def fun():
> >> new = search.get()
> >> url = "http://duckduckgo.com/html";
> >> br = mechanize.Browser()
> >> br.set_handle_robots(False)
> >> br.open(url)
> >> br.select_form(name="x")
> >> br["q"] = str(new)
> >> res = br.submit()
> >> content = res.read()
> >> with open("result1.xls", "w") as f:
> >> f.write(content)
> >> fun()
> >> Go = Button(obj,text="GO",width=5,command=fun)
> >> Label.pack()
> >> search.pack()
> >> Go.pack()
> >> mainloop()
> >> 
> > 
> > It is possible to display the output in my GUI itself?
> 
> Instead of the strings "searching", "writing result", and "done" you could 
> put search results into the queue and display them in a Text widget. To 
> signal when you're done you can use an out-of-band value, typically None.
> 
> See also .

I just want to save it as a xls file and also from xls file i need to get the 
data and display in a text box.
 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Terry Reedy

On 1/30/2017 8:58 AM, Peter Otten wrote:

Jussi Piitulainen wrote:



It doesn't seem to be documented.


For functions with a C equivalent a look into the man page is usually
helpful.


Man pages do not exist on Windows.  I suspect that there are more 
individual Python programs on Windows than *nix.  I am more sure of this 
as applied to beginners, most of whom have no idea what a 'man' page is 
(sexist docs? ;-).


--
Terry Jan Reedy

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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Meeran Rizvi
On Monday, January 30, 2017 at 8:26:07 PM UTC+5:30, Meeran Rizvi wrote:
> On Monday, January 30, 2017 at 8:19:47 PM UTC+5:30, Peter Otten wrote:
> > Meeran Rizvi wrote:
> > 
> > > On Monday, January 30, 2017 at 12:02:40 PM UTC+5:30, Meeran Rizvi wrote:
> > >> Hello Guys,
> > >> Here i am creating a GUI which will act as a search engine that will find
> > >> the results from the browser and save the results as a xls file. When i
> > >> typed something in my search box and click the (GO)button.It should
> > >> display search in progress.when the file is saved it should display done.
> > >> How to do that? My button gets hung for a seconds.We should give any
> > >> timeout for that?
> > >> 
> > >> Here is my script
> > >> 
> > >> from Tkinter import *
> > >> import mechanize
> > >> def clear_search(event):
> > >> search.delete(0,END)
> > >> obj = Tk()
> > >> Label = Label(obj,text="Top Findings:",font="-weight bold")
> > >> search = Entry(obj,width=100)
> > >> search.insert(0, "Enter the value to search")
> > >> search.bind("", clear_search)
> > >> def fun():
> > >> new = search.get()
> > >> url = "http://duckduckgo.com/html";
> > >> br = mechanize.Browser()
> > >> br.set_handle_robots(False)
> > >> br.open(url)
> > >> br.select_form(name="x")
> > >> br["q"] = str(new)
> > >> res = br.submit()
> > >> content = res.read()
> > >> with open("result1.xls", "w") as f:
> > >> f.write(content)
> > >> fun()
> > >> Go = Button(obj,text="GO",width=5,command=fun)
> > >> Label.pack()
> > >> search.pack()
> > >> Go.pack()
> > >> mainloop()
> > >> 
> > > 
> > > It is possible to display the output in my GUI itself?
> > 
> > Instead of the strings "searching", "writing result", and "done" you could 
> > put search results into the queue and display them in a Text widget. To 
> > signal when you're done you can use an out-of-band value, typically None.
> > 
> > See also .
> 
> I just want to save it as a xls file and also from xls file i need to get the 
> data and display in a text box.

Or either i just display the output in my text widget and also save it as an 
xls file
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Joseph L. Casale
> What do you mean by "both platforms"? Python scripts already run on
> three major operating systems (Win/Lin/Mac) and a good number of
> less-popular OSes; a well-written Python script will run in four major
> Pythons (CPython, PyPy, Jython, IronPython) and a number of others;
> and all manner of CPU architectures and so on.

I meant exactly what you did, in hindsight it was less than perfect wording
on my part...

Thanks,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Chris Angelico wrote:

> On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__pete...@web.de> wrote:
>>> I looked at help(os.link) on Python
>>> 3.4 and the corresponding current library documentation on the web. I
>>> saw no mention of what happens when dst exists already.
>>>
>>> Also, creating a hard link doesn't seem to work between different file
>>> systems, which may well be relevant to Steve's case.
>>
>> In his example above he operates inside a single directory. Can one
>> directory spread across multiple file systems?
> 
> Yep. Try unionfs.
> 
> ... make ourselves some scratch space ...
> $ mkdir space modifier
> $ dd if=/dev/zero of=space.img bs=4096 count=65536
> $ mkfs space.img
> $ sudo mount space.img space
> $ dd if=/dev/zero of=modifier.img bs=4096 count=1024\
> $ mkfs modifier.img
> $ sudo mount modifier.img modifier
> 
> ... put some content into the base directory ...
> $ sudo -e space/demo.txt
> 
> ... and now the magic:
> $ unionfs modifier=RW:space=RO joiner/
> $ cd joiner
> 
> At this point, you're in a directory that is the union of the two
> directories. One of them is read-only, the other is read/write. It is
> thus possible to view a file that you can't hard-link to a new name,
> because the new name would have to be created in the 'modifier' file
> system, but the old file exists on the 'space' one.

Interesting example, thanks!

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


Re: Display a label while pressing the button in my GUI

2017-01-30 Thread Peter Otten
Meeran Rizvi wrote:

>  I just want to save it as a xls file and also from xls file i need to get
>  the data and display in a text box.
> 
> Or either i just display the output in my text widget and also save it as
> an xls file

What exactly you want to do is up to you; just try your hands at it and come 
back here when you run into problems.
Keep in mind that whatever you do in the GUI thread you should only spend a 
small amount of time on it at once. 

If the task is big break it into parts, e. g. instead of reading a complete 
web page or file break it into smaller blocks that you insert into the 
widget one after another.

As to the XLS file, I recommend that you first try the conversion from HTML 
to XLS in a small separate script that reads HTML from a file on your 
computer. Once it works is should be easy to integrate a working conversion 
function into a working Search GUI.

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Peter Otten <__pete...@web.de> wrote:
> Jon Ribbens wrote:
>> On 2017-01-30, Peter Otten <__pete...@web.de> wrote:
>>> However, the current Python version of link() is sufficiently different
>>> from
>>>, say, to warrant its own documentation.
>> 
>> What are you referring to here? As far as I can see, the current
>> Python implementation of link() just calls the underlying OS call
>> directly.
>
> The current signature differs from that of link()
>
> os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)
>
> but it looks like you are right in so far as link() is still called by 
> default:

Yeah, it's been extended to call linkat() if the extra parameters are
provided, but it still calls link() if you call it like link().

So basically it's been extended analogously to how Linux has been,
and the OS manpage is still the right place to look to understand
what it does. (linkat() is documented as part of the same manpage as
link() anyway.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Grant Edwards
On 2017-01-30, Jussi Piitulainen  wrote:

> It doesn't seem to be documented. I looked at help(os.link) on Python
> 3.4 and the corresponding current library documentation on the web. I
> saw no mention of what happens when dst exists already.

The functions in the os module are thin-as-possible wrappers around
the OS's libc functions.  The authors of the os module don't really
have any way of knowing the details of what your os/libc combination
is going to do.

If you're calling os.foo(), you're sort of expected to know what foo()
does on your OS.

> Also, creating a hard link doesn't seem to work between different file
> systems, which may well be relevant to Steve's case. I get:
>
> OSError: [Errno 18] Invalid cross-device link: [snip]
>
> And that also is not mentioned in the docs.

Again, that's a detail that depends on your particular
OS/libc/filesystem implementation.  It's not determined by nor
knowable by the authors of the os module.

-- 
Grant Edwards   grant.b.edwardsYow! I'm ANN LANDERS!!
  at   I can SHOPLIFT!!
  gmail.com

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


Re: Rename file without overwriting existing files

2017-01-30 Thread Grant Edwards
On 2017-01-30, Terry Reedy  wrote:
> On 1/30/2017 8:58 AM, Peter Otten wrote:
>> Jussi Piitulainen wrote:
>
>>> It doesn't seem to be documented.
>>
>> For functions with a C equivalent a look into the man page is usually
>> helpful.
>
> Man pages do not exist on Windows.  I suspect that there are more 
> individual Python programs on Windows than *nix.  I am more sure of this 
> as applied to beginners, most of whom have no idea what a 'man' page is 
> (sexist docs? ;-).

IMO, beginners shouldn't be using the os module.  This is implied by
the first parapgraph of the doc:

 If you just want to read or write a file see open(), if you want
 to manipulate paths, see the os.path module, and if you want to
 read all the lines in all the files on the command line see the
 fileinput module. For creating temporary files and directories
 see the tempfile module, and for high-level file and directory
 handling see the shutil module.

When you're using the OS module, you're just writing C code in Python.
[Which, BTW, is a _very_ useful thing to be able to do, but it's
probably not what beginners are trying to do.]

I always found the first sentence to be a bit funny:

 This module provides a portable way of using operating system
 dependent functionality.

I understand whay they're tying to say, but I always found it amusing
to say you're going to provide a portable way to do something
non-portable.

-- 
Grant Edwards   grant.b.edwardsYow! Somewhere in DOWNTOWN
  at   BURBANK a prostitute is
  gmail.comOVERCOOKING a LAMB CHOP!!

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


Re: count points where is within the polygons using shapely and fiona

2017-01-30 Thread Xristos Xristoou
tell your proposal to count
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 2:00 AM, "Chris Angelico"  wrote:

(Interestingly, Ellipsis is not
included in that.)


Perhaps because it's rather unusual for a program to depend upon the value
of Ellipsis.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 1:32 AM, "Irv Kalb"  wrote:

I teach intro to programming using Python.  In my first assignment,
students are asked to assign variables of different types and print out the
values.

One student (who really did not understand Booleans) turned in the
following for his/her interpretation of Booleans (Python 2.7):

True = 'shadow'
False = 'light'
print "If the sun is behind a cloud, there is", True
print "If it is a clear day, there is", False

And it printed:

If the sun is behind a cloud, there is shadow
If it is a clear day, there is light


It seems very odd that Python allows you to override the values of True and
False.  In the code, True and False were clearly recognized as keywords as
they were colored purple.  But there was no error message.


Plenty of people have remarked that this is fixed in Python 3, but nobody
that I see has explained yet the reason for this behavior in the first
place. True and False are not keywords in Python 2 for backward
compatibility. Many versions ago, Python did not define True and False at
all. It was common in that era to lead scripts with:

False = 0
True = 1

To define useful boolean constants. When the built-in constants were added,
the developers did not want to break programs that followed this pattern.

This is also the reason why bool is a subclass of int -- so that False == 0
and True == 1 remain true for programs that depend on this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Rename file without overwriting existing files

2017-01-30 Thread Marko Rauhamaa
Grant Edwards :

> IMO, beginners shouldn't be using the os module.

Hard to know. Depends on what the beginner wants to accomplish.

> I always found the first sentence to be a bit funny:
>
>  This module provides a portable way of using operating system
>  dependent functionality.
>
> I understand whay they're tying to say, but I always found it amusing
> to say you're going to provide a portable way to do something
> non-portable.

One of the best things in Python is that it has exposed the operating
system to the application programmer. I don't think a programming
language should abstract the operating system away.


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


What library/package to use for parsing XML?

2017-01-30 Thread Chris Green
I want to parse some XML data, it's the address book data from the
linux program osmo.  The file I want to parse is like this:-



  
  

  None
  Peter and Denise
  Smith
  0
  0
  Some address
  AAA BBB
  Oxfordshire

  



I basically want to be able to extract the data and output in other
formats - e.g. write to a Sqlite3 database and/or CSV.

So what do I need to import (and probably install first) to do this?


-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Juan C.
On Sun, Jan 29, 2017 at 1:06 AM, Juan C.  wrote:
>
> As you guys might know, .NET Core is up and running, promising a 
> "cross-platform, unified, fast, lightweight, modern and open source 
> experience" (source: .NET Core official site). What do you guys think about 
> it? Do you think it will be able to compete with and overcome Python in the 
> opensource medium?

Oh, I forgot to say, I was indeed talking about .NET Core in general,
the framework, but I also had C#/ASP .NET Core in mind. The real
comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I
personally had to work with C# a few times, it's a great language, but
my main problem was that it was Windows only and most of my personal
programs run on Linux. Yes, we have Mono, but it really didn't feel
"right" to me, it seemed like a workaround, I wanted something really
"native".

Python still has my heart, but .NET Core tempts me. One great thing of
coding in C# would be no GIL.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Joseph L. Casale
> Python still has my heart, but .NET Core tempts me. One great thing of
> coding in C# would be no GIL.

Seriously, check out the benchmarks at https://github.com/aspnet/benchmarks.

I think aside from the obvious, you'll find the Razor engine and the overall 
library
to be a pleasure to work with. The framework is truly a representation of what
a high quality, brilliantly engineered piece of software looks like.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Irmen de Jong
On 30-1-2017 21:33, Joseph L. Casale wrote:
>> Python still has my heart, but .NET Core tempts me. One great thing of
>> coding in C# would be no GIL.
> 
> Seriously, check out the benchmarks at https://github.com/aspnet/benchmarks.

Results vary quite a bit there.
For instance take the json serialization benchmark:
https://www.techempower.com/benchmarks/#section=data-r13&hw=ph&test=json
There's 6 python frameworks performing better than aspcore in this chart...


Irmen

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


Re: What library/package to use for parsing XML?

2017-01-30 Thread Irmen de Jong
On 30-1-2017 18:58, Chris Green wrote:
> I want to parse some XML data, it's the address book data from the
> linux program osmo.  The file I want to parse is like this:-
> 

[snip]

> 
> I basically want to be able to extract the data and output in other
> formats - e.g. write to a Sqlite3 database and/or CSV.
> 
> So what do I need to import (and probably install first) to do this?

No need to install anything. Python has batteries included.

Parse your XML with xml.etree.ElementTree
[https://docs.python.org/3/library/xml.etree.elementtree.html]
Read/write CSV with csv
[https://docs.python.org/3/library/csv.html]
Put stuff in sqlite3 database and query it using sqlite3
[https://docs.python.org/3/library/sqlite3.html]


-irmen


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


Re: What library/package to use for parsing XML?

2017-01-30 Thread Juan C.
On Mon, Jan 30, 2017 at 3:58 PM, Chris Green  wrote:
> I want to parse some XML data, it's the address book data from the
> linux program osmo.  The file I want to parse is like this:-

Just like Irmen said, use the default xml.etree.ElementTree, it's
amazing and very simple to use.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What library/package to use for parsing XML?

2017-01-30 Thread Rob Gaddi

On 01/30/2017 02:14 PM, Juan C. wrote:

On Mon, Jan 30, 2017 at 3:58 PM, Chris Green  wrote:

I want to parse some XML data, it's the address book data from the
linux program osmo.  The file I want to parse is like this:-


Just like Irmen said, use the default xml.etree.ElementTree, it's
amazing and very simple to use.



And if you get halfway into your project and find ElementTree was 
insufficient, you can switch to lxml without changing practically 
anything you've already written.


--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list


How to write libreoffice python macros in venv?

2017-01-30 Thread Jim
I have all the necessary libreoffice modules installed in my Mint 18.1 
system to allow me to write libreoffice calc macros in python.


I now have venv installed. If I try to import uno for a calc macro in it 
I get an error that there is no uno module.


How can I get my venv to find uno and the other necessary modules so I 
can write calc macros in it?


Thanks,  Jim


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


Re: Overriding True and False ?

2017-01-30 Thread Steve D'Aprano
On Tue, 31 Jan 2017 03:12 am, Ian Kelly wrote:

> On Jan 30, 2017 1:32 AM, "Irv Kalb"  wrote:
> 
> I teach intro to programming using Python.  In my first assignment,
> students are asked to assign variables of different types and print out
> the values.
[...]

Hey Ian,

Your news reader or mail client has stopped quoting the text you are
quoting, so it appears as if you have written it.

See:

https://mail.python.org/pipermail/python-list/2017-January/719015.html



-- 
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: Rename file without overwriting existing files

2017-01-30 Thread Ben Finney
Peter Otten <__pete...@web.de> writes:

> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions
>
> and from a quick test it appears to work on Linux:

By “works on Linux”, I assume you mean “works on filesystems that use
inodes and hard links”. That is not true for all filesystems, even on
Linux.

-- 
 \  “[Entrenched media corporations will] maintain the status quo, |
  `\   or die trying. Either is better than actually WORKING for a |
_o__)  living.” —ringsnake.livejournal.com, 2007-11-12 |
Ben Finney

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Michael Torrie
On 01/30/2017 11:44 AM, Juan C. wrote:
> On Sun, Jan 29, 2017 at 1:06 AM, Juan C.  wrote:
>>
>> As you guys might know, .NET Core is up and running, promising a 
>> "cross-platform, unified, fast, lightweight, modern and open source 
>> experience" (source: .NET Core official site). What do you guys think about 
>> it? Do you think it will be able to compete with and overcome Python in the 
>> opensource medium?
> 
> Oh, I forgot to say, I was indeed talking about .NET Core in general,
> the framework, but I also had C#/ASP .NET Core in mind. The real
> comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I
> personally had to work with C# a few times, it's a great language, but
> my main problem was that it was Windows only and most of my personal
> programs run on Linux. Yes, we have Mono, but it really didn't feel
> "right" to me, it seemed like a workaround, I wanted something really
> "native".

C# under Mono is as native as C# on the DotNet runtime on Windows or
DotNet core on Linux.  However now that DotNet Core is open source and
supported on Linux, Mono is likely to fade away since all of the Xamarin
developers work for MS now.

> Python still has my heart, but .NET Core tempts me. One great thing of
> coding in C# would be no GIL.

C# hardly seems any better than Java to me as far as a language goes.
Being forced into working with classes even when they are not
appropriate is jarring.  Because everything is forced into a class, one
often ends up with very long classes in C#, spanning more than one file!
 Makes the code much harder to follow from a human point of view. After
working in C# I very much appreciate Python's explicit self requirement
for accessing local instance variables.


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


What are your opinions on .NET Core vs Python?

2017-01-30 Thread Gerald Britton
On Sun, Jan 29, 2017 at 1:06 AM, Juan C. https://mail.python.org/mailman/listinfo/python-list>> wrote:
>
> >
> >* As you guys might know, .NET Core is up and running, promising a 
> >"cross-platform, unified, fast, lightweight, modern and open source 
> >experience" (source: .NET Core official site). What do you guys think about 
> >it? Do you think it will be able to compete with and overcome Python in the 
> >opensource medium?
> *

Oh, I forgot to say, I was indeed talking about .NET Core in general,
> the framework, but I also had C#/ASP .NET Core in mind. The real
> comparison here would be C#/ASP .NET Core vs Python/Django,Flask. I
> personally had to work with C# a few times, it's a great language, but
> my main problem was that it was Windows only and most of my personal
> programs run on Linux. Yes, we have Mono, but it really didn't feel
> "right" to me, it seemed like a workaround, I wanted something really
> "native".

Python still has my heart, but .NET Core tempts me. One great thing of
> coding in C# would be no GIL.

For what it's worth, IronPython, which is implemented in .NET, has no GIL
and doesn't need it since ir runs on the CLR.  That means that, for some
things, IronPython can be more performant.

No word yet if the IronPython project intends to port to .NET core or
enable to run it on OS's other than Windows.

Also, it's worth noting that the PythonNet project has a branch working on
.NET core support


-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Joseph L. Casale
> C# hardly seems any better than Java to me as far as a language goes.

Which sounds pretty good to me, they are both high performance, mature
and rich languages.

> Being forced into working with classes even when they are not
> appropriate is jarring.

And 100% irrelevant, it doesn't prevent you from doing anything you
otherwise could without.

> Because everything is forced into a class, one
> often ends up with very long classes in C#, spanning more than one file!

Sorry, sounds like you need to learn SOLID, none of my classes
have ever taken this form.

>  Makes the code much harder to follow from a human point of view. After
> working in C# I very much appreciate Python's explicit self requirement
> for accessing local instance variables.

So, prefix them with "this." and they will look the same? 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Nathan Ernst
I mostly agree with this

On Mon, Jan 30, 2017 at 7:18 PM, Joseph L. Casale  wrote:

> > C# hardly seems any better than Java to me as far as a language goes.
>
> Which sounds pretty good to me, they are both high performance, mature
> and rich languages.
>
> > Being forced into working with classes even when they are not
> > appropriate is jarring.
>
> And 100% irrelevant, it doesn't prevent you from doing anything you
> otherwise could without.
>
> > Because everything is forced into a class, one
> > often ends up with very long classes in C#, spanning more than one file!
>
> Sorry, sounds like you need to learn SOLID, none of my classes
> have ever taken this form.
>
There is no reason you cannot introduce a static class with pure static
members (i.e. the Math class in System). A static class effectively becomes
another namespace in C++ parlance. I'll admit the syntax is a bit odd, and
enforces you, at a minimum to use the outer name a as a qualifier, but it's
effectively the same effect.

>
> >  Makes the code much harder to follow from a human point of view. After
> > working in C# I very much appreciate Python's explicit self requirement
> > for accessing local instance variables.
>
> So, prefix them with "this." and they will look the same?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
self vs this, and you might start a language holy war. Of course "self" is
the preferred to refer to an instance, just by python convention. Whether I
agree with or not, I've seen "this" used to refer to instances where it is
ambiguous in Python parlance between a "self" instance or a class instance
- those instances are a bit weird, but there are rare occasions you do want
to override class variables where this makes sense (they should be rare -
because it's generally very hard to follow).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Michael Torrie
On 01/30/2017 06:18 PM, Joseph L. Casale wrote:
> Which sounds pretty good to me, they are both high performance, mature
> and rich languages.

Sure it's a matter of personal preference and need.  I happen to find
the expressivity and flexibility of Python (warts and all) to be rather
liberating compared to Java.  I find Java rather stifling particularly
because of the forced OOP structure.

Not really sure what you mean by "rich language."  Java has a rich
runtime library and a rich ecosystem of utility libraries to draw on.
So does C#.  And so does Python.

> Sorry, sounds like you need to learn SOLID, none of my classes
> have ever taken this form.

Never said they were my classes, or even my programs.  What is this
"SOLID" thing?

In any case, partial classes seems like a misfeature of C# to me but
apparently they are used when making Windows Forms GUI-based apps.
Apparently VS autogenerates the class that backs the form, and then the
developer creates a partial class that extends that to flesh out the
logic and handle the events.  I'm surprised you haven't encountered this.

>>  Makes the code much harder to follow from a human point of view. After
>> working in C# I very much appreciate Python's explicit self requirement
>> for accessing local instance variables.
> 
> So, prefix them with "this." and they will look the same? 

Probably.  But C# developers never do that (nor is it normal to do that
in C++ either).  Not sure what your point is there. I was merely saying
that I now appreciate the logic of the Python explicit self.

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Chris Angelico
On Tue, Jan 31, 2017 at 12:57 PM, Michael Torrie  wrote:
>> Sorry, sounds like you need to learn SOLID, none of my classes
>> have ever taken this form.
>
> Never said they were my classes, or even my programs.  What is this
> "SOLID" thing?

https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Five well-respected principles that every class designer should be
aware of. You won't necessarily follow them all slavishly, but know
what the rules are and why you're breaking them.

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Michael Torrie
On 01/30/2017 06:52 PM, Nathan Ernst wrote:
> self vs this, and you might start a language holy war.

Actually no, you misread his point. He was speaking of C#, not Python.
In C#, the only word you can use is "this."  He was saying that you can
use the explicit self paradigm in C#. Simply prefix each member variable
with "this."


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


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 6:07 PM, "Steve D'Aprano" 
wrote:
> Hey Ian,
>
> Your news reader or mail client has stopped quoting the text you are
> quoting, so it appears as if you have written it.
>
> See:
>
> https://mail.python.org/pipermail/python-list/2017-January/719015.html

Well, nuts. It looks fine in my client (the Gmail Android app) so I guess
this must be an issue of HTML versus text content. Unfortunately the mobile
client doesn't have any option for text-only that I can find, so my options
appear to be to laboriously replace the quoting as I've done here, or just
stop using the mobile client.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ben Finney
Ian Kelly  writes:

> Well, nuts. It looks fine in my client (the Gmail Android app) so I
> guess this must be an issue of HTML versus text content. Unfortunately
> the mobile client doesn't have any option for text-only that I can
> find, so my options appear to be to laboriously replace the quoting as
> I've done here, or just stop using the mobile client.

For composing discussion posts, yes, I recommend people stop using
mobile clients. It can wait until you're at a real keyboard and a decent
authoring environment.

-- 
 \  “Very few things happen at the right time, and the rest do not |
  `\ happen at all. The conscientious historian will correct these |
_o__)  defects.” —Mark Twain, _A Horse's Tale_ |
Ben Finney

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Gregory Ewing

Nathan Ernst wrote:

There is no reason you cannot introduce a static class with pure static
members (i.e. the Math class in System). A static class effectively becomes
another namespace in C++ parlance. I'll admit the syntax is a bit odd, and
enforces you, at a minimum to use the outer name a as a qualifier,


That's the thing I find most frustrating about both C# and Java,
the lack of anything like Python's "from ... import ...". You get
a choice of either effectively doing "import *" on the contents
of a whole class and polluting your namespace, or Fully.Qualifying.
Every.Little.Thing.

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


Re: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Gregory Ewing

Michael Torrie wrote:

He was saying that you can
use the explicit self paradigm in C#. Simply prefix each member variable
with "this."


One can do that in one's own code, but it doesn't help
you to read the code of someone else who hasn't done
that. Since it's not part of the C# culture, the vast
majority of other C# code you encounter won't be using
it.

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


Re: Overriding True and False ?

2017-01-30 Thread Christian Gollwitzer

Am 31.01.17 um 04:27 schrieb Ian Kelly:

On Jan 30, 2017 6:07 PM, "Steve D'Aprano" 
wrote:

Hey Ian,

Your news reader or mail client has stopped quoting the text you are
quoting, so it appears as if you have written it.

See:

https://mail.python.org/pipermail/python-list/2017-January/719015.html


Well, nuts. It looks fine in my client (the Gmail Android app) so I guess
this must be an issue of HTML versus text content. Unfortunately the mobile
client doesn't have any option for text-only that I can find, so my options
appear to be to laboriously replace the quoting as I've done here, or just
stop using the mobile client.


This one looks fine, FYI.

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


Re: RE: What are your opinions on .NET Core vs Python?

2017-01-30 Thread Bob Martin
in 770457 20170131 011814 "Joseph L. Casale"  wrote:
>> C# hardly seems any better than Java to me as far as a language goes.
>
>Which sounds pretty good to me, they are both high performance, mature
>and rich languages.
>
>> Being forced into working with classes even when they are not
>> appropriate is jarring.
>
>And 100% irrelevant, it doesn't prevent you from doing anything you
>otherwise could without.
>
>> Because everything is forced into a class, one
>> often ends up with very long classes in C#, spanning more than one file!
>
>Sorry, sounds like you need to learn SOLID, none of my classes
>have ever taken this form.
>
>>  Makes the code much harder to follow from a human point of view. After
>> working in C# I very much appreciate Python's explicit self requirement
>> for accessing local instance variables.
>
>So, prefix them with "this." and they will look the same?

Please include the identity of people you are quoting.
-- 
https://mail.python.org/mailman/listinfo/python-list