Re: From JoyceUlysses.txt -- words occurring exactly once

2024-05-31 Thread Pieter van Oostrum via Python-list
HenHanna  writes:

> Given a text file of a novel (JoyceUlysses.txt) ...
>
> could someone give me a pretty fast (and simple) Python program that'd
> give me a list of all words occurring exactly once?
>
>   -- Also, a list of words occurring once, twice or 3 times
>
>
>
> re: hyphenated words(you can treat it anyway you like)
>
>but ideally, i'd treat  [editor-in-chief]
>[go-ahead]  [pen-knife]
>[know-how]  [far-fetched] ...
>as one unit.
>

That is a famous Unix task : (Sorry, no Python)

grep -o '\w*' JoyceUlysses.txt | sort | uniq -c | sort -n


-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A technique from a chatbot

2024-04-03 Thread Pieter van Oostrum via Python-list
r...@zedat.fu-berlin.de (Stefan Ram) writes:

>   It can lead to errors:
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
> something_to_be_done_at_the_end_of_this_function()
> 
>   The call sometimes will not be executed here!
>   So, "return" is similar to "break" in that regard.

That can be solved with finally:

def first_word_beginning_with_e( list_ ):
try:
for word in list_:
if word[ 0 ]== 'e': return word
finally:
    print("something_to_be_done_at_the_end_of_this_function()")

-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Are Floating Point Numbers still a Can of Worms?

2022-10-23 Thread Pieter van Oostrum
Mostowski Collapse  writes:

> I also get:
>
> Python 3.11.0rc1 (main, Aug 8 2022, 11:30:54)
>>>> 2.718281828459045**0.8618974796837966
> 2.367649
>
> Nice try, but isn't this one the more correct?
>
> ?- X is 2.718281828459045**0.8618974796837966.
> X = 2.36764897.
>

That's probably the accuracy of the underlying C implementation of the exp 
function.

In [25]: exp(0.8618974796837966)
Out[25]: 2.367649

But even your answer can be improved:

Maxima:

(%i1) fpprec:30$

(%i2) bfloat(2.718281828459045b0)^bfloat(.8618974796837966b0);
(%o2)  2.367648983187397393143b0

but:

(%i7) bfloat(%e)^bfloat(.8618974796837966b0);
(%o7)  2.36764900085638369695b0
surprisingly closer to Python's answer.

but 2.718281828459045 isn't e. Close but no cigar.

(%i10) bfloat(2.718281828459045b0) - bfloat(%e);
(%o10)   - 2.35360287471352802147785151603b-16

Fricas:

(1) -> 2.718281828459045^0.8618974796837966 

   (1)  2.367648_98319

(2) -> exp(0.8618974796837966)

   (2)  2.367649_00086

-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: jupyter console vs. ipython in Emacs

2021-12-20 Thread Pieter van Oostrum
Pieter van Oostrum  writes:

> My Python development environment is Emacs. I used to use 'jupyter
> console --simple-prompt' (driven by Emacs comint) to do interactive
> work, but it has the disadvantage that it doesn't work properly with
> multiline input, including pastes.
>
> However, I discovered that 'ipython --simple-prompt' in Emacs does work
> with multiline statements. No idea why jupyter console doesn't.
>
> Is there any advantage in using jupyter console over ipython?

Actually, it also happens when run in Terminal, so it is not an Emacs issue.
By the way, my OS is MacOS Catalina. I have no idea how it works on other 
systems.
-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


jupyter console vs. ipython in Emacs

2021-12-19 Thread Pieter van Oostrum
My Python development environment is Emacs. I used to use 'jupyter console 
--simple-prompt' (driven by Emacs comint) to do interactive work, but it has 
the disadvantage that it doesn't work properly with multiline input, including 
pastes.

However, I discovered that 'ipython --simple-prompt' in Emacs does work with 
multiline statements. No idea why jupyter console doesn't.

Is there any advantage in using jupyter console over ipython?
-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: HTML extraction

2021-12-08 Thread Pieter van Oostrum
Roland Mueller  writes:

> But isn't bs4 only for SOAP content?
> Can bs4 or lxml cope with HTML code that does not comply with XML as the
> following fragment?
>
> A
> B
> 
>

bs4 can do it, but lxml wants correct XML.

Jupyter console 6.4.0

Python 3.9.9 (main, Nov 16 2021, 07:21:43) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from bs4 import BeautifulSoup as bs

In [2]: soup = bs('AB')

In [3]: soup.p
Out[3]: A

In [4]: soup.find_all('p')
Out[4]: [A, B]

In [5]: from lxml import etree

In [6]: root = etree.fromstring('AB')
Traceback (most recent call last):

  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/IPython/core/interactiveshell.py",
 line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

  File 
"/var/folders/2l/pdng2d2x18d00m41l6r2ccjrgn/T/ipykernel_96220/3376613260.py",
 line 1, in 
root = etree.fromstring('AB')

  File "src/lxml/etree.pyx", line 3237, in lxml.etree.fromstring

  File "src/lxml/parser.pxi", line 1896, in lxml.etree._parseMemoryDocument

  File "src/lxml/parser.pxi", line 1777, in lxml.etree._parseDoc

  File "src/lxml/parser.pxi", line 1082, in 
lxml.etree._BaseParser._parseUnicodeDoc

  File "src/lxml/parser.pxi", line 615, in 
lxml.etree._ParserContext._handleParseResultDoc

  File "src/lxml/parser.pxi", line 725, in lxml.etree._handleParseResult

  File "src/lxml/parser.pxi", line 654, in lxml.etree._raiseParseError

  File "", line 1
XMLSyntaxError: Premature end of data in tag hr line 1, line 1, column 13
-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Negative subscripts

2021-11-26 Thread Pieter van Oostrum
Frank Millman  writes:

> Hi all
>
> In my program I have a for-loop like this -
>
>>>> for item in x[:-y]:
> ...    [do stuff]
>
> 'y' may or may not be 0. If it is 0 I want to process the entire list
> 'x', but of course -0 equals 0, so it returns an empty list.
>
> In theory I can say
>
>>>> for item in x[:-y] if y else x:
> ...    [do stuff]
>
> But in my actual program, both x and y are fairly long expressions, so
> the result is pretty ugly.
>
> Are there any other techniques anyone can suggest, or is the only
> alternative to use if...then...else to cater for y = 0?

If you put it in a function with x and y as parameters, then both x and y are 
just a simple identifier inside the function.
And then you can then even eliminate the if with

for item in x[:len(x)-y]:
-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Recursion on list

2021-11-04 Thread Pieter van Oostrum
ast  writes:

>> li = []
>> li.append(li)
>> li
> [[...]]
>
>>li[0][0][0][0]
> [[...]]
>
> That's funny
>

