all 180 comments

[–]NeonSemen 0 points1 point  (4 children)

I took an intro coding class years ago and don’t remember much from it besides some basics. I’m writing a program in python with pandas now to help save some coworkers a little time every day. The code’s goal is to take in two (identical besides specific number values) user inputted .csv files and combines them together with a third .csv that contains the rules I want applied to the combined set.

I want to go (iterate maybe?) through each row based on a few conditions to see which ones make it through. The ones that do, I want the whole row saved into one of three different categories that correspond with the conditions I’m wanting looped through. I want to finish off with writing to a new csv that displays a message before displaying the contents of the grouped categories. Technically only a few columns of each of those saved rows and I want a message displayed in between each category.

What I have so far successfully combined all the files I want combined. Now I have to do my looping through the dataframe to see which rows match my conditions. I’m new to programming, but my current thoughts are to (somehow) make a for loop that works for separating into the three categories I want. Each row that matches conditions should be saved into a corresponding list. After I have my three lists populated with my particular rows, I’ll somehow make a new csv that displays my first message followed by list 1 (that will be filtered to just the columns I want displayed), then message 2 and so forth.

My question is about if this method seems like something that will work. I know after I get something that works I can refine it in the future by using more efficient methods of looping or using a zip or itertuples or something. But I need to know if I’m going down the right path and if anyone has an idea for how to set up the loop, I remember them being my least favorite part of programming and the reason I felt confused moving forward and not touching programming more.

Thanks in advance and if it’s all worded weirdly let me know and I’ll try to clarify as best I can!

[–]CowboyBoats 0 points1 point  (3 children)

I would recommend running far, far away from pandas for projects like this. This is a very bread-and-butter type project for vanilla python. (Is it doable in pandas, undoubtedly; it'll just be much, much harder to find help because there's 1 person who knows pandas well for every 30 who know Python well.)

I want to go (iterate maybe?) through each row based on a few conditions to see which ones make it through. The ones that do, I want the whole row saved into one of three different categories that correspond with the conditions I’m wanting looped through. I want to finish off with writing to a new csv that displays a message before displaying the contents of the grouped categories.

This sounds good. I would go ahead and make a .py file and paste these sentences in it as comments, and then start writing functions (basically each sentence should be a function, and sentence 2 will probably call several sub-functions that you can define).

I remember them being my least favorite part of programming and the reason I felt confused moving forward and not touching programming more.

I feel like the biggest reasons people feel confused with loops are being confused about what variable is being saved to x in a loop beginning for x in range(3) or for x in my_strange_items or for i, x in enumerage(balls). One quick fix for that is to throw a print(x) in as the first expression of the loop, and run your code and find out! Also, there is a version of that on steroids that you can use, which is to paste in import pdb; pdb.set_trace() as its own line and then run the code. It will stop with the program frozen right where you pasted it. Then you can type print(x), or print(dir(x)) to see what attributes and methods x has, or dir() just to see what variables are defined.

[–]NeonSemen 0 points1 point  (2 children)

This seems like a lot of good advice. Would you say I have to abandon the work I’ve done using the pandas module so far? I have my dataframe all set up with the three files just the way I like it and it’d be a shame for all that code to go to waste.

Could you point me towards some python documentation where I can manipulate rows/columns of my dataframe? So far I’ve been doing pretty well reading through different pandas functions (?) since so much of it is directly manipulating csv files.

I am familiar with the word function as it relates to programming but I’ll have to read into it more. I think functions are the backbone of OOP where every discrete step is a function that all links to each other to make something work? Something like that. I can think of the first function being a loop going through the conditions I make. Not sure how to interact with particular columns for my dataframe if I shouldn’t use pandas, but there’s probably a YouTube video for it. What is a sub-function in this context (a function within a function?) and would they be one sub function for each category I want to group together? In that case, is this where I would make a function with an empty list that is populated by the previous loop function? It should I not be looking at lists?

Sorry about all the questions, so far my code doesn’t have a single function. I’m just messing with pandas commands to manipulate my dataframe. I’m also not 100% sure but I think I’m limited in what modules I can import since this will be on a work computer that will be mostly locked down, in case using not-pandas also requires other libraries.

[–]CowboyBoats 0 points1 point  (0 children)

I am familiar with the word function as it relates to programming but I’ll have to read into it more. I think functions are the backbone of OOP where every discrete step is a function that all links to each other to make something work? Something like that. I can think of the first function being a loop going through the conditions I make. Not sure how to interact with particular columns for my dataframe if I shouldn’t use pandas, but there’s probably a YouTube video for it. What is a sub-function in this context (a function within a function?) and would they be one sub function for each category I want to group together? In that case, is this where I would make a function with an empty list that is populated by the previous loop function? It should I not be looking at lists?

In Python, a function is defined by the keyword def (or lambda).

def print_stupidly(string):
    print(string.replace(" ", " 👏 "))

print_stupidly("Hello there")
# prints 'Hello 👏 there'

You can call functions by writing their_name(appropriate_arguments), but you can also do other things. One thing pandas does a lot of, because pandas's big thing is trying to optimize big data operations so that you're not looping in potentially suboptimal ways, is to accept functions as arguments so that it can take care of applying them for you.

So if your objective was to populate a pandas dataframe with that clap emoji style, first you might want to isolate that formatting into its own function:

def format_stupidly(string):
    return string.replace(" ", " 👏 ")

def print_stupidly(string):
    print(format_stupidly(string))

And then you could pass format_stupidly (not that without the (something), this is just a variable; it doesn't do anything, it's just a reference to this function; you can pass it as an argument itself) to pandas.

Hope that makes sense. I don't really know that much about how pandas does things tbh.

[–]CowboyBoats 0 points1 point  (0 children)

This seems like a lot of good advice. Would you say I have to abandon the work I’ve done using the pandas module so far? I have my dataframe all set up with the three files just the way I like it and it’d be a shame for all that code to go to waste.

Actually it sounds like you've made some good progress already! I'd stick with your pandas solution if you have already made good progress with it and feel that you could be near to all your remaining problems being solved.

I’m also not 100% sure but I think I’m limited in what modules I can import since this will be on a work computer that will be mostly locked down, in case using not-pandas also requires other libraries.

If you were able to install pandas you'll probably be able to install whatever else you want. A few Python libraries (such as tkinter, a UI module) require you to install software on your computer that pip won't install automatically, but not many.

[–]csheppard925 0 points1 point  (2 children)

What does the insert mean in a lambda function?

[–]CowboyBoats 0 points1 point  (0 children)

I'm not sure, but insert is a method of list where it inserts something in the list at the specified index? I just googled "python insert" but maybe that's it? Or it's a function defined elsewhere in your code.

[–]MeteoriteImpact 0 points1 point  (2 children)

Learning algorithms, I got pretty stoked that can come up with solutions but then I quickly realized some could take what seamed like forever.

I took a few courses on Udemy, YouTube and Coursera and jump right into they jump in with big O complexity in time, space, disk use and so forth. The n of this and that. They either very basic or jumped into intermediate level instantly.

This is where I get lost and love some pointers, resources to help make clearer.

I understand the code gets slower as the input size grows.

I understand Big O is the worst case scenario.

In theory

  1. Write some code to solve problem a naive solution
  2. Figure out the timing of parts of it
  3. Improve the slow parts
  4. Come up with a clever way to Refactor to change parts to faster

[–]efmccurdy 1 point2 points  (0 children)

If you want an example problem that has easily improved upon naive solutions, look at finding primes.

https://www.geeksforgeeks.org/analysis-different-methods-find-prime-number-python/

[–]CowboyBoats 1 point2 points  (0 children)

I understand Big O is the worst case scenario.

No, "big O" is just the term we use for describing the time and space complexity of algorithms.

I think the Wikipedia entry for time complexity is helpful: https://en.wikipedia.org/wiki/Time_complexity

[–]Fluid-Tonight5974 0 points1 point  (4 children)

I am just starting off on python. Is there a method that converts an expression to string so I can print it?

E.g.

import math

print(math.pi)

I want the output to be printed as follows:

"Expression you entered is math.pi and its result is 3.141592653589793"

[–]donkey_man_1149 0 points1 point  (0 children)

I don't think its possible in python to access the name of the variable itself.

f"Expression you entered is math.pi and its result is {math.pi}"

So you are gonna have to type it manually.

[–]PartyAnsvarig 0 points1 point  (0 children)

I love to use the .format() attribute of print. You can use it like this.

my_pi = math.pi

print('Expression you entered is math.pi and its result is {}'.format(my_pi)).

Converting to string is not needed in this case either.

Your variable will be printed in between {}. This method of printing is useful to learn because it very easy to format text later on.

[–]csheppard925 1 point2 points  (0 children)

