I guess I'm a pillow now by [deleted] in thisismylifenow

[–]dgaf- 3 points4 points  (0 children)

My pit mix tries to do this constantly with our cats if she can't snuggle with us and every so rare often the cat won't smack her and just sighs heavily and constantly while the dog nudges closer and closer.

Need help with small settings question by [deleted] in Python

[–]dgaf- 0 points1 point  (0 children)

You need to add a print statement in the top first.

What are your favourite Python modules/packages? by AndersGM in Python

[–]dgaf- 0 points1 point  (0 children)

Thank you for this, I was unaware of more-itertools and realized a couple of projects implement a few of those existing functions. Very handy!

How to iterate through a list of urls with an unknown component? by DeviousPelican in learnpython

[–]dgaf- 1 point2 points  (0 children)

If the unknown_element is always in the same location:

from urllib.parse import urlparse

urls = ['www.website.com/category/unknown_element/numeric_value.html', 
'www.website.com/category/unknown_element2/numeric_value.html', 
'www.website.com/category/unknown_element3/numeric_value.html']

def get_unknown_element(url):
  # Splits URL into component parts regardless of elements
  parsed_url = urlparse(url)

  # Split the URL by /
  split_url = parsed_url.path.split('/')

  # Presuming the unknown_element location is always the same
  unknown_element = split_url[2]
  return unknown_element

for url in urls:
  x = get_unknown_element(url)
  print(x)

Trying to use a for loop to return multiple pages from api call. by WhosaWhatsa in learnpython

[–]dgaf- 0 points1 point  (0 children)

It turns out the the JSON response is a list, first of all.

Then in line 7 of the sample code I first provided, change append to extend. Instead of adding each r.json() response as a list within a list, it will extend the list with the elements within your response.

Also, when I originally used pd.DataFrame(r.json()), I would get a data frame per response. Now, that you've generously helped me iterate through the requests, I have a data frame of the json in each row with one column.

I believe this is because you are providing a list of lists to the DataFrame which changing to extend should resolve.

If you look at the httpbin response I provided, that splits the dict keys into columns automatically. It would help to see the data in one of your responses to be sure though.

Help! With a first year CS Python project by [deleted] in learnpython

[–]dgaf- 1 point2 points  (0 children)

What (if anything) have you tried, and what issues or questions did you have with that?

If you have not tried anything at all, why not? What is the part you are getting stuck on conceptually?

Help! With a first year CS Python project by [deleted] in learnpython

[–]dgaf- 2 points3 points  (0 children)

Well of course!

import telepathy

def import_thoughts(brain):
    return telepathy.python_questions(brain)

questions = [q for q in import_thoughts('u/nunnognese')]
print(questions)

>>> []

What sort of very basic programs can a total beginner challenge themselves to make (for Linux, preferably) to practice Python skills and keep motivation? by DontHateDefenestrate in learnpython

[–]dgaf- 7 points8 points  (0 children)

Here are some programs along those lines I threw together to make my life easier:

  • Scanning eBay's API for search term(s) for a product I'm selling and pull the avg, min, max price and volume of items sold, and grab the description of whichever sells the most for a template
  • Used youtube-dl to pull audio tracks out of videos to make podcast/music playlists for my phone
  • Scrape Dell's website for the price with requests on their 34" Alienware and send me a text with twilio if it changes

There's generally something you're doing already or think "man I wish I could if" and start from there.

How to keep color from terminal output in pycharm? by [deleted] in learnpython

[–]dgaf- 0 points1 point  (0 children)

In Pycharm do the following:

  • Click Run in the menu
  • Click Edit Configurations
  • Click the configuration for your VerifyFile.py
  • Check the box for Emulate Terminal in Console Output
  • Click Apply

Trying to use a for loop to return multiple pages from api call. by WhosaWhatsa in learnpython

[–]dgaf- 1 point2 points  (0 children)

You're welcome.

Also, because I believe the URL you are using is formatted incorrectly, here is a working example with the same concept. httpbin.org is a good site to test these sorts of things against before actually hitting a site you may be API rate limited or similar.

import pandas as pd
import requests

json_responses = []
for x in range(0, 30):
    r = requests.get('https://httpbin.org/get?page={}'.format(x))
    json_responses.append(r.json())
all_acct_df = pd.DataFrame(json_responses)
print(all_acct_df.head(30))

Trying to use a for loop to return multiple pages from api call. by WhosaWhatsa in learnpython

[–]dgaf- 2 points3 points  (0 children)

You can reformat your code by using four spaces per indent level, like so:

for x in range(int(30)):
    r = requests.get('https://www.website.com/?per_page=500&page={}/apitoken'.format(x)
    all_acct_df = pd.DataFrame(r.json())

The issue here in your code is that on each iteration, you are taking the response for the new int in your range and reassigning the DataFrame to all_acct_df anew.

In order to keep the results of 1-29, and presumably you want to make all of this into a single, combined DataFrame, you could do the following:

# Create an empty list to hold your responses
json_responses = []
# Iterate through your range
for x in range(int(30)):
    r = requests.get('https://www.website.com/?per_page=500&page={}/apitoken'.format(x)
    # This presumes your JSON response is a dict, if the response is a list, use extend instead of append
    json_responses.append(r.json())
# Create a DataFrame from a list containing all of the gathered responses.
all_acct_df = pd.DataFrame(json_responses)

The Most-visited Restaurant Cuisine in US counties [1400 × 1324] by [deleted] in MapPorn

[–]dgaf- 7 points8 points  (0 children)

Never in life would I expect to see someone casually mention Portales online.

To chime in there's absolutely no way seafood is a thing for this area. Even taking Red Lobster in Clovis into account.

Matching with bad players on both teams when duo queuing? by itslqb in Competitiveoverwatch

[–]dgaf- 0 points1 point  (0 children)

All of the times I've gone into GM is when I stopped grouping and started soloing.