You made a list whose only element is itself.

In [1]: li = []

In [3]: li.append(li)

In [4]: li[0] is li
Out[4]: True

-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7 and 3.9

2021-02-18 Thread Pieter van Oostrum
Ethan Furman  writes:

> On 2/16/21 12:09 PM, Kevin M. Wilson via Python-list wrote:
>
>> My employer has hundreds of scripts in 2.7, but I'm writing new
>> scripts in 3.9! I'm running into 'invalid syntax' errors.I have to
>> maintain the 'Legacy' stuff, and I need to mod the path et al., to
>> execute 3.7 w/o doing damage to the 'Legacy' stuff...IDEA' are
>> Welcome!
>
> My first idea/request: have a blank line, a line with only a '--'
> (without quotes), and then your signature at the bottom of your posts. 
> White space aids readability and readability counts.  :)

The separator line should be '-- ' (without quotes), i.e. with a trailing space.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Jupyter notebooks to A4 (again)

2021-01-31 Thread Pieter van Oostrum
Martin Schöön  writes:

> Hello all,
>
> Some years ago I asked about exporting notebooks to pdf in
> A4 rather than US Letter. I got help, rather detailed
> instructions from you in general and Piet von Oostrum in
> particular. Following the advice helped and I was happy.
>
> Now it does not work any longer:
>
>  nbconvert failed: A4article
>
> I am stumped. I have not changed anything and all
> looks OK.
>
> Today I tried up-dating all things Python and Jupyter
> but that did not help.
>
> I have also tried removing the A4 stuff and after 
> restarting Jupyter I can export to PDF and get US Letter
> paper format.
>
> A quick and (obviously) not very clever internet search
> yielded nothing helpful.

The template system has changed in nbconvert version 6, so the original 
templates don't work anymore.
And they still haven't supplied standard provisions for A4 paper (like an 
option or so). Probably a mild case of cultural blindness.

I found out how to do it in the new template system, however, there is a bug 
that gives some troubles.

To get all your latex (and therefore also PDF) output in A4 format:

Find out where nbconvert expects user templates.

Issue the shell command:
bash-3.2$ jupyter --paths
Look up the data part:
...
data:
/Users/pieter/Library/Jupyter
...
The first one is your personal directory in my case ~/Library/Jupyter.

In this directory create the directory nbconvert/templates/latex
In this directory create these two files:
conf.json
{
"base_template": "latex",
"mimetypes": {
"text/latex": true,
"text/tex": true,
"application/pdf": true
}
}
index.tex.j2

((=- Default to the notebook output style -=))
((*- if not cell_style is defined -*))
((* set cell_style = 'style_jupyter.tex.j2' *))
((*- endif -*))

((=- Inherit from the specified cell style. -=))
((* extends cell_style *))

%===
% Latex Article
%===

((*- block docclass -*))
\documentclass[a4paper,12pt]{article}
((*- endblock docclass -*))

Now jupyter nbconvert --to latex my_nb.ipynb will generate latex with a4paper.

If you don't want this to be the default, but you want a special template, 
let's say A4article, to do this, place these files in a directory 
.../nbconvert/templates/A4article rather than .../nbconvert/templates/latex.

Then you would use jupyter nbconvert --to latex --template A4article my_nb.ipynb

However, due to a bug this won't work unless you patch the nbconvert export 
code.
This is a simple one-line patch.
See 
https://github.com/jupyter/nbconvert/pull/1496/commits/a61a2241a87912005720d3412ccd7ef7b5fce6dd

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Jupyter notebooks to A4 (again)

2021-01-28 Thread Pieter van Oostrum
Martin Schöön  writes:

> Hello all,
>
> Some years ago I asked about exporting notebooks to pdf in
> A4 rather than US Letter. I got help, rather detailed
> instructions from you in general and Piet von Oostrum in

Who now calls himself Pieter van Oostrum, just like his passport says :)
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: news.bbs.nz is spewing duplicates to comp.lang.python

2020-04-22 Thread Pieter van Oostrum
Skip Montanaro  writes:

>> This just arrived at my newserver:
>>
> ...
>> I find that very curious because the post is mine but which I
>> sent out with these headers:
>>

I filter out these messages in my news setup (using gnus on Emacs) on
the header:

 ("head"
  ("Injection-Info: news.bbs.nz" -1002 nil s))

i.e. each message that contains "news.bbs.nz" in the "Injection-Info"
header will be made invisible.
This solved the problem for me.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is a generator expression called a expression?

2020-04-21 Thread Pieter van Oostrum
Veek M  writes:

> The docs state that a expression is some combination of value, operator, 
> variable and function. Also you cannot add or combine a generator 
> expression with a value as you would do with 2 + 3 + 4. For example, 
> someone on IRC suggested this
> all(a == 'a' for a in 'apple') but
>
> 1. all is a function/method
> 2. so (whatever) in this case is a call but obviously it works so it must 
> be a generator object as well.. so.. how does 'all' the function object 
> work with the generator object that's being produced? 
>
> I can't for example do min 1,2,3 but i can do min (1,2,3) and the () are 
> not integral to a tuple - therefore one could argue that the () are part 
> of the call - not so with a generator Expression where the () are 
> integral to its existence as an object.
>
> Could someone clarify further.

The Language Reference Manual says:

generator_expression ::=  "(" expression comp_for ")"

The parentheses can be omitted on calls with only one argument. See
section Calls for details.

Calls:

call ::=  primary "(" [argument_list [","] | comprehension] ")"
comprehension ::=  expression comp_for

The last part is the inner part (i.e. without the parentheses) of
generator_expression.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Floating point problem

2020-04-21 Thread Pieter van Oostrum
Chris Angelico  writes:

