Re: [Tutor] A required question

2018-11-24 Thread Avi Gross
Alan,

I appreciate the feedback. I am guilty of starting with what was meant to be an 
example that wandered off into something that looks like I am developing an 
actual set of methods. 

But on the main topic, I wondered if the language or user community had already 
built some FUNCTIONALITY along these lines.I was thinking, as usual, across 
languages and environments so let me explain.

The import statement in R is something like:
library("ggplot2")

When it fails, it generates a serious error.

A sister routine is available with a similar call that instead returns 
True/False. It happens to be called:

require("ggplot2")

Since it return a TRUE/FALSE value (R uses full uppercase for the pythonic 
True/False) you can write a function that tests to see if the package was 
loaded and if not, tries to install it and load it.

if (! require(GARBARGE)) {
  install.packages("GARBARGE")
  library(GARBARGE)
}

Of course since there is no such package, I still get an error. But if it was 
something that the user on that machine had not loaded but needed, it might 
pause and lock and load and be able to continue.

BTW, R sometimes requires strings to be quoted but sometimes because of very 
lazy evaluation, you can skip the quotes and the function can take the naked 
value off the argument stack before it is evaluated, turn it into a quoted 
string, and off you go. The install.packages command does not do this but the 
other two do.

Back to Python. I know how to check if a module is already loaded into a 
program. But I am not aware of how you can check if it can be loaded except by 
trying to load it. That may be enough.

<>

print("-=" * 20)
print("Trying to import non-existent module garbarge")
try:
import garbarge
except:
print("failed")
else:
print("I am drowning in garbarge!!!")

print("=-" * 20)
print("Getting ready to process numbers with module numpy.")
try:
import numpy
except:
print("drat! failed again")
else:
print("I can calculate on numpy!")

print("-=" * 20)

<>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Trying to import non-existent module garbarge
failed
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Getting ready to process numbers with module numpy.
I can calculate on numpy!
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

<>
In that code, I suppose, you could insert an appropriate call to load the 
failed module and try again. I could design and write a function like that BUT 
I am going to behave 😉

Hope that helps explain where I am coming from. Since I can see how to write 
such tools, I have to suspect it has been done, not just once, but a seemingly 
infinite number of times.

And note actually importing a module to test if it can be can have side effects.
So, to add value, I am going to partially answer my own question. There are 
modules that help.


import imp
>>> imp.find_module('eggs')
Traceback (most recent call last):
  File "", line 1, in 
imp.find_module('eggs')
  File 
"C:\Users\avid2016\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 
297, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named 'eggs'

>>> imp.find_module('pandas')
(None, 
'C:\\Users\\avid2016\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas',
 ('', '', 5))

So I took a quick try to see if I could slightly replicate an attempt to load. 
No guarantees this exact code works anywhere else:

I hardc-oded in "pandas" as the first try:

modulosity = "pandas"

print(" importing " + modulosity + "By hook or by crook.")

import imp
var = imp.find_module(modulosity)

if var:
print ("FOUND: " + modulosity + " at " + str(var))
else:
print("Module needs loading:" + modulosity)
import os
os.system("python -m pip install " + modulosity)

exec(f"import {modulosity}")

Output:

importing pandasBy hook or by crook.

Warning (from warnings module):
  File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", 
line 28
import imp
DeprecationWarning: the imp module is deprecated in favour of importlib; see 
the module's documentation for alternative uses
FOUND: pandas at (None, 
'C:\\Users\\avid2016\\AppData\\Roaming\\Python\\Python37\\site-packages\\pandas',
 ('', '', 5))

Now replacing pandas by non-existent koala:

importing koalaBy hook or by crook.

Warning (from warnings module):
  File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", 
line 28
import imp
DeprecationWarning: the imp module is deprecated in favour of importlib; see 
the module's documentation for alternative uses
Traceback (most recent call last):
  File "C:/Users/avid2016/AppData/Local/Programs/Python/Python37-32/temp.py", 
line 29, in 
var = imp.find_module(modulosity)
  File 
"C:\Users\avid2016\AppData\Local\Programs\Python\Python37-32\lib\imp.py", line 
297, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named 'koala'

Amazingly it failed!

Calling it a night as I f

Re: [Tutor] A required question

2018-11-24 Thread Avi Gross
David,

As I suspected. Yes, I am aware how to do those things. Just wondered if anyone 
automated the process so a fairly simple interface worked. 

I am dropping the request.

Avi

-Original Message-
From: Tutor  On Behalf Of David 
Rock
Sent: Friday, November 23, 2018 9:28 PM
To: Tutor Python 
Subject: Re: [Tutor] A required question