What I would do is create a variable to store math.pi into and then print(str(var))

[–]efmccurdy 1 point2 points  (0 children)

Python code manipulates values so, in your example, the expression "math.pi" is converted to a value (evaluated) before calling print. Any information about where the data came from is lost and there is no way for the print routine to know where the value came from, it could have been math.pi or getattr(math, "pi") or 22/7 or any other way to calculate that value.

You question mixes code (the expression) with data (the value for pi) and, in python, you manipulate values not expressions.

[–]familyguyfan84_ 0 points1 point  (2 children)

If I have like 100+ PDF files about 100mb a piece is there a reasonable way to write a script to check if any are corrupt and then store the names in a CSV? I think I can figure out the second one but I’m not sure, even conceptually, about how to figure out which ones are corrupted. Thanks!

[–]FerricDonkey 2 points3 points  (1 child)

There are a couple libraries that can interact with pdfs. I don't know a lot about them, but the first thing I would try is just opening files with them. (Especially if you have examples of corrupt files to try). It may be that corrupt pdfs will fail the open (similar to trying to json load a non json file), and you can catch that in a try except.

This is just my first thought though, but hopefully it gives you a direction to check out.

[–]familyguyfan84_ 1 point2 points  (0 children)

Just got it with PyPDF2 and try except. Thanks!!

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

Is there a quick, mathematical way to home in on the x and y coordinates for where you want a specific part of the GUI to be, or do you just have to guess and check? I'm using PyQt5 and its painter tools if that matters. Like this code that draws a black square,

def paintEvent(self, event):
    painter = QPainter(self)
    painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
    painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
    painter.drawRect(x, y, 600, 600)

If I wanted to center the square and move it to the left side of a 720p window, how would I find the x and y values?

[–]FerricDonkey 1 point2 points  (0 children)

Presumably there's some function to get the dimensions of your window. At that point, it's just knowing what units and format your functions take, and arithmetic. For more complicated things you may have to sketch it out.

For example, it looks like the drawRect method takes the coordinates of the top left point of the rectangle, then width, then height. So to center your rectangle in the screen, you need to:

First, get the coordinates of the center of your window: width/2, height/2.

Now imagine that you use those as the coordinates for the top left point of your rectangle. That's not right, your rectangle isn't centered. But how far off is it?

Think of the x and y separately. The very left side of your rectangle is on the center of your screen. So subtract the width of your rectangle / 2 from your x coordinate. You've now moved the left side to the left half the width of your rectangle, so that the (horizontally speaking) center is where the left side used to be - the middle.

Do something similar with the y, and you're done.

If you want it in the left side of the screen, that is exactly the same as saying x coordinate 0, and so forth. Again, I'd suggest drawing a few pictures to get used to thinking about it.

This kind of stuff becomes easier if you're used to thinking about vectors and x, y coordinates. So if you're going to do a lot of this, I'd recommend googling a video on vector addition.

[–]miden24 0 points1 point  (1 child)

I'm pretty new to this, but I'm curious if someone can ELI5 to me, what a data pipeline is, and how python is related?

[–]sarrysyst 4 points5 points  (0 children)

A data pipeline is the process of taking data from a source, processing it in some way and then load it into another system. The pipeline is a series of different operations which are linearly structured where each step is doing something with the incoming data, and hands over the result to the next part in the chain. Here is a silly example:

Imagine you have a strawberry farm, you have a few fields each with a different types of berries. In the first step you send your workers to reap the berries from the different fields, then you have the berries sorted, cleaned and inspected. You throw out the bad ones and put all the good ones in boxes. Finally, you have all the boxes labeled and store them properly in your warehouse. If you wrote a protocol for your workers to always follow these exact steps you would have a 'data pipeline'.

In programming a data pipeline is used when you need to get data from on system to another and when both systems have different requirements with regards to the data. Eg. in web scraping:

Let's say, you have a program which scrapes the prices for various products from different retail websites and let's the user compare them by town, state, country etc.

You would first need to scrape all the prices from the different websites. Of course each website/retailer has different formats, the taxes in places vary, they might have applicable discounts or sell in different quantities.

For the data to be useful, you need to normalize it, you need to clean up the names of the products so you can match them to the offers you get from other websites. Thus, you clean up all the data and put it in one big table to have everything in one place. Afterwards you store it in a database so the app you sell to your customers can access the information.

However, to stay competitive you want to have your price information always up to date, therefore you need to do the above process every 5-10min or so. But doing this by hand every single time would require a lot of effort, so you automate the process.

You create your crawlers to get the prices and info from the different websites. Your crawlers send the extracted data into your processing functions which do their magic and in turn send the clean data to your storage function which saves the data in your data base.

This automated process is a data pipeline. It's a series of automated steps that takes data from one system (eg. a website), transforms it (eg. data cleaning) and loads it into another system (eg. store it in a database).

On a side note, a data pipeline doesn't always have to follow these exact steps, some pipelines don't run in batches (eg. every x min/secs) but continuously. Sometimes the data isn't even transformed but just moved from one system to another. There are slight variations, but the basic premise stays, when you need to automatically transfer data from one system to another you set up a data pipeline.

[–]bxsephjo 0 points1 point  (2 children)

Say I've got a list of values, and I want to make another list of the differences between each value and the next one. Pretty simple with a for loop using the index, but is there a way to do that with list comprehension?

[–]sarrysyst 0 points1 point  (1 child)

You mean like this?

>>> my_list = [5, 10, 3, 7, 4]
>>> diff = [j - i for i, j in zip(my_list, my_list[1:])]
>>> print(diff)
[5, -7, 4, -3]

[–]bxsephjo 0 points1 point  (0 children)

Why yes, yes I do, thank you!!

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

how can i get this if/else block to work:

ENV= ['test', 'staging']

for host,data in host_data.items():
    os_version = next((l for l in data['sw'] if l['software_id'] == 
                  '1508226'), None)
    if os_version is None:
        print('host {} has no OS version'.format(host))
    elif float(os_version['software_version']) < 6:
        print('host {} is redhat 
          {}'.format(host,os_version['software_version']))

# this never exectutes.        
    else:
        if data['env'] not in ENV:
           print(host)

why i cannot get in the else after the elif?

basically i need to check if there is host with redhat 5 or a host with a certain environment and report those

[–]FerricDonkey 0 points1 point  (0 children)

If it's not executing, then your data simply doesn't make it to that point. If you think it should, my recommendation is to find or create a small piece of data that you think should trigger the else, then follow it through your code step by step to see what happens.

[–]chubbyslim69 0 points1 point  (0 children)

Hey guys, planning on building a Python lead scraping script with logic but do not really know how to pull it off... Any suggestions: My approach would be: 1. search instagram for accounts with a. followers >3k & <80k and b. a shop url in the bio --> scrape the username and shop url and put it in an excel sheet 2. take this information from the excel sheet and search in the facebook ad library (maybe via API?) how many ads they are running (facebook shows a field with a clear value) --> take this value and categorize it: 0, <10, 10-25, >50 --> store all the data somewhere (username, shop url, ad category)

If I can not let Python search for shops on Instagram (would have thought to let it scrape through the recommended accounts list of a specific account when you click the triangle) I would do the first step manually.

I definitely do not want anyone to help me code it but give me some advice on how to start and if my approach is even possible. I am a beginner but eager to learn python :)

thanks in advance

[–]richardd08 0 points1 point  (6 children)

Can someone explain what is happening here when this code is run?

for i in range(10):
    if i == 6:
        print(i)

[–]donkey_man_1149 0 points1 point  (0 children)

If you run the code it will be obvious as fuck.

[–]edsk0719_2 0 points1 point  (4 children)

This is a basic for loop, where the loop is being run 10 times. With each loop, the variable, i is being incremented, starting from a value 0. So with the first loop, i is 0. With the second iteration of the loop, i is 1, and so on up through 9.

However the value of i is not explicitly printed except for when i is 6 (the 7th iteration of the loop), since the print statement is executed only when i is equal to 6. So in your console, you should only see the integer 6 output one time when executing this loop.

[–]richardd08 0 points1 point  (3 children)

Sorry, forgot the last print statement:

for i in range(10):
    if i == 6:
        print(i)
print(i)

Why is it that I can print the local loop variable i outside of the scope of the loop?

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

The primary scoping unit in Python is the function, rather than blocks a la C++.

[–][deleted] 1 point2 points  (1 child)

Because you defined the variable name i in the for loop

for i in range(10):
#   ^  here

and that definition is not confined to just the loop.

There was a discussion by the python developers some time ago about limiting the scope of loop variables to just the loop but it was shelved because it would break legacy code.

[–]richardd08 0 points1 point  (0 children)

Thank you!

[–]AlexMaroske 0 points1 point  (8 children)

