Re: Question about floating point

2018-08-30 Thread Gregory Ewing

Steven D'Aprano wrote:
Why in the name of all that's holy would anyone want to manually round 
each and every intermediate calculation when they could use the Decimal 
module and have it do it automatically?


I agree that Decimal is the safest and probably easiest way to
go, but saying that it "does the rounding automatically" is
a bit misleading.

If you're adding up dollars and cents in Decimal, no rounding
is needed in the first place, because it represents whole
numbers of cents exactly and adds them exactly.

If you're doing something that doesn't result in a whole
number of cents (e.g. calculating a unit price from a total
price and a quantity) you'll need to think about how you want it
rounded, and should probably include an explicit rounding step,
if only for the benefit of someone else reading the code.

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


Re: How to sort over dictionaries

2018-08-30 Thread harish



> > sort = sorted(results, key=lambda res:itemgetter('date'))
> > print(sort)
> > 
> > 
> > I have tried the above code peter but it was showing error like 
> > TypeError: '<' not supported between instances of 'operator.itemgetter'
> > and 'operator.itemgetter'
> 
> lambda res: itemgetter('date')
> 
> is short for
> 
> def keyfunc(res):
> return itemgetter('date')
> 
> i. e. it indeed returns the itemgetter instance. But you want to return 
> res["date"]. For that you can either use a custom function or the 
> itemgetter, but not both.
> 
> (1) With regular function:
> 
> def keyfunc(res):
> return res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (1a) With lambda:
> 
> keyfunc = lambda res: res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (2) With itemgetter:
> 
> keyfunc = itemgetter("date")
> sorted_results = sorted(results, key=keyfunc)
> 
> Variants 1a and 2 can also be written as one-liners.



Thanks PeterNo 2 has worked.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread pjmclenon
On Wednesday, June 13, 2018 at 7:14:06 AM UTC-4, INADA Naoki wrote:
> ​> 1st is this script is from a library module online open source
> 
> If it's open source, why didn't you show the link to the soruce?
> I assume your code is this:
> 
> https://github.com/siddharth2010/String-Search/blob/6770c7a1e811a5d812e7f9f7c5c83a12e5b28877/createIndex.py
> 
> And self.collFile is opened here:
> 
> https://github.com/siddharth2010/String-Search/blob/6770c7a1e811a5d812e7f9f7c5c83a12e5b28877/createIndex.py#L91
> 
> You need to add `encoding='utf-8'` argument.

August 30 2018
reference to same open source script which you solved for unoicode error

hello i have qustion
my script runs correctly ..either in the original state on python 2.7 or after 
several adjustments in python 3

my question is ... at the moment i can only run it on windows cmd prompt with a 
multiple line entry as so::

python createIndex_tfidf.py stopWords.dat testCollection.dat testIndex.dat 
titleIndex.dat

and then to query and use the newly created index as so:

python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat

how can i run just one file at a time?..or actually link to a front end GUI ,so 
when an question or word or words is input to the input box..it can go to the 
actiona dnrun the above mentioned lines of code

any one on the forum know??

if you have the time kindly reply when you have some time

thank you very much
tommy
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread pjmclenon
On Thursday, August 30, 2018 at 8:21:47 AM UTC-4, pjmc...@gmail.com wrote:
> On Wednesday, June 13, 2018 at 7:14:06 AM UTC-4, INADA Naoki wrote:
> > ​> 1st is this script is from a library module online open source
> > 
> > If it's open source, why didn't you show the link to the soruce?
> > I assume your code is this:
> > 
> > https://github.com/siddharth2010/String-Search/blob/6770c7a1e811a5d812e7f9f7c5c83a12e5b28877/createIndex.py
> > 
> > And self.collFile is opened here:
> > 
> > https://github.com/siddharth2010/String-Search/blob/6770c7a1e811a5d812e7f9f7c5c83a12e5b28877/createIndex.py#L91
> > 
> > You need to add `encoding='utf-8'` argument.
> 
> August 30 2018
> reference to same open source script which you solved for unoicode error
> 
> hello i have qustion
> my script runs correctly ..either in the original state on python 2.7 or 
> after several adjustments in python 3
> 
> my question is ... at the moment i can only run it on windows cmd prompt with 
> a multiple line entry as so::
> 
> python createIndex_tfidf.py stopWords.dat testCollection.dat testIndex.dat 
> titleIndex.dat
> 
> and then to query and use the newly created index as so:
> 
> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> 
> how can i run just one file at a time?..or actually link to a front end GUI 
> ,so when an question or word or words is input to the input box..so it can go 
> to the line of code action and run the above mentioned lines of code
> 
> any one on the forum know??
> 
> if you have the time kindly reply when you have some time
> 
> thank you very much
> tommy

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


__init__ patterns

2018-08-30 Thread Tim


I saw a thread on reddit/python where just about everyone said they never put 
code in their __init__ files.

Here's a stackoverflow thread saying the same thing.
https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-package-init-py-files

That's new to me. I like to put functions in there that other modules within 
the module need. 
Thought that was good practice DRY and so forth. And I never do 'from whatever 
import *' 
Ever.

The reddit people said they put all their stuff into different modules and 
leave init empty.

What do you do?  I like my pattern but I'm willing to learn.

thanks,
--Tim
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about floating point