> On Tue, Apr 21, 2020 at 6:07 AM Schachner, Joseph
>  wrote:
>>
>> 16 base 10 digits / log base10( 2) = 53.1508495182 bits. Obviously,
>> fractional bits don't exist, so 53 bits. If you note that the first
>> non-zero digit as 4, and the first digit after the 15 zeroes was 2,
>> then you got an extra bit. 54 bits. Where did the extra bit come from?
>> It came from the IEEE format's assumption that the top bit of the
>> mantissa of a normalized floating point value must be 1. Since we know
>> what it must be, there is no reason to use an actual bit for it. The
>> 53 bits in the mantissa do not include the assumed top bit.
>>
>> Isn't floating point fun?
>>
>
> IEEE 64-bit packed floating point has 53 bits of mantissa, 11 scale
> bits (you've heard of "scale birds" in art? well, these are "scale
> bits"), and 1 sign bit.
>
> 53 + 11 + 1 == 64.

I don't know if this was meant to be sarcasm, but in my universe 53 + 11 + 1 == 
65.
But the floating point standard uses 53 bits of precision where only 52 bits 
are stored.
52 + 11 + 1 == 64.

> Yep, floating point is fun.
>
> That assumed top 1 bit is always there, except when it isn't. Because
> denormal numbers are a thing. They don't have that implied 1 bit.

Yes, for subnormal numbers the implicit bit *is* stored. They are characterized 
by the biased exponent being 0. So these have only 52  bits of precision in the 
mantissa.

> Yep, floating point is fun.
>
> ChrisA

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Floating point problem

2020-04-19 Thread Pieter van Oostrum
"R.Wieser"  writes:

> Souvik,
>
>> I have one question here. On using print(f"{c:.32f}") where c= 2/5
>> instead of getting 32 zeroes I got some random numbers. The exact
>> thing is 0.40002220446049250313 Why do I get
>> this and not 32 zeroes?
>
> Simple answer ?   The conversion routine runs outof things to say.
>
> A bit more elaborate answer ? You should not even have gotten that many 
> zeroes after the 0.4.The precision of a 32-bit float is about 7 digits. 
> That means that all you can depend on is the 0.4 followed by 6 more digits. 
> Anything further is, in effect, up for grabs.
>
Most Python implementations use 64-bit doubles (53 bits of precision). See 
https://docs.python.org/3.8/tutorial/floatingpoint.html
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Helping Windows first time users

2020-04-19 Thread Pieter van Oostrum
boB Stepp  writes:

> On Wed, Apr 15, 2020 at 2:04 PM Barry Scott  wrote:
>>
>> I post some suggestion to improve the Python installer for Windows
>> to better sign post users on the next steps.
>>
>> https://mail.python.org/archives/list/python-id...@python.org/message/TKHID7PMKN5TK5QDQ2BL3G45FYAJNYJX/
>>
>> It also seems like we could do with drafting the text of a helpful
>> reply to help the Windows first time users. This would help folks that
>> reply to the Windows first time users to have a quick way to reply
>> without drafting the text a reply every time.
>>
>> What are your thoughts on the installer changes and reply text?
>
> I wonder if the last screen of the installer should have a checkbox,
> checked by default, that launches IDLE.  IDLE would then display a
> helpful text file on this first launch describing how to use IDLE, how
> to find and launch it again, etc.  As well have it describe all of the
> help you are advocating here as well.  It would be a minor annoyance
> for experienced users to have to uncheck the box before clicking
> finish, but I think it might have a good chance of getting new users
> off to a reliable start.  Many other Windows program installers are
> setup similarly to launch the program being installed.  This is a
> little different as IDLE is not Python, but that is a minor quibble, I
> think.

Or just display a short HTML page describing the first steps to start
using Python. It could mention the command line (py) to be used with a
text editor (some recommendations) and IDLE. And how not to double click
.py files :)
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python launcher

2020-04-15 Thread Pieter van Oostrum
Angel V  writes:

> Hello,
>
> I'm new to Python and recently began to self learn the language.
> Unfortunately, whenever I try to launch it, I'm met with a black pop-up
> screen the disappears as soon as it comes up. I've tried uninstalling and I
> just run into the same issue. I tried downloading the program onto my
> brother's computer and it did the same things.

It seems to me that a Python installation on Windows needs some "First
steps" documentation to direct the beginners to the essentials of how to
start a Python program. Preferably something that is displayed
immediately after installation of in some other way is prominently
displayed. I am not on Windows myself, so I am afraid I will not be of
much help in this respect.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to inheret a metaclass.

2020-04-11 Thread Pieter van Oostrum
Pieter van Oostrum  writes:

> Your Pardon is not a class, it is a function. Class A is created by
> type(cls, *args), so 'type' is the metaclass of A, and therefore also of
> B.
> Creation of B does not call Pardon.

With a class it *does* work:

In [74]: class Pardon(type):
 ... def __init__(cls, *args):
 ... print("Calling Pardon with", cls)

In [75]: class A(metaclass=Pardon): pass
Calling Pardon with 

In [76]: class B(A): pass
Calling Pardon with 

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Pieter van Oostrum
Antoon Pardon  writes:

> Op 9/04/20 om 18:37 schreef Peter Otten:
>> Antoon Pardon wrote:
>> 
>>> I am experimenting with subclasses that all need the same metaclass as the
>>> base class. Is there a way to make the metaclass be inherited, so that you
>>> don't have to repeat the "metaclass = MetaClass" with every subclass.
>> ?
>> This is not only possible, this is the default:
>>   >>> class Pardon(type): pass
>> ...
>>>>> class A(metaclass=Pardon): pass
>> ...
>>>>> class B(A): pass
>> ...
>>>>> type(B)
>> 
>
> Can you explain what is wrong with the code below:
>
> This produces only: Calling Pardon with A.
>
>
> def Pardon(cls, *args):
> print("Calling Pardon with", cls)
> return type(cls, *args)
>
> class A(metaclass=Pardon): pass
>
> class B(A): pass
>
Your Pardon is not a class, it is a function. Class A is created by type(cls, 
*args), so 'type' is the metaclass of A, and therefore also of B.
Creation of B does not call Pardon.

I find it peculiar that you can give a function as metaclass. But that seems to 
be part of the specification. The function result is what the 'class' becomes. 
You can even have it return something else. Then the 'class' wouldn't really be 
a class.

In [65]: def meta(cls, *args): return 1

In [66]: class A(metaclass=meta): pass

In [67]: A
Out[67]: 1

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python script to give a list of prime no.

2020-04-05 Thread Pieter van Oostrum
Sathvik Babu Veligatla  writes:

> hi,
> I am new to python, and i am trying to output the prime numbers beginning 
> from 3 and i cannot get the required output.
> It stops after giving the output "7" and that's it.
>
> CODE:
> a = 3
> l = []
> while True:
> for i in range(2,a):
> if a%i == 0:
> l.append(1)
> else:
> l.append(0)
>
> if l.count(1) == 0:
> print(a)
> a = a + 2
> elif l.count(1) >> 0:
> a = a + 2
> 
>
>
> Any help is appreciated,
> Thank you.

As others have already noticed, the l.count(1) >> 0: is faulty. You probably 
meant
l.count(1) > 0:, but else would be simpler.

But then, once there is a 1 in l that never disappears, so after that it will 
never print any more numbers. You should put the l = [] AFTER the while True, 
so that you start with a new empty list every cycle.

And then, putting 0 an 1's in a list and then counting how many 1's there are 
is quite inefficient. Just counting from the beginning would be more efficient. 
Like:

a = 3
while True:
count = 0
for i in range(2,a):
if a%i == 0:
count += 1
if count == 0:
print(a)
a = a + 2
else:
a = a + 2

Which can be simplified to:

