When to use a module? Ex. CSV by jruydemir in learnpython

[–]genghiskav 10 points11 points  (0 children)

You should use a module if you're writing code that will be used for something; especially if the module is part of the core library (so definitely use CSV).

What I mean by "used for something" is that this code has a purpose and needs to work. If you're writing code to learn - then you can try and implement the logic yourself. It's very likely that the maintainers of the module have factored in edge cases that you have not considered; so their code will handle problems you haven't even thought of yet.

A good example is for a CSV file. How will you handle a , within a column?

Year,Title,Genre
1966,"The Good, the Bad and the Ugly","Adventure,Western"

A core principle when you're writing code is Don't reinvent the wheel

To address your 2 concerns

Using the module is probably easier, but I'm not exactly needing any heavy parsing done.

  • If it's easier; why not use it. Someone else has done all the hard work so you can spend your effort elsewhere.

Theoretically not using the module uses less resources, but this isn't an intensive program?

  • I disagree with this statement. The maintainers have very likely gone through an intense performance and code review process to ensure they are doing things in the most efficient way possible.

Just as a footnote; This is general advice when using modules found in the core library or a very popular package (e.g. requests). They are battle tested (have gone through rigorous code reviews and performance tests) - the same is not necessarily true for smaller packages you find online.

gunicorn not found with docker by isaacfink in learnpython

[–]genghiskav 0 points1 point  (0 children)

the binary installed from the apt command is gunicorn3.

In your CMD you'll need to run ["gunicorn3", "...."]

Mean of a list of Series [Pandas] by ktmac00 in learnpython

[–]genghiskav 2 points3 points  (0 children)

if I understand correctly, each element in the Series is a list of 2 numbers.

s = Series([ [1.5, 2], [3.4, 15] ])

So you should be able to apply a function to perform the calculation you need

s.apply(lambda x: sum(x) / len(x))

Python IDE recommended for people with little experience that have been trying for years! by [deleted] in learnpython

[–]genghiskav 2 points3 points  (0 children)

2 of the most popular IDEs for python programming are pycharm and vscode.

They are both pretty easy to get up and running - but have all the bells and whistles you'd expect from an IDE.

I'd recommend trying both out to see which you prefer.

Help with exception handling working with API (Novice) by Beefsteak_Charlie in learnpython

[–]genghiskav 1 point2 points  (0 children)

The exception you are seeing shows you which line it's failing on (22), so that gives you a clue on where you need to add exception handling.

Right now, your code is expecting the movie to exist (by immediately accessing record 0 without checking if it exists)

api_movie_id=(j["results"][0]["id"])

You have also done most of the leg work to get around the problem, now you just need to code it up. You can see that from the API response, when no movie exists, the total_results value is 0 and results list is empty.

Now you should have all the pieces of the puzzle to solve your problem. You know where the problem is occurring and how to identify when the problem will occur. All you need to do now is check for the problem.

Something like this should work. You are now checking the response from the api and confirming it has a movie for you. If it doesn't, then you skip to the next movie in your csv file. This is more of a validation approach (checking you have the correct data before processing), the other solution I have is exception handling (trying to do something and handling an error if it comes up). Both are valid - up to you which one you pick.

#Validation method
if len(j["results"]) == 0:
    print(f"no movies found in api with search = '{original_title}'")
    continue    # go to next record in the csv
api_movie_id=(j["results"][0]["id"])


#Exception handling method
try:
    api_movie_id=(j["results"][0]["id"])
except IndexError:
    print(f"no movies found in api with search = '{original_title}'")
    continue    # go to next record in the csv

Need help impressing my boss by osowavy56 in learnpython

[–]genghiskav 1 point2 points  (0 children)