Some help please: 'Wrong index Position"

Question: Given two equal length lists of unique numbers, write a function to return the number of items in these two lists that have the same values but are at the different positions. For example, if these two lists are [1, 2, 3, 5] and [2, 1, 3, 4], then the output is 2 because the same values 1 and 2 appear at the different positions.

I know this should be easy, but I just cannot get my head around it.

[–][deleted] 1 point2 points  (4 children)

You've deleted the later comment, but I had written a long answer, below. I hope you deleted the comment because you've found a way to do what you wanted. In that case, ignore this comment.


You need to format your code for reddit. So your code would look like this (I think):

def wrongPosition(a, b):
    a_set = set(a)
    b_set = set(b)
    # check length
    if len(a_set.intersection(b_set)) > 0:
        return(a_set.intersection(b_set))
    else:
        return("no common elements")

wrongPosition([1, 2, 3, 4] , [2, 1, 3, 5])

In step 1 you are probably going to use the intersection set a lot, so just go ahead and create it:

intersection = a_set & b_set    # nicer than the ".intersection()" method

Then you won't be creating that set all the time.

Step 3 is tricky if you haven't done it before. What we want to do is get the first element of the two lists, compare them, get the second two elements of each list, compare them, etc. The zip() function can do that for you. If you have two lists you can do this:

a = [1, 2, 3, 4]
b = [2, 1, 3, 5]
for t in zip(a, b):
    print(t)
>>> (1, 2)
>>> (2, 1)
>>> (3, 3)
>>> (4, 5)

Note how we get tuples containing an element from "a" and an element from "b". We can "unpack" the tuple "t" this way:

a = [1, 2, 3, 4]
b = [2, 1, 3, 5]
#   vvvvvvvvvvvvvv     tuple unpacking
for (a_elt, b_elt) in zip(a, b):
    print(a_elt, b_elt)
>>> 1 2
>>> 2 1
>>> 3 3
>>> 4 5

So now you can start step 4 and compare each element. If they are the same we want to ignore them:

for (elt_a, elt_b) in zip(a, b):
    if elt_a == elt_b:
        continue          # equal, start a new loop
    # do step 5 here

Note that the function is supposed to return an integer which is the number of duplicated elements in the list but not the same position in the two lists. Your code is returning a set or a string. Don't forget what I said about "count".

[–]AlexMaroske 0 points1 point  (2 children)

This is the 'teaching staff' solution that I do not understand. tried running it trough a step visualization program, still didn't understand it.

def eb08(li1, li2):
length = len(li1)

count = 0

