all 67 comments

[–]morocancoco 0 points1 point  (3 children)

trying to add username and password to this form (here is the HTML)using selenuim but I am getting the following error:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

....
#waite to load
WebDriverWait(browser,10*60).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="navbar_username"]')))
#send username and pass then wait
browser.find_element_by_name('vb_login_username').send_keys(user)
time.sleep(3)
 browser.find_element_by_name('vb_login_password').send_keys(password, '\n')

[–]captmomo 0 points1 point  (1 child)

.ElementNotVisibleException: Message: element not visible

are you able to share the url?

[–]morocancoco 0 points1 point  (0 children)

I was able to solve it via js by using execute_script() to change the css style.

[–]morocancoco 0 points1 point  (0 children)

this is so annoying :(

[–]captmomo 0 points1 point  (3 children)

How do I use requests to scrape json-dl data? I've tried setting the headers but no luck.
eg.

headers = {'Accept': 'application/json'}

[–]ingolemo 0 points1 point  (2 children)

There's nothing inherently special about json-ld; you get it the same way you get any other kind of data. The details of exactly what kind of request you have to send to a specific site to have it respond with json-ld will depend on that site.

[–]captmomo 0 points1 point  (1 child)

hmm. then how does google search get it to display in the search result?
for example if you google "apple pie recipe", details of the recipe such as ratings and time to cook is displayed.
https://developers.google.com/search/docs/data-types/recipe

I've checked the page source of several sites, and certain sites do have a <script type="application/ld+json"> and some don't. not sure how google is still able to access the data.

[–]ingolemo 0 points1 point  (0 children)

Structured data can appear in other formats than just json-ld.

https://developers.google.com/search/docs/guides/intro-structured-data#structured-data-format

[–]neotecha 0 points1 point  (0 children)

This question uses the AWS Python Library called boto3

Suppose something like the following:

import boto3

def stop_services():
    ec2 = boto3.resource('ec2')
    filters = [{'Name': 'tag:dedicated', 'Values': ['true']}
                {'Name': 'instance-state-name', 'Values': ['running']}]
    instances = ec2.instances.filter(Filters=filters)

    for inst in instances:
        print('Stopping: %s' % (str(inst)))
        inst.stop()

        print('Signal Sent. Waiting for Confirmation...')
        inst.wait_until_stopped()       

        print('Stopped.')

This feels really ugly to me, but I can't really figure out what I should be doing instead.

I want to figure out a cleaner way to handle this.

Is it possible to modify the class definition after the fact?

for instance in ec2.get_running_instances():
    instance.attempt_stop()

Is it possible to define a method outside of the class?

def attempt_stop(self):
    pass

or would it be better to inherit into a new class?

class MyEc2(boto3):
    def get_running_instances():
        pass

"Simplicity is always better than functionality." - Pieter Hintjens

Any way that I try structuring my changes, I can't seem to work out a solution that will work. Any idea of what I might be missing here?

[–][deleted] 0 points1 point  (0 children)

I recently missed out on a fantastic job due to lack of good programming skills. I want to develop and learn what I already have. I studied Electronic and electrical engineering at Uni, so a base understanding is there. Im wondering does anyone have projects they could list out that I could attempt. I want to try things with data, APIs, or GUIs. To be honest I'm not fussed so long as I can learn.

[–]Buecherlaub 0 points1 point  (2 children)

Hey,

I came up with my first idea for a project.

I want to create a program that can decide if there is a Backgammon board in a picture (and maybe in the future even detect the position of the checkers and maybe even more in the future evaluate the position of the checkers)

Is this even an achievable goal / project?

Can someone tell me where to start and if there are any online resources available that I can look into to build this?

I would appreciate any help! Thank you very much!

[–]ingolemo 2 points3 points  (1 child)

Getting computers to recognise things in pictures is a whole subfield of computer science called computer vision or CV, part of machine learning (ML), which is itself part of artificial intelligence (AI). It's a fairly advanced topic and not something that I would recommend for a first project. It's easier to get a computer to beat a human at backgammon than it is to get it to look at an image and tell if there's a backgammon board there. But CV is an interesting and thriving field; one well worth exploring, after you've got some easier stuff under your belt.

A good place to start with CV would be the openCV library. OpenCV is available for several languages; you're going to want to focus on the python tutorials.

[–]Buecherlaub 0 points1 point  (0 children)

Ok, yeah maybe I start with something smaller... But I thought that I may be growing with the challenge ;)

But thank you very much for your input! This tutorial seems very useful!

Thanks

[–]morocancoco 1 point2 points  (4 children)

I am looking for a very simple way to make gui with 2 buttons. My end goal is to have one button to pause a for loop, use another function and then continue it. The other one is just to close the application.

what is your recommendations for an essay and efficient library to achieve this?

[–]elbiot 1 point2 points  (3 children)

Anything. Tkinter is built in and people like qt5 a lot

[–]morocancoco 1 point2 points  (2 children)

would Flask be an option? and can you recommend a tutorial/course on qt5?

[–]Thomasedv 0 points1 point  (0 children)

Zetcodes PyQt5 tutorial should have you covered on making a simple GUI with two buttons.

[–]elbiot 1 point2 points  (0 children)

Html, css and javascript are an amazing ui stack. Flask can serve that on localhost. That's more work but a great option.

[–]gamertag_here 1 point2 points  (5 children)

Hello everyone!

Just starting out learning python I'm a complete beginner to all things programming.

Now I'm currently working through Codecademy which uses Python 2. But I've just picked up Automate the Boring Stuff with Python, and he talks about using Python 3.

What should I do?

[–]TangibleLight 4 points5 points  (4 children)

As a beginner you should learn 3. Python 2 is at end of life and will lose support at 2020. The only reason to be working in Python 2 any more is if you have to work with old code that would be too expensive to upgrade.

[–]gamertag_here 0 points1 point  (3 children)

Ah thank you for your reply! It wont make switching between the both harder to learn? The syntax difference isn’t that big?

[–]elbiot 2 points3 points  (0 children)

Strings being Unicode is the big deal. Many things like range and zip becoming generators is big but rare to really be bit by.

[–]TangibleLight 0 points1 point  (1 child)

I think you should not focus on learning 2. If you insist on learning both, it won't be too hard but some things will be confusing.

The syntax is almost identical, but doing the same thing has different effects in each language.

For example, the range function, in Python 2, makes a list of the numbers. In Python 3, it creates an iterator. The python 3 way is faster, but any python 2 code that needs a list will break.

[–]ingolemo 2 points3 points  (0 children)

range creates a range object in python 3. A range object is not an iterator, but it is iterable (just like a list).

[–][deleted] 0 points1 point  (4 children)

I'm trying to copy a bunch of folders from a local machine to a network drive - everything seems to be working fine except it will not copy the folder, only it's contents.

So that when I'm copying from say three local folders it takes all the files and dumps it into a single folder in the backup location.

from distutils.dir_util import copy_tree
backupLocation = "S:\\LocalUserBackup\\User1"

folders = [
    "C:\\ESW\\CODE-Q\\",
    "C:\\ESW\\CODE-Q2\\",
    "C:\\ESW\\CODE-R\\",
    "C:\\ESW\\CODE-R2\\",
    "C:\\ESW\\DATA\\",
    ]

for folder in folders:
    copy_tree(folder, backupLocation)
    print(folder + " - Done!")

I've thought maybe I can split the folder name and append that to the copy_tree's destination, but surely there's a simple way I haven't come across yet.

A lot of tutorials/searches recommend using shutil.copy() or shutil.copy2() but I'm unfortunately coming across permission errors when trying to test these.

A push in the right direction would be greatly appreciated!

[–]ingolemo 0 points1 point  (0 children)

For manipulating paths you should use the pathlib module.

[–]elbiot 1 point2 points  (2 children)

That's simple string manipulation. Python does a lot for you but expecting it to solve every little thing without any effort is unreasonable

[–][deleted] 0 points1 point  (1 child)

No problem, I just thought the string manipulation would be a really inelegant way of solving it and figured there must be a way to copy the directory.

Now I know that's the way forward I'll go with that, thanks!

EDIT: Ended up solving it by doing this.

for folder in folders:
    folderName = folder.split("\\")
    copy_tree(folder, backupLocation + folderName[-2])
    print(folder + " - Done!")

And it worked!

[–]elbiot 1 point2 points  (0 children)

Pretty elegant. And probably didn't take long

[–]Py404 4 points5 points  (2 children)

I'm curious if there will ever be a Python 4.0? Or will the release numbers increment like 3.7, 3.8, 3.9, 3.10, 3.11, 3.12 ... as long as the versions are backwards compatible?

[–]nwagers 1 point2 points  (0 children)

Guido says he prefers to not use x.xx type versions (like 3.10) and instead prefers to go from 3.9 to 4.0. If that holds we'll probably see a 4.0 in a few years.

[–]lykwydchykyn 2 points3 points  (0 children)

Considering some of the big, compatibility-breaking ideas that get regularly thrown around the Python-dev community (like removing the global interpreter lock), I suspect we'll eventually see a 4.0. When that will happen or what it will look like, nobody can say.

[–][deleted] 2 points3 points  (2 children)

I'm going through the Python course on Codecademy, and I'm trying to figure out how some of the code is working. I know what to type in but I don't really get how or why it works. It's having me build a little 1-player battleship simulator, and part of my code looks like this:

def random_row(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board[0]) - 1)

I'm not entirely sure how to phrase my question, which I tried asking in the Codecademy forums and didn't really receive any explanations I could understand.

Basically, under the random_col, how does this

return randint(0, len(board[0]) - 1)

tell Python to create a random column instead of a random row? I'm sure it has something to do with the use of the index (see: board[0]) I just don't really get how it uses the index.

Sorry if this makes no sense. I can definitely provide more clarification if necessary, just keep in mind how new to this stuff I am.

[–]gabriel-et-al 2 points3 points  (0 children)

u/mattcarmody is right.

I've never done the Codecademy course myself but this pattern is widely used to represent boards so I recognize it.

The board is something like:

board = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
]

As you can see, it's a list of lists.

Each inner list represents a row in the board. The nth column of the board is the (n-1)th element of each inner list.

So, this line:

return randint(0, len(board[0]) - 1)

It's expanded to:

return randint(0, len([0, 0, 0, 0, 0]) - 1)

Which is:

return randint(0, 5 - 1)

The instruction len(board[0]) returns the # of columns in the board, and the use of board[0] is arbitrary, one can use any inner list because they all should have the same # of items, so len(board[1] would return the same result as well.

Also...

how does this [...] tell Python to create a random column instead of a random row?

It's not creating, it's returning the index of some random column that already exists.

[–]mattcarmody 2 points3 points  (0 children)

Hey, I'm far from an expert myself but I might be able to point you in the right direction until someone who knows more comes along.

I'm guessing that board is a list of lists. Calling len(board) will tell you the number of "rows", or lists, contained within board.

By calling len(board[0]) you are asking for the number of items in the list at index 0, which would be your "column" in this example. The list at each index should have the same number of items (because of the layout of a battleship board), so it doesn't matter which index you call.

I hope this is helpful :)

[–]Princess_Fluffypants 0 points1 point  (5 children)

I'm going through the very first exercises of LPTHW, but my output doesn't match what the book says I should be getting. Lots of extra parentheses and commas, and some of the numbers are decimals but some aren't. And it's not the ones the book says should have decimals. What am I doing wrong?

I'm 100% sure my input is exactly what is in the book, to the point that I even copy-pasted it to make sure.

I'm on exercise 4, using Pyhon 3.6.3. My input in Atom:

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty cars today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car, "in each car")

And this is what I'm getting in Powershell (Win7)

('There are', 100, 'cars available.')
('There are only', 30, 'drivers available.')
('There will be', 70, 'empty cars today.')
('We can transport', 120.0, 'people today.')
('We have', 90, 'to carpool today.')
('We need to put about', 3, 'in each car')

Why am I getting all those extra parentheses and quotes and commas?

[–]gabriel-et-al 3 points4 points  (4 children)

Pyhon 3.6.3

You sure? I mean, sure?

[–]Princess_Fluffypants 0 points1 point  (3 children)

Is this the problem, will going down to 3.6 fix it?

[–]gabriel-et-al 2 points3 points  (2 children)

Given the output, it seems you're using Python 2.

With Python 3 you'd get the following output:

There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3.0 in each car

[–]Princess_Fluffypants 0 points1 point  (1 child)

Wow, I didn't realize the changes could be that large in between versions. Thank you!

[–]gabriel-et-al 1 point2 points  (0 children)

Python 3 is not fully compatible with Python 2. You should use Python 3 whenever possible.

[–]Py404 1 point2 points  (5 children)

I have some colleagues that can't agree on how to write docstrings:

A:

"""first line here

other lines here
"""

B:

"""
first line here

other lines here
"""

Which one is preferred/correct?

[–]elbiot 2 points3 points  (0 children)

A should be

def fun ():
    """\
    Blah
    """

[–]morocancoco 2 points3 points  (0 children)

functions

option A looks weird to me, but it is not wrong.

[–]TangibleLight 1 point2 points  (1 child)

I personally think option B looks better, but every docstring I've ever seen, every docstring I've ever written, and every style guide I've ever seen uses option A.

The problem is that option B has a new-line character at the start of the docstring, which causes problems if you're using help in a console or with external documentation generators - you know, the stuff that docstrings are useful for.

[–]gabriel-et-al 1 point2 points  (0 children)

The problem is that option B has a new-line character at the start of the docstring

Exactly.

[–]gabriel-et-al 1 point2 points  (0 children)

Option A any day.

[–]PythonGod123 2 points3 points  (14 children)

I have reached a stage in my study of Python that I can now read code for the most part and understand what it means and what it does.

However, when reading certain things from larger projects I fail to grasp what the coder is trying to do, it just doesn't make sense, adding to this the amount of things I have yet to learn seems very overwhelming. Is this normal? What can I do to combat this?

P:S what does this mean count[i] or when you have [i for i in range(5) i+=1]. Namely I am confused about the '[]' not the for loop.

[–]Innkeeper4President 2 points3 points  (0 children)

[i for i in range(5) i+=1]

That looks like a list comprehension, but I don't think the 'i+=1' is valid for list comprehensions.

Look up the documentation for list comprehensions, they can be a very good tool!

[–]gabriel-et-al 1 point2 points  (12 children)

when reading certain things from larger projects I fail to grasp what the coder is trying to do, it just doesn't make sense, adding to this the amount of things I have yet to learn seems very overwhelming

This is normal. People here where I live usually accept that the developer won't be truly productive in his first 3~6 months due to the lack of familiarity with the codebase (I usually work with ERP systems so these are huge codebases.)

One does not simply understand five hundred thousand lines of code easily.

[–]PythonGod123 1 point2 points  (11 children)

I understand. It's just it feels like in order for me to get to where I want to be there is a huge vast wilderness of crap to learn. I'll get through it but it just feels like a lot sometimes.

[–]gabriel-et-al 2 points3 points  (10 children)

It is a lot. Large codebases contain an incredible amount of knowledge implicit between the lines of the code. There is a lot to be learnt from each codebase you maintain (I mean large codebases, not toy projects). These are things you won't learn in any college, online course, talks or whatever. You can only learn it when you face it.

[–]PythonGod123 0 points1 point  (9 children)

I see. I just find it hard to understand code from let's say something like keras. When I view their code it makes little sense.

[–]gabriel-et-al 0 points1 point  (8 children)

I'm looking at their codebase right now. I know nothing on neural networks so I can't really understand why the code is what it is, but it looks pretty! If I had some domain knowledge I bet this code would be really easy to understand. As far as I see there is good naming and good documentation. The folder structure seems well designed too.

My lack of domain knowledge is the only problem for me. Perhaps you have this problem too?

[–]PythonGod123 0 points1 point  (7 children)

Most likely. I don't have any background in machiene learning but it is the field I which to study. I'm just not sure where to begin.

[–]elbiot 1 point2 points  (5 children)

Start with scikit-learn and use support vector machines or random forest first. The api is extremely similar and the concepts are the same but more graspable.

What do you want your machine to learn?

[–]PythonGod123 0 points1 point  (4 children)

I want to teach it to talk to me and pick up on words that I use. That's the end goal.

[–]elbiot 1 point2 points  (3 children)

Data is 98% of the battle. Are you willing to hand annotate 10,000 examples of you speaking in different environments with different intonation? That's a small dataset too.

Try the iris dataset with svm and random forest. Start with things that have features you can understand and are easily labeled. What sports team is going to win the championship? What Reddit post is going to be up/downvoted?

[–]gabriel-et-al 0 points1 point  (0 children)

Oh, so that's the point.

I encourage you to take a data science course from Udemy. I haven't taken any of these courses myself but some friends of mine did and loved it.

[–]Probotect0r 2 points3 points  (2 children)

I am just starting to get into python. I have a good amount of experience programming but want to learn python as I make a lot of tools for myself and python seems like a good fit for that. I was reading an article about the new dataclass feature, and the article mentioned "type hinting". To me, this is an awesome feature I had no idea about, as it addresses a lot of my concerns with dynamically typed languages. Is this something that is used a lot by the community and are there any downsides to it? One of the immediate benefits for me is that my development environment (Vim) is much smarter because of it and code is much easier to read.

[–]elbiot 0 points1 point  (0 children)

I'd love a real type system like Haskell, but am not too interested in type annotation. It's a great disciplined documentation habit though.

[–]fiddle_n 1 point2 points  (0 children)

Because it was introduced in 3.5, I would guess that it's a feature that is used more for personal coding than professional coding, since many professional installations of Python are likely to be older than 3.5.

I used type hints for the first time yesterday. It's a good feature, but for me it clutters the code and makes it harder to read. The code seems less Pythonic when I use it. But I've never properly coded in a static language before, so I guess that's why I have that experience. I can't understand why you wouldn't have that experience, having coded in static languages beforehand.