Re: Importing from within package

2020-09-23 Thread Dieter Maurer
Abdur-Rahmaan Janhangeer wrote at 2020-9-22 20:14 +0400:
>I have this main script:
>https://github.com/Abdur-rahmaanJ/shopyo/blob/dev/shopyo/__main__.py
>
>However, i get errors after package installation
>saying:
>ImportError: cannot import name 'shopyoapi' from 'shopyo'

It is right: `shopyyoapi` is not a module (no corresponding file in
`shopyo` and `shopyo:__init__.py` does not define a corresponding name.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Importing from within package

2020-09-23 Thread Abdur-Rahmaan Janhangeer
In shopyo/shopyo (the same folder as __main__.py) there is a folder called
shopyoapi.

What i want is to import create_module from cmd.py from shopyoapi in
__main__.py but it does not work or more precisely i am looking how to make
it work!

Since this is not py2 i guess no init needed in shopyoapi

Kind Regards,


Abdur-Rahmaan Janhangeer

https://www.github.com/Abdur-RahmaanJ

Mauritius

sent from gmail client on Android, that's why the signature is so ugly.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.8.5 Not Launching

2020-09-23 Thread Terry Reedy

On 9/22/2020 11:54 PM, yehudis...@gmail.com wrote:


I installed Python 3.8.5 on Windows 10
When I click on a python file it launches the program but it closes
immediately.


When you run a program that way, the console/terminal window closes when 
the program finishes executing.



--
Terry Jan Reedy

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


Re: Importing from within package

2020-09-23 Thread Dieter Maurer
Abdur-Rahmaan Janhangeer wrote at 2020-9-23 22:41 +0400:
>In shopyo/shopyo (the same folder as __main__.py) there is a folder called
>shopyoapi.

Not in the distribution:
...
shopyo-1.1.45/shopyo/
shopyo-1.1.45/shopyo/__init__.py
shopyo-1.1.45/shopyo/__main__.py
shopyo-1.1.45/shopyo/app.py
shopyo-1.1.45/shopyo/config.py
shopyo-1.1.45/shopyo/manage.py
shopyo-1.1.45/shopyo/test_frontend.py
shopyo-1.1.45/shopyo.egg-info/
...

Maybe, that is your problem (missing folder in the distribution).
-- 
https://mail.python.org/mailman/listinfo/python-list


python show .

2020-09-23 Thread pascal z via Python-list
Hello, I'm working on a script where I want to loop into folders somehow 
recursively to get information but I want to limit the infos for the files on a 
certain level of folders for example:

/home/user/Documents/folder1
/home/user/Documents/folder2
/home/user/Documents/folder3/folder1/file1
/home/user/Documents/folder4/file1
/home/user/Documents/file1
/home/user/Documents/file2
/home/user/Documents/file3

I only want file1, 2, 3 at the root of Documents to show (write to a csv) and 
I'm using the script below

### SCRIPT###
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

csv_contents = ""
output_file = '/home/user/Documents/csv/output2csv.csv'
Lpath = '/home/user/Documents/'

csv_contents = "FOLDER PATH;Size in Byte;Size in Kb;Size in Mb;Size in Gb\n"

for root, dirs, files in os.walk(Lpath, topdown=False):
counter = Lpath.count(os.path.sep)
if counter < 5:
for f in os.listdir(root):
path = os.path.join(root, f)
f_size = 0
f_size = os.path.getsize(path)
csv_contents += "%s   ;%.2f   ;%.2f   ;%.2f   ;%.2f  \n" % (path, 
f_size, f_size/1024, f_size/1048576, f_size/1073741824)

fp = open(output_file, "w")
fp.write(csv_contents)
fp.close()
### END OF SCRIPT###

When I run this script, I get files in subfolders. For now, I need to keep 
using "os.walk" because the script includes functions that I didn't include to 
make thing simple.

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


Re: Importing from within package

2020-09-23 Thread Abdur-Rahmaan Janhangeer
Thanks for the lead. Checking tomorrow!
If so i'll have to include other folders as well ...

Kind Regards,


Abdur-Rahmaan Janhangeer

https://www.github.com/Abdur-RahmaanJ

Mauritius

sent from gmail client on Android, that's why the signature is so ugly.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python show .

2020-09-23 Thread Cameron Simpson
On 23Sep2020 13:24, pascal z  wrote:
>Hello, I'm working on a script where I want to loop into folders somehow 
>recursively to get information but I want to limit the infos for the files on 
>a certain level of folders for example:
>
>/home/user/Documents/folder1
>/home/user/Documents/folder2
>/home/user/Documents/folder3/folder1/file1
>/home/user/Documents/folder4/file1
>/home/user/Documents/file1
>/home/user/Documents/file2
>/home/user/Documents/file3
>
>I only want file1, 2, 3 at the root of Documents to show (write to a csv) and 
>I'm using the script below

Like the -depth option to the find command?

First I would note that if you only want the top level, you don't need 
to use os.walk, just look at os.listdir.

But if you want to be more flexible (eg down to level 3, or just 
particular directories), then you need os.walk. So, to your script...

The first thing to note is that os.walk hands you to actual list of 
subdirectory names it is going to use as "dirs". If you modify that list 
(in place), os.walk uses the modified list. You could sort that list to 
do a lexically ordered tree walk, and you can delete things from the 
list to prevent os.walk walking into those items.

For the deletion thing, indeed for the sort thing, it is often easiest 
to construct a new list with the desired result, then update the 
original - you need to update the original because that is the object 
os.walk is using:

for root, dirs, files in os.walk(Lpath, topdown=False):
# make a new list of "x*" names, sorted
new_subdirs = sorted(
dirname for dirname in dirs if dirname.startswith('z')
)
# update the original list in place
dirs[:] = new_subdirs

For the general form of your problem (limiting the depth) you need to 
know the depth of "root", which os.walk does not tell you directly.

Two approaches com to mind:

The lexical approach is to look at the path from Lpath to root and 
measure its length. os.path.relpath will get you the relative path, and 
you could split that on os.sep to get path components. There's also the 
Pathlib library.

The other approach to it compute the expected path depths. Keep a 
mapping (a dict) keyed on path with depth as the value. Prepare it with:

depths = {Lpath: 0}

Inside the loop, get the current depth:

depth = depths[root]

You can make decisions on that. For example, if the depth reaches your 
threshold you could simply empty the "dirs" list (remember to do that in 
place) to prevent os.walk going any deeper.

Before the end of the loop, compute and store the depth for each 
subdirectory:

for subdir in dirs:
subpath = os.path.join(root, subdir)
depths[subpath] = depth + 1

so that they are ready for use when os.walk gives them to you on later 
iterations.

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


Re: Pythonic style

2020-09-23 Thread Stavros Macrakis
Thanks, Chris, for the useful comments. Comments in line (with snipping of
unnecessary content):


> > some more variants which *don't* use tuple unpacking, on the theory
> that the coding patterns may be useful in
> > other cases where unpacking doesn't apply.
>
> When doesn't it apply? Can you elaborate on this? It might be easier
> to advise on Pythonic style when the specific requirements are known.
>

No specific requirement. These are *finger exercises* intended to isolate
one issue for discussion rather than be useful in themselves.


> > ...but there are ways to avoid the verbose exception handling.
> In Python, exception handling IS the way to do these things.


Good to know. Since *try* statements are at least 4 lines long, I was
trying for something more concise. And maybe somewhere in the back of my
mind, I have the anti-Pythonic principle that exceptions should be reserved
for errors. I'll try to suppress that. I remember one (very old) paper

arguing that exceptions were a bad idea in general... but I have
implemented and used exceptions in many languages, both before and after
that paper!


> > _uniq = []
> > def firstg(iterable):
> > it = iter(iterable)
> > val0 = next(it,_uniq)
> > val1 = next(it,_uniq)
> > if val0 is not _uniq and val1 is _uniq:
> > return val0
> > else:
> > raise ValueError("first1: arg not exactly 1 long")
> >
> > But I don't know if the *_uniq* technique is considered Pythonic.
>
> It is when it's needed, but a more common way to write this would be
> to have the sentinel be local to the function (since it doesn't need
> to be an argument):
>

What do you mean by an argument?

Is it not considered Pythonic for constants to be calculated once at the
module or class level rather than be recalculated every time a function is
entered?

def firstg_variant(iterable):
> it = iter(iterable)
> sentinel = object()
> first = next(it, sentinel)
> if first is sentinel:
> raise ValueError("empty iterable")
> second = next(it, sentinel)
> if second is not sentinel:
> raise ValueError("too many values")
> return first
>
> But getting a return value and immediately checking it is far better
> spelled "try/except" here. (Note, BTW, that I made a subtle change to
> the logic here: this version doesn't call next() a second time if the
> first one returned the sentinel. This avoids problems with broken
> iterators that raise StopException and then keep going.)
>

Yes, I hadn't thought of that. I was trying to avoid repeating the *raise*
 statement.

Thanks again for the helpful comments. They give me a better idea of good
Python style.

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


RE: Python 3.8.5 Not Launching

2020-09-23 Thread yehudisgru
   It’s a py file with simple python code

    

   From: [1]Igor Korot
   Sent: Wednesday, September 23, 2020 12:39 AM
   To: [2]yehudis...@gmail.com
   Cc: [3]python-list@python.org
   Subject: Re: Python 3.8.5 Not Launching

    

   Hi,

    

   On Tue, Sep 22, 2020 at 11:25 PM  wrote:

   > 

   >    Hi,

   > 

   > 

   > 

   >    I installed Python 3.8.5 on Windows 10

   > 

   >    When I click on a python file it launches the program but it closes

   >    immediately.

    

   What is the content of this file?

   Is it a py or pyc file?

    

   Thank you.

    

   > 

   > 

   > 

   >    Please help, thanks.

   > 

   > 

   > 

   >    Yehudis Gruber

   > 

   > 

   > 

   > 

   > --

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

    

References

   Visible links
   1. mailto:ikoro...@gmail.com
   2. mailto:yehudis...@gmail.com
   3. mailto:python-list@python.org
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.8.5 Not Launching

2020-09-23 Thread Eryk Sun
On 9/23/20, yehudis...@gmail.com  wrote:
>It’s a py file with simple python code

If .py files are associated with py.exe or python.exe, then running a
.py script either inherits or allocates a console and attaches to it.
The console closes automatically as soon as the last reference to it
closes. If you need the console to remain open, then it should be
inherited from the parent process. Typically that's a shell such as
CMD or PowerShell, but any process can manually allocate an
inheritable console via WinAPI AllocConsole.

To run a script from a shell command line, simply enter
"path\to\some_script.py". If the script is in a PATH directory and .PY
is in PATHEXT, then you can simply run "some_script" in CMD or
PowerShell.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python show folder files and not subfolder files

2020-09-23 Thread pascal z via Python-list
Please advise if the following is ok (i don't think it is)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

csv_contents = ""
output_file = '/home/user/Documents/csv/output3csv.csv'
Lpath = '/home/user/Documents/'

csv_contents = "FOLDER PATH;Size in Byte;Size in Kb;Size in Mb;Size in Gb\n"

d_size = 0
for root, dirs, files in os.walk(Lpath, topdown=False):
for i in files:
d_size += os.path.getsize(root + os.sep + i)
csv_contents += "%s   ;%.2f   ;%.2f   ;%.2f   ;%.2f  \n" % (root, d_size, 
d_size/1024, d_size/1048576, d_size/1073741824)

counter = Lpath.count(os.path.sep)
if counter < 5:
for f in os.listdir(Lpath):
path = os.path.join(Lpath, f)
f_size = 0
f_size = os.path.getsize(path)
csv_contents += "%s   ;%.2f   ;%.2f   ;%.2f   ;%.2f  \n" % (path, 
f_size, f_size/1024, f_size/1048576, f_size/1073741824)

fp = open(output_file, "w")
fp.write(csv_contents)
fp.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python show folder files and not subfolder files

2020-09-23 Thread pascal z via Python-list
ok, i came up with 
if os.path.isfile(path)
following 
path = os.path.join(Lpath, f)

and it seems to be ok, no dupplicates or wrong sizes...

thanks

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