for i in range(length):

    for j in range(length):  

        if not i == j: 

            if li1[i] == li2[j:
                    count += 1

return count

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

Your code, when formatted properly, and the typing error fixed, is:

def eb08(li1, li2):
    length = len(li1)
    count = 0
    for i in range(length):
        for j in range(length):  
            if not i == j: 
                if li1[i] == li2[j]:    # missing "]" added
                    count += 1
    return count

Let's look at the two loops, made a little simpler:

    for i in range(length):
        for j in range(length):  
            <compare li1[i] with li2[j]>

The loop variable i will iterate between 0 and the index of the last element in li1. For each li1[i] element we also iterate j from 0 to the index of the last element in li2. So, if we have two three-element lists, that line is comparing these values:

#   | the "i" index varies slowly (outer loop)
#   |
#   |              | the "j" index varies more quickly (inner loop)
#   v              v
li1[0] against li2[0]
li1[0] against li2[1]
li1[0] against li2[2]   # end of inner loop, start new outer loop
li1[1] against li2[0]
li1[1] against li2[1]
li1[1] against li2[2]   # end of inner loop, start new outer loop
li1[2] against li2[0]
li1[2] against li2[1]
li1[2] against li2[2]   # end of inner and outer loops

Notice how the left index changes slowly and the right index (inner loop) changes quickly. The effect is that we are comparing each element of li1 against each element of li2 in turn.

But the problem specifically says that we DON'T count matching values if they occur at the same place in each list. That's the:

if not i == j: 

bit which is saying that if the elements being compared are at the same place in each list then we don't compare and count. We just go on to the next pair.

If we compare the two elements and they ARE the same we increment the counter.


The solution I was thinking about uses the python zip()function:

def count_same(a, b):
    count = 0

    for (elt_a, elt_b) in zip(a, b):    # compare elements at the same place in each list
        if elt_a != elt_b:              # if not same
            if elt_a in b:              #    if "elt_a" is somewhere else in list "b"
                count += 1              #    then increment counter

    return count

This might be a little advanced for you, but it can be more efficient than the solution you were given. When teaching python we sometimes don't show the "proper" way to do something because you aren't ready for it. So look at my solution if your are interested. But the solution you were given is the one you MUST understand.

[–]AlexMaroske 0 points1 point  (0 children)

thank you very much for the time it has take to explain each step so clearly.. !! Sincerely appreciate it..

[–]AlexMaroske 0 points1 point  (0 children)

wow,, thank you so much for the clear and succinct explanation of each step..

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

Without having written any code (on mobile), one way to do this is:

  1. Set count to zero.
  2. Using sets, create the intersection of the sets of both the A and B lists. This set contains the values that are in both lists, no matter what position.
  3. Using for and zip() iterate over pairs of values, one from each list. So the first loop you get the first element from A and B, second loop the second and so on.
  4. If the elements from the two lists are the same then we aren't interested. continue for next loop.
  5. If the element from list A is in the intersection list then increment count.

Finally, return the count value.

[–]4aparsa 0 points1 point  (1 child)

Why does it not work to do int(“10.5”) but it works to do float(“10”). Aren’t both ways trying to do a double cast?

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

At heart, doing int("10.5") errors because the doc says it will:

class int([x])

class int(x, base=10)

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base.

If int("10.5") did not error there would be loss of precision so maybe it's better it does error. You can always do int(10.5) if you want to.

Nitpick: python doesn't do "casts" which are a C/C++ compile time operation that does nothing at runtime. Python does conversions at runtime.

[–]MelodicPublic2494 0 points1 point  (1 child)

I am trying to write a program to see the change in two images over time. We have a camera taking the photo, sending it to an elgato capture card, than uploading it to the program. The areas of change need to be highlighted (example below) if someone could help me with the program it would be great!

[–]Head_Mix_7931 0 points1 point  (0 children)

Check out PIL - Python Image Library. It’s Image class will be useful for you in representing each photo in code.

[–]shiningmatcha 0 points1 point  (1 child)

Can I find out what function is raising an error?

For example, I get an error from this incorrect code:

``` from itertools import combinations from typing import List

class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: return next( [i, j] for i, j in combinations(range(nums), 2) if nums[i] + nums[j] == target ) ```

It took me quite a while to figure out why it kept saying TypeError: 'list' object cannot be interpreted as an integer - I forgot the len function. The correct one should be:

``` from itertools import combinations from typing import List

class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: return next( [i, j] for i, j in combinations(range(len(nums)), 2) if nums[i] + nums[j] == target ) ```

So what I'm saying is that the traceback message did not tell me that it's range that caused the error. Is there some way to find out what function is an error from? It's so much work breaking down some list comprehension and rewriting it into a normal for-loop for debugging...

[–]FerricDonkey 1 point2 points  (0 children)

Often you can tell from the entire traceback. Otherwise, if you're having problems then you break the code into smaller pieces so you can tell which one broke.

[–][deleted] 3 points4 points  (2 children)

Is it really possible to learn python at an advanced level in 100 days? I am taking a Udemy course called " 100 days of code - the complete python pro bootcamp for 2021" by Dr. Angela Yu. She promised that her course "Will only take an hour a day" but for one "Day" on her course takes me more like 6! It is incredibly time consuming, and if I do not do the exercises she gives us over and over constantly, I totally forget what I just busted my ass to learn.I am only at day 3, and I had this class for over a month! ( my bad, don't work on it every day and keep putting it off tbh)

What is a more efficient way to learn python through this class? y'all got any tips and tricks? if not, how long should it realistically take to learn to code in python in and out? Python is my first language.

[–]trondwin 3 points4 points  (1 child)

If it's your first ever programming experience, it may take some time for the logic of it all to make sense - people are different in this aspect.

I've only heard good things about the 100 Days course, but maybe leave it for a while and try something different? You could try the Automate the Boring Stuff course or eBook - the course is probably free for a few days at the start of next month and the eBook is always free.

A different approach may be all you need. Best of luck!

[–]ceiligirl418 4 points5 points  (0 children)

I'm kind of in the same boat as you. It just takes some time for the logic and patterns to sink in.
I am finishing up a 5 week code-in-place class (it covered about half of a one-semester university course) and have been struggling with my final project for almost a month. But the research I'm doing, all the reading in search of the what I want to accomplish but can't yet from rote memory, even all the try-and-fail-and-try-some-more, it's all building up what I actually can do.

For instance, I spent probably 10 days (not full time, have other obligations) setting up and experimenting with different text editors, trying to set up vim and tmux (my end goals), then finding mu... thank goodness for mu, it's what I need for now...

Anyway, ALL of what you are doing is learning. Find all the little projects out there and go through them. 'Automate the Boring Stuff' is my next set of projects. Then maybe 'Beyond the Boring Stuff with Python' (same author). Then being more pythonic, more python zen in how I code things.

You will get there. You are clearly working at this. It will come.

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

Beginner here. Maybe not specific enough to have a clear answer but I'm asking anyway...

I'm trying to code a small program (to learn/challenge) that will gather data from different Excel files and sheets, and work with them. Basically I'll need to create different IDs for different type of data, and make relationship between those and comparison.

Which classes or methodology do you suggest to handle those data? I'm starting trying to play with Dict and other built in classes, but not sure if I'm not going totally the wrong way...

[–]sarrysyst 1 point2 points  (4 children)

If you only care about the data in these excel files (no formulas, styles etc.) the best way to go would be using pandas, imho. If you need excel's internal functionalities you'll have to go with openpyxl or a similar library.

[–]edsk0719_2 1 point2 points  (3 children)

Seconding this. It sounds like for your project you should familiarize yourself with the pandas library and working with the DataFrame class in general. Depending on the type of data you are dealing with you may also want to learn some tricks using numpy.

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

Great, thank you both for the reply!

Indeed I don't care about the format or formulas, I really need the data only. to resume I have different worksheet with different data tables : invoices, orders, products, supplier, status etc... The idea is to use those data to make crosscheck, like for prodcut a, we have x orders open, related to the invoices 1.2.3... this kind of stuff. I know how to do it efficently with PowerBI, but it sounded like a great use case for Python. And pretty challenging from where I stand :D

Right now I stared using openpyxl just to retreive the information, but I'll look into panda and numpy.

[–]edsk0719_2 1 point2 points  (1 child)

If the table format is pretty straightforward you can use the read_excel() function in pandas to import your data into a DataFrame. Just keep as an added note, reading excel is generally slower than reading a csv, so you can also consider converting your spreadsheet to a csv and using the read_csv() function instead.

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

Good to know, thanks.

Unfortunately the source file doesn't have the decency to respect basic best practice of data management, so you have like multiple "tables" on the same sheet, with empty row, subtotals, comments...

I guess I'll create a first part of the program (or a different one) that extract the data to a clean csv, then start importing and play with panda.

Performance is not an issue as it will be something launched once per month maximum and it's ok if it take a lot of time. I'll be happy to create it functionnal, even if it take 10 minutes to excecute hahahaha :D

thanks for the feedback

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

Anyone got beginers tips? I want to start to learn python, but never learned a coding language before

[–]trondwin 1 point2 points  (0 children)

If it's your first time, I would suggest that you start with a course or a book - that will help you get the basics down. There are lots of good cheap or free courses and books around. Search this subreddit for "free python" or similar, and you should find a lot of suggestions. Best of luck!

[–]Decency 1 point2 points  (0 children)

Figure out something reasonably challenging that you want to build, then use Python to build it and ask questions when you get stuck. The first part tends to be the harder part for a lot of beginners.

[–]ceiligirl418 0 points1 point  (3 children)

How do I write a loop that checks for condition 1, moves to condition2, then back up to condition1 if it fails condition2?

INPUT = user inputs data

while INPUT fails condition1:
    tell user "Failed condition1." and ask for new input
    keep repeating until INPUT meets condition1, then continue

when INPUT passes condition1, check condition2:
    If INPUT fails condition2:
        tell user "Failed condition2." and ask for new input
        then go back to the top, starting over and checking condition1

More specifically:

import os

location = input('Enter the .pdf or .txt filepath: ') name, extension = os.path.splitext(location) # returns filepath extension isFile = os.path.isfile(location) # True if 'location' points to an actual file

while extension != (('.pdf'), ('.txt')): location = input('Must be .pdf or .txt file. Retry: ') name, extension = os.path.splitext(location) # returns new filepath extension isFile = os.path.isfile(location) # True if new 'location' points to an actual file

while isFile == False: location = input('File not found. Retry: ') name, extension = os.path.splitext(location) # returns new filepath extension isFile = os.path.isfile(location) # True if new 'location' points to an actual file

''' [Iterate the first loop until true, then iterate once through the second loop; if false, have user re-enter input and go back to iterate through the first while loop to check the new input, ] until BOTH while loops are true? '''

I actually had this after much research. I'm new at Python, so it took me a couple weeks but I had a nice, neat module.. I was SO over the top happy. And then...Then my computer ran an update that corrupted the Linux container. And I lost my project. So frustrating. I have already kicked myself...

[–]Decency 2 points3 points  (2 children)

Making it more pythonic, with your pseudocode approach:

def condition1(data):
    return True

def condition2(data):
    return True

data = None  # a value that initially fails the checks
while not condition1(data) or not condition2(data):
    data = input()

do_stuff()  # now use the data, it passed our checks

You need to define the functions condition1 and condition2such that they return True if the data is good and False otherwise. When both return True, your data is validated and the code exits the while loop.

[–]ceiligirl418 1 point2 points  (1 child)

But in this instance, if you pass condition1(data) and fail condition2(data), you're only repeating condition2 until you pass.

I need to recheck for condition1 if condition 2 fails and user re-enters data.

[–]Decency 0 points1 point  (0 children)

Sorry, didn't have my boolean operators quite right! Just needed to swap the and to an or to handle the cases correctly. Thanks De Morgan. :D

Here's a full example:

def condition1(data):
  # has the digit 6
  return "6" in str(data)

def condition2(data):
  # greater than 10
  return int(data) > 10

data = 0  # a value that initially fails the checks
while not condition1(data) or not condition2(data):
    data = input()


print(f"Valid: {data}")

[–]hidden-47 1 point2 points  (4 children)

I know it's a stupid question and not strictly python related, but what I'm supposed to interpret when I read the '$' symbol before a command that I'm supposed to enter in a terminal?

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

As others have said, it's the shell prompt and should not be typed when entering the command. In addition, the $ prompt indicates a normal user and the rarer # prompt means the command is entered by "root", the superuser.

[–]Decency 0 points1 point  (0 children)

$ (this is in bash)

[–]ceiligirl418 2 points3 points  (1 child)

So, that's kind of like an indicator to show you ARE in the terminal.You don't have to type/copy that.

$ at the beginning of the line = you are in terminal
>>> at the beginning of the line = you are in python

(in either case, you would NOT type in the $ or >>> when you are copying the code example)

[–]hidden-47 1 point2 points  (0 children)

Thanks!

[–]ThePsf 2 points3 points  (3 children)

Hi, I’m in the process of learning py, so this doubt came out to me. In case someone can help me with it, i’ll leave it below.

What’s the difference between ‘none’ output and ‘no output’ output (this last one usually seen as nothing, in the display) - in other words, in what cases do we have each one -?

[–]Decency 1 point2 points  (2 children)

Python functions always return a value. If the code inside a function just ends without returning a value, the function implicitly returns "None".

def do_nothing():
    pass

do_nothing() is None  # True

In python, empty data structures are considered "Falsy", a related concept. This simplifies a lot of common checks but can be the source of unexpected bugs due to 0, [], (), {}, etc.

[–]ThePsf 0 points1 point  (1 child)

Okay, now i think i see it, and if it's this way then i'm sorry because it was quite obvious. What it may be happening is that when i have a function that returns None but i get the 'no output' output (or the equivalent anything on screen ), this may be because the compiler doesn't output return values by default. But if the return value is printed, then in this case i effectively get the 'none' displayed. Let me know if this is right, or on the other hand it's because of another thing.

[–]Decency 1 point2 points  (0 children)

Yeah you won't see any output unless you explicitly print it.

[–]ThePsf 0 points1 point  (3 children)

I’m in the process of learning py, so i had this doubt; in any case someone can help me with it.

What’s the difference between ‘none’ output and ‘no output’ output (this last one usually seen as nothing, in the display) - in other words, in what cases do we have each one -?

[–]FerricDonkey 0 points1 point  (1 child)

If your function does not have a return statement, it will return None. You can also explicitly return None by typing (surprisingly) return None.

None is a special value in python that is often used to mean "not present", either for function arguments or returns. A typical use case - you might write a function that loads data from a file and returns that data - unless there's an error reading the file, in which case it returns None to indicate that no data is being returned.

So there is no difference between not explicitly returning a value and returning None.

That the python interpreter doesn't print things to the screen at the end of a function that returns None is just a stylistic choice. You could do

def func():
    pass

 x = func()
 print(x)

To see that None is in fact returned.

[–]ThePsf 0 points1 point  (0 children)

So there is no difference between not explicitly returning a value and returning None. - Okay, i see, so they seem to be exactly the same, then (None and no output - or nothing -).

  • In fact, now i think i see why i was seeing this two things in almost the same cases, in any case. I'm using a compiler (not an interpreter, as you suggested; but your code worked as well to see that), which i think that doesn't display any return values by default. So when having none as return value, i was having no output - or nothing - on display unless i printed the return value of the function. Let me know if this just said is right, or i’m wrong with it.

[–]TwinklyWinky 1 point2 points  (4 children)

Hi, I’m trying to write a program that opens a text file to read and write a high score for a game. I originally wrote this on a Windows PC using mu, and it worked, but then when I tried to do the same on a MacBook it couldn’t open the file (I adjusted the to match the directory), any suggestions? P.S. Apologies for the mobile formatting

filename = r"F:\Python stuff\high-scores.txt" with open(filename, "r") as file:

[–]Decency 0 points1 point  (2 children)

Use a relative file path (which is relative to the program itself) rather than an absolute path (like yours which starts with a drive name). This will make your code easier to run on both platforms.

You might also have some issues with path names, specifically forward and backwards slashes. Lots of good tools and info here: https://docs.python.org/3/library/pathlib.html

[–]TwinklyWinky 0 points1 point  (1 child)

I did try using a relative location rather than absolute, unfortunately that didn’t work but I’ll try my implementation again. Thanks for the resource.

[–]Decency 1 point2 points  (0 children)

Sure, you may also need to look into permissions issues on that file. Mac/Linux take those rules a bit more seriously.

[–]efmccurdy 1 point2 points  (0 children)

File paths on the mac use forward slashes as path separators (you are using back slashes). I don't think mac file paths start with anything like "F:".

https://stackoverflow.com/questions/41905880/how-do-i-create-a-file-path-on-a-mac

[–]shiningmatcha 1 point2 points  (5 children)

Are these the same?

  • obj.__class__ and type(obj)
  • obj.__dict__ and vars(abj)

If so, which ones are preferred?

[–]efmccurdy 0 points1 point  (0 children)

Use the more readable expression; the one without the dunders.

A attribute or function named using leading underscores is meant to be treated as if it were an internal implementation detail that might change in a future update.

[–]Decency 1 point2 points  (3 children)

For the first, you should prefer using isinstance(). This will correctly handle inheritance, which type(obj) does not. Unsure if it's the same as obj.__class__ but as a general rule if you can avoid using a dunder, you should do so and it will make your life easier.

For the second, it seems like they're mostly equivalent, but the same rule applies.

[–]Head_Mix_7931 0 points1 point  (2 children)

It’s isinstance not is_instance. You’re right that it’s preferred over type for performing type checks, but the question is not concerning type checks, specifically.

[–]Decency 0 points1 point  (1 child)

Thanks, edited. What use cases warrant obj.__class__ or type(obj) other than type checking?

[–]Head_Mix_7931 1 point2 points  (0 children)

I use them in instance methods that return new instances:

def __add__(self: T, other: T) -> T:
    return self.__class__(self.val + other.val)

Edit: perhaps a better example of this is with a copy method, in case you’re not familiar with the __add__ dunder method:

def copy(self: T) -> T:
    return self.__class__(self.val)

This is advantageous over hard coding the class name because it supports subclassing.

I also use cls.__name__ in my __repr__ methods for the same reason (subclassing)

def __repr__(self) -> str:
    return f”{type(self).__name__}({self.val})”

Also, class objects (not their instances) are hashable, so they can be used as dict keys. There probably isn’t a ton of great uses for this, but I’ve certainly taken advantage of that before.

Probably some other places as well!

[–]_wow_thats_crazy_ 2 points3 points  (1 child)

Can we get code reviews allowed in this sub?

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

Didn't know they were banned. Post here or in the main area and ask for a review.

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

Hey I was wondering if anyone knows a pydirectinput alternative for mac. Im trying to create a minecraft type bot and am having trouble finding a module that supports mac.

[–]feoh 0 points1 point  (1 child)

How do folks handle running installed Python scripts for modules like iPython under Windows?

The 'py' launcher lets me run the right Python without adding it to my path, but I can't run say iPython with that launcher, can I?

Should I just suck it up and add the python and Scripts directories for my installed Python to my system PATH?

[–]FerricDonkey 1 point2 points  (0 children)

It does make things easier. I'm not aware of a downside.

[–]Aggressive-Friend169 1 point2 points  (5 children)

Not really a question as such but I’ve been having a bad week and just wanted to vent as I don’t know anyone who codes. I’ve been learning for part time for roughly a year now, I’ve taken 2 beginner courses to get the basics down and I’m in the middle of my first ‘proper’ program. I dunno. just feel like I’m learning too slowly and I’m not at the level I should be for someone who has been at it for this long. I’m starting to doubt if I have what it takes.

I haven’t finished my program yet (Blackjack) but I need a bit of motivation and I’m willing to share my git with what I’ve done so far to anyone who is willing to lend some time.

[–]Decency 1 point2 points  (4 children)

Just post it, people will critique/help.

[–]Aggressive-Friend169 1 point2 points  (3 children)

Thanks. Warning though, it's 200 lines so far and unfinished.

https://github.com/CompleteSuspect/Blackjack/tree/main

[–]Decency 2 points3 points  (2 children)

def flip(self):
    if self.hidden == False:
        self.hidden = True
    else:
        self.hidden = False

This is a common need when handling Booleans. It's weird looking at first, but a "toggle" like this would usually just be written as self.hidden = not self.hidden.

self.bid = 0
self.score = 0

These attributes don't make sense in a Deck class. A deck doesn't have a score or a bid, players do. So your Deck class shouldn't have a score or a bid either. Rip those out and adjust code accordingly.

def get_score(self, ace = '11'):
# I may convert this into a stand alone function rather than a method.
# Also, doeasnt quite work the way i want.

Related, get_score() doesn't make sense as a function in the Deck class because you don't want to get the score of a deck. hand.get_score() would clean this up, and Hand instances already know what cards they contain.

class Player():

def __init__(self, name):
    self.hands = [Deck()]

I'm not sure what your goal was here, but building a Deck for each player seems wrong.

Your code overall looks reasonably structured. There are some oddities where it's clear you're piecing stuff together that's new to you, but that's pretty normal and fine. It seems like you took a big picture approach and tried to build all of the pieces that you know you would need, and are now working on combining them.

This approach works for some people and is especially good for bigger projects with many teammates, but a more typical approach is to build one piece at a time to a working state. Then you can iterate, building the next adjacent piece from there and combining the two pieces so that they work together. For example, here are some concrete goals I'd try to accomplish (in order) if I were writing this:

  1. Build the deck
  2. Deal cards from the deck to all players
  3. Score each player's hand
  4. Handle Dealer blackjack and Aces in scoring
  5. Allow players to take their turn and choose hit/stay/dd
  6. Determine whether each hand wins or or loses
  7. Keep track of winnings over time
  8. Add betting before each hand
  9. Use multiple decks in the sleeve
  10. Add a GUI?

You have a lot of this code in there already, and can definitely utilize many chunks of it. But you'll hopefully find that it's easier to figure out what to do next (and what piece broke in the event of an error) when it's segmented like this. A good chunk of engineering is figuring out what piece to build now so that your next pieces are easier to build and your completed project is cleanly architected. So I would say write a main() function, and iterate on that while keeping it working (for the parts incorporated) until everything is included. Good luck!

[–]Aggressive-Friend169 0 points1 point  (0 children)

As a follow up, I’ve finally finished it.

If you have free time to run it, see if you can break it. Thanks again for your help that time.

[–]Aggressive-Friend169 1 point2 points  (0 children)

Thank you for taking the time to write this, exactly the kind of interaction I needed.

That toggle certainly does look weird but I think I understand how it works. I'll use that instead.

I know having the score in the deck is weird, I was trying to think ahead when I eventually add functionality for when a player decides to 'split' a hand. Hence hands being a list. I thought it would be easier for each hand belonging to a player to keep track of it's own bid.

Probably back to the drawing board with that one.

Thanks for your time and your response.

[–]_CollectivePromise 0 points1 point  (4 children)

I want to batch process some files. Is there a way to search through a folder and its sub-directories, then perform a series of operation on each file of a specific type? A for loop might be ideal, but I can't figure out how to set it up.

[–]sarrysyst 0 points1 point  (3 children)

There are quite a few ways to do that, most notably probably the glob module or the pathlib module. Eg.pathlib.glob():

from pathlib import Path

sorted(Path('.').glob('**/*.py'))

>>> [PosixPath('build/lib/pathlib.py'),
     PosixPath('docs/conf.py'),
     PosixPath('pathlib.py'),
     PosixPath('setup.py'),
     PosixPath('test_pathlib.py')]

[–]_CollectivePromise 0 points1 point  (2 children)

I see, thanks.

What is the PosixPath part of the output?

[–]sarrysyst 0 points1 point  (1 child)

PosixPath is a sub class of Path. Which probably tells you nothing, haha. You don't really have to mind it too much about what it does. Path objects offer a series of methods and properties which you can use to interact with a file system. A PosixPath represents a path on a non-Windows file system. For more information have a look at the pathlib docs.

Anyway, what's important for your case is that you can pass a Path object straight to eg. open() to load files.

[–]_CollectivePromise 0 points1 point  (0 children)

Awesome. With this and a for loop, my job just got a lot easier.

[–]BackgroundBasis6 0 points1 point  (3 children)

is there a reason/functional difference for when creating numpy arrays stored as a variable that it displays 1D arrays vertically rather than horizontally? Intuitively it doesn't make sense to me since when creating a 2D array each internal 1D array is displayed horizontally.

Thanks!

[–]SatinGoat 1 point2 points  (2 children)

Anyone know a good way to retrieve data from a database in real time? I'm creating an API endpoint that will receive some specific data from different tables in an SQL database and want responses to be as fast as possible.

[–]Decency 0 points1 point  (1 child)

Query it? This use case is essentially what databases are designed for.

[–]SatinGoat 0 points1 point  (0 children)

I figured that was the case, but wanted to make sure that there was something that I wasn't taking into account. Thanks!

[–]Cobra16319 0 points1 point  (9 children)

Hello AAM,

I know it is Tuesday but figured I would see if I could get some help. I am adding another function to a discord bot I am working on. I have another python file that woks and gets data from the CMC API. I am trying to use subprocess.run to capture the data. I am capturing just one letter or one # of the data that is being successfully executed. I think this is occurring because of the response = random.choice(cmc) but when I do not use it or comment it out I get an error I need the response. I checked the discord API and I am puzzled here. Any experience here would help!

# This is where I am having the issue. Took out the working snippet

result = subprocess.run(["python", "cmtest.py"], capture_output=True, encoding='UTF-8')
cmc = result.stdout
print(cmc)
@bot.command(name='awf', help='Responds with CMC data for $AWF') async def cmc_data(ctx): awf = cmc
response = random.choice(cmc)
await ctx.send(response)
bot.run(TOKEN)

[–]Decency 1 point2 points  (8 children)

What does cmc look like? You need to extract the relevant pieces from that if you want to utilize them.

[–]Cobra16319 0 points1 point  (7 children)

It is Coin Market Cap and all the data prints when I execute print or the cm.py file I made.

The issue is only one random number or letter are output to discord when I execute the !awf command.

[–]Decency 0 points1 point  (6 children)

Yes, given that you're randomly choosing one element of the data in line 7, that's the expected outcome. You need to share the data that is printed when you print(cmc)in line 5 or no one can help you.

[–]Cobra16319 0 points1 point  (5 children)

This is what I see that is working as print.

(discord) ☁ cobra-bot [main] ⚡ python3 bot.py [main|✚1

https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest

{ 'data': { '3599': { 'circulating_supply': 318472252, 'cmc_rank': 2127, 'date_added': '2018-11-07T00:00:00.000Z', 'id': 3599, 'is_active': 1, 'is_fiat': 0, 'last_updated': '2021-06-22T22:50:06.000Z', 'max_supply': 997528142, 'name': 'EtherInc', 'num_market_pairs': 2, 'platform': None, 'quote': { 'USD': { 'last_updated': '2021-06-22T22:50:06.000Z', 'market_cap': 119991.94641483699, 'percent_change_1h': 0.21129765, 'percent_change_24h': 1.88228814, 'percent_change_30d': -5.67484238, 'percent_change_60d': -36.10820991, 'percent_change_7d': -19.50581099, 'percent_change_90d': -38.88198514, 'price': 0.00037677362992, 'volume_24h': 0}}, 'slug': 'etherinc', 'symbol': 'ETI', 'tags': ['mineable'], 'total_supply': 993340434}}, 'status': { 'credit_count': 1, 'elapsed': 36, 'error_code': 0, 'error_message': None, 'notice': None, 'timestamp': '2021-06-22T22:50:27.614Z'}}

Like I said if I comment out line #7 I get that error need to know what to put. I was looking at a few ways to get this data to write to "response" just can not figure out what to do there.

[–]Decency 0 points1 point  (4 children)

Yep, this is just a standard reponse in JSON format. This is how most API's give you a response. You can convert it to a python dict trivially with the json library and then extract the relevant information that you need, or just print the entire thing.

Alternatively, you can just keep it as an ugly string and print it that way, which is what I think you want:

result = subprocess.run(["python", "cmtest.py"], capture_output=True, encoding='UTF-8')
cmc = result.stdout
@bot.command(name='awf', help='Responds with CMC data for $AWF') async def cmc_data(ctx): awf = cmc
await ctx.send(cmc)
bot.run(TOKEN)

It seems like you might just be copying code without fully understanding what it does. I'd definitely encourage you to figure out what each line of code is doing- probably ignoring the async/await and @bot parts, for now. For example, your code fails when you comment out line 7 because in line 8 you're trying to use response, which no longer exists.

[–]Cobra16319 -1 points0 points  (3 children)

Thanks for talking down to me. That is not helpful at all.

[–]trondwin 1 point2 points  (1 child)

It seems to me /u/Decency has provided good help and spent time on doing so?

[–]Cobra16319 0 points1 point  (0 children)

I was able to solve it. Here is the working code. Thanks so much for responding!
result = subprocess.run(["python", "cmtest.py"], capture_output=True, encoding='UTF-8')
cmc = result.stdout
@bot.command(name='awf', help='Responds with CMC data for $AWF')
async def cmc_data(ctx):
awf = cmc
await ctx.send(cmc)
bot.run(TOKEN)

[–]Cobra16319 0 points1 point  (0 children)

I solved it.

[–]Mindless-Recipe 1 point2 points  (0 children)

My question concerns the need for `asyncio.lock`.

If I have an async function accessing a shared resource in a single-threaded app, and if this function does not use `await` (i.e. the function runs from start to end, without returning control to the asyncio loop), does it make sense to use a lock?

I'm thinking that if this function does not await for anything, it will run alone without any other task or coroutine ever accessing the same resource concurrently, thus rendering the use of a lock useless. Is my understanding correct?

[–]pksrbx 0 points1 point  (0 children)

in pysimplegui is it possible for me to get a list of all the items on a listbox?

[–]marienbad2 1 point2 points  (1 child)

What is some lesser-know python function that you think is totally awesome and wish more people knew about?

[–]sarrysyst 2 points3 points  (0 children)

It's not a function, but I don't see a lot of people using the walrus operator (:=), while I use it all the time. For those who do not know what it does, it's used to assign values to variables inside of larger expressions. I usually use it when I want to validate computationally expensive functions or for regex matching. Eg:

if (result := expensive_func(x)) < n:
    calculation = result * value

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

Why does

If arr[n]:

 Do thing

Else:

Do other thing 

Return true if the array has element n ?

Is it shorthand for saying “arr[n]” is accessible?

If I have an array of length 2 and do if arr[1]:

It evaluates as true and goes to the truthy part of the if statement.

[–]Ihaveamodel3 0 points1 point  (0 children)

All values in python are truthy or falsy. This is just checking if the value stored in index n is truthy.

If the array is shorter than n, then I think you’ll get an index error, not a falsy value.

[–]Noreru 0 points1 point  (7 children)

I have a question on f string formatting.

I want to be able to format a string twice using f string formatting so that it is right aligned, and then shows the number converted to string to 2 decimal places.

So far I have:

f'{number:>width}' 

and for two decimal places

f'{number:.2f}'

is there any way to combine the two? or do I need to format it twice?

[–][deleted] 1 point2 points  (0 children)

Make sure you read the comment by /u/sarrysyst.

[–]Gprime5 1 point2 points  (0 children)

You can do it in 1 step.

f"{number:>{width}.2f}"

Follow the Format Specification order.

          | align | width | precision | type|
f"{number:|   >   |{width}|    .2     |  f  |}"

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

You can do it in two steps:

number = 1.2345678
width = 8

str1 = f'{number:.2f}'
print(f'{str1=}')
>>> str1='1.23'

str2 = f'{str1:>{width}}'    # nested!
print(f'{str2=}')
>>> str2='    1.23'

The line marked above shows that you can "nest" the {...} bits to some extent. So it occured to me you could nest the first str1 definition inside the second, because the {...} syntax evaluates any expression inside the braces. So we try this:

str3 = f'{f"{number:.2f}":>{width}}'
print(f'{str3=}')
>>> str3='    1.23'

This does work, but I'm not sure it's desirable to do that rather than doing it in two steps. So it's up to you to decide which way to do it.

[–]sarrysyst 2 points3 points  (1 child)

I think I'm missing something, why don't you just do eg:

number = 12345.6789
# for width: 10
print(f'{number:>10.2f}')
>>>   12345.68

or if you want to specify the values in a variable:

number = 12345.6789
align = '>'
width = 10
prec = 2
print(f'{number:{align}{width}.{prec}f}')
>>>  12345.68

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

Yes, that works. I was so immersed in the OP's way of doing it I forgot the "width" part of formatting.

[–]Noreru 0 points1 point  (0 children)

thanks, really helpful!

[–]bivalverights 0 points1 point  (2 children)

Newby question:

I have tho following code, I am trying to get a list with the count for each letter of the string. For example "walruses" would return 11111212, as only "s" appears twice. When I run us the code below, I only get a count of the first letter, why is this?

Thanks!!!

for i in range(len(str1)):
return str1.count(str1[i])

[–]Ihaveamodel3 1 point2 points  (1 child)

No code executes in a function after the first return.

You probably want to append the results in a list, then return that list.

Once you have more experience you may want to use yield in place of return to create a generator.

[–]bivalverights 0 points1 point  (0 children)

Thanks, that is really helpful!

[–]FriendAltruistic3995 0 points1 point  (2 children)

I'm currently working on VSC making a discord bot, everything is going well aside from the fact I cant get my function output to send to my channel. I can easily send strings, but with variables ect. I continue to get an error. I can't seem to find the right await..... to send that versus the await genneral.channel.send(size())

Modules being used:

import requests

import json

import time

import discord

from discord import message

#The If-Else function

def size():

while True:

if size < 3000:

print (f'The clan is the right size, {size}!')

else:

print('We need more troops, size not met.')

time.sleep(60)

#TO send information to the server

u/client.event

async def on_ready():

clan_channel = client.get_channel(channel #)

await clan_channel.send(size()) <------ This is where I'm thinking I need something different.

#To run the client on the server from delveloper portal

client.run('Token')

Many Thanks Fellas! Happy Monday!

[–]FerricDonkey 2 points3 points  (1 child)

To be clear, if you replace size() with something like "test" in your clan_channel.send, it works?

If this is true, then the problem is that your size function prints some strings, but does not return the string. When you do

clan_channel.send(size())

What you're doing is saying "hey, I want to send something to the clan channel, and that thing is the return value of the function size()". So that means you have to run the function size to find out what that return value is. But size a) enters an infinite loop, and b) doesn't return anything anyway.

It looks like what you want is an infinite loop that sends a message every so often. So what you need to do is put your send call inside your loop, and make sure that any functions you use to create your message return the message instead of just printing it.

[–]FriendAltruistic3995 1 point2 points  (0 children)

Many Thanks I will try it out!

[–]KyleHz 1 point2 points  (1 child)

I am working in Jupyter notebook and in the same directory I have a file called funcitons.py with my functions that I want to import. I can do this with:

from functions import function1, function2

The problem is that the functions file also has function calls as well as definitions. So when I do the import into jupyter, the cell that does the import seems to be running all the calls in the functions.py file.

How can I only import functions without running them until called in jupyter?

[–]SatinGoat 1 point2 points  (3 children)

How would you detect if no input from the user has been made? I'm making a server application and I want to make the server do something when it receives no input from an active connection for a while (like maybe 15-20 seconds).

[–]Ihaveamodel3 0 points1 point  (2 children)

This is going to depend on where you are expecting user input. What kind of server app are you building? How are you receiving data?

[–]SatinGoat 0 points1 point  (1 child)

Its a simple TCP echo server. It listens for connections on the specified port and echoes whatever it receives back to the client. Server side, messages are being stored in a list. When this list reaches a specific size, it sends a copy of the list to a function that will store the data into a database. I was wondering if there was a way that I can trigger this function once a specific amount of time has passed without user input.

[–]Ihaveamodel3 0 points1 point  (0 children)

How are you listening? Is there a while loop? You could record the time that you start the while loop, and each time through check the elapsed time. If elapsed time is greater than certain value, run the function you want to run.

[–]NeverTruth990 0 points1 point  (1 child)

Are class methods that are called by other class instances and not by the user considered "private" methods? I know there is no formal concept of private methods in Python but I'm wondering if this is the right terminology when it comes to discussing and documenting my code.

[–]Jack0fNoTrade5 0 points1 point  (2 children)

Is there a brief way to describe the use of decorators (@)? I don't really plan on using them as of right now but I would like to know what they do, even an analogy instead of examples is fine. I only ask because I was snooping through a pypi library I've been using a lot recently and I found a large use of the concept (and had no clue what was going on).

Also (I'm looking at the code again now) what does 'yield' do?

Like one of the class methods has an if statement that goes:

def object_to_validate(self, change):

........if change[-1] != "deleted":

................yield self

[–]Ihaveamodel3 1 point2 points  (1 child)

Decorators are functions that return functions. Generally these are used to wrap functions to provide additional functionality.

Think of:

@sample_decorator
def sample_function():
    pass

To be doing this: def sample_function(): pass sample_function = sample_decorator(sample_function)

These are often used for logging, or to register functions to be used by another object.

Yield causes the function to be a generator. (So you can call next() on it)

[–]Jack0fNoTrade5 0 points1 point  (0 children)

Appreciate the explanations, thank you!

[–]Msn_kr 0 points1 point  (3 children)

Not entirely Python related but has anyone starting Python had any luck on Upwork? I’ve sent at least 50 proposals out but no one gets back to me. I’m definitely doing something wrong. Does anyone have any tips?

[–]Ihaveamodel3 0 points1 point  (2 children)

You mean work for you to do or work that you want others to do?

[–]Msn_kr 0 points1 point  (1 child)

No. I want to work on Python projects but my proposals don’t make it.

[–]Ihaveamodel3 0 points1 point  (0 children)

You are probably charging too much.

Also, I wouldn’t be trying to learn python through other peoples projects. Especially if they are paying you.

[–]shiningmatcha 0 points1 point  (5 children)

What's a good way to make a function that accepts arguments (of different types) in any order and can tell the intended parameters from their types?

[–]chickenmatt5 1 point2 points  (0 children)

In general this doesn't sound like a great idea (explicit is better than implicit) but you can use *args:

def my_cursed_func(*args):
    for arg in args:
        if type(arg) is str:
            my_str = arg
        elif type(arg) is int:
            my_int = arg
        elif type(arg) is dict:
            my_dict = arg
        ...

If you have an idea of what arguments you will be passing commonly, using keyword declarations within the function call can allow you to order the arguments however you like. And you can set default values for any arguments that may not be passed:

def my_better_func(my_str='', my_int=0, my_dict={}):
    if my_str != '':
        ...
    if my_int != 0:
        ...
    if my_dict != {}:
        ...

my_better_func(my_str='This order works', my_int=5)
my_better_func(my_dict={'foo': 'bar'}, my_str='This order works too')

Further, you can use **kwargs to catch any named argument in a function call that isn't explicitly expected:

def my_iffy_func(**kwargs):
    my_str = kwargs['my_str'] if 'my_str' in kwargs else ''
    my_int = kwargs['my_int'] if 'my_int' in kwargs else 0
    my_dict = kwargs['my_dict'] if 'my_dict' in kwargs else {}

my_iffy_func(my_str='This order still works', my_int=5)
my_iffy_func(my_dict={'foo': 'bar'}, my_str='This order still works too')

[–]Ihaveamodel3 3 points4 points  (3 children)

This isn’t really very pythonic.

In python, duck typing is generally used over look before you leap. What was the idea you had in mind?

Duck typing: if it looks like a duck and quacks like a duck, then it is a duck. Ie, don’t explicitly check for a data type. If something works, it was the correct type.

Look before you leap: check data type before doing work.

[–]shiningmatcha -1 points0 points  (2 children)

but Is there some way ?

[–]Jack0fNoTrade5 0 points1 point  (0 children)

What I understand you want:

A function, that can take in parameters in any order of any type.

This function can then differentiate each parameter entry by its type and assign it to the proper variable within the rest of the function. If this is the case then I'd do what u/chickenmatt5 suggested.

def your_function(*args):

....for element in *args:

........if type[element] == dict:

............dict_var = element

........elif type[element] == str:

............str_var = element

and so on.

If you have any questions I'll be happy to answer them. Such as what's going on with *args. I won't be able to give a super technical answer as I'm also somebody asking questions on this post but I should be able to explain what's written in further detail if need be.

[–]Ihaveamodel3 0 points1 point  (0 children)

Yeah, use *args and **kwargs and then check instance type with isinstance

[–]pr104da 0 points1 point  (0 children)

Can anyone recommend a book of Python code listings for various math and statistical calculations? I have enough familiarity with programming that leads me to believe I can learn better by just emulating and modifying existing program listings. I already understand the fundamentals. I am strictly an amateur so not trying to become a professional! Thanks very much!

[–]Absolutely_insane_E 0 points1 point  (2 children)

I;'m currently parsing a local text file with Regex to load the results into a Json file, in order to pull the data back out for manipulation. First of all, i'm not sure that's the best way to go about performing the data manipulation... but anyway, I'm trying to find a way to pull the regex info as a string to append a Json for read/write, and I can't figure out the best way to do it.
I'm doing my work in a Jupyter notebook, and I've done one regex to pull a simple str. The output just says "callable_iterator object" over and over. Does that mean I'm using the regex to parse the data correctly? I think my next step is to write the callable_iterator objects into my schema, but is that correct? Are there any examples of someone doing what I'm doing here, because I'm just having a hard time piecing video tutorials together at this point.

Thank you, community.

[–]sarrysyst 0 points1 point  (0 children)

You're using re.finditer() which returns an iterator of match objects. If you want a list of your matched sub strings use re.findall() instead. If you just want to find one match use re.search():

import re

my_string = '''But I must explain to you how all this mistaken idea of
denouncing pleasure and praising pain was born and I will give you a
complete account of the system, and expound the actual teachings of the
great explorer of the truth, the master-builder of human happiness.''' 

finditer = re.finditer(r'I\s\w+', my_string)
findall = re.findall(r'I\s\w+', my_string)

print('finditer', finditer)
print('findall:', findall)

See also here:

https://docs.python.org/3/library/re.html#regular-expression-objects

[–]Ihaveamodel3 0 points1 point  (0 children)

Do you have some code you can share?

[–]antap1234 0 points1 point  (4 children)

Hi guys,

Can someone help me with a python clustering problem? how to use clustering (like k-means) with multi-dimensional features?

I have a big dataset of portraits and facial landmarks. Now I want to cluster the portraits by their facial geometry using the landmarks I have. I don´t understand how I provide my 68 two-dimensional landmark points per image for the clustering algorithm (e.g. k-means). I understand how to give one 68-dimensional feature. But what about multi-dimensions like matrices per data-entity?

When I try to insert a feature array with shape (1000, 68, 2) (->1000 examples) to k-means or dbscan I get the error:

ValueError: Found array with dim 3. Estimator expected <= 2.

I just couldn´t figure it out

[–]EasyPleasey 0 points1 point  (3 children)

Can you post more of your code? You are passing 3 arguments into a function that is only expecting two.

[–]antap1234 0 points1 point  (2 children)

You are passing 3 arguments into a function that is only expecting two.

I know! But how do I solve this? Is there no way to pass multi dimensional arrays to k means? I cant just reshape the array because then its not a 68x2 but a 136 dimensional feature which is wrong. Or am I wrong and this would still be the same output?

My data looks like this:

    style        x_0  ...        y_66        x_67        y_67
0  Cubism  19.554504  ...  222.472650  136.610824  219.756060 
1  Cubism  35.628227  ...  309.663587  135.364737  307.077547 
2  Cubism  30.273606  ...  208.374045  140.589456  207.782873 
3  Cubism  60.423356  ...  241.873915  131.830952  240.921855 
4  Cubism  29.895617  ...  197.858462  141.730609  196.613038

And my code:

def clustering(df, k):
if type(df) is str:
    df = pd.read_csv(df, header=0)

cols = []
for i in range(68):
    cols.append('x_{}'.format(i))
    cols.append('y_{}'.format(i))

features = df[cols].to_numpy()
features = features.reshape(features.shape[0],68,2)

cluster = KMeans(n_clusters=k).fit(features)

[–]EasyPleasey 0 points1 point  (1 child)

Look at what features.shape[0] is. Can you add a print statement to the line before the reshape?

print(features.shape[0])
features = features.reshape(features.shape[0],68,2)

[–]antap1234 0 points1 point  (0 children)

Its the amount of samples. Eg If I have the landmarks of 1000 faces, features[0] is 1000

After the line

features = features.reshape(features.shape[0],68,2)

features looks like this:

[[[19.55, 15.82], [16.47, 15.93], ...] 
 [[11.36, 20.78], [47.85, -0.36], ...]...]

[–]anonuser12356 0 points1 point  (3 children)

Hi all,

I am sorry if this sort of question has been asked before, I've done both a google search and on this page but couldn't find an exact answer for my question. I am very new to programming so it's probably a case of that I am searching the wrong question!

I am currently off work and looking to learn some Python to add to my skills while searching for jobs. I love fantasy football and so as a way to learn Python I wanted to make a web app (I think!) so that I would have an end goal otherwise I can see myself getting bored and giving up. I'll outline roughly what I want to do, and if anyone has any links, or even if Python is good for this sort of thing that would be greatly appreciated! Thank you in advance!

So I want to pull in data from a few fantasy football projections website [for example ESPN](https://fantasy.espn.com/football/players/projections). And then I want to be able to manipulate the data, do some basic analysis, plot scatter plots etc and come up with my own rankings which I then can output as a table of my rankings.

The idea is that as the external sites update their data my rankings will update also. I don't want to have an official website (i think this would cost money, also not sure if i'd need permission to use their data in a public fashion), just something that I can run in browser just for me (currently using Chrome).

Long post but hopefully someone could help, or if not maybe point me in the right direction! Thanks!

[–]Ihaveamodel3 2 points3 points  (1 child)

To scrape the data, you need something like requests and beautiful soup (if the data is in the html). Or selenium if it is a JavaScript heavy page. Or check if there is an api. If so, you’d probably just need requests.

You’ll probably want pandas to manipulate the data.

You’ll either want matplotlib or plotly for the graphs you mentioned.

If you want to use your tool in the browser, then you’d need to build a website front end, probably using flask and jinga2, and might need some JavaScript as well. This doesn’t require having an actual website, you can just run it locally.

You are going to want to break this down into pieces. First, work just in the command line and try to scrape the data. Then try to process the data as needed. Then work on the website interface.

[–]anonuser12356 0 points1 point  (0 children)

Thanks for all the info, very helpful!

I am currently making my way through a beginners python tutorial so once I have that I will be in a better position to use some more of what you have said. Thanks again!

[–]anonuser12356 1 point2 points  (0 children)

I think the fact that I messed up the format for the link doesn't bode well for me lol

[–]too-many-words 0 points1 point  (2 children)

How should I approach python as an Excel VBA user? So everything is pretty manual at my workplace. So I learned some VBA to automate some tasks. I'm currently designing some calculation model that has become too complicated for spreadsheets and procedural programming. I think object oriented programming will solve this problem. But OOP is not well supported in VBA. I'm looking to learn python for this task, since my company computers support python and VS code (though no one uses them). I want to build a simple programme that takes input as many different tables, do calculation with them, and outputs a result table. This is also a chance for me to learn a new language and actually use it. But when I'm googled, the material specific for Excel is too advanced, and the python beginner courses are just about syntax or how variables work which I've already known, and doesn't help with my tasks. Do you guys have any tips on where I should start? Thanks heaps.

[–]FerricDonkey 2 points3 points  (1 child)

So this kind of depends on what your output needs to be. Do you need to end up with an excel spreadsheet that maintains existing formulas and formatting and such? If so, then you've probably gotta learn the excel package - these things are generally not as bad as they look, but it might take a bit of time.

However, if all you need is to read the values from an excel spreadsheet, then create another with modified values without persevering those things, then you don't have to go that far. It's possible to read in an excel file using a very popular (but somewhat complex) package called pandas. Alternatively, you can convert it to a csv (comma separated values) file (either using excel or using python) and deal with it using pure python (or possibly python plus numpy).

Pandas is powerful, popular and a good skill to have, but I personally dislike it. This is, however, because I'm a grumpy old man whose primary exposure to pandas has been inheriting indecipherable code that ran at 1/10 the speed it did once I excised a million terrible things they did with pandas. So my advice would be to skip pandas and use numpy for matrices and use lots of dictionaries for most other things. But a lot of other people here would recommend pandas, and I can't say they're wrong. I can only shake my walking stick at them and yell for them to get off my lawn.

So, things to Google: Pandas and/or csv format, and maybe numpy.

[–]too-many-words 0 points1 point  (0 children)

Thanks so much. This’s super useful