RE: Optimizing Small Python Code

2021-06-22 Thread Avi Gross via Python-list
I had a similar question, Greg. Optimized for what, indeed.

Some might suggest removing a VISIBLE loop is an optimization and some might
not. 

First you need to look at what your code does. It is fairly primitive. 

Here is a two-line version but is it simpler:

for n in range(1, 7):
   print (n, "\n", ''.join([("\t" + str(x) + "\n") for x in range(0, n)]))

It has the same output:

1 
0

2 
0
1
...
6 
0
1
2
3
4
5

This can be made a one-liner too! LOL!

But there are other optimizations as the calculation sort of keeps
recalculating what is a simple enough pattern.

You can for example in a loop of some kind keep appending to a structure
like a list but perhaps simpler is using indexing into a tuple or list made
in advance and only once. Here is the code that works for any size by
changing the value of "last" without constant recalculation. Is it better?
Who knows?

last = 7; nl = "\n"; tab = "\t"
using = [ tab + str(num) for num in range(0,last)]
print(''.join([str(n) + nl + nl.join(using[:n]) + nl  for n in range(1,
last)]))

Now why you want this is beyond me!

-Original Message-
From: Python-list  On
Behalf Of Greg Ewing
Sent: Tuesday, June 22, 2021 7:05 PM
To: python-list@python.org
Subject: Re: Optimizing Small Python Code

On 23/06/21 3:03 am, Kais Ayadi wrote:
> for n in range(1, 7):
>  print (n)
>  for x in range(0, n):
>  print(" ", x)
> 
> can this code be more optimised?

Optimised for what? Readability? Execution speed? Code size?
Memory usage?

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

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


Re: Optimizing Small Python Code

2021-06-22 Thread Benjamin Schollnick
How would you measure the steps that it takes?

- Benjamin



> On Jun 22, 2021, at 7:04 PM, Greg Ewing  wrote:
> 
> On 23/06/21 3:03 am, Kais Ayadi wrote:
>> for n in range(1, 7):
>> print (n)
>> for x in range(0, n):
>> print(" ", x)
>> can this code be more optimised?
> 
> Optimised for what? Readability? Execution speed? Code size?
> Memory usage?
> 
> -- 
> Greg
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Optimizing Small Python Code

2021-06-22 Thread Greg Ewing

On 23/06/21 3:03 am, Kais Ayadi wrote:

for n in range(1, 7):
 print (n)
 for x in range(0, n):
 print(" ", x)

can this code be more optimised?


Optimised for what? Readability? Execution speed? Code size?
Memory usage?

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


Optimizing Small Python Code

2021-06-22 Thread Kais Ayadi
Hi There!
this is a small python code executed in 61 steps

for n in range(1, 7):
print (n)
for x in range(0, n):
print(" ", x)

can this code be more optimised?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python threading comparison

2021-06-22 Thread Chris Angelico
On Wed, Jun 23, 2021 at 5:34 AM Dan Stromberg  wrote:
>
> I put together a little python runtime comparison, with an embarallel,
> cpu-heavy threading microbenchmark.
>
> It turns out that the performance-oriented Python implementations, Pypy3
> and Nuitka3, are both poor at threading, as is CPython of course.
>
> On the plus side for CPython, adding cpu-heavy threads doesn't cause
> performance to get significantly worse anymore.
>
> The pleasant result is that Micropython threads pretty well!
>
> There's a graph of the performance curves at:
> https://stromberg.dnsalias.org/~strombrg/python-thread-comparison/

The legend's collided with some of the graph itself, making it a bit
hard to read, but if I'm not mistaken, you have CPython and Nuitka
basically identical and worst, and then PyPy much faster but not
benefiting from threads, and uPy as the only one that benefits.

So what that means is that uPy has a completely different
threading/locking model, which is interesting, but all the others are
basically just behaving the same way as each other.

I'd be curious to know how this would be affected if the threads had a
small amount of shared data. Once they get into their main work
(summing a range), they're completely independent, apart from
potentially sharing integer objects in the low range (which will be
completely insignificant in the stats). What if, instead, you create a
single global range object and all the threads iterate over it?

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


Python threading comparison

2021-06-22 Thread Dan Stromberg
I put together a little python runtime comparison, with an embarallel,
cpu-heavy threading microbenchmark.

It turns out that the performance-oriented Python implementations, Pypy3
and Nuitka3, are both poor at threading, as is CPython of course.

On the plus side for CPython, adding cpu-heavy threads doesn't cause
performance to get significantly worse anymore.

The pleasant result is that Micropython threads pretty well!

There's a graph of the performance curves at:
https://stromberg.dnsalias.org/~strombrg/python-thread-comparison/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: IDLE is not working after Python installation .

2021-06-22 Thread Mats Wichmann



On 6/21/21 11:14 PM, Ayaana Soni wrote:

I  have installed python from your site. After installation my IDLE doesn't
work.  IDLE is not in my search list. Plz help!!




Thank you!



you asked this before, and didn't answer the questions you got in reply.