a = 3
while True:
count = 0
for i in range(2,a):
if a%i == 0:
count += 1
if count == 0:
print(a)
a = a + 2

And you effectively use the counter as a boolean, so replace is by a boolean.

a = 3
while True:
is_prime = True
for i in range(2,a):
if a%i == 0:
is_prime = False
# once we found a divisor, it is no use to continue
break
if is_prime:
print(a)
a = a + 2

Further optimizations are possible, for example use range(2,a/2) or even range 
(2, sqrt(a)).
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dynamic import of dynamically created modules failes

2020-03-31 Thread Pieter van Oostrum
Pieter van Oostrum  writes:

>
> The first import creates a file __pycache__ in the directory p1.
That should be 'a directory __pycache__'
> To remove it use rmtree(path.join(P1,'__pycache__'))
> Then the second import will succeed.
> -- 
> Pieter van Oostrum
> www: http://pieter.vanoostrum.org/
> PGP key: [8DAE142BE17999C4]

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dynamic import of dynamically created modules failes

2020-03-31 Thread Pieter van Oostrum
"Dieter Maurer"  writes:

> Stephan Lukits wrote at 2020-3-31 17:44 +0300:
>>background:
>>
>>- a daemon creates package p1 (e.g. directory with __init__.py-file) and
>>in p1 a module m1 is created.
>>
>>- Then the daemon wants to import from m1, which functions (so far all
>>the time).
>>
>>- Then a module m2 is created in p1 and the daemon wants to import from
>>m2 which fails (most of the time but *not* always) with ModuleNotFoundError.
>>
>>See the little test script at the end which reproduces the problem.
>> ...
>
> I remember a similar report (some time ago). There, caching has
> been responsible. I have forgotten how to invalidate the caches.
> But, you could try logic found in
> `importlib._bootstrap_external.PathFinder.invalidate_caches`.

The first import creates a file __pycache__ in the directory p1.
To remove it use rmtree(path.join(P1,'__pycache__'))
Then the second import will succeed.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 0x80070643 python download error

2020-03-30 Thread Pieter van Oostrum
JimathyJim Jim <2maxleed...@gmail.com> writes:

> I am trying to download python on my laptop and this error came up. How do
> I fix this error appearing and download python 3.8.2?

Maybe you could specify what OS, and from what site/URL you are downloading. 
And at which moment the error appears.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PEP Idea: Multi-get for lists/tuples and dictionaries (inspired in NumPy)

2020-03-19 Thread Pieter van Oostrum
Santiago Basulto  writes:

> Hello community. I have an idea to share with the list to see what you all
> think about it.
>
> I happen to use both Python for Data Science (with our regular friends
> NumPy and Pandas) as well as for scripting and backend development. Every
> time I'm working in server-side Python (not the PyData stack), I find
> myself missing A LOT features from NumPy, like fancy indexing or boolean
> arrays.
>
> So, has it ever been considered to bake into Python's builtin list and
> dictionary types functionality inspired by NumPy? I think multi indexing
> alone would be huge addition. A few examples:
>
> For lists and tuples:
> >>> l = ['a', 'b', 'c']
> >>> l[[0, -1]]
> ['a', 'c']
>
> For dictionaries it'd even be more useful:
> d = {
> 'first_name': 'Frances',
> 'last_name': 'Allen',
> 'email': 'fal...@ibm.com'
> }
> fname, lname = d[['first_name', 'last_name']]
>
> I really like the syntax of boolean arrays too, but considering we have
> list comprehensions, seems a little more difficult to sell.
>
> I'd love to see if this is something people would support, and see if
> there's room to submit a PEP.

How about implementing it yourself:

In [35]: class MultiDict(dict):
 ... def __getitem__(self, idx):
 ... if isinstance(idx, list):
 ... return [self[i] for i in idx]
 ... return super().__getitem__(idx)

In [36]: d = MultiDict({
 ... 'first_name': 'Frances',
 ... 'last_name': 'Allen',
 ... 'email': 'fal...@ibm.com'
 ... })

In [37]: d
Out[37]: {'first_name': 'Frances', 'last_name': 'Allen', 'email': 
'fal...@ibm.com'}

In [38]: d['email']
Out[38]: 'fal...@ibm.com'

In [39]: d[['first_name', 'last_name']]
Out[39]: ['Frances', 'Allen']

The implementation of other methods, like __setitem__ and __init__, and maybe 
some more subtle details like exceptions, is left as an exercise for the reader.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is the program not printing three lines?

2020-03-19 Thread Pieter van Oostrum
Souvik Dutta  writes:

> I should have been more clear
> class first():
> print("from first")
> def second():
> print("from second")
> first()
>
> When I run the above code the output is
> "from first"

And where do you think this comes from? Are you thinking this comes from
the call 'first()'?
If so, that is not correct. See my Demo below.
It is from the class definition.

Run only this code, and you will see it also print "from first":

class first():
print("from first")
def second():
print("from second")

> (2ND CODE)
>
> class first():
> print("from first")
> def second():
> print("from second")
> first.second()
>
> When I run this code the output is
> "from first"
> "from second"
>
> Thus going by the above logic

Which logic?

> class first():
> print("from first")
> def second():
> print("from second")
> first()
> first.second()
>
> This should have given the following output
> from first
> from first
> from second

No, because this is not the combination of the first two codes. To have this 
combination, you should include the class definition twice.

> That is I should have got from first 2 times. But instead I got this output.
> from first
> from second
> Why is this so?

'From first' is the result of the class definition. 'from second' is the result 
of first.second().
And first() doesn't produce any output.

Your problem is probably that you think that the call first() executes all the 
statements in the class definition. It doesn't. That's not how Python class 
definitions work.

Check the Demo below another time.

> On Thu, Mar 19, 2020, 5:30 PM Pieter van Oostrum 
> wrote:
>
>> Chris Angelico  writes:
>>
>> > Creating the class runs all the code in the class block, including
>> > function definitions, assignments, and in this case, a print call.
>> >
>> > Classes are not declarations. They are executable code.
>>
>> Demo:
>>
>> In [26]: class first():
>>  ... print("From first")
>>  ... def second():
>>  ... print("From second")
>> From first
>>
>> You see, the print "From first" occurs at class definition time.
>>
>> In [27]: first()
>> Out[27]: <__main__.first at 0x10275f880>
>>
>> Calling the class (i.e. creating an instance) doesn't print anything,
>> because the print statement is not part of the class __init__ code.
>>
>> In [28]: first.second()
>> From second
>>
>> That's expected.
>>
>> In [29]: first.second()
>> From second
>>
>> Again.

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is the program not printing three lines?

2020-03-19 Thread Pieter van Oostrum
Chris Angelico  writes:

