Re: String slices

2019-08-10 Thread Peter J. Holzer
On 2019-08-10 09:10:12 +1000, Cameron Simpson wrote:
> On 09Aug2019 22:28, Paul St George  wrote:
> > On 09/08/2019 16:29, Rhodri James wrote:
> > > (Actually I would probably use outstream.write() and do my own
> > > formatting, but let's not get side-tracked ;-)
> > > 
> > I would love to hear your outstream.write() side-track!
> 
> I am not Rhodri James, but you can just write strings to text files:
> 
>  outstream.write("X: ")  # note, includes the space separator
>  outstream.write(str(thing[0]))
>  outstring.write("\n")

You can also format the string before passing it to write (which is
probably what Rhodri meant by "do my own formatting", like this:

outstream.write("X: %7.2f\n" % thing[0])

or this

outstream.write("X: {0:7.2f}\n".format(thing[0]))

or (since Python 3.6) this:

outstream.write(f"X: {thing[0]:7.2f}\n")

There are of course many variants to all three methods.

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | [email protected] | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: String slices

2019-08-10 Thread Paul St George


On 10/08/2019 17:35, Dennis Lee Bieber wrote:

On Sat, 10 Aug 2019 11:45:43 +0200, "Peter J. Holzer"
declaimed the following:



There are of course many variants to all three methods.

And then one can get downright nasty...


X = 3.14
Y = 2.78
Z = 6.226E23
print("".join(["Plane rotation %s: %s\n" % (nm, vl)

... for (nm, vl) in [("X", X), ("Y", Y), ("Z", Z)]]))
Plane rotation X: 3.14
Plane rotation Y: 2.78
Plane rotation Z: 6.226e+23

(replace "print" with "outstream.write" for use with the file)


|outstream.write| could be very useful, thank you Peter and Cameron 
(neither being Rhodri James). If I wanted to learn more about formatting 
strings is there a better place to go than:


https://docs.python.org/release/3.6.5/library/string.html?highlight=string#format-string-syntax

https://pyformat.info

https://python-reference.readthedocs.io/en/latest/docs/str/formatting.html

And Dennis, whatever you did there is very impressive and works 
perfectly but I don’t know enough to be able to use it. Please will you 
say more or direct me to some reference? I couldn’t find ‘nasty’ in the 
Python docs.

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


Re: class definition question

2019-08-10 Thread Cameron Simpson

On 07Aug2019 16:36, Terry Reedy  wrote:

On 8/7/2019 3:26 PM, Manfred Lotz wrote:

On Wed, 07 Aug 2019 14:39:00 -0400
Dennis Lee Bieber  wrote:

On Wed, 7 Aug 2019 20:11:15 +0200, Manfred Lotz 
declaimed the following:

More often I see something like this:

class Myclass:
...
but sometimes I see
class Myclass(object):
...

Question: which way is preferable?


It's historical...

Python v1.x had a form of classes.

Python v2.x introduced "new-style" classes. "New-style"
classes /had/ to inherit from "object", as they had different
behavior from "v1.x old-style" classes which were still supported (it
would have broken too many programs). Old-style were then deprecated,
and one should have used new-style for new code.

Python v3.x unified (removed old-style behavior differences)
and all classes inherit from "object" whether one specifies object or
not.


Thanks a lot for the explanations.

As a Python newbie (with no Pythons legacies) I only deal with Python 3.
So, I will happily ignore 'object'.


That is preferred, because it is extra work to write and read, with no 
benefit, and because myclass(object) can be seen as implying that the 
code once ran or is still meant to be compatible with Python 2.


By contrast, I generally use MyClass(object) because I try to write 
Python 2/3 compatible code unless I have a reason not to.


If I _know_ I'll never use a module with Python 2, the bare MyClass: is 
indeed preferable. However, a lot of my code is in modules which might 
conceivably be used by anyone. Artificially contraining their use seems 
counterproductive.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


NotADirectoryError: [Errno 20] Not a directory

2019-08-10 Thread Youssef Abdelmohsen
Note: Beginner

I'm trying to create an html parser that will go through a folder and all
its subfolders and export all html files without any html tags, in file
formats CSV and TXT with each html labeled with the title of the web page
in a new CSV and TXT.

However I keep getting an error saying:




*"Traceback (most recent call last):  File
"/Users/username/Documents/htmlparser/parser10.py", line 59, in 
for subentry in os.scandir(entry.path):NotADirectoryError: [Errno 20] Not a
directory: '/Users/username/site/.DS_Store'"*

Here's what I've done so far (I have bolded line 59):

"
import bs4 as bs
import csv
import glob
import os
import re

directory = "/Users/username/site"


with os.scandir(directory) as it:
for entry in it:
if ".html" in entry.name or
re.match(r'.*?(?:\.html?$|\.html?\?.*)', entry.name) is not None:
print(entry.name, entry.path)
my_data = (entry)
listofp = []
soup = bs.BeautifulSoup(open(my_data, "r").read())
for paragraph in soup.find_all('p'):
listofp.append(paragraph.string)
title = soup.title.string
leftitle = [title]
listception = [leftitle]
for moreshit in soup.find_all('h1', 'h2', 'h3', 'h4', 'h5'):
listception.append([str(moreshit.text)])
for paragraph in soup.find_all('p'):
listception.append([str(paragraph.text)])
for elements in soup.find_all('li', 'td', 'div', 'span'):
listception.append([str(elements.text)])
for evenmoreshit in soup.find_all('h6', 'a'):
listception.append([str(evenmoreshit.text)])
num = 0
with open('export/' + title + '.csv', 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerows(listception)

file_path = os.path.join(directory, entry)
text = open(file_path, mode='r').read()
results = str(listception).strip('[]')
results = results.replace("[", " ")
results = results.replace("]", " ")
results = results.replace("""\n""", " ")
results_dir = "/Users/username/site/done"
results_file = title + '.txt'
file_path = os.path.join(results_dir, results_file)
open(file_path, mode='w', encoding='UTF-8').write(results)
continue


*for subentry in os.scandir(entry.path):*for file in
os.scandir(subentry.path):
if ".html" in entry.name or
re.match(r'.*?(?:\.html?$|\.html?\?.*)', entry.name) is not None:
print(entry.name, entry.path)
my_data = (entry)
listofp = []
soup = bs.BeautifulSoup(open(my_data, "r").read())
for paragraph in soup.find_all('p'):
listofp.append(paragraph.string)
title = soup.title.string
leftitle = [title]
listception = [leftitle]
for moreshit in soup.find_all('h1', 'h2', 'h3', 'h4',
'h5'):
listception.append([str(moreshit.text)])
for paragraph in soup.find_all('p'):
listception.append([str(paragraph.text)])
for elements in soup.find_all('li', 'td', 'div',
'span'):
listception.append([str(elements.text)])
for evenmoreshit in soup.find_all('h6', 'a'):
listception.append([str(evenmoreshit.text)])
num = 0
with open('export/' + title + '.csv', 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerows(listception)

file_path = os.path.join(directory, entry)
text = open(file_path, mode='r').read()
results = str(listception).strip('[]')
results = results.replace("[", " ")
results = results.replace("]", " ")
results = results.replace("""\n""", " ")
results_dir = "/Users/username/site/done"
results_file = title + '.txt'
file_path = os.path.join(results_dir, results_file)
open(file_path, mode='w',
encoding='UTF-8').write(results)
continue


Would love any help whatsoever or any suggestions of any kind. Thank you
very much!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help with tkinter

2019-08-10 Thread Rob Walton
Doubly agreed. Avoid import * as it makes the code a nightmare to reason
about later amongst other things

On Fri, 9 Aug 2019 at 20:02, Peter J. Holzer  wrote:

> On 2019-08-09 12:43:45 -0600, Michael Torrie wrote:
> > On 8/9/19 4:52 AM, Peter J. Holzer wrote:
> > > You didn't import 'tkinter', you imported all symbols ('*') from
> > > tkinter. So, since you imported colorchooser, you can call
> > > colorchooser.askcolor() (not tkinter.colorchooser.askcolor())
> >
> > The better thing to do is remove the import * line and just "import
> > tkinter" and then make sure all your calls to tkinter objects are fully
> > qualified with the "tkinter." part like you've already been doing.
>
> Agreed.
>
> hp
>
> --
>_  | Peter J. Holzer| we build much bigger, better disasters now
> |_|_) || because we have much more sophisticated
> | |   | [email protected] | management tools.
> __/   | http://www.hjp.at/ | -- Ross Anderson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [poliastro-dev] ANN: poliastro 0.13.0 released 🚀

2019-08-10 Thread Samuel Lelièvre
Le mar. 6 août 2019 à 08:33, Samuel Dupree  a écrit :

> Juan Luis Cano,
>
> When will poliastro ver. 0.13.0 become available from Anaconda? At the
> time of this note, only ver. 0.12.0 is available.
>
> Lastly what is the recommended procedure to update poliastro from vers.
> 0.12.0 to 0.13.0?
>
> Sam Dupree
>

A pull request was automatically created by a bot:

https://github.com/conda-forge/poliastro-feedstock/pull/29

and Juan Luis merged it yesterday, so that the new version is
available since yesterday, see the version badge on the README at

https://github.com/conda-forge/poliastro-feedstock

Clicking on that badge sends to:

https://anaconda.org/conda-forge/poliastro

which has the installation instructions.

Use `conda upgrade` instead of `conda install` if you want to upgrade.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to setup for localhost:8000

2019-08-10 Thread sanketsawla34
please set up my localhost:8000
-- 
https://mail.python.org/mailman/listinfo/python-list