What does "doesn't work" mean? How is the problem manifested?  Doesn't 
IDLE selected from the start menu come up? Normally the installation 
creates a shortcut there that "does the right thing".  On Windows you 
don't normally start IDLE by typing a command name. You *can* bring it 
up from a command-line if you type


py -m idlelib

but this is definitely not the most common way to launch.

If you haven't read it, look at

https://docs.python.org/3/using/windows.html


among other things, the Microsoft Store version of Python is sometimes a 
little easier to get started with (it's the same programs, just packaged 
differently)

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


Re: IDLE is not working after Python installation .

2021-06-22 Thread Terry Reedy

On 6/22/2021 1:14 AM, Ayaana Soni wrote:

I  have installed python from your site.


For what OS.


After installation my IDLE doesn't work.


How did you try to start it?  Did you read the Using Python doc on the 
website?  Can you start python?



 IDLE is not in my search list.


On Windows and macOS, IDLE is usually started from an installed icon.

--
Terry Jan Reedy

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


Re: create an empty RGB image with specified number of cells (rows, columns)

2021-06-22 Thread Arak Rachael
On Monday, 21 June 2021 at 21:20:18 UTC+2, Dan Stromberg wrote:
> I don't know about OpenCV, but here's a way of creating a ppm image file of 
> arbitrary size and arbitrary solid color: 
> https://stromberg.dnsalias.org/svn/solid/trunk 
> 
> On Mon, Jun 21, 2021 at 4:01 AM Arak Rachael  
> wrote:
> > Hi guys! 
> > 
> > Does anyone know how to do this, if possible with OpenCv?
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list 
> >
Thanks for the help! I am new to Python, one company gave me a 2 month test 
period to see if I can, but its really hard.

This is the assignment:
[code]
TopGIS Dataset Visualization

In this assignment, you will work with satellite images downloaded along a GPS 
route from TopGIS service. You can download the dataset here: 
http://download.mykkro.cz/valeo/route.zip .

1. Create a new Python module (package) named topgis-viz. This module will have 
this basic directory structure:

topgis-viz/
config/ # this will contain your YAML configuration files (if any is 
necessary)
config.yaml# a default configuration file
data/   # unpack the dataset into this directory (so it contains a 
subdirectory named topgis with images)
target/ # this folder will contain all of your outputs
topgisviz/  # this folder will contain the module code
__init__.py
# all the other stuff
setup.py# the setup script for the package 
requirements.txt
README.md   # a documentation page, with a short description of the module 
and usage instructions
topgis-test.py
.gitignore  # don't forget to .gitignore data/ and target/ folders

You will use the module by calling it from command line as follows:

python topgis-test.py [--config=config/config.yaml]

The --config parameter is optional, if not used, the default value 
config/config.yaml will be used. Use argparse library to read the command line 
arguments, like this:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Path to the config file."
)

args = parser.parse_args()
config = args.config

When run, this script will read the configuration file in YAML format (read 
about YAML here: https://yaml.org/) into a Python dictionary. You can use this 
code:

def load_yaml(yaml_path):
with open(yaml_path, "r", encoding="utf-8") as infile:
return yaml.load(infile, Loader=yaml.FullLoader)

The configuration file will look similarly to this:

# configuration file for topgis-viz

# glob pattern for finding the source files
sources: "data/topgis/utm_*.jpg"
output_dir: "target/mosaic/"
mosaic:
columns: 5
rows: 2
cell_width: 250
cell_height: 250
pipeline:
- "A0 = src"
- "A1 = greyscale A0"
- "A2 = red A0"
- "A3 = green A0"
- "A4 = blue A0"
- "B0 = equalized A0"
- "B1 = equalized_greyscale A1"
- "B2 = equalized A2"
- "B3 = equalized A3"
- "B4 = equalized A4"

These parameters govern what the script will do with the images. The script 
will:

get a list of available images (based on sources parameter, via glob.glob 
function) and sort it alphabetically in increasing order
iterate over the list of images and for each of those source images:
create an empty RGB image with specified number of cells (rows, columns)
the cells of the grid are denoted by letter-number code indicating 
column and row. E.g. A0 is top-left cell.
run a processing pipeline on the source image. The pipeline is defined 
by a list of strings, which are evaluated in sequence. The format of each 
string is as follows: CELL = FUNCTION [ARGS]. E.g. string B0 = equalized A0 
means that cell B0 of the grid will be filled with an image that comes as 
result of calling image function equalized on an image in cell A0.
the original size of the images is 1000x1000 pixels. Do the pipeline 
operations on images with this original size. Resize the images (to cell_width 
* cell_height) only when putting them into the big mosaic image.
the result of the pipeline will be stored in the specified output 
directory as a JPG image (Use this naming pattern: f"{index:05}.jpg").

The following pipeline functions should be supported:

src - just the source image
greyscale RGB_IMAGE - returns a greyscale image constructed from RGB_IMAGE
red RGB_IMAGE - returns a color image that contains only red channel
green RGB_IMAGE - returns a color image that contains only green channel
blue RGB_IMAGE - returns a color image that contains only blue channel
equalized RGB_IMAGE - returns a color image with equalized intensities
equalized_greyscale BW_IMAGE - returns a greyscale image with equalized 
intensities

Most of the functions should be easy to implement. For equalization of colored 
images, use this function:

def equalize_histogram_rgb(rgb_img):
# convert from RGB color-space to YCrCb
ycrcb_img = cv2.c

Re: create an empty RGB image with specified number of cells (rows, columns)

2021-06-22 Thread Arak Rachael
Thanks for the help! I am new to Python, one company gave me a 2 month test 
period to see if I can, but its really hard.

This is the assignment:
[code]
TopGIS Dataset Visualization

In this assignment, you will work with satellite images downloaded along a GPS 
route from TopGIS service. You can download the dataset here: link removed do 
to NDA.

1. Create a new Python module (package) named topgis-viz. This module will have 
this basic directory structure:

topgis-viz/
config/ # this will contain your YAML configuration files (if any is necessary)
config.yaml # a default configuration file
data/ # unpack the dataset into this directory (so it contains a subdirectory 
named topgis with images)
target/ # this folder will contain all of your outputs
topgisviz/ # this folder will contain the module code
__init__.py
# all the other stuff
setup.py # the setup script for the package
requirements.txt
README.md # a documentation page, with a short description of the module and 
usage instructions
topgis-test.py
.gitignore # don't forget to .gitignore data/ and target/ folders