> Creating the class runs all the code in the class block, including
> function definitions, assignments, and in this case, a print call.
>
> Classes are not declarations. They are executable code.

Demo:

In [26]: class first():
 ... print("From first")
 ... def second():
 ... print("From second")
>From first

You see, the print "From first" occurs at class definition time.

In [27]: first()
Out[27]: <__main__.first at 0x10275f880>

Calling the class (i.e. creating an instance) doesn't print anything,
because the print statement is not part of the class __init__ code.

In [28]: first.second()
>From second

That's expected.

In [29]: first.second()
>From second

Again.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you help me solve this?

2020-03-16 Thread Pieter van Oostrum
Pieter van Oostrum  writes:

> Joseph Nail  writes:
>
>> Hello,
>> I have one problem. Somehow in my function when I wrote y=x, they are the
>> same variable and then it also changes age or height (which were x) in the
>> main program. See more in attached file.
>> Maybe it is bug or maybe it should run that way.
>
> If you write y = x, then they are not the same variable, but they point
> to (the proper Python language is they are bound to) the same object.
>
> Now if you say x.age = 20, then y.age will also be 20 (it's the same object).

If you want y to be an independent copy of x (i.e. a new object that has the 
same values), use:

import copy
y = copy.copy(x)

Now you can change x.age and it won't affect y.age
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you help me solve this?

2020-03-16 Thread Pieter van Oostrum
Joseph Nail  writes:

> Hello,
> I have one problem. Somehow in my function when I wrote y=x, they are the
> same variable and then it also changes age or height (which were x) in the
> main program. See more in attached file.
> Maybe it is bug or maybe it should run that way.

If you write y = x, then they are not the same variable, but they point to (the 
proper Python language is they are bound to) the same object.

Now if you say x.age = 20, then y.age will also be 20 (it's the same object).
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using classes

2020-03-13 Thread Pieter van Oostrum
joseph pareti wrote:

 > one more question. In the code below, there are 2 init() methods, one for 
 > the class 'Fahrzeug' and
 > one for the class 'PKW'.
 > The program works when I instantiate the class as:
 > 
 > fiat = PKW("Fiat Marea",50,0)
 > 
 > but it fails if I say:
 > 
 > fiat = PKW("Fiat Marea",50,0,1)
 > 
 > Traceback (most recent call last):
 >   File "erben_a.py", line 19, in 
 >     fiat = PKW("Fiat Marea",50,0,1)
 > TypeError: __init__() takes 4 positional arguments but 5 were given
 > 
 > yet the statement in bold matches IMO the init() method of the PKW class. 
 > Can anyone explain why?
 > Thanks.

No, the 'self' in the definition of init is the object being initialised. It is 
supplied by the class code that creates the new instance, not something you 
provide yourself. Your arguments are bez, ge, ins.

class PKW(Fahrzeug):
    def __init__(self, bez, ge, ins):

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: using classes

2020-03-12 Thread Pieter van Oostrum
joseph pareti  writes:

> thank you, that fixes it. I also noticed that both statements work:
>
>  super(PKW, self).__init__(bez,ge)
>
> or
>
>super().__init__(bez,ge)

The first is the required Python 2 calling (at least the first argument is 
required). The second way can be used in Python 3.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: iterate through an irregular nested list in Python

2020-03-07 Thread Pieter van Oostrum
sinnd...@gmail.com writes:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>

Use a recursive iterator/generator: It becomes a bit peculiar because you want 
special treatment for empty lists inside, but otherwise it is quite standard 
Python:

aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]

def reclist(aList):
for item in aList:
if isinstance(item, list):
if item == []:
yield ''
else:
yield from reclist(item)
else:
yield item

for i in reclist(aList):
print(i, end=',')

This gives you an extra comma at the end, unfortunately. But it is the pattern 
for other types of processing.

Or use it like this:

print (','.join(str(i) for i in reclist(aList)))

--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: iterate through an irregular nested list in Python

2020-03-06 Thread Pieter van Oostrum
sinnd...@gmail.com writes:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>

Use a recursive iterator/generator: It becomes a bit peculiar because you want 
special treatment for empty lists inside, but otherwise it is quite standard 
Python:

aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]

def reclist(aList):
for item in aList:
if isinstance(item, list):
if item == []:
yield ''
else:
yield from reclist(item)
else:
yield item

for i in reclist(aList):
print(i, end=',')

This gives you an extra comma at the end, unfortunately. But it is the pattern 
for other types of processing.

Or use it like this:

print (','.join(str(i) for i in reclist(aList)))

--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ÿ in Unicode

2020-03-06 Thread Pieter van Oostrum
Jon Ribbens  writes:

> On 2020-03-06, moi  wrote:
>> Le jeudi 5 mars 2020 13:20:38 UTC+1, Ben Bacarisse a ÄäCcritÄø :
>>> moi  writes:
>>> >>>> 'Ääâ¿'.encode('utf-8')
>>> > b'\xc3\xbf'
>>> >>>> 'Ääâ¿'.encode('utf-16-le')
>>> > b'\xff\x00'
>>> >>>> 'Ääâ¿'.encode('utf-32-le')
>>> > b'\xff\x00\x00\x00'
>>
>>> That all looks as expected.
>> Yes
>>
>>>Is there something about the output that puzzles you?
>> No
>>
>>>Did you have a question?
>> No, only a comment
>>
>> This buggy language is very amusing.
>
> What's the bug, or source of amusement?

The bug is in the mental world of the OP.
--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ÿ in Unicode

2020-03-06 Thread Pieter van Oostrum
Jon Ribbens  writes:

> On 2020-03-06, moi  wrote:
>> Le jeudi 5 mars 2020 13:20:38 UTC+1, Ben Bacarisse a ÄCcritâ :
>>> moi  writes:
>>> >>>> 'Ä¿'.encode('utf-8')
>>> > b'\xc3\xbf'
>>> >>>> 'Ä¿'.encode('utf-16-le')
>>> > b'\xff\x00'
>>> >>>> 'Ä¿'.encode('utf-32-le')
>>> > b'\xff\x00\x00\x00'
>>
>>> That all looks as expected.
>> Yes
>>
>>>Is there something about the output that puzzles you?
>> No
>>
>>>Did you have a question?
>> No, only a comment
>>
>> This buggy language is very amusing.
>
> What's the bug, or source of amusement?

The bug is in the mental world of the OP.
--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: iterate through an irregular nested list in Python

2020-03-06 Thread Pieter van Oostrum
sinnd...@gmail.com writes:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>

Use a recursive iterator/generator: It becomes a bit peculiar because you want 
special treatment for empty lists inside,
but otherwise it is quite standard Python:

aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]

def reclist(aList):
for item in aList:
if isinstance(item, list):
if item == []:
yield ''
else:
yield from reclist(item)
else:
yield item