2018-08-30 Thread Steven D'Aprano
On Thu, 30 Aug 2018 19:22:29 +1200, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> Why in the name of all that's holy would anyone want to manually round
>> each and every intermediate calculation when they could use the Decimal
>> module and have it do it automatically?
> 
> I agree that Decimal is the safest and probably easiest way to go, but
> saying that it "does the rounding automatically" is a bit misleading.
> 
> If you're adding up dollars and cents in Decimal, no rounding is needed
> in the first place, because it represents whole numbers of cents exactly
> and adds them exactly.

"Round to exact" is still rounding :-P

I did already say that addition and subtraction was exact in Decimal. (I 
also mentioned multiplication, but that's wrong.)


> If you're doing something that doesn't result in a whole number of cents
> (e.g. calculating a unit price from a total price and a quantity) you'll
> need to think about how you want it rounded, and should probably include
> an explicit rounding step, if only for the benefit of someone else
> reading the code.

If you're not using Banker's Rounding for financial calculations, you're 
probably up to no good *wink*

Of course with Decimal you always have to option to round certain 
calculations by hand, if you have some specific need to. But in general, 
that's just annoying and error-prone book-keeping. The right way is to 
set the rounding mode at the start of your application, and then let the 
Decimal type round each calculation that needs rounding.

The whole point of Decimal, the reason it was invented, was to do this 
sort of thing. We have here a brilliant hammer specially designed for 
banging in just this sort of nail, and you're saying "Well, sure, but you 
probably want to bang it in with your elbow, if only for the benefit of 
onlookers..."

:-)


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

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


Re: __init__ patterns

2018-08-30 Thread Paul Moore
On Thu, 30 Aug 2018 at 14:07, Tim  wrote:
>
> I saw a thread on reddit/python where just about everyone said they never put 
> code in their __init__ files.
>
> Here's a stackoverflow thread saying the same thing.
> https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-package-init-py-files
>
> That's new to me. I like to put functions in there that other modules within 
> the module need.
> Thought that was good practice DRY and so forth. And I never do 'from 
> whatever import *'
> Ever.
>
> The reddit people said they put all their stuff into different modules and 
> leave init empty.
>
> What do you do?  I like my pattern but I'm willing to learn.

What matters is the user interface, not where you put your code
"behind the scenes". If your documented interface is

import foo
foo.do_something()

then it's perfectly OK (IMO) for do_something to be implemented in
foo/__init__.py. It's *also* perfectly OK for it to be implemented in
foo/internals.py and imported into __init__.py. Whatever makes your
development process easier. Of course, if you *document* that
do_something is available as foo.internals.do_something, then you can
no longer take the first option - that's up to you, though :-)

Conversely, if your package interface is

import foo.utilities
foo.utilities.do_something()