> On Nov 23, 2018, at 09:35, Alan Gauld via Tutor  wrote:
> 
> On 23/11/2018 05:34, Avi Gross wrote:
>> What I was thinking was the ability to do something like this:
>> 
>> import ReChoir as require
>> 
>> require.version(condition, before=True, after=False) 
>> require.modules(module list, recursive=True) require.os([“Eunuchs”, 
>> “Windblows”])
>> require.functionality(“print3”)
> 
> I can see the logic but suspect discussion of new features is probably 
> the preserve of the main Python list. If you can get traction there 
> somebody might actually go ahead and write one!

discussion of a “require” library probably isn’t necessary.  It’s pretty 
straightforward to include the logic using existing methods.

For the version of python, test against sys.version_info For the modules, put 
your import calls in a try block and handle exceptions For the OS version, test 
against os.name or sys.platform The last one, “functionality,” is a bit vague.  
Probably another candidate for a try block.


—
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python 3] Threads status, join() and Semaphore queue

2018-11-24 Thread Dimitar Ivanov
Hi Cameron,

Massive apologies for the delayed answer!

Your explanation definitely clears up quite a bit of my misunderstanding,
thank you for that!

There was a reason why I shy away from using Queue, but for the life of me
I can't remember right now what that reason was. I will have to modify my
code using your example and give it another try, I will make sure to let
you know if I run into any issues or additional questions. :)

Regards,
Dimitar

On Tue, 20 Nov 2018 at 08:39, Cameron Simpson  wrote:

> On 19Nov2018 23:52, Dimitar Ivanov  wrote:
> >I'm having a hard time getting my head around threads so I was hoping
> >someone who has better understanding of their underlying functionality
> >could lend me a helping hand, in particular how threads work with each
> >other when using thread.join() and Semaphore set with maximum value. I'll
> >try to keep it as clear and concise as possible, but please don't hesitate
> >to ask if anything about my approach is unclear or, frankly, awful.
> >
> >I'm writing a script that performs a couple of I/O operations and CLI
> >commands for each element in a list of IDs. The whole process takes a
> while
> >and may vary based on the ID, hence the threading approach sounded like
> the
> >best fit since next ID can start once space has freed up. I'm parsing an
> >extract of my code below and will explain what I can't properly understand
> >underneath.
>
> I'm just going to scatter a few random remarks about your code in here
> with your listing before addressing your other queries lower down...
>
> >file1.py
> >-
> >ids = []
> >threadsPool = []
> >for id in ids:
>
> The name "id" is unfortunate as it conflicts with the id() builtin
> function. Maybe "element_id"? Wordier, but also more clear.
>
> >  thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id,
> >))
> >  threadsPool.append(thread)
> >for thread in threadsPool:
> >  thread.start()
>
> You could start each thread right after creating it if you wished.
>
> >for thread in threadsPool:
> >  print(thread.enumerate())
>
> "enumerate" is a function from the threading module, not a method of a
> Thread. So try:
>
>   print(threading.enumerate())
>
> Frankly I'm surprised that "thread.enumerate" works at all.
>
> >  print("Queuing thread" + str(thread))
> >  thread.join()
> >
> >file2.py
> >--
> >queue = threading.Semaphore(2)
>
> I'd be disinclined to call this a "queue", which usually implies a FIFO
> list of some variety: put things onto it, and pull things off it,
> usually first in first off. Maybe just "sem" or "thread_capacity" or
> something?
>
> >def runStuff(id):
> >  queue.acquire()
> >  print("Lock acquired for " + str(id))
> >  file3.doMoreStuff()
> >  file4.evenMoreStuff()
> >  queue.release()
> >
> >Onto my confusion - as long as I don't try to print information about the
> >thread that's being queued or the total amount of threads using
> >.enumerate(), the script is working absolutely flawlessly, each thread
> that
> >doesn't have a lock is waiting until it acquires it and then moves on. I
> >decided it'd be nice to be able to provide more information about which
> >thread starts next and how many threads are active right now (each can
> take
> >a different amount of time), however, when I tried to do that, my log was
> >showing me some pretty funky output which at first made me believe I've
> >messed up all my threads, example:
> >
> >
> ><<  2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for
> >ID09 < this is from file2.py
> >-- some time later and other logs in here -
> >[<_MainThread(MainThread, started 140431033562880)>,  >140430614177536)>] < output from thread.enumerate(), file1.py
> ><<  2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread -
> > < output from print() right
> >after thread.enumerate()
> >
> >After some head scratching, I believe I've finally tracked down the reason
> >for my confusion:
> >
> >The .start() loop starts the threads and the first 2 acquire a lock
> >immediately and start running, later on the .join() queue puts the rest in
> >waiting for lock, that's fine, what I didn't realize, of course, is that
> >the .join() loop goes through threads that have already been instantly
> >kicked off by the .start() loop (the first 2 threads since Semaphore
> allows
> >2 locks) and then my print in that loop is telling me that those threads
> >are being queued, except they aren't since they are already running, it's
> >just my text is telling me that, since I wasn't smart enough to realize
> >what's about to happen, as seen below:
> >
> ><<  2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread -
> > <--- makes it clear the thread has
> >already even finished
>
> Yes. The .join() has NO EFFECT on the Thread itself: it doesn't start it
> or stop it. It just waits for the Thread to complete. So yes, your log
> message is misleading.
>
> >Which finally gets me to my cry for hel