You will use the module by calling it from command line as follows:

python topgis-test.py [--config=config/config.yaml]

The --config parameter is optional, if not used, the default value 
config/config.yaml will be used. Use argparse library to read the command line 
arguments, like this:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Path to the config file."
)

args = parser.parse_args()
config = args.config

When run, this script will read the configuration file in YAML format (read 
about YAML here: https://yaml.org/) into a Python dictionary. You can use this 
code:

def load_yaml(yaml_path):
with open(yaml_path, "r", encoding="utf-8") as infile:
return yaml.load(infile, Loader=yaml.FullLoader)

The configuration file will look similarly to this:

# configuration file for topgis-viz

# glob pattern for finding the source files
sources: "data/topgis/utm_*.jpg"
output_dir: "target/mosaic/"
mosaic:
columns: 5
rows: 2
cell_width: 250
cell_height: 250
pipeline:
- "A0 = src"
- "A1 = greyscale A0"
- "A2 = red A0"
- "A3 = green A0"
- "A4 = blue A0"
- "B0 = equalized A0"
- "B1 = equalized_greyscale A1"
- "B2 = equalized A2"
- "B3 = equalized A3"
- "B4 = equalized A4"

These parameters govern what the script will do with the images. The script 
will:

get a list of available images (based on sources parameter, via glob.glob 
function) and sort it alphabetically in increasing order
iterate over the list of images and for each of those source images:
create an empty RGB image with specified number of cells (rows, columns)
the cells of the grid are denoted by letter-number code indicating column and 
row. E.g. A0 is top-left cell.
run a processing pipeline on the source image. The pipeline is defined by a 
list of strings, which are evaluated in sequence. The format of each string is 
as follows: CELL = FUNCTION [ARGS]. E.g. string B0 = equalized A0 means that 
cell B0 of the grid will be filled with an image that comes as result of 
calling image function equalized on an image in cell A0.
the original size of the images is 1000x1000 pixels. Do the pipeline operations 
on images with this original size. Resize the images (to cell_width * 
cell_height) only when putting them into the big mosaic image.
the result of the pipeline will be stored in the specified output directory as 
a JPG image (Use this naming pattern: f"{index:05}.jpg").

The following pipeline functions should be supported:

src - just the source image
greyscale RGB_IMAGE - returns a greyscale image constructed from RGB_IMAGE
red RGB_IMAGE - returns a color image that contains only red channel
green RGB_IMAGE - returns a color image that contains only green channel
blue RGB_IMAGE - returns a color image that contains only blue channel
equalized RGB_IMAGE - returns a color image with equalized intensities
equalized_greyscale BW_IMAGE - returns a greyscale image with equalized 
intensities

Most of the functions should be easy to implement. For equalization of colored 
images, use this function:

def equalize_histogram_rgb(rgb_img):
# convert from RGB color-space to YCrCb
ycrcb_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2YCrCb)

# equalize the histogram of the Y channel
ycrcb_img[:, :, 0] = cv2.equalizeHist(ycrcb_img[:, :, 0])

# convert back to RGB color-space from YCrCb
equalized_img = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR)

return equalized_img

The cv2.equalizeHist function can be used for equalization of black and white 
images.

Running this script should result with the target/mosaic directory populated 
with 624 of JPEG images named 0.jpg to 00623.jpg. Each of these images 
should be 1250 pixels wide and 500 pixels high and contain a mosaic of images, 
first row containing original, greyscale, red, green and blue images and the 
second row should contain the equalized versions of these images.
[code]

This is my co