then do_something needs to be implemented in foo/utilities.py
(assuming utilities isn't itself a subpackage :-)) Whether you choose
to have a convenience alias of foo.do_something() in that case
determines whether you also need to import it in foo/__init__.py, but
that's fine.

tl;dr; Design your documented interface, and make sure that's as you
(and your users) want it. Don't let anyone tell you how you should
structure your internal code, it's none of their business.

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


RE: __init__ patterns

2018-08-30 Thread Dan Strohl via Python-list
I will put imports into my __init__ files, so that I can import things from the 
module directly instead of having to import from a file in the module.

I almost never put code in the __init__'s, I have a couple of times put in 
something that was designed to modify which routine was imported (i.e., pull 
this module in if it's a windows box, or that one for other.  Or, use this 
routine for py 2.x and that one for 3.x.  Even there, I prefer to do it in the 
files themselves, but sometimes it's just easier to do it at the __init__ level.

I may also put common documentation in the __init__ file that explains the file 
structure for the module,  how to interact with it, etc...

For many shared functions, I try to maintain a common helper module that is 
shared by a number of programs, or a helper file in a module if I need to 
rather than putting it in the __init__

Dan



> -Original Message-
> From: Python-list  On
> Behalf Of Tim
> Sent: Thursday, August 30, 2018 6:01 AM
> To: python-list@python.org
> Subject: __init__ patterns
> 
> EXTERNAL MAIL: python-list-bounces+d.strohl=f5@python.org
> 
> I saw a thread on reddit/python where just about everyone said they never
> put code in their __init__ files.
> 
> Here's a stackoverflow thread saying the same thing.
> https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-
> package-init-py-files
> 
> That's new to me. I like to put functions in there that other modules within
> the module need.
> Thought that was good practice DRY and so forth. And I never do 'from
> whatever import *'
> Ever.
> 
> The reddit people said they put all their stuff into different modules and 
> leave
> init empty.
> 
> What do you do?  I like my pattern but I'm willing to learn.
> 
> thanks,
> --Tim
> --
> https://mail.python.org/mailman/listinfo/python-list

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


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread Steven D'Aprano
On Thu, 30 Aug 2018 05:21:30 -0700, pjmclenon wrote:

> my question is ... at the moment i can only run it on windows cmd prompt
> with a multiple line entry as so::
> 
> python createIndex_tfidf.py stopWords.dat testCollection.dat
> testIndex.dat titleIndex.dat
> 
> and then to query and use the newly created index as so:
> 
> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> 
> how can i run just one file at a time?

I don't understand the question. You are running one file at a time. 
First you run createIndex_tfidf.py, then you run queryIndex_tfidf.py

Maybe you mean to ask how to combine them both to one call of Python?

(1) Re-write the createIndex_tfidf.py and queryIndex_tfidf.py files to be 
in a single file.

(2) Or, create a third file which runs them both one after another.

That third file doesn't even need to be a Python script. It could be a 
shell script, it would look something like this:


python createIndex_tfidf.py stopWords.dat testCollection.dat 
testIndex.dat titleIndex.dat
python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat


and you would then call it from whatever command line shell you use.


> ..or actually link to a front end
> GUI ,so when an question or word or words is input to the input box..it
> can go to the actiona dnrun the above mentioned lines of code

You can't "link to a front end GUI", you have to write a GUI application 
which calls your scripts.

There are many choices: tkinter is provided in the Python standard 
library, but some people prefer wxPython, PyQT4, or other GUI toolkits.

https://duckduckgo.com/?q=python+gui+toolkits



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

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


Re: __init__ patterns

2018-08-30 Thread Steven D'Aprano
On Thu, 30 Aug 2018 06:01:26 -0700, Tim wrote:

> I saw a thread on reddit/python where just about everyone said they
> never put code in their __init__ files.

Pfft. Reddit users. They're just as bad as Stackoverflow users. *wink*


> Here's a stackoverflow thread saying the same thing.
> https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-
package-init-py-files
> 
> That's new to me. I like to put functions in there that other modules
> within the module need. Thought that was good practice DRY and so forth.

Its fine to put code in __init__.py files.

If the expected interface is for the user to say:

result = package.spam()

then in the absence of some specific reason why spam needs to be in a 
submodule, why shouldn't it go into package/__init__.py ?

Of course it's okay for the definition of spam to be in a submodule, if 
necessary. But it shouldn't be mandatory.


> And I never do 'from whatever import *' Ever.
> 
> The reddit people said they put all their stuff into different modules
> and leave init empty.


Did any one of them state *why* they do this? What benefit is there to 
make this a hard rule?

Did anyone mention what the standard library does?

Check out the dbm, logging, html, http, collections, importlib, and 
curses packages (and probably others):

https://github.com/python/cpython/tree/3.7/Lib



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

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


RE: How to sort over dictionaries

2018-08-30 Thread David Raymond
Remember to check what the res["date"] types actually are. If they're just 
text, then it looked like they were in M/D/Y format, which won't sort correctly 
as text, hence you might want to include using datetime.strptime() to turn them 
into sortable datetimes.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
har...@moonshots.co.in
Sent: Thursday, August 30, 2018 4:31 AM
To: python-list@python.org
Subject: Re: How to sort over dictionaries



> > sort = sorted(results, key=lambda res:itemgetter('date'))
> > print(sort)
> > 
> > 
> > I have tried the above code peter but it was showing error like 
> > TypeError: '<' not supported between instances of 'operator.itemgetter'
> > and 'operator.itemgetter'
> 
> lambda res: itemgetter('date')
> 
> is short for
> 
> def keyfunc(res):
> return itemgetter('date')
> 
> i. e. it indeed returns the itemgetter instance. But you want to return 
> res["date"]. For that you can either use a custom function or the 
> itemgetter, but not both.
> 
> (1) With regular function:
> 
> def keyfunc(res):
> return res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (1a) With lambda:
> 
> keyfunc = lambda res: res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (2) With itemgetter:
> 
> keyfunc = itemgetter("date")
> sorted_results = sorted(results, key=keyfunc)
> 
> Variants 1a and 2 can also be written as one-liners.



Thanks PeterNo 2 has worked.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling an unbound method in C using the Public API

2018-08-30 Thread Serhiy Storchaka

29.08.18 17:33, Matthieu Dartiailh пише:

I tried to look at the public C API for a way to call an unbound method with a 
minimal cost (in term of speed and memory). It seems to me, but please correct 
me if I am wrong, that one cannot call a MethodDef using only the public API. 
To use the public C API, one has to use PyCFunction_Call (or a variant) that 
expect a PyCFunctionObject which binds a the MethodDef to an instance. In my 
case, to avoid creating a temporary PyCFunctionObject each time I call 
list.insert on my custom subclass instance, I have to store that 
PyCFunctionObject for each instance. But this means storing  7 
PyCFunctionObject per instance (one for each method of list I need to wrap). So 
I can either use the public API and increase the memory footprint or slow down 
the code by creating PyCFunctionObject for each call
  , or use large amount of the private API.

Am I missing something ?


In general, you need to cache the unbound method object, and call it 
with self as the first argument.


list_insert = PyObject_GetAttrString((PyObject *)&PyList_Type, "insert");
...
res = PyObject_CallFunctionObjArgs(list_insert, self, index, value, NULL);

But in the particular case of the insert method it will be easier and 
more efficient to use PyList_Insert().


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


Re: Cannot pass a variable given from url to route's callback fucntion and redirect issue

2018-08-30 Thread Rurpy via Python-list
On Wednesday, August 29, 2018 at 10:57:35 AM UTC-6, Νίκος Βέργος wrote:
> Flask app.py
> ==
> @app.route( '/' )
> @app.route( '/' )
> def index( page ):
> 
> # use the variable form template for displaying
> counter = '''
>   
>   td> Αριθμός Επισκεπτών: 
>  %d 
> 
>   
>   ''' % (page, pagehit)
> 
> if page != 'index.html':
> pdata = redirect( 'http://superhost.gr/cgi-bin/' + page )
> return pdata
> 
> 
> Template ndex.html
> ==
> 
>  src="/static/images/π.gif"> 
> 
> 
>   src="/static/images/download.gif">
> 
> 
> 1. All i want ro do is when the user clicks on the images within the html 
> tamplate, to pass those variables value to '/' route so to perfrom then asome 
> action with those values
> 
> The error iam receiving is this:
> ===
> builtins.TypeError
> TypeError: index() missing 1 required positional argument: 'page'
> 
> I mean i do have  declared in route and also do  have 'page' as 
> variable to the callback fucntion, why cant it see it?
> 
> 2. Also about the redirect funtion iam using... is there a way to get back 
> the HTML response of running that cgi-scrit and then add another HTML value 
> to that response? I used subprocess and Response but they weren able to 
> deliver the content back the pdata variable for display.
> What i actually want to do among other things is from within my flask app 
> script to try to run and get theresponse back of a cgi-script


I haven't looked at your problem in detail (I seldom read this group
anymore) but I think you want something like:

  def index( page='myhomepage' ):

If the function gets called via the "/" url, there will be no 'page'
argument so you need to write the function signature to make that 
argument optional.

No idea about question #2.

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


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread pjmclenon
On Thursday, August 30, 2018 at 9:28:09 AM UTC-4, Steven D'Aprano wrote:
> On Thu, 30 Aug 2018 05:21:30 -0700, pjmclenon wrote:
> 
> > my question is ... at the moment i can only run it on windows cmd prompt
> > with a multiple line entry as so::
> > 
> > python createIndex_tfidf.py stopWords.dat testCollection.dat
> > testIndex.dat titleIndex.dat
> > 
> > and then to query and use the newly created index as so:
> > 
> > python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> > 
> > how can i run just one file at a time?
> 
> I don't understand the question. You are running one file at a time. 
> First you run createIndex_tfidf.py, then you run queryIndex_tfidf.py
> 
> Maybe you mean to ask how to combine them both to one call of Python?
> 
> (1) Re-write the createIndex_tfidf.py and queryIndex_tfidf.py files to be 
> in a single file.
> 
> (2) Or, create a third file which runs them both one after another.
> 
> That third file doesn't even need to be a Python script. It could be a 
> shell script, it would look something like this:
> 
> 
> python createIndex_tfidf.py stopWords.dat testCollection.dat 
> testIndex.dat titleIndex.dat
> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> 
> 
> and you would then call it from whatever command line shell you use.
> 
> 
> > ..or actually link to a front end
> > GUI ,so when an question or word or words is input to the input box..it
> > can go to the actiona dnrun the above mentioned lines of code
> 
> You can't "link to a front end GUI", you have to write a GUI application 
> which calls your scripts.
> 
> There are many choices: tkinter is provided in the Python standard 
> library, but some people prefer wxPython, PyQT4, or other GUI toolkits.
> 
> https://duckduckgo.com/?q=python+gui+toolkits
> 
> 
> 
> -- 
> Steven D'Aprano
> "Ever since I learned about confirmation bias, I've been seeing
> it everywhere." -- Jon Ronson

thank you for the reply
actually your response was pretty much exactly what i am trying to do

so i will explain what i meant more better 
also i will have to learn shell script cuz i looked it up online and it seems i 
have to download something and then some other stuffunless you have  the 
short steps on how to write a shell script for a win 7 cmd prompt to call 2 or 
more python scripts

so what i meant to say in my 1st post is  i have to 1st run the create index 
program , which i have to run as so:

python createIndex_tfidf.py stopWords.dat testCollection.dat
testIndex.dat titleIndex.dat 

i tried b4 a while ago to run the .py file alone but it returns index out of 
range errors..so thats why i have to run it with the other 4 .dat files

it takes a few minutes to build the index and then the prompt returns me to the 
folder im in and 
then i run this:

python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat 

same reason as b4 , i cant run just .py file alone , as it returns index out of 
range errors

so that takes another few more minutes and then the cursor keeps blinking and i 
have to adjust the script some how because i just wait till i can try to input 
a query at the blinking cursor

so for example..hello world 
and it will return the titles up to 100, cuz thats the max returns i put in the 
script

so thats completetly perfect what you said ..create a 3rd script..a shell 
script to call the total i beleive its 9 files in total.but like i said i 
dont know shell scripting , so i will try to learn it ..hope its not too 
complicated...or i can make a 3rd python script ...but not sure how to call the 
2 python files and 7 .dat files...sorry even though its a year or so i know 
python..still get lost on import and from and include directives


as for the GUI ok i will check out your advice..cuz actually i do have a GUI 
version of this program here..and i wanted to merge them..but its seeming to be 
complex...i would rather make the GUI for this program cuz its actually exactly 
as i like it
thank you for any more explanation on how i can make and run the 3rd merge file 
script

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


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread MRAB

On 2018-08-30 17:57, pjmcle...@gmail.com wrote:

On Thursday, August 30, 2018 at 9:28:09 AM UTC-4, Steven D'Aprano wrote:

On Thu, 30 Aug 2018 05:21:30 -0700, pjmclenon wrote:

> my question is ... at the moment i can only run it on windows cmd prompt
> with a multiple line entry as so::
> 
> python createIndex_tfidf.py stopWords.dat testCollection.dat

> testIndex.dat titleIndex.dat
> 
> and then to query and use the newly created index as so:
> 
> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> 
> how can i run just one file at a time?


I don't understand the question. You are running one file at a time. 
First you run createIndex_tfidf.py, then you run queryIndex_tfidf.py


Maybe you mean to ask how to combine them both to one call of Python?

(1) Re-write the createIndex_tfidf.py and queryIndex_tfidf.py files to be 
in a single file.


(2) Or, create a third file which runs them both one after another.

That third file doesn't even need to be a Python script. It could be a 
shell script, it would look something like this:



python createIndex_tfidf.py stopWords.dat testCollection.dat 
testIndex.dat titleIndex.dat

python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat


and you would then call it from whatever command line shell you use.


> ..or actually link to a front end
> GUI ,so when an question or word or words is input to the input box..it
> can go to the actiona dnrun the above mentioned lines of code

You can't "link to a front end GUI", you have to write a GUI application 
which calls your scripts.


There are many choices: tkinter is provided in the Python standard 
library, but some people prefer wxPython, PyQT4, or other GUI toolkits.


https://duckduckgo.com/?q=python+gui+toolkits



--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson


thank you for the reply
actually your response was pretty much exactly what i am trying to do

so i will explain what i meant more better
also i will have to learn shell script cuz i looked it up online and it seems i 
have to download something and then some other stuffunless you have  the 
short steps on how to write a shell script for a win 7 cmd prompt to call 2 or 
more python scripts

so what i meant to say in my 1st post is  i have to 1st run the create index 
program , which i have to run as so:

python createIndex_tfidf.py stopWords.dat testCollection.dat
testIndex.dat titleIndex.dat

i tried b4 a while ago to run the .py file alone but it returns index out of 
range errors..so thats why i have to run it with the other 4 .dat files

it takes a few minutes to build the index and then the prompt returns me to the 
folder im in and
then i run this:

python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat

same reason as b4 , i cant run just .py file alone , as it returns index out of 
range errors

so that takes another few more minutes and then the cursor keeps blinking and i 
have to adjust the script some how because i just wait till i can try to input 
a query at the blinking cursor

so for example..hello world
and it will return the titles up to 100, cuz thats the max returns i put in the 
script

so thats completetly perfect what you said ..create a 3rd script..a shell 
script to call the total i beleive its 9 files in total.but like i said i 
dont know shell scripting , so i will try to learn it ..hope its not too 
complicated...or i can make a 3rd python script ...but not sure how to call the 
2 python files and 7 .dat files...sorry even though its a year or so i know 
python..still get lost on import and from and include directives


as for the GUI ok i will check out your advice..cuz actually i do have a GUI 
version of this program here..and i wanted to merge them..but its seeming to be 
complex...i would rather make the GUI for this program cuz its actually exactly 
as i like it
thank you for any more explanation on how i can make and run the 3rd merge file 
script


On Windows you can create a .bat batch file.

Put what you would enter at the command line into a plain textfile and 
give the file the extension ".bat".


Then, to run it, just type the name of the file on the command line.
--
https://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread pjmclenon
On Thursday, August 30, 2018 at 1:29:48 PM UTC-4, MRAB wrote:
> On 2018-08-30 17:57, pjmcle...@gmail.com wrote:
> > On Thursday, August 30, 2018 at 9:28:09 AM UTC-4, Steven D'Aprano wrote:
> >> On Thu, 30 Aug 2018 05:21:30 -0700, pjmclenon wrote:
> >> 
> >> > my question is ... at the moment i can only run it on windows cmd prompt
> >> > with a multiple line entry as so::
> >> > 
> >> > python createIndex_tfidf.py stopWords.dat testCollection.dat
> >> > testIndex.dat titleIndex.dat
> >> > 
> >> > and then to query and use the newly created index as so:
> >> > 
> >> > python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> >> > 
> >> > how can i run just one file at a time?
> >> 
> >> I don't understand the question. You are running one file at a time. 
> >> First you run createIndex_tfidf.py, then you run queryIndex_tfidf.py
> >> 
> >> Maybe you mean to ask how to combine them both to one call of Python?
> >> 
> >> (1) Re-write the createIndex_tfidf.py and queryIndex_tfidf.py files to be 
> >> in a single file.
> >> 
> >> (2) Or, create a third file which runs them both one after another.
> >> 
> >> That third file doesn't even need to be a Python script. It could be a 
> >> shell script, it would look something like this:
> >> 
> >> 
> >> python createIndex_tfidf.py stopWords.dat testCollection.dat 
> >> testIndex.dat titleIndex.dat
> >> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> >> 
> >> 
> >> and you would then call it from whatever command line shell you use.
> >> 
> >> 
> >> > ..or actually link to a front end
> >> > GUI ,so when an question or word or words is input to the input box..it
> >> > can go to the actiona dnrun the above mentioned lines of code
> >> 
> >> You can't "link to a front end GUI", you have to write a GUI application 
> >> which calls your scripts.
> >> 
> >> There are many choices: tkinter is provided in the Python standard 
> >> library, but some people prefer wxPython, PyQT4, or other GUI toolkits.
> >> 
> >> https://duckduckgo.com/?q=python+gui+toolkits
> >> 
> >> 
> >> 
> >> -- 
> >> Steven D'Aprano
> >> "Ever since I learned about confirmation bias, I've been seeing
> >> it everywhere." -- Jon Ronson
> > 
> > thank you for the reply
> > actually your response was pretty much exactly what i am trying to do
> > 
> > so i will explain what i meant more better
> > also i will have to learn shell script cuz i looked it up online and it 
> > seems i have to download something and then some other stuffunless you 
> > have  the short steps on how to write a shell script for a win 7 cmd prompt 
> > to call 2 or more python scripts
> > 
> > so what i meant to say in my 1st post is  i have to 1st run the create 
> > index program , which i have to run as so:
> > 
> > python createIndex_tfidf.py stopWords.dat testCollection.dat
> > testIndex.dat titleIndex.dat
> > 
> > i tried b4 a while ago to run the .py file alone but it returns index out 
> > of range errors..so thats why i have to run it with the other 4 .dat files
> > 
> > it takes a few minutes to build the index and then the prompt returns me to 
> > the folder im in and
> > then i run this:
> > 
> > python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> > 
> > same reason as b4 , i cant run just .py file alone , as it returns index 
> > out of range errors
> > 
> > so that takes another few more minutes and then the cursor keeps blinking 
> > and i have to adjust the script some how because i just wait till i can try 
> > to input a query at the blinking cursor
> > 
> > so for example..hello world
> > and it will return the titles up to 100, cuz thats the max returns i put in 
> > the script
> > 
> > so thats completetly perfect what you said ..create a 3rd script..a shell 
> > script to call the total i beleive its 9 files in total.but like i said 
> > i dont know shell scripting , so i will try to learn it ..hope its not too 
> > complicated...or i can make a 3rd python script ...but not sure how to call 
> > the 2 python files and 7 .dat files...sorry even though its a year or so i 
> > know python..still get lost on import and from and include directives
> > 
> > 
> > as for the GUI ok i will check out your advice..cuz actually i do have a 
> > GUI version of this program here..and i wanted to merge them..but its 
> > seeming to be complex...i would rather make the GUI for this program cuz 
> > its actually exactly as i like it
> > thank you for any more explanation on how i can make and run the 3rd merge 
> > file script
> > 
> On Windows you can create a .bat batch file.
> 
> Put what you would enter at the command line into a plain textfile and 
> give the file the extension ".bat".
> 
> Then, to run it, just type the name of the file on the command line.

ok i will try than...didint know its staight as creating .bat file and put my 
command line entries to run the program in the file and then type for 
example..run.bat inside the command prompt..i wil

Re: __init__ patterns

2018-08-30 Thread Terry Reedy

On 8/30/2018 9:43 AM, Steven D'Aprano wrote:

On Thu, 30 Aug 2018 06:01:26 -0700, Tim wrote:


I saw a thread on reddit/python where just about everyone said they
never put code in their __init__ files.


Pfft. Reddit users. They're just as bad as Stackoverflow users. *wink*



Here's a stackoverflow thread saying the same thing.
https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-

package-init-py-files


That's new to me. I like to put functions in there that other modules
within the module need. Thought that was good practice DRY and so forth.


Its fine to put code in __init__.py files.

If the expected interface is for the user to say:

result = package.spam()

then in the absence of some specific reason why spam needs to be in a
submodule, why shouldn't it go into package/__init__.py ?

Of course it's okay for the definition of spam to be in a submodule, if
necessary. But it shouldn't be mandatory.



And I never do 'from whatever import *' Ever.

The reddit people said they put all their stuff into different modules
and leave init empty.



Did any one of them state *why* they do this? What benefit is there to
make this a hard rule?

Did anyone mention what the standard library does?

Check out the dbm, logging, html, http, collections, importlib, and
curses packages (and probably others):

https://github.com/python/cpython/tree/3.7/Lib


tkinter.__init__ has 'from tkinter.constants import *' and has the code 
defining tk widgets, among other things.  tkinter/ has submodules for 
ttk, fonts, and other optional facilities.  The setup is partly for 
historical reasons, but is partly intended and not a bad design for a 
module with 'main' stuff and 'optional' stuff.


The user interface would be the same if widget code were in 
tkinter.widgets with .__init__ containing 'from tkinter.widgets import *'.


--
Terry Jan Reedy

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


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2018-08-30 Thread pjmclenon
On Thursday, August 30, 2018 at 2:05:16 PM UTC-4, pjmc...@gmail.com wrote:
> On Thursday, August 30, 2018 at 1:29:48 PM UTC-4, MRAB wrote:
> > On 2018-08-30 17:57, pjmcle...@gmail.com wrote:
> > > On Thursday, August 30, 2018 at 9:28:09 AM UTC-4, Steven D'Aprano wrote:
> > >> On Thu, 30 Aug 2018 05:21:30 -0700, pjmclenon wrote:
> > >> 
> > >> > my question is ... at the moment i can only run it on windows cmd 
> > >> > prompt
> > >> > with a multiple line entry as so::
> > >> > 
> > >> > python createIndex_tfidf.py stopWords.dat testCollection.dat
> > >> > testIndex.dat titleIndex.dat
> > >> > 
> > >> > and then to query and use the newly created index as so:
> > >> > 
> > >> > python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> > >> > 
> > >> > how can i run just one file at a time?
> > >> 
> > >> I don't understand the question. You are running one file at a time. 
> > >> First you run createIndex_tfidf.py, then you run queryIndex_tfidf.py
> > >> 
> > >> Maybe you mean to ask how to combine them both to one call of Python?
> > >> 
> > >> (1) Re-write the createIndex_tfidf.py and queryIndex_tfidf.py files to 
> > >> be 
> > >> in a single file.
> > >> 
> > >> (2) Or, create a third file which runs them both one after another.
> > >> 
> > >> That third file doesn't even need to be a Python script. It could be a 
> > >> shell script, it would look something like this:
> > >> 
> > >> 
> > >> python createIndex_tfidf.py stopWords.dat testCollection.dat 
> > >> testIndex.dat titleIndex.dat
> > >> python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> > >> 
> > >> 
> > >> and you would then call it from whatever command line shell you use.
> > >> 
> > >> 
> > >> > ..or actually link to a front end
> > >> > GUI ,so when an question or word or words is input to the input box..it
> > >> > can go to the actiona dnrun the above mentioned lines of code
> > >> 
> > >> You can't "link to a front end GUI", you have to write a GUI application 
> > >> which calls your scripts.
> > >> 
> > >> There are many choices: tkinter is provided in the Python standard 
> > >> library, but some people prefer wxPython, PyQT4, or other GUI toolkits.
> > >> 
> > >> https://duckduckgo.com/?q=python+gui+toolkits
> > >> 
> > >> 
> > >> 
> > >> -- 
> > >> Steven D'Aprano
> > >> "Ever since I learned about confirmation bias, I've been seeing
> > >> it everywhere." -- Jon Ronson
> > > 
> > > thank you for the reply
> > > actually your response was pretty much exactly what i am trying to do
> > > 
> > > so i will explain what i meant more better
> > > also i will have to learn shell script cuz i looked it up online and it 
> > > seems i have to download something and then some other stuffunless 
> > > you have  the short steps on how to write a shell script for a win 7 cmd 
> > > prompt to call 2 or more python scripts
> > > 
> > > so what i meant to say in my 1st post is  i have to 1st run the create 
> > > index program , which i have to run as so:
> > > 
> > > python createIndex_tfidf.py stopWords.dat testCollection.dat
> > > testIndex.dat titleIndex.dat
> > > 
> > > i tried b4 a while ago to run the .py file alone but it returns index out 
> > > of range errors..so thats why i have to run it with the other 4 .dat files
> > > 
> > > it takes a few minutes to build the index and then the prompt returns me 
> > > to the folder im in and
> > > then i run this:
> > > 
> > > python queryIndex_tfidf.py stopWords.dat testIndex.dat titleIndex.dat
> > > 
> > > same reason as b4 , i cant run just .py file alone , as it returns index 
> > > out of range errors
> > > 
> > > so that takes another few more minutes and then the cursor keeps blinking 
> > > and i have to adjust the script some how because i just wait till i can 
> > > try to input a query at the blinking cursor
> > > 
> > > so for example..hello world
> > > and it will return the titles up to 100, cuz thats the max returns i put 
> > > in the script
> > > 
> > > so thats completetly perfect what you said ..create a 3rd script..a shell 
> > > script to call the total i beleive its 9 files in total.but like i 
> > > said i dont know shell scripting , so i will try to learn it ..hope its 
> > > not too complicated...or i can make a 3rd python script ...but not sure 
> > > how to call the 2 python files and 7 .dat files...sorry even though its a 
> > > year or so i know python..still get lost on import and from and include 
> > > directives
> > > 
> > > 
> > > as for the GUI ok i will check out your advice..cuz actually i do have a 
> > > GUI version of this program here..and i wanted to merge them..but its 
> > > seeming to be complex...i would rather make the GUI for this program cuz 
> > > its actually exactly as i like it
> > > thank you for any more explanation on how i can make and run the 3rd 
> > > merge file script
> > > 
> > On Windows you can create a .bat batch file.
> > 
> > Put what you would enter at the command line into a plain text

Re: Cannot pass a variable given from url to route's callback fucntion and redirect issue

2018-08-30 Thread Rurpy via Python-list
On Thursday, August 30, 2018 at 10:08:34 AM UTC-6, Νίκος Βέργος wrote:
> I did try it with 'None' and as page='index.html' Flask return an error both 
> ways (while bottle framework does not)

I think you are mistaken, making the change I suggested
fixes the "TypeError: index() missing 1 required positional 
argument" error you asked about.

Appended below is a runnable version of your code.  I couldn't
tell from your post whether you just left out code after the 
"if page !=..." statement for posting or whether is is acually
missing in your code.  I filled what was need to run.  Neither 
the html in your code or the temple are referenced by the code
you posted so you'll need put the pieces together.

> 2. Also about the redirect funtion iam using... is there a way to get back 
> the HTML response of running that cgi-scrit and then add another HTML value 
> to that response? I used subprocess and Response but they weren able to 
> deliver the content back the pdata variable for display.
> What i actually want to do among other things is from within my flask app 
> script to try to run and get theresponse back of a cgi-script

A subprocess should work if you run it with environment variables 
set up the way web server would call it with.  Alternatively
you could take the code in the cgi script (assuming its python)
and put it in a module function somewhere, then the cgi script
becomes a wrapper around a function call to it, and you can call 
the same function in your flask code.

One last point: redirecting to an arbitrary page based on a url 
provided by an untrusted user strikes me as dangerous.  I would 
at least whitelist the pages with something like:
   if page not in ('products', 'about', help'): abort (404)

Hope this helps...
--

from flask import Flask, redirect
app = Flask (__name__)

@app.route( '/' )
@app.route( '/' )
def index( page='index.html' ):
# use the variable form template for displaying
pagehit = 333  # Tempory for testing
counter = '''

td> Αριθμός Επισκεπτών: 

 %d 

''' % (page, pagehit)
if page != 'index.html':
pdata = redirect( 'http://xxsuperhost.gr/cgi-bin/' + page )
else: pdata = "Hi, I'm the index page"
return pdata

@app.route('/log')
def log(): return "Hi, I'm the /log page"

if __name__ == '__main__': 
app.run (host='0.0.0.0', debug=True)
-- 
https://mail.python.org/mailman/listinfo/python-list


Shall I worry about python2/3 compatibility when using library?

2018-08-30 Thread Stone Zhong
Hi there,

I think the fact is:
- There are still considerable amount of people still using python2
- Python2 user will eventually upgrade to python3

So any library not written in a compatible way will either break now for 
python2 user, or will break in the future for python3 user. So I suppose all 
library developer are writing compatible code, is that a fair assumption?

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


Re: Shall I worry about python2/3 compatibility when using library?

2018-08-30 Thread Terry Reedy

On 8/30/2018 10:27 PM, Stone Zhong wrote:

Hi there,

I think the fact is:
- There are still considerable amount of people still using python2
- Python2 user will eventually upgrade to python3

So any library not written in a compatible way will either break now for 
python2 user, or will break in the future for python3 user. So I suppose all 
library developer are writing compatible code, is that a fair assumption?


No.  Many people write new libraries only for recent version of Python 
3.  Many people who have written Python 2 and 3 compatible libraries, or 
Python 2 and Python 3 versions of of their library, have or will drop 
Python 2 support for enhancements and even bugfixes for their library.


That said, some people will continue to use existing python 2 code for a 
decade or more.


--
Terry Jan Reedy

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


Re: Shall I worry about python2/3 compatibility when using library?

2018-08-30 Thread Stone Zhong
On Thursday, August 30, 2018 at 10:19:34 PM UTC-7, Terry Reedy wrote:
> On 8/30/2018 10:27 PM, Stone Zhong wrote:
> > Hi there,
> > 
> > I think the fact is:
> > - There are still considerable amount of people still using python2
> > - Python2 user will eventually upgrade to python3
> > 
> > So any library not written in a compatible way will either break now for 
> > python2 user, or will break in the future for python3 user. So I suppose 
> > all library developer are writing compatible code, is that a fair 
> > assumption?
> 
> No.  Many people write new libraries only for recent version of Python 
> 3.  Many people who have written Python 2 and 3 compatible libraries, or 
> Python 2 and Python 3 versions of of their library, have or will drop 
> Python 2 support for enhancements and even bugfixes for their library.
> 
> That said, some people will continue to use existing python 2 code for a 
> decade or more.
> 
> -- 
> Terry Jan Reedy

Thanks for the reply Terry. Got it.

So some people main separate libraries for python2 and python3 (although this 
way it may have extra cost), and may eventually drop support for python2 lib or 
may not even have library for python2.

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


Re: Shall I worry about python2/3 compatibility when using library?

2018-08-30 Thread Cameron Simpson

On 30Aug2018 19:27, Stone Zhong  wrote:

I think the fact is:
- There are still considerable amount of people still using python2
- Python2 user will eventually upgrade to python3

So any library not written in a compatible way will either break now for 
python2 user, or will break in the future for python3 user. So I suppose all 
library developer are writing compatible code, is that a fair assumption?


That depends on the developer and the library.

For myself, I try to write compatible code. For many things this is easy, and 
for most things this is possible, especially with the help of some kind of 
compatibility module like "six".


There are some tutorials on writing portable Python 3 code, and on porting 
Python 2 code.


I have a little module of my own "cs.py3" which covers most of the things I've 
needed to port; it aims to provide Python 3 flavoured versions of things. That 
way (a) my code expects Python 3 (where that matters) and (b) the code runs 
maximally efficiently under Python 3 because most of the things are just a 
direct import of the Python 3 builtin - the Python 2 code is the emulation (for 
want of a term).


Why my own? To some extent so I learn myself what's changed. Nothing like 
implenting something for Python 2 to match Python 3 for these things.


I'll assume you're aware of the main differences; if not we can elaborate.

Not everything can be done seamlessly, but you might be surprised how small an 
issue it usually is.


That said, I do have some modules which are Python 3 only. So far I think these 
are all modules doing byte level parsing of binary data - this is outstandingly 
easier in Python 3 - you're never unsure if you're dealing with bytes versus 
text (str). I was looking at making them Python 2 capable, but the pain level 
was too high.


My advice: try to be portable (same code good for both 2 and 3), try to port 
when this is an issue, try to use third party modules which work with both 2 
and 3 when you have a choice, look at the 'six" module if things get harder.


And aim for Python 2.7 - this goes a long way to making 2 and 3 easy to work 
with. The fursther back you go in 2.x the harder it gets.


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


Re: Shall I worry about python2/3 compatibility when using library?

2018-08-30 Thread Chris Angelico
On Fri, Aug 31, 2018 at 3:50 PM, Cameron Simpson  wrote:
> And aim for Python 2.7 - this goes a long way to making 2 and 3 easy to work
> with. The fursther back you go in 2.x the harder it gets.

Yep; also, aim for 3.5+, since you get the ability to write u"..."
(from 3.3) and b"..."%... (from 3.5). That can make your life quite a
bit easier.

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


Re: Question about floating point

2018-08-30 Thread Gregory Ewing

Steven D'Aprano wrote:
The right way is to 
set the rounding mode at the start of your application, and then let the 
Decimal type round each calculation that needs rounding.


It's not clear what you mean by "rounding mode" here. If you
mean whether it's up/down/even/whatever, then yes, you can
probably set that as a default and leave it.

However, as far as I can see, Decimal doesn't provide a
way of setting a default number of decimal places to
which results are rounded. You can set a default *precision*,
but that's not the same thing. (Precision is the total
number of significant digits, not the number of digits
after the decimal point.)

So if you're working with dollars and cents and want all
your divisions rounded to 2 places, you're going to have
to do that explicitly each time.

I don't think this is a bad thing, because often you
don't want to use the same number of places for everything,
For example, people dealing with high-volume low-value goods
often calculate with unit prices having more than 2 decimal
places. In those kinds of situations, you need to know
exactly what you're doing every step of the way.

We have here a brilliant hammer specially designed for 
banging in just this sort of nail,


Except that we don't, we actually have an impact screwdriver,
so you've going to have to bring your hammer to deal with
nails properly. And a single size of hammer isn't going to
suit all kinds of nail.

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