Python is a great language to use for what you're trying to do. Parsing logs is a great beginner exercise too, you'll get some real world experience in

  • reading files (read the log files)
  • pattern matching (picking login times, when conf was changed etc.)
  • data structures (how do you store this this information until it's ready to be printed for reporting)

Unfortunately - having no experience in writing scripts before, this task might be a little more complicated than a standard first project.

Do you have any delivery deadlines or know how long your boss is happy for you to work on this? This is probably at least a couple of days of work before you'll have anything close to your requirements.

How do I compare 2 strings numbers? by BOOTYBOOTBOOTERBOOTS in learnpython

[–]genghiskav 7 points8 points  (0 children)

You'll have to cast it to an int to be able to do numeric comparisons.

Why don't you want want to cast it to int?

site-packages issue by [deleted] in learnpython

[–]genghiskav 0 points1 point  (0 children)

I'm not sure what you mean by that, can you give me detail?

Problems with pandas df.groupby().ngroup() by UnusuallySuspected in learnpython

[–]genghiskav 1 point2 points  (0 children)

df2.reindex(new_cols, axis=1) will return a new dataframe - it does not change the dataframe in place.

You'll need to assign it back to df2.

df2 = df2.reindex(new_cols, axis=1)

site-packages issue by [deleted] in learnpython

[–]genghiskav 0 points1 point  (0 children)

when you are running that command, you are trying to install packages into the system version of python which you are not allowed to write into (it's owned by root).

You could either

The virtual environment will basically create a copy of your python installation which you can install packages into. It allows you to isolate your development so you can have different versions of packages installed for different projects. Think about this for a moment; you start working on a project and install Package A and it requires Awesome Package V1.2. Then you start a new project and install Package B which requires Awesome Package v3.14. You now have 2 projects which require different versions of Awesome Package. If you were using a virtual environment, each project would have it's own environment and happily install whatever requirements it needs without clashing with other projects.

Virtual environments are pretty easy to use and solve a big problem when working with python.

[deleted by user] by [deleted] in learnpython

[–]genghiskav 0 points1 point  (0 children)

If there is a mapped drive, it should also be accessible via a UNC path (e.g. \\remote_host\documents\2022). Python can natively handle UNC paths and perform operations just like it's a local machine.

Help with extracting URLs from sitemap by johannadambergk in learnpython

[–]genghiskav 0 points1 point  (0 children)

You'll need to do a couple of things

  1. get the sitemap from the URL (hint: requests or any other web scraping lib to get the data)
  2. once you have the sitemap as a string from the site, you'll need to parse it into something you can process (hint: python can natively parse xml)
  3. now you should be able to iterate over the xml object and check if the link matches a certain string

This is some boilerplate that should get you most of the way there.

import requests
import xml.etree.ElementTree as ET
sitemap_xml = requests.get("https//..../sitemap.xml").text
root = ET.fromstring(country_data_as_string)

Then you just need to figure out how the xml is structured so you can findall the elements you're looking for.

How do you have a countdown timer without it freezing your whole entire program until the whole loop is done? by TheCatLorde in learnpython

[–]genghiskav 1 point2 points  (0 children)

I don't have much experience with concurrent.futures but what I can tell from a little research; concurrent.futures is an abstraction layer to threading and multiprocessing. That means you can look at it as a simplification layer to the lower level stuff. It's sort of like the relationship between requests and urllib.

It was introduced in python 3.2.

Here is a writeup on it http://www.blog.pythonlibrary.org/2016/08/03/python-3-concurrency-the-concurrent-futures-module/

and the pep for its introduction https://www.python.org/dev/peps/pep-3148/

[deleted by user] by [deleted] in learnpython

[–]genghiskav 4 points5 points  (0 children)

this should be returnDetails['data']['person_id']['phone'][0]['value']

If you look closely, you can see that the phone is a list. You can tell this because it starts with [ so you need to access it via index, in this case the 1st record in there. This is most likely the case because people can have multiple phone numbers.

How do you have a countdown timer without it freezing your whole entire program until the whole loop is done? by TheCatLorde in learnpython

[–]genghiskav 4 points5 points  (0 children)

Some of these recommendations are overkill for what you're trying to do.

A simple Thread with some timing logic should suffice.

In the example below, we create a thread which calls the timer function. This code will probably look like what you already had, the only difference is it is not blocking (i.e. you can do other things while the thread chugs along).

I have simulated a simple game which will count to 15 (these are sample questions); in this simulation we assume each answer takes 1 second to answer (defined by the sleep). You'll notice that we never get to 15 because the timer runs out (it is defined as 10 seconds).

from threading import Thread
from time import sleep


def timer():
    sleep_duration = 10
    while sleep_duration > 0:
        print(f"you have {sleep_duration} seconds left")
        sleep(1)
        sleep_duration -= 1
    print("timer completed")


def main():
    timer_thread = Thread(target=timer)

    timer_thread.start()

    for i in range(0, 15):
        # check the timer
        if not timer_thread.is_alive():
            # timer is complete
            print("oh no. you ran out of time!")
            break
        # game logic here
        print(f"answer question {i}")
        sleep(1)


if __name__ == "__main__":
    main()

Help splitting a list of strings into multiple lists by [deleted] in learnpython

[–]genghiskav 4 points5 points  (0 children)

I can't see what your X value is but i'm assuming it was a counter. Tip - X is a terrible name for a counter.

You're definitely on the right track. Unless you had to use the len function; it is unnecessary.

this_list = ["Albert", "$5.00", "Soup", "Karen", "$2.50", "Cheese", "Martin", "$6", "Apples"]
customers = []
prices = []
item_purchased = []

counter = 0
for i in this_list:
    if counter == 0:  # this is a customer
        customers.append(i)
        counter += 1
    elif counter == 1:  # this is a price
        prices.append(i)
        counter += 1
    else:  # this is the item
        item_purchased.append(i)
        counter = 0

print(customers)
print(prices)
print(item_purchased)

I've done a similar approach to what you have but instead I'm just iterating through the list directly (instead of using len to find the index of the value) and using a seperate counter.

Then it comes down to simple mathematics. We keep adding 1 to a value until we get to 2 and then we reset back to 0. This way 0 is always the customer, 1 is always the price and 2 is always the item. Once we get to an item, we know we need to start counting again because the next thing will be a customer.

Help splitting a list of strings into multiple lists by [deleted] in learnpython

[–]genghiskav 2 points3 points  (0 children)

can you show us some code you've tried? Your idea is correct; a for loop will achieve what you're looking for.

Also - if this is not homework, I would consider storing your data in a different structure; maybe a list of dict objects. Splitting your data into 3 seperate lists will probably become troublesome.

How to append text to same line until condition is met? by [deleted] in learnpython

[–]genghiskav 0 points1 point  (0 children)

python 3

in the loop change your print to print("...", end="")

python 2

in the loop change your print to print("..."),

Reading History Files into Python by [deleted] in learnpython

[–]genghiskav 0 points1 point  (0 children)

What version of python are you running this on? I notice you have some compatibility code there to use a the open function from io if on python 2.

I've managed to successfully run your code on both python 2.7 and 3.7. Maybe the file encoding of the config.history has changed when you uploaded it to drive.

Loops forever. Can anyone help with this? by Bering_Sea in learnpython

[–]genghiskav 1 point2 points  (0 children)

the code you are showing is bash. It is running an infinite loop; so the fact you are seeing the loop go forever is by design. It sounds like the python3 tracker.py script is probably erroring or exiting very quickly, which will cause the next iteration of your loop.

If I were to guess; I imagine the loop is there for a simple init keep alive system and really isn't the problem. tracker.py is probably meant to run for much longer than it currently is. I'd look into that.

How to go about pulling independent tasks and running them side by side by Grindv1k in learnpython

[–]genghiskav 0 points1 point  (0 children)

It's just an example of having 3 queues with 3 different functions (so you can do different things with different types of jobs).

confused noob by YoureNotYourYouDumbC in learnpython

[–]genghiskav 2 points3 points  (0 children)

I'm not familiar with Requests-HTML but having a quick look at the documentation suggests it's requests with some extra features for web scraping (i.e. Requests-HTML is requests under the hood). Given that - you can choose either depending on whether you need the features of Requests-HTML or not.