for i in reclist(aList):
print(i, end=',')

This gives you an extra comma at the end, unfortunately. But it is the pattern 
for other types of processing.

Or use it like this:

print (','.join(str(i) for i in reclist(aList)))

--
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: iterate through an irregular nested list in Python

2020-03-06 Thread Pieter van Oostrum
sinnd...@gmail.com writes:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex 
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>

Use a recursive iterator/generator:
It becomes a bit peculiar because you want special treatment for empty lists 
inside,
but otherwise it is quite standard Python:

aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]

def reclist(aList):
for item in aList:
if isinstance(item, list):
if item == []:
yield ''
else:
yield from reclist(item)
else:
yield item

for i in reclist(aList):
print(i, end=',')

This gives you an extra comma at the end, unfortunately.
But it is the pattern for other types of processing.

Or use it like this:

print (','.join(str(i) for i in reclist(aList)))

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ÿ in Unicode

2020-03-06 Thread Pieter van Oostrum
Jon Ribbens  writes:

> On 2020-03-06, moi  wrote:
>> Le jeudi 5 mars 2020 13:20:38 UTC+1, Ben Bacarisse a écrit :
>>> moi  writes:
>>> >>>> 'ÿ'.encode('utf-8')
>>> > b'\xc3\xbf'
>>> >>>> 'ÿ'.encode('utf-16-le')
>>> > b'\xff\x00'
>>> >>>> 'ÿ'.encode('utf-32-le')
>>> > b'\xff\x00\x00\x00'
>>
>>> That all looks as expected.
>> Yes
>>
>>>Is there something about the output that puzzles you?
>> No
>>
>>>Did you have a question?
>> No, only a comment
>>
>> This buggy language is very amusing.
>
> What's the bug, or source of amusement?

The bug is in the mental world of the OP.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to compare in python an input value with an hashed value in mysql table?

2020-01-16 Thread Pieter van Oostrum
Growth Hacking Formation  writes:

> Thanks for helping. That is what I thought.
> Lets say it is the case and I get the key. We know it uses sha256 and it 
> apply to the ascii code.
> What should be the python code in this scenario?
> I am novice and the hash python module is a bit too complex for me. I read 
> the doc.
>
> Thanks.

Some of the details are still vague, but I think it must be something like this:
Here is some code that works in both Python 2 and Python 3.

import hashlib
import hmac

secretfile = '.../lmfwc-files/secret.txt' ## Fill in proper file name

with open(secretfile, 'rb') as fd:
secret = fd.read()

key = 'goldQ3T8-1QRD-5QBI-9F22'

bkey = key.encode('ascii')

h = hmac.new(secret, bkey, hashlib.sha256)

print('hd (hex): ', h.hexdigest())

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help

2020-01-16 Thread Pieter van Oostrum
kiran chawan  writes:

> Whenever Iam trying to run this 'New  latest version python software 3.8.4
> python ' but it doesn't show any install option and it say ' modify set up
> ' So tell what to do sir plz help me out.

There is no Python 3.8.4.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to compare in python an input value with an hashed value in mysql table?

2020-01-14 Thread Pieter van Oostrum
ad...@formationgrowthhacking.com writes:

> I have a wordpress 5.3 websites which sell a software with license key.
>
> The license key is encrypted and stored in Mysql table. there are 2 columns 
> "license" and "hash":
>
> license_key   
> def50200352f5dc4bd8181a9daebbf4f9177fe725111a5a479d64636d01c2a10074e0c645abe898dea18210af563a5334288420551ab61c18ca4506cd03aa5d2bdd40933ddf7ca4d4b61b1c0f58a3830cbe0891cf4ff526311d5d637a55a574eca2c3a1b487b56
>
> hash
> 9498cbf8bf00d6c55e31f98ba6d8294afa3127a84f31aa622c4158ac7377c6dd
>
Slightly off-topic:

Why would you store *both* an encrypted key and a hash?
If you have the encrypted key in the database and the encryption key on your 
site (presumably in the Python code) and your site is hacked, all the license 
keys are potentially in the open.
And if your key is on the site you can as well encrypt the entered license key 
and compare it to the stored encrypted key.

end off-topic

> My python program get an input for user (the license key in string
> without any encrypton) and need to compare it with the official license
> key stored in Mysql database of our Wordpress website.
>
> I read a lot of hashlib python, functions and methods. But I didn't find
> anywhere how could I "hash" the string input typed by user with some
> hash values from the table, in order to compare both values (the input
> license and the license stored in mysql table).

This supposes Python 3:

Let's assume the entered key is in ASCII and stored in the variable 'key'.

from hashlib import sha256
key = bytes(key, 'ascii')
hash = sha256(key).hexdigest()

Now you can compare hash with the stored hash in the database. Of course this 
only works if that stored hash has been calculated in the same way from the 
same key.

On Python 2 (which you shouldn't use) you can leave out the "key = bytes(key, 
'ascii')" part.

You can of course make it more sophisticated, for example by using a salt. 
Unless your keys are extremely valuable, I wouldn't bother with that.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Relative import cannot find .so submodule?

2020-01-13 Thread Pieter van Oostrum
Patrick Stinson  writes:

> I have a module named rtmidi, and its C submodule named rtmidi/_rtmidi. The 
> distills script builds successfully and successfully creates a build/lib dir 
> with a rtmidi dir in it and the submodule file 
> rtmidi/_rtmidi.cpython-36dm-darwin.so. I have set PYTHONPATH to this lib dir, 
> but rtmidi/__init__.py gives the following error:
>
> Traceback (most recent call last):
>   File "main.py", line 6, in 
> from pkmidicron import MainWindow, util, ports
>   File "/Users/patrick/dev/pkmidicron/pkmidicron/__init__.py", line 1, in 
> 
> from .mainwindow import *
>   File "/Users/patrick/dev/pkmidicron/pkmidicron/mainwindow.py", line 2, in 
> 
> import rtmidi
>   File "/Users/patrick/dev/pkmidicron/pyrtmidi/build/lib/rtmidi/__init__.py", 
> line 1, in 
> from ._rtmidi import *
> ModuleNotFoundError: No module named 'rtmidi._rtmidi’
>
> How does the module finder work in the import system? I assume ti 
> automatically resolves the name _rtmidi.cpython-36dm-darwin.so to _rtmidi? I 
> didn’t have any luck reading the docs on the import system.

Are you running python 3.6?