[Tutor] Help.Please!

2018-11-24 Thread Brian Ngari
Hi there, I'm struggling mightily on this problem.
  Write a program that determines if you need an oil change. This program
should determine if your car needs an oil change. Assume you change your
oil every 7,500 miles and you want to change the oil if you’re within 500
miles of that number. Please prompt the user for the current mileage
recorded on the car and also for the mileage recorded on the car at the
time of the last oil change. Then, please let the user know if it’s time to
change the oil. Be sure to check for invalid entries (e.g., if the mileage
at the time of the last oil change is greater than the current mileage)
and, if there are invalid entries, please continue to prompt the user.
Here is my code:
 input('Enter the number of miles')
x=int(input('Enter the last milage you went for an oil change.'))

if x>5000:
 print('You need an oil change.')

else:
 x<5000
print('No oil change.')
I'm getting two different answers as outputs. I don't know why.
thanks for your help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: Python Selenium

2018-11-24 Thread stephen.m.smith
I am working with a site that allows you to make reservations for different 
times of the day. The HTML (controlled by someone else) does not include 
specific names for each time – the HTML is very similar. With help from this 
group, I have figured out how to locate an element using an xpath with the time 
spelled out (this way the module can substitute a time based on user input). 
Here is the line of code (for 7:35):

 

   br.find_element_by_xpath("//div[contains(.,'7:35')]")

 

Here is where I need assistance: I need to access an element associated with 
that element to see if it is blank or occupied. I have included the HTML for 
two different time slots. The first (7:30) is not occupied as shown by the 4 
class statements at the bottom with the ><.

 



 

The second, for 7:35 is partially occupied – the names are shown as is the slot 
count information.

 



 

I am trying to figure out a clever way to look down in the HTML once I have 
found the element to see if it is partially occupied or not. Any help with that 
would be appreciated.

 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help.Please!

2018-11-24 Thread Alan Gauld via Tutor
On 24/11/2018 20:37, Brian Ngari wrote:

> miles of that number. Please prompt the user for the current mileage
> recorded on the car and also for the mileage recorded on the car at the
> time of the last oil change. Then, please let the user know if it’s time to
> change the oil. 

You do some of this but you don't check if they need an oil change.

> Be sure to check for invalid entries (e.g., if the mileage...
> and, if there are invalid entries, please continue to prompt the user.

And you don't do this. You need a loop somewhere...

I suggest before worrying about the oil change code that
you get the input bit working, just print the two values
initially. Once you can correctly read two valid values
move on to using them to check the oil.

>  input('Enter the number of miles')

You don't store this value anywhere

> x=int(input('Enter the last milage you went for an oil change.'))
> 
> if x>5000:
>  print('You need an oil change.')

This claims you need an oil change if the mileage at the
last change was >5000.

Even if I haven't driven any miles at all since then.
Something wrong. You need to use both the values that
you read from the user.

> else:
>  x<5000

This doesn't make much sense. You compare x to 5000
but don't do anything with the result.
I'm not even sure what you are trying to do here.

> print('No oil change.')

Remember indentation is all important in Python.
Here you always print the message regar5dless of what
happened further up.

> I'm getting two different answers as outputs. I don't know why.

Probably that last indentation issue to blame for that.
More seriously you are not doing what the homework
assignment asks you to do. Read it again and see
my comments above.

Have another go and if still stuck come back to us.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python 3] Threads status, join() and Semaphore queue

2018-11-24 Thread Cameron Simpson

On 24Nov2018 16:08, Dimitar Ivanov  wrote:
Your explanation definitely clears up quite a bit of my 
misunderstanding,

thank you for that!

There was a reason why I shy away from using Queue, but for the life of me
I can't remember right now what that reason was. I will have to modify my
code using your example and give it another try, I will make sure to let
you know if I run into any issues or additional questions. :)


Questions are welcome.

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor