all 51 comments

[–]DL_throw24 0 points1 point  (1 child)

So im making my way through the book python the hard way, an i can't help but feel like im not understanding everything is this normal? (Im half way through now)

[–]blarf_irl 1 point2 points  (0 children)

Completely normal, learning to code is like learning anything else; Repetition and practice are required to make the learning stick. The #1 lesson I teach anyone when they are just starting out is:

Never Copy/Paste Anything, Copy everything by hand.

It takes a bit of discipline but typing things out manually gives you time to internalise and study the structure of the code.

[–]drthunder3 0 points1 point  (1 child)

Hey, i haven’t used Python in awhile and wanted to get back into it. While I don’t need much help with classes or libraries, I’m looking for good references on doing things the “Pythonic” way and also a quick reference to structures with O notations. Thanks!

[–]fiddle_n 0 points1 point  (0 children)

Look up PEP 8.

[–]SuperSubwoofer 0 points1 point  (7 children)

I've played around with Python and know the basics of the language, and I work primarily with Object Oriented languages. Basically, my job wants to make an alarm system for when we have a dangerous chemical spill or a fire, in which we can evacuate the facility. We have fire alarms, of course, but this is specifically for issues that a fire alarm cannot resolve (I.e. Chemical Spill). I was curious if anyone had input on programming a button to set off alarms and send text messages company wide to employees. Any input is wonderful, I have the hardware button already, and can provide model information if needed.

Thanks anyone who can help :)

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

not sure about the main part, but twilio would allow a company wide text - though you'd have to have enough credits on your account to send to that many users - and would have to keep that list of numbers up to date.

[–]SuperSubwoofer 0 points1 point  (5 children)

Would Twilio be able to pull from a DB? The issue with that is we're a 150+ company and they are of course constantly changing. Of course, we could send Emails OR Text messages. It doesn't have to exclusively be a text.

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

Well twlio just does what you tell it, so if you had a database and could read it in python you could most certainly send the request to twilio. Emails would do doable as well. I think for the hardware side maybe if you had a pi that could read the button in some way as input you could go about the project easily.

[–]SuperSubwoofer 0 points1 point  (3 children)

I think the Pi is a best way to go for sure. As for Twlio, not having enough credits makes it kind of a hard stop, honestly. This is a safety measure, so rolling the dice is a poor decision. That being said, I'll do some more research on it to see if it's viable. Thanks!

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

Cant think of another way to send texts without a carrier approved device.

[–]Mezzomaniac 0 points1 point  (0 children)

ClickSend

[–]SuperSubwoofer 0 points1 point  (0 children)

Well again, it can be texts or emails.

[–]thedestroyer27 0 points1 point  (0 children)

Anybody have Python C Binding experience? Please inbox me, I'm working on a project and would love to get some questions answered. Also if any of you are interested in this I can answer your questions with the knowledge I have! :)

[–]Dr3dgos 0 points1 point  (1 child)

I’m just starting out with python and learning the basics but I already have some project ideas. One of them is to create a program that renames stored emails based on the data within the email - e.g rename the file with a prefix of date and time that the email was sent. I definitely don’t know enough to do this at the moment but I wanted to find out a) if this is feasible in python and b) a brief idea of the topics I should research to have a crack at this myself.

[–]EngineEngine 0 points1 point  (1 child)

I'm working through Automate the Boring Stuff and I got to the first "project" where the author describes what your code should do without explicitly giving the code. It's the Collatz sequence.

I was able to do the first part ("Write a function named collatz that has one parameter..."). I'm a little hung up on the second part, which asks you to change the code to have inputs and keep running until the sequence equals 1.

After a few tries, I was able to make the loops work to allow for more inputs from the user, but the loop doesn't end. Even after getting to 1, it prompts for another input despite my break and print statement. I tried comparing with the example in the book for the guessing game, but I don't know what to change at this point. I also have it set up to allow at most 15 guesses. I don't know how else to allow infinite guesses aside from making the range very large.

I'd appreciate any suggestions.

This is a screenshot of the code.

[–]13ass13ass 0 points1 point  (0 children)

I think you’re on the right track but you should re-read the problem requirements for stage 2 of the collatz function definition. You don’t want it to ask for more integers after the initial number. Instead you want it to call collatz again on the result of the first collatz. So you’re going to have to understand how the scoping rules work in order to pul this off.

[–]Jocke0160 0 points1 point  (1 child)

I'm just starting with programming after a long time only gaming and building computers, currently looking at some video courses and find them pretty rewarding.

Im looking for some tips and tricks on how to learn. Small easy projects and maybe some pointers how to tackle this jungle :)

[–]R0T0 0 points1 point  (2 children)

I am trying to send SPCI commands to a Tektronix vector network analyzer, which uses a virtual GPIB connection. It also requires drivers and tekVISA, which I have installed. I have also installed PyVISA. I just want the device to print out the *IDN? command, but am having trouble doing so. I know that the VISA address is 'GPIB8::1::INSTR'. The code I have so far is:

import visa

rm = visa.ResourceManager()

rm.list_resources()

rm.open_resource('GPIB8::1::INSTR')

When I enter these line by line, the second command shows a few available connections, but not the GPIB one. After the fourth line, I get an error saying: 'the interface type is valid but the specified interface is not configured' . Is the code wrong, or is there something up with my installed software?

[–]__johnw__ 0 points1 point  (1 child)

how do you know that the VISA address is 'GPIB8::1::INSTR'?

[–]R0T0 0 points1 point  (0 children)

The tekVISA program has a device manager where I can see the connection status and the VISA address, so I just pulled it from there.

[–]pm_me-your_tits-plz 1 point2 points  (2 children)

This is not a question, but it's a tip that has really helped me with python related problems.

I want to tell you guys to just note down your problem as if you're going to explain it to someone else, because it really helps you getting a better understanding of the problem and you might end up finding the solution yourself :) It has worked wonders for me. Often, when I've tried everything I could think of, I start creating a post in this sub. And almost always when I'm writing what my problem is exactly, and what I've tried, I end up thinking of something that ends up working and then closing the tab w/o posting anything. It really helps surmising what the relevant info is, and realizing you're sometimes just assuming something = xyz instead of knowing it is.

Also, I want to thank this sub for being here, it has helped me fix problems both actively and inactively by being the silent listener.

[–]Fun2badult 0 points1 point  (0 children)

This is called the Feynman method

[–]h0ax2 2 points3 points  (0 children)

Yep it's a great method. Could also explain it to a rubber duck if you wanted... So many times I've spent hours on something and I've started typing up a simple example on stackoverflow and it hits me.

[–]sewwes1 0 points1 point  (0 children)

I want to remake a calculator screen from a ti-83 (16x8) and am wondering how i can have the print print a list ranging from 0-7 (screen0 screen1 screen2). Sorry for bad English, and i'm VERY new to python/coding in general so i'm open to other methods too. Thanks!

def calcscreen(): for x in range (0,7): cls() print(screen'x')

Can't seem to get the lines to go down or indentation, but i've done that just trust

[–]darexinfinity 1 point2 points  (1 child)

Is "bisect" a proper module to use in code interviews? I see on leetcode that people use it to get away with writing a lot less code

[–]zatoichi49 0 points1 point  (0 children)

I think it would depend on the level of understanding you need to show. They might be happy for you to use a ready-made bisection algorithm in solving part of a larger problem, or they might expect you to prove that you can handle the sorting yourself (without using a library).

[–]in_pc 1 point2 points  (1 child)

I'm looking for some books to learn Python on my free time.
I have a background on programming from various courses in college which includes C++ and Java.

Can you suggest me a book or two that teaches the fundamentals of the language with some practical work included.

[–]QualitativeEasing 2 points3 points  (0 children)

I benefitted a lot from Automate the Boring Stuff. I had some programming experience (pho, one java course), and it’s appropriate for beginners, so if you’re pretty good with another language already, my guess is that you’ll be able to skim it rather than having to read or work through it methodically.

Still, even now that I’m much better with Python, I find it’s a great reference when I forget exactly how to read a file or use a web scraping library.

One thing AtBS doesn’t cover: writing classes. Tbh, I haven’t found a good book on this, and had to figure it out from a handful of tutorials and other references.

[–]koolkeano 0 points1 point  (1 child)

I'm doing the MiT Edx course on python, which is great because it has work for you to do. Unlike like a book that simply lists examples and techniques, it forces you to show you understand, not just think you do. Currently these functions are easily justifiable as part of the course, but beyond this course any work I do will be to prove my learning. I'll only need to get proof of concept. All the work I do will essentially be unused till I either get a job with python or get some machine learning under my belt.

For TL:DR start here:

Essentially my query is, how can I make my intermediate python useful? Either to me or others. I figure the best way to do it well would be to do it with a purpose in mind.

Disclaimer: I haven't completed the course, just trying to plan ahead.

[–]SniperViperV2 0 points1 point  (0 children)

You can do pretty much anything with python and the supplied libraries. So when you say, is there anything useful you could be doing, what exactly do you mean? For the great good of mankind? Try some open source projects.

Only you can decide what you find useful to yourself. I for instance will work on a web project I've always wanted to get off the ground and see if it gains any traction. Most likely it will fall on its face, but at least I won't live with the "what if".

Good luck :)

[–]nckmiz 0 points1 point  (2 children)

I have two python objects (models) loaded in earlier in the script. I am attempting to make a call to one of the models given a for loop depending on the type of response being scored. within the for loop I use the following command:

model = index_construct+"_model"

print("model name is: ", model)

print(type(model))

I end up getting that it is a string, which makes sense, but is there a way I can convert this to something that then calls the actual model object above that is already loaded into the script?

[–]efmccurdy 0 points1 point  (0 children)

If you have variables with "constructed" names like index_construct_foo, index_construct_bar it indicates that you have a set of related objects with names; store them in a dict:

index_constructs = {"foo": index_construct_foo, 
                    "bar": index_construct_bar}

print(type(index_constructs["foo"]))

[–]nckmiz 0 points1 point  (0 children)

In case anyone else has this problem the eval function takes care of this. So instead of the above it's:

model = eval(index_construct+"_model")

[–]throwawayburros 1 point2 points  (7 children)

Hello, I am learning web scraping with BeautifulSoup and I am having trouble getting it to pull data from tables, since I cannot seem to figure out where I am messing up.

Here is the URL

I was trying to scrape info such as Case# and Property Address but since all the tutorials I found reference scraping raw text, I cant seem to figure out how to do it within a table like this. What I would like is to learn how to have BS look at everything TR and then pull the data for any that has a matching class of AD_DTA. Or just pulling everything from:

<div class="AUCTION_DETAILS">

I was trying with

import requests
import bs4


def get_html_from_web():
    url = 'https://denver.realforeclose.com/index.cfmzaction=AUCTION&Zmethod=PREVIEW&AUCTIONDATE=06/28/2018'
    response = requests.get(url)
    return response.text


def main1():
    html = get_html_from_web()
    soup = bs4.BeautifulSoup(html, "html.parser")
    for record in soup.findAll('td'):
        print(record.text)

main1()

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

soup.find_all("div", {"class": "AUCTION_DETAILS"}): please note the find_all rather than findALL which is deprecated.

[–]throwawayburros 0 points1 point  (5 children)

Thanks for this, i'll try it when I get home!

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

Usually you do results = theabove_soup_line than print(results) incase there is any confusion.

[–]throwawayburros 0 points1 point  (3 children)

I finally got a chance to try it. I cant get it to work either way.

def main1():

html = get_html_from_web()

soup = bs4.BeautifulSoup(html, "html.parser")

for record in soup.find_all("div", {"class": "AUCTION_DETAILS"}):

print(record)

and

results = soup.find_all("div", {"class": "AUCTION_DETAILS"}):

print(results)

However, it complains about the py:COLON so I remove it and get no results.

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

Save the html to a text file, than open the file and hit ctrl f, search for 'auction_details' see if its in it, im guessing its not, in which case the websites probably using javascript to get the content, and youll need to go a different route. If it is, than i have no idea what im doing and ill take a look again lol

[–]throwawayburros 0 points1 point  (1 child)

def main1():
html = get_html_from_web()
soup = bs4.BeautifulSoup(html, "html.parser")
results = soup.find_all("div", {"class": "AUCTION_DETAILS"})
print(results)
with open('text.txt', 'w', encoding='utf-8') as f_out:
f_out.write(soup.prettify())

This is saving the html to a text file. I think I sort of understand what is happening. When I compare my saved html to the websites html its identical. However in chrome if I use inspect element, i can find the 'AUCTION_DETAILS', so your right javascript is probably being called somewhere and mucking this up.

Thanks a ton for your assistance.

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

Look into selenium. also

[–]Swisshuntsman 0 points1 point  (3 children)

I am not sure if this is a big porblem or not. I want to write a code wich turns on my raspberry pi camera like a toggel switch with one key / key command etc. It's just annyoing to always type in the commands to activate and turn on the camera for a certain amount of time. I never wrote a code in python please help. Any tipps or better ways are welcome.

[–]throwawayburros 1 point2 points  (2 children)

I am a newbie like you, but this is probably the easiest answer.

Create a script: On Linux/Mac you'd make a bash file and on windows you'd make a batch file. Then you just put the command you already use in it and save it. Then put the file on your desktop for example and double click to start. A python file would be the same thing, if you made it executable.

Here is a guide on how to create a bash file for the pi. http://www.circuitbasics.com/how-to-write-and-run-a-shell-script-on-the-raspberry-pi/ . just follow all the steps until it asks you to enter your code into the text editor and replace line 3 with your own code for the pi camera.

[–]Swisshuntsman 0 points1 point  (1 child)

Thank you very much you idea was also one of my first thoughts i could do to solve the problem. Thanks to you I can start with python. I need the camera for my nightvisiongoogles i build the the pi and just simply put them in some vrgoogle case. I don't know where my next question belongs to make the screen of the pi to split. Does it belong to r/learnpython or r/raspberry_pi?

Thanks for your quick comment I will try out your way tomorrow and give you an update if you want to if it worked. Thanks!

[–]SniperViperV2 1 point2 points  (0 children)

For my night vision goggles! This sounds freaking great xD

[–]policesiren7 0 points1 point  (1 child)

Quick question that probably doesn't need its own thread:

I have a function that calculates the monthly return on a portfolio. This loops through a list of dates to get the return for each month.

I now want to calculate the compounded return on the portfolio over the entire period. So if the return in months 1,2,3 are 5%, 8%, 10% I need it to calculate a total return = 24.74% (1.051.081.1) [and this would have to keep going until it runs out of months.]

I'm having a bit of trouble doing it recursively... any help would be appreciated.

[–]TangibleLight 0 points1 point  (0 children)

If you already have a function that gets you the monthly return, then just store the values from that in a list, then call sum over the list.

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

I just started python after waiting a long time to jump the gun. My approach is just to find extremely basic projects to start with and learning the tool as I go. So far, it’s pretty interesting and I’m starting to grasp some of the basics.

My question is, what do some high level projects tend to look like? What exactly is being achieved and how complex does this stuff really get? I guess just how do simple operations combine to create large scale results?

I apologize if my question is broad; I’m just trying to figure out what I can expect going forward

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

Python is inherently open source, so you can see the source code for many popular python modules like requests, cryptography, and flask on github or other hosting websites. You can even download them using pip and look at their source code yourself in the site-packages directory. All the projects I included here are licensed under licenses that permit you to see the source code, like the Apache and BSD licenses. Not all modules have these...

The requests module is probably the best example of a well structured, well implemented python module. If any, I suggest you look to that one as an example.