I tried this on python 3.7 and it worked, but the file is called 
_rtmidi.cpython-37m-darwin.so there (37 for python3.7, and the d is missing, I 
don't know what that indicates).
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I need to create .odt or .rtf documents in Python3. what is the best tool to do this....

2020-01-12 Thread Pieter van Oostrum
christopher rehm  writes:

> hi all im new here. i need to create .odt or rtf documents from a python
> 3 program. i need a package or library that actually has real
> documentation and examples, using python3 and the tool ( sorry PyRTF3 is
> a major failure here)
> does anyone have any suggestions?
>
> chris

https://github.com/eea/odfpy
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: looking for git with a solution - merge many pdfs to 1 pdf (no matter what language)

2020-01-08 Thread Pieter van Oostrum
alon.naj...@gmail.com writes:

> hi
> looking for git with a solution - merge many pdfs to 1 pdf (no matter what 
> language)

There is a clone of pdftk on github: https://github.com/ericmason/pdftk

Another possibility is mupdf: http://git.ghostscript.com/?p=mupdf.git
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: name 'sys' is not defined

2019-12-30 Thread Pieter van Oostrum
safiq...@gmail.com writes:

> Deal all,
> Could you please help me how can I avoid this problem
> my Jupyter Notebook code was 
> help pls
>
>
>
> # init a treemix analysis object with some param arguments
> tmx = ipa.treemix(
> data=data,
> imap=imap,
> minmap=minmap,
> seed=123456,
> root="Petronia_petronia",
> m=2,
> )
>
> error
>
> NameError Traceback (most recent call last)
>  in 
>   6 seed=123456,
>   7 root="Petronia_petronia",
> > 8 m=2,
>   9 )
>
> ~/opt/miniconda3/envs/py3/lib/python3.6/site-packages/ipyrad/analysis/treemix.py
>  in __init__(self, data, name, workdir, imap, minmap, seed, quiet, 
> raise_root_error, binary, *args, **kwargs)
> 118 
> 119 # others
> --> 120 self.binary = os.path.join(sys.prefix, "bin", "treemix")
> 121 self.binary = (binary if binary else self.binary)
> 122 self.raise_root_error = raise_root_error
>
> NameError: name 'sys' is not defined

As the name sys is used in the imported module, that module has to import sys.
Importing it in the calling code doesn't help.
So I would say this is a bug in the module.

You should report the bug to its author. In the meantime you can correct your 
own copy at
~/opt/miniconda3/envs/py3/lib/python3.6/site-packages/ipyrad/analysis/treemix.py
by adding
import sys
for example around line 10

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib unqoute providing string mismatch between string found using os.walk (Python3)

2019-12-21 Thread Pieter van Oostrum
Ben Hearn  writes:

> Hello all,
>
> I am having a bit of trouble with a string mismatch operation in my tool I am 
> writing.
>
> I am comparing a database collection or url quoted paths to the paths on the 
> users drive.
>
> These 2 paths look identical, one from the drive & the other from an xml url:
> a = '/Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! 
> _PromoMix_.wav'
> b = '/Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! 
> _PromoMix_.wav'
>
> But after realising it was failing on them I ran a difflib and these 
> differences popped up.
>
> import difflib
> print('\n'.join(difflib.ndiff([a], [b])))
> - /Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! 
> _PromoMix_.wav
> ? ^^
> + /Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! 
> _PromoMix_.wav
> ? ^
>
>
> What am I missing when it comes to unquoting the string, or should I do
> some other fancy operation on the drive string?
>

In [8]: len(a)
Out[8]: 79

In [9]: len(b)
Out[9]: 78

The difference is in the ó. In (b) it is a single character, Unicode 0xF3,
LATIN SMALL LETTER O WITH ACUTE.
In (a) it is composed of the letter o and the accent "́" (Unicode 0x301).
So you would have to do Unicode normalisation before comparing.

For example:

In [16]: from unicodedata import normalize

In [17]: a == b
Out[17]: False

In [18]: normalize('NFC', a) == normalize('NFC', b)
Out[18]: True

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: hexdump module installation error

2019-12-19 Thread Pieter van Oostrum
tommy yama  writes:

> user@USERnoMacBook-Air LibraBrowser % python3 hexdump.py
>
> /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python:
> can't open file 'hexdump.py': [Errno 2] No such file or directory
>
> user@USERnoMacBook-Air LibraBrowser %

Could it be that your pip3 belongs to a different Python than the one above 
(for example a Python 3.8 or 3.6)? What is the output of 'pip3 --version' 
(without quotes)?

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: INHERITANCE in python3

2019-12-19 Thread Pieter van Oostrum
Random832  writes:

> On Wed, Dec 18, 2019, at 23:10, vahid asadi via Python-list wrote:
>> HI guys this is my first post on python mailing lists ever and i glad 
>> to do this.
>> my problem here is why this attribute is not recognize by python and it 
>> raise an traceback error that said 'there is no such p.family 
>> attribute'. although i use multiple   inheritance with 'super ' it not 
>> works. thanks for your help.
>> 
>> ``` class Username:    def __init__(self,name,*args):        self.name 
>> = name
>> class Userfamily:    def __init__(self,family,*args):        
>> self.family = family
>> class Person(Username,Userfamily):    def __init__(self,*args):        
>> super().__init__(*args)
>> 
>> 
>> p = Person("v","aaa")print(p.name)print(p.family)```

Please next time, supply a properly indented Python source, with only normal 
ASCII spaces, not no-break spaces, i.e. exactly like in your Python source code.
>
> The Username class also needs to call super(). In general, super() is
> intended to be used with all classes that might be part of a multiple
> inheritance hierarchy, not just the derived one.

Just for safety, also add it to the Userfamily class.
-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: os.system vs subrocess.call

2019-11-28 Thread Pieter van Oostrum
Ulrich Goebel  writes:

> Hi,
>
> I have to call commands from inside a python skript. These commands are
> in fact other python scripts. So I made
>
> os.system('\.Test.py')
>
> That works.

In a string \. is the same as . So this should execute the command '.Test.py'. 
Is that really what you did? Or did you mean os.system('./Test.py') which is 
much more probable.
NOTE: Never retype the commands that you used, but copy and paste them.

>
> Now I tried to use
>
> supprocess.call(['.\', 'test.py'])
>
That can't have given the error below, as it would have to be subprocess.call,
not supprocess.call.
And then also '.\' would have given a syntax error.
NOTE: Never retype the commands that you used, but copy and paste them.

> That doesn't work but ends in an error:
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python3.5/subprocess.py", line 557, in call
> with Popen(*popenargs, **kwargs) as p:
>   File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
> restore_signals, start_new_session)
>   File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
> raise child_exception_type(errno_num, err_msg)
> PermissionError: [Errno 13] Permission denied
>
> Using
>
> subprocess.call(['./', 'Test.py'], shell=True)
>
> I get
>
> Test.py: 1: Test.py: ./: Permission denied
>
Why would you do that, splitting './Test.py' in two parts? That doesn't work.
> Is there a simple way to use subprocess in this usecase?
>

subprocess.call(['./Test.py'])

-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Global variable is undefined at the module level

2019-11-23 Thread Pieter van Oostrum
jacksonhaenc...@gmail.com writes:

> This is not accurate. I just tested this in python3 and using the global
> keyword allowed me to declare a variable inside the function that was
> then visible from the outer scope.

What do you mean by that?
Anything other than what was said here?

> On Tuesday, September 13, 2016 at 6:50:39 AM UTC-5, dieter wrote:

>> In order to define "VVV", you must assign a value to it -- either
>> directly in the "global" (i.e. module) namespace or in your function
>> (together with a "global VVV" declaration).

Without assignment the variable will not exist in the module namespace:
>>> def testfunc():
... global globvar
... pass
... 
>>> globvar
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'globvar' is not defined
>>> testfunc()
>>> globvar
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'globvar' is not defined

>>> def testfunc():
... global globvar
... globvar = 1
... 
>>> globvar
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'globvar' is not defined
>>> testfunc()
>>> globvar
1


-- 
Pieter van Oostrum
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: revise "([^/]+)$" into '([^/]+)$' in a lot of files under a directory.

2019-11-18 Thread Pieter van Oostrum
Hongyi Zhao  writes:

> On Sun, 17 Nov 2019 20:28:55 +0100, Pieter van Oostrum wrote:
>
>> To be honest, I myself would use Emacs, with rgrep and wgrep to do this.
>
> Are these tools superior to grep?

They are based on grep. But rgrep does a grep through a whole directory tree, 
or a selection thereof, specified by a file pattern.
The wgrep allows you to edit the grep output, for example just changing 
"([^/]+)$" to '([^/]+)$'.
And then you let it write the changes back to those files.
The advantage is that you see what you are doing.

But now this has become off-topic with regard to Python.
-- 
Pieter van Oostrum
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: revise "([^/]+)$" into '([^/]+)$' in a lot of files under a directory.

2019-11-17 Thread Pieter van Oostrum
Pieter van Oostrum  writes:

> It isn't that difficult with sed, only you have to chose a different
> character than / in the substitute command, one that is not present in
> both texts, e.g instead of s/a/b/ use s=a=b=.
>
> And then the special characters " ' () [ and $ must be escaped for the
> shell, and [ and $ also for the regexp.
> Then it comes down to
> sed -e s=\"\(\\[^/]+\)\\$\"=\'\(\[^/]+\)\$\'= file

To be honest, I myself would use Emacs, with rgrep and wgrep to do this.
-- 
Pieter van Oostrum
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: revise "([^/]+)$" into '([^/]+)$' in a lot of files under a directory.

2019-11-17 Thread Pieter van Oostrum
Hongyi Zhao  writes:

> Hi,
>
> I have a lot of files which resides under a directory, and I want to 
> resive all of the content  "([^/]+)$" in these files into '([^/]+)$'.
>
> I have tried with sed/xargs, but it seems so difficult for dealing with 
> the \ ,   is there any convinent method to to this job with python? 
>
> Regards

It isn't that difficult with sed, only you have to chose a different character 
than / in the substitute command, one that is not present in both texts, e.g 
instead of s/a/b/ use s=a=b=.

And then the special characters " ' () [ and $ must be escaped for the shell, 
and [ and $ also for the regexp.
Then it comes down to
sed -e s=\"\(\\[^/]+\)\\$\"=\'\(\[^/]+\)\$\'= file
-- 
Pieter van Oostrum
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: nonlocal fails ?

2019-11-15 Thread Pieter van Oostrum
"R.Wieser"  writes:

> Jan,
>
>> So what you want to do is dynamic scope?
>
> No, not really.I was looking for method to let one procedure share a 
> variable with its caller - or callers, selectable by me.   And as a "by 
> reference" argument does not seem to exist in Python ...
>
> And yes, I am a ware that procedures can return multiple results.  I just 
> didn'want to go that way (using the same variable twice in a single 
> calling).

Do you mean, if Proc1() is called from Func1, it uses MyVar defined in Func1, 
and if it is called from Func2, it uses MyVar from Func2?

If that is what you mean, that would be dynamic scope.
-- 
Pieter van Oostrum 
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Funny behavior of IDLE 3.7.0

2019-11-12 Thread Pieter van Oostrum
r...@zedat.fu-berlin.de (Stefan Ram) writes:

>   When I enter 
>
> i = 4
> x = 2.3
> s = 'abc'
> print( f'{i=}' )
>
>   into a file window of IDLE 3.7.0, it marks the '=' 
>   sign in the /first/ line and says 'invalid syntax'.
>
>   Remove the »f«, and the error will disappear.

I did this in IDLE 3.7.5, and it gives a syntax error on the last line.
This is correct, because the f-string is in error. Between the curly
braces there should be an expression, possibly followed by a conversion
(!...) and/or a format specification (:...). {i=} is not a correct
expression. When you remove the »f«, it becomes a normal string, where
the {} don't have a special meaning.

-- 
Pieter van Oostrum 
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __instancecheck__ metaclasses, how do they work: why do I get True when I tuple, why doesn't print run?

2019-11-05 Thread Pieter van Oostrum
Chris Angelico  writes:

> On Tue, Nov 5, 2019 at 5:43 PM dieter  wrote:
>> I suppose that "isinstance" (at least under Python 2) does not
>> behave exactly as stated in PEP 3119. Instead, "isinstance"
>> first directly checks for the instance to be an instance of the
>> class *AND ONLY IF THIS FAILS* calls the class' "__instancecheck__".
>
> PEP 3119 is specifically about Python 3.0; I don't know how much, if
> any, was backported into Python 2.

Yes, it has been there since Python 2.6.

I looked in the implementation, and isinstance(inst, cls) first checks if the 
class of ins is cls, then the result is True.
There are a few other shortcut cases, and only at the end the __instancecheck__ 
method is called.

You can check this with the original example:

In [88]: class MA(type):
...:   def __instancecheck__(cls, inst):
...: print "MA", cls, inst
...: 
...: class AM(list): __metaclass__ = MA
...: class AM2(AM): pass
...: 
...: am = AM2()

In [89]: isinstance(am, AM)
MA  []
Out[89]: False

It returns False because __instancecheck__ returns None

Same for Python3:

In [8]: class MA(type):
   ...:   def __instancecheck__(cls, inst):
   ...: print ("MA", cls, inst)
   ...: 
   ...: class AM(list, metaclass = MA): pass
   ...: class AM2(AM): pass
   ...: 
   ...: am = AM2()

In [9]: isinstance(am, AM)
MA  []
Out[9]: False

-- 
Pieter van Oostrum 
WWW: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list