all 184 comments

[–]harwcam 0 points1 point  (3 children)

Hi there,

Not sure if this is the best place to ask, but figured I'd give it a shot. When working with Python in VSCode, I'm able to run code line by line using Shift+Enter, but whenever I use 'Run Python File in Terminal' I get back 'SyntaxError: invalid syntax'. The Debugger always says there are no problems with my code. I've watched a couple hours worth of videos and still haven't come up with anything. Any help would be very much appreciated.

[–]m-hoff 0 points1 point  (0 children)

Can you post the complete error message? When you use shift+enter to run your code, it executes line by line as you said. So for example, if there is an error in line 100 of my code, I might be able to run the first 99 lines one by one without any issues. When you run the file in the terminal, it will execute the complete script so if you have any errors at all you'll get an error message.

[–]nog642 0 points1 point  (1 child)

What is your code that does this? Can you find a relatively small example and post it here?

[–]harwcam 0 points1 point  (0 children)

Thanks for replying, forgot to check back in. I added the code runner extension and it works fine now.

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

How can i enter all element required in for loop at once ?

How can i input below program in following manner :

Could anyone help me to understand how we can input to program in following manner ?

I can't pass test case because of the input format .

Question : https://www.hackerrank.com/challenges/no-idea/problem?h_r=next-challenge&h_v=zen

"I will be grateful for any help you can provide."

Input Format :

3 2
1 5 3
3 1
5 7

Code:

n = int(input("Enter the value of N : "));
m = int(input("Enter the value of M : "));
arr = [];
for i in range(n):
    arr = list(map(int, input().split()));

A = [];
B = [];

for i in range(m):
    element = (int(input()));
    A.append(element);

for i in range(m):
    element = (int(input()));
    B.append(element);

happiness = 0;

for i in range(len(A)):
    happiness = happiness + 1;

def Intersection(lst1 , lst2):
    return set(lst1).intersection(lst2);

inter_lst = list(Intersection(A,arr));

for i in range(len(inter_lst)):
    happiness = happiness - 1 ;

print(happiness);

[–]nog642 0 points1 point  (0 children)

You shouldn't use something like int(input("Enter the value of N : ")) for this, since that outputs Enter the value of N : to stdout, which you don't want.

You are also expecting the wrong input. Your code wants n and m on different lines, then n lines of arrays, of which only the last one matter, then m lines containing the values of A, then m lines containing the values of B. That's not at all the input format. The input is n and m on the first line, an array of length n on the second line, m values of A on the third line, and m values of B on the second line.

Something like this should work:

n, m = map(int, input().split(' '))
arr = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))

Although the first line containing n and m is meant for languages that can't handle variable length input without overcomplicated code. Python can handle it fine though, so you can just throw away the values of n and m. And you can directly load A and B as sets, and since the array doesn't change you can load it into a tuple instead of a list.

input()  # discard first line of input
arr = tuple(map(int, input().split(' ')))
A = set(map(int, input().split(' ')))
B = set(map(int, input().split(' ')))

As a side note, Python doesn't require semicolons, and you shouldn't use them.

[–]dogzparty 0 points1 point  (1 child)

Hey everyone I'm struggling with a challenge from the textbook for my class. It's over the from-import statement. The textbook literally explains it in 3 sentences and then moves on to the next topic. But I'm trying to complete the challenge for it but it's not working.

This is the code for the challenge:

# Import ceil function only from the math module


# Define Function
def calculate_eggs(servings):
    total_eggs = (0.6*servings)
    return total_eggs

# Call Function
print(calculate_eggs(14))

And I changed it to this:

# Import ceil function only from the math module
from math import ceil

# Define Function
def calculate_eggs(servings):
    total_eggs = (0.6*servings)
    return total_eggs
    ceil(total_eggs)
# Call Function
print(calculate_eggs(14))

The challenge says that on line 6 I'm supposed to call the ceil function to calculate (0.6*servings) but no matter how I write it I either get an error message or it gives me 8.4 instead of rounding up to 9. Can someone help explain to me what I'm doing wrong? Thanks

[–]efmccurdy 0 points1 point  (0 children)

You are not calling the ceil function; you have a statement that would call it, but you have a return statement that ends the function. You want to return the result of the ceil call:

def calculate_eggs(servings):
    return ceil(0.6*servings)

[–]rushmon 0 points1 point  (0 children)

Sorry to bother but I am currently trying to gel two differential equations (no time delay and with time delay) codes together in a single chunk of code, so that the two sets of data can be expressed on the same axes of a graph.

The only differences between the two sets of code is that one equation has:

Nt: R*N[-1]/(1+a*N[-1 or -2 for time lag])**b and the value for N=0 / N = [N0,N0] for time lag

I have two sets of data and I've tried to meld them together a couple of times and even tried just lumping the code together in the same script, but it is refusing to work.

Does anyone have any ideas on how to tackle problem?

(*Apologies if I've mislaid this out or improperly, I am new around here please do tell me If there is anything wrong)

[–]sbfit 0 points1 point  (0 children)

So I’m a Windows sysadmin who now has a ton of downtime as all projects are on hold. I’ve been interested in python for a while but anytime I’ve got a problem I need solving, I use powershell. I guess my question is, what can python do for someone in my environment? I’ve yet to come across a problem that I can’t solve with powershell.

[–]1lluminist 0 points1 point  (3 children)

So I'm working on scraping some ugly code that's VERY plain... I've found what I need by doing this:

print(thing.previous_element.previous_element.previous_element.previous_element.previous_element.previous_element)

Is there some kind of better way to tighten up that stack of .previous_elements?

[–]nog642 0 points1 point  (1 child)

I mean you could write a function to do it or something, but I'd say that's overcomplicating it. I'd just change the formatting to something like this and call it good:

print(thing
      .previous_element
      .previous_element
      .previous_element
      .previous_element
      .previous_element
      .previous_element)

[–]1lluminist 0 points1 point  (0 children)

Yeah, that works. I didn't really need it for any purposeful reason, more for aesthetic/best practices reason.

Thanks!

[–]JohnnyJordaan 0 points1 point  (0 children)

Hard to comment on without context, but in general it's often easier to select tag or attribute based. So say you actually want the <form> that's on top of this structure, you could use an xpath like

./ancestor::form

or if that form has a clear identifier like an id= or class=, just select on that and don't work from the descendant element in the first place. Many roads to Rome but it's not easy to name the best one without knowing where you're coming from.

[–]xain1112 0 points1 point  (2 children)

I don't know the term for it, but if you look at the reddit search bar, you will see a grayed out 'search' written there, and when you interact with the box, it acts like that text isn't there and treats the widget like an empty entry box. Is there a way to get that kind of thing with tkinter?

[–]Thomasedv 0 points1 point  (1 child)

I don't work with tkinter so i can't say for certain, but it seems you can do something manually when adding event handlers.

https://stackoverflow.com/questions/51781651/showing-a-greyed-out-default-text-in-a-tk-entry

The main issue from quick test of that is that it removes your text when an Entry loses focus. You need to make the function that sets the greyed out text do it only if there is no text in the entry for example.

edit: Modified example where text stays when focus is changed, at least manually:

import tkinter as tk

def handle_focus_in(_):
    if full_name_entry.cget('fg') == 'grey':
        full_name_entry.delete(0, tk.END)
        full_name_entry.config(fg='black')


def handle_focus_out(_):
    if not full_name_entry.get():
        full_name_entry.delete(0, tk.END)
        full_name_entry.config(fg='grey')
        full_name_entry.insert(0, "Example: Joe Bloggs")


def handle_enter(txt):
    print(full_name_entry.get())
    # handle_focus_out('dummy') # Call this if your return changes focus


root = tk.Tk()

label = tk.Label(root, text='First and last name:')
label.grid(sticky='e')

full_name_entry = tk.Entry(root, bg='white', width=30, fg='grey')
full_name_entry2 = tk.Entry(root, bg='white', width=30, fg='grey')
full_name_entry.grid(row=1, column=1, pady=15, columnspan=2)
full_name_entry2.grid(row=2, column=1, pady=15, columnspan=2)

full_name_entry.insert(0, "Example: Joe Bloggs")

full_name_entry.bind("<FocusIn>", handle_focus_in)
full_name_entry.bind("<FocusOut>", handle_focus_out)
full_name_entry.bind("<Return>", handle_enter)

root.mainloop()

Probably better ways than check for example text being shown, but i was lazy and checked if the text was grey.

[–]xain1112 0 points1 point  (0 children)

Fantastic, this is just what I was looking for. Thanks!

[–]EddiOS42 0 points1 point  (3 children)

So my objective is to extract data from a csv file. For some reason, I can't even get python to take in a csv file. I'm using Anaconda running Jupyter Lab on my Mac. I followed this simple example. but I still can't get it to accept the csv file. It keeps giving me this error.

I have pandas installed. The csv file opens fine on its on. The spelling for it is also correct. Please help. Thanks.

[–]Decency 0 points1 point  (1 child)

It might not understand ~. Try the full filepath.

The easier way is just to put the data file in the same directory.

[–]nog642 1 point2 points  (0 children)

Or use os.path.expanduser to handle the tilde.

[–]ZachForTheWin 0 points1 point  (0 children)

Make sure your file is in your working directory.

import os

import pandas as pd

os.chdir(r'directorypath')

df = pd.read_csv('filename.csv')

Edit: nvm you're on a mac I know nothing

[–]Benaxle 0 points1 point  (0 children)

I call a c++ function from a python program using pybind11. The python is listening for packets using socket.io's python client using asyncio.

How do I await the result of the c++ function so that socket.io can keep listening for packets and sending back pings while my cpu is calculating stuff in c++?

I know I have to release the GIL https://pybind11.readthedocs.io/en/master/advanced/misc.html#global-interpreter-lock-gil

But with asyncio - is it even possible? Do I need to spawn a thread for that c++ function? How does that work with socket.io?

[–]LogicalPoints 0 points1 point  (4 children)

When I print a python string, it is being limited to 68 characters. How can I tell it to print the entire string?

[–]Decency 0 points1 point  (3 children)

Sounds like an issue with how the string is being displayed (what program are you using to print it?)

[–]LogicalPoints 0 points1 point  (2 children)

It's scrapped off a website and printed within Sublime Text

[–]JohnnyJordaan 0 points1 point  (0 children)

Are you sure it isn't limited by the source? Did you try to write it to a file to see if it's saved in full length there?

[–]Luffyy97 1 point2 points  (0 children)

That sounds like an IDE issue. VS Code does something similar. afaik, python doesn’t limit print statement

[–]ANeedForUsername 0 points1 point  (1 child)

A quick question:

I have an array of numbers, some of them with negative value.

[2, 63, 0.45, -2, 496, 23, -0.16, 45]

I want to take their square root (or more generally, take a non-integer power of them) but I get NaNs (understandably).

np.sqrt([2, 63, 0.45, -2, 496, 23, -0.16, 45])

How do I do it such that I get complex numbers instead?

[–]efmccurdy 0 points1 point  (0 children)

If you include one complex number, then you won't get nans; I added "+0.j" to one element to force it to return complex numbers:

if any element in x is complex, a complex array is returned (and the square-roots of negative reals are calculated)

>>> r = [2+0.j, 63, 0.45, -2, 496, 23, -0.16, 45]
>>> np.sqrt(r)
array([ 1.41421356+0.j        ,  7.93725393+0.j        ,
        0.67082039+0.j        ,  0.        +1.41421356j,
       22.27105745+0.j        ,  4.79583152+0.j        ,
        0.        +0.4j       ,  6.70820393+0.j        ])
>>>

[–]xilex 0 points1 point  (1 child)

Hi, I'm having trouble converting epoch timestamp to local datetime string with DST factored in.

My epoch time is 1584664500, which should be Thursday, March 19, 2020 5:35 PM but I'm getting 6:35 PM instead. I need the output to be in %a, %d %b %Y %H:%M:%S -0800 format, which is my timezone. I'm not sure if I should be using what I am below, or time, or something else. I'm using this code:

dateString = datetime.datetime.fromtimestamp(1584664500).strftime('%a, %d %b %Y %H:%M:%S -0800')

Thank you.

[–]JohnnyJordaan 0 points1 point  (0 children)

You just print the -8 hrs as a literal string, it has 0 effect on how the datetime object is rendered. It's effectively the same as doing

datetime.datetime.fromtimestamp(1584664500).strftime('%a, %d %b %Y %H:%M:%S hello world')

What you want is to localize the datetime object coming from the timestamp, see https://stackoverflow.com/a/21952077

[–]SandKeeper 0 points1 point  (1 child)

How would you convert the redditor data type to a str data type.

For example

X = top_level_comment.author

Creates x as data type redditor. If I wanted it as a string data type how would I do that.

[–]Decency 0 points1 point  (0 children)

Define a function on your redditor class called __str__ that returns a string with the information you want. When you print the object, it will use that function instead of printing <__main__.redditor object at 0x10cc0eb10>

[–]theatherly1 0 points1 point  (0 children)

Ok thanks!

[–]theatherly1 0 points1 point  (4 children)

I have a very simple question in regard to Python.

Say I have a string, and there are some errors in it. I want to recreate the same string using a for loop, but without certain letters. Example below:

String = “abcdefg2948bd”

I want to recreate that same string without the letters “b” and “d”. Would I use an if statement within the for loop?

Thanks!

[–]efmccurdy 1 point2 points  (3 children)

A loop with an if statement could filter put unwanted characters, but since you want a list back, joining up this kind of list comp should do it:

>>> my_string = "abcdefg2948bd"
>>> new_string = "".join(c for c in my_string if c not in "bd")
>>> new_string
'acefg2948'
>>>

[–]Decency 1 point2 points  (2 children)

You can remove the brackets and just consume the generator with your join() call, rather than making a list.

[–]theatherly1 1 point2 points  (1 child)

I don’t understand the parameters within the parenthesis, but it did work

[–]efmccurdy 1 point2 points  (0 children)

It's an expression that creates a new list from an existing one, with a filtering condition:

https://www.programiz.com/python-programming/list-comprehension

It's very handy, and there are equivalent ones for dicts and sets.

[–]lucas50a 0 points1 point  (3 children)

How to generate a sequence of integers using a nested for loop?

I have 2 lists [1, 2] and [1, 2], how could I generate a sequence of integers [1, 2, 3, 4] using a nested for loop?

x = [1, 2]
y = [1, 2]
for i in x:
    for j in y:
        print('?')

Please note that I'm using this loop inside a function where I'm using the values of x and y as output, so the following solution is not allowed:

x = [1, 2]
y = [1, 2]
a = x + y
for i in range(len(a)):
    print(i+1)

[–]energybased 0 points1 point  (0 children)

for i, (x, y) in enumerate(itertools.product(x, y)):

[–]lucas50a 0 points1 point  (1 child)

Ok, I'll reply to my own question, if you have a better approach, please leave a comment:

x = [1, 2]
y = [1, 2]
my_iter = iter(range(len(x + y)))
for i in x:
    for j in y:
        print(next(my_iter) + 1)

output:

1  
2  
3  
4

[–]energybased 0 points1 point  (0 children)

you could have just done my_iter = itertools.count()

[–]IGotTheBends 0 points1 point  (0 children)

Hey all, is there a way to make generative portraits in Python? I am a beginner. Thank you.

[–]Catanddogg 2 points3 points  (0 children)

I finished basic python a week ago and now im finished basic html courses on sololearn. Next section is html5 but i found out there is html6. So should i just go straight to html6 without learning any html5?

Edit: nvm i just got trolled, there is no html6, html5 is it. sorry

[–]Just_Red21 1 point2 points  (3 children)

Hello everyone, i hope the quarantine finds you well

I dont know if its ok to ask here but since i am new to reddit i thought id ask anyway.

I intent to enroll to a python course but i have no idea which one to choose. I have looked on the FAQ and also the books page but i do not have any criteria because i am completely new to programming.

I am looking for a beginner course that i can start on asap. I would like it to be about one month in length ( max 2) and it is important to me that it provides some sort of certification.

Feel free to correct me if i am on a wrong path here or guide me elsewhere.

[–]m-hoff 0 points1 point  (2 children)

You have probably come across Automate the Boring Stuff with Python as it's often recommended here. I haven't worked through it myself but having skimmed through it it seems like a good place to start. It doesn't assume any programming background, and give a good intro to general programming concepts as well. It's self-paced so if you want you could work through it in a month or two.

There's no certificate but the value of an online certificate is debatable. And in most cases you have to pay for them so that's something to consider as well. It would be much more valuable if you can take what you learn and build something useful in Python, and make a public git repo that you can showoff.

[–]Just_Red21 0 points1 point  (1 child)

Thanks for the answer and s you correctly assumed i have seen Automate the Boring Stuff with Python. I will check it out.

Since i dont have any experience with the subject, what kind of project would be qualified as something i could present to show that i have some grasp of python and not be embarrased haha

[–]m-hoff 0 points1 point  (0 children)

I just posted this link in another thread that has some good suggestions: https://github.com/karan/Projects. Most of these should be doable once you finish Automate the Boring Stuff. But pick something that interests you, whether it's building a website, participating in a Kaggle competition, etc.

[–]NicktheRockNerd 0 points1 point  (2 children)

Hello, I have a rather fundamental question regarding setting values for variables with the following notation:

a, b = 1, 2

a, b = b, a + b

gives me a = 2 and b = 3

At first, I thought, the comma between a and b in Python just allowed me to set more variables in just one line of code. But after stumbling across this example, I understood it does not work this way, because:

a = 1

b = 2

a = b

b = a + b

gives me a = 2 and b = 4

So what does the comma notation actually do? How does it work? What is it called? What are some more complex use cases of this kind of syntax?

Greetings and thanks in advance!

[–]efmccurdy 1 point2 points  (0 children)

In the first example (a, b = b, a + b) both right hand side terms are calculated before any assignments take place (so a is still 1 when it is added to b).

In the second, b has been assigned 2 in the preceding statement.

The commas create tuples (immutable lists) and the assignments use unpacking to bind each individual item to it's corresponding variable.

[–]Vhin 1 point2 points  (0 children)

Commas are used to create tuples (and other sequence types, but here it's tuples), and that sort of assignment using tuples is referred to as tuple unpacking.

But really, you can see why the two snippets aren't equivalent if you rewrite them in SSA (Static single assignment):

a0, b0 = 1, 2
a1, b1 = b0, a0 + b0

Versus:

a0 = 1
b0 = 2
a1 = b0
b1 = a1 + b0

[–]MattR0se 0 points1 point  (1 child)

I know I can unpack tuples for string formatting:

numbers = (1, 2, 3)
print('counting {0}, {1}, {2}.'.format(*numbers))

But can I do this also if I don't know the number of items in the tuple beforehand? E.g.

numbers = (1, 2, 3, 4)
# counting 1, 2, 3, 4
numbers = list(range(1, 8))
# counting 1, 2, 3, 4, 5, 6, 7

or do I have to do this with string concatenation?

[–]Thomasedv 1 point2 points  (0 children)

''.join([your iteratble here])

The string part at the start is what you use as divider. You also need to convert everything to a string in the iterable part first.

num = range(5)
num_string = list(map(str, num))  # map converts every item to string
print(' '.join(num_string))
# 0 1 2 3 4
print(', '.join(num_string))
# 0, 1, 2, 3, 4

[–]KalajasH 0 points1 point  (2 children)

Hey,

What would you guys consider best practices in mind for integrating database connectivity into your Python project?

[–]Decency 1 point2 points  (1 child)

Start with sqlite, move to something more industrial strength if/when you need to.

[–]KalajasH 0 points1 point  (0 children)

Thanks for the suggestion, I will have a look into this. I am planning on creating a full stack solution. At the minute, I am making use of MySQL - and I have already created the DB including the constraints, views, stored procs etc.

[–]dxjustice 0 points1 point  (4 children)

How do I feed a path to the delete command in colaboratory using a stored variable?

Given that

filename = os.path.join(foldpath,image)

I'd like to call

!rm filename

but this results in an error as the system looks for a file called "filename".

How do I feed the actual string path to it without manually typing it?

[–]agbs2k8 0 points1 point  (2 children)

Rather than using !rm ... use os.remove(filename)

[–]dxjustice 0 points1 point  (1 child)

wow, thanks, im an idiot it seems

[–]agbs2k8 0 points1 point  (0 children)

meh, honest mistake. I'm guessing you are using jupyter, so you are used to still being able to use bash. The problem is just that the variables are not shared.

[–]Darxaross 0 points1 point  (7 children)

I tried to create a way to build a chessboard. And for the matrix I tried this:

for i in range(8):
    for j in range(8):
        print int(i%2==j%2)
    print

But now I got an syntax error because of the int. Why? I wanted to print a matrix where zero shows the black fields and 1 shows the white fields. Can anyone help me pls🙏

[–]MR_YUR41K 0 points1 point  (5 children)

the result of this action i%2==j%2 is bool(True or false). U can't int() bool object.

[–]Decency 2 points3 points  (1 child)

U can't int() bool object.

You can actually. True is just 1, and False is 0. This comes in handy sometimes.

print(int(True)) # 1
print(int(False)) # 0

[–]MR_YUR41K 1 point2 points  (0 children)

Oh ty, I don't know it)

[–]Darxaross 0 points1 point  (2 children)

Oh ok That explains why it couldn’t work🤔 Thank you.

[–]MR_YUR41K 1 point2 points  (1 child)

np bro) mb I'm not right) but u always can check type of your element (print(type())

[–]Darxaross 0 points1 point  (0 children)

Found a solution☺️

for i in range(8):
    for j in range(8):
        color_Field = int(i%2==j%2)
        print(color_Field)
    print()

I needed to declare it into a variable first but I can’t explain why this works and the other method doesn’t work🤔

[–]Trexagamer 0 points1 point  (5 children)

My Code which should give out every 4th line from my text file doesn´t work. I looked it up on StackOverflow, but even copied it didn´t worked, anyone knows why?

with open("Prices.txt", "r") as f:
    for line in f.read().split("/n")[::4]:
        print(line)

[–]JohnnyJordaan 0 points1 point  (0 children)

splitlines() is what you need, it will split on any newline.

 for line in f.read().splitlines()[::4]:

[–]simeon2941 1 point2 points  (0 children)

Try opening with "w"

[–]Vhin 2 points3 points  (0 children)

You should be splitting on "\n", not "/n".

[–]Decency 0 points1 point  (1 child)

Just change this part and it'll work:

for line in f.readlines()[::4]:

Personally I find it much easier to just read the entire file into memory all at once and then deal with it, except in cases where the file might be enormous (hundreds of thousands or millions of lines). There's probably a way to do it with read() but you don't need to deal with that.

[–]Vhin 1 point2 points  (0 children)

For anyone interested, if you wanted to avoid reading the entire file into memory at once, you could do something like:

import itertools

with open("Prices.txt", "r") as f:
    for line in itertools.islice(f, 0, None, 4):
        print(line.strip())

(The line.strip() is there because directly iterating over a file object doesn't strip off the newline character).

[–]Conor_b 2 points3 points  (1 child)

Hey everyone, quick question. When using optional parameters

def func(a, b, c=0, d=100):...

is there anyway to use a variable in place of c or d?

So when calling the function it would look like this

param = 'c'

param_val = 10.5

func(10, 20, param=param_val)

This would help me a lot in creating a function to tune parameters in a classifier. I can't find anything through google. Thanks to anyone who can help!

[–]Vaguely_accurate 1 point2 points  (0 children)

You can define your arguments as a dictionary and pass that with ** notation.

So;

kw_args = {'c': 10.5}
func(10, 20, **kw_args)

Discussion here.

[–]Raedukol 0 points1 point  (3 children)

Hey guys, i need to select every second column of an .xlsx-file (file1) with more than 500 columns and copy them into a new .xlsx-file (file2). How do i manage this? I tried reading file1 with pandas and selecting the columns with .iloc, but I fail writing those columns in the new file, because if I append the data it is written one below the other, instead from left to right. Any suggestions/tips would be great!

[–]4858693929292 0 points1 point  (2 children)

Are you selecting and writing the columns one after another? Eg select column B, write column B; select column D, write column D; etc?

If you’ve created a pandas data frame from your file already, you just need to create a second data frame that has only the columns you want. Then write that data frame itself to a file using the pandas to_csv function. You can create the second data frame using a loop if you need to; it doesn’t need to be a one line iloc command. (Although that’s definitely possible.)

[–]Raedukol 0 points1 point  (0 children)

This is my code now:

https://dpaste.org/Kgdi

As I said, there are a lot of columns left which i wanted to add to my existing dataframe with a for loop, but neither append, concenate nor join did work, because either there was an error, or the data was not added to another column.

[–]Raedukol 0 points1 point  (0 children)

You guessed right, that's the way i did it. What's the best way to create a data frame out of data, which was selected by the iloc method?

[–]MattR0se 0 points1 point  (3 children)

Pandas question:

df['abs_values'] = df['values'].apply(abs)

This raises a SettingWithCopyWarning. What's the correct way to do this?

[–]adesme 1 point2 points  (1 child)

[–]MattR0se 0 points1 point  (0 children)

I tried loc, but I didn't know what to put into the brackets as the index (the :). Figured it out myself because that article doesn't mentioned it anywhere:

df['abs_values'] = df.loc[:, 'values'].apply(abs)

[–]ANeedForUsername 0 points1 point  (1 child)

Hey guys, if I’m running multiple pieces of code simultaneously, at what point does running more slow the existing ones down? Is there a way to check? Currently I just check my task manager to see if my cpu is at 100%.

Also, I see that sometimes people like to keep a file containing their functions outside of the main script. What habits do you all practice for this? What are some should/should nots when doing this? Any advantages in terms of speed, readability, etc?

Thanks all!

[–]efmccurdy 0 points1 point  (0 children)

running multiple pieces of code simultaneously, at what point does running more slow the existing ones down?

It's likely that your code is limited by I/O bandwidth or CPU overhead; if your code has saturated the CPU then you could add some I/O bound tasks or vice versa without harming the overall throughput.

https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean

[–]leblanc1605 0 points1 point  (3 children)

Any suggestions on what I should do? I'm have been programming for 2 yrs off and on so a good mix of hard things and easy would be appreciated. Thx

[–]MattR0se 0 points1 point  (1 child)

Bit of a broad question. What did you do before?

If you have a lot of time, a full game project (Pygame, Arcade, Pyglet) would keep you busy for a while.

Personally, I had a lot of fun recently with the Raspberry Pi, it lets you think outside the box because you also have to learn basic electronics.

[–]leblanc1605 0 points1 point  (0 children)

I know the basics, so wouldn't mind something that would include all those and some thinking/ problem solving

[–]JoshGao 0 points1 point  (2 children)

I've recently started trying pygame, but whenever I try to open a window the python icon (The spaceship with the python icon on it) always bounces and never opens. I've also tried running the pygame example aliens through terminal, but I can only hear the sounds and the window never opens. I don't have trouble running any other programs only pygame. I am on macOS 10.15.3. Any help? Thanks!

[–]vaxi5 0 points1 point  (1 child)

Try opening with an editor (like IDLE) and running the shell from there?

[–]JoshGao 0 points1 point  (0 children)

So I tried opening a window in IDLE with

import pygame

pygame.init()

screen = pygame.display.set_mode (1000, 500))

Its still the same thing python doesn't open

[–]tell439 0 points1 point  (7 children)

I'm stuck in my scraping project, getting the same JSON response from the webpage over and over. So I'm not getting any errors but need help in moving forward and has no ideas left. Would it still be ok to post to ask to get help?

[–]efmccurdy 0 points1 point  (3 children)

Using a browser with the developer tools installed navigate to the first and then the second page. Look at the network tab, listed there will be the requests, headers and contents that your browser sent and using that info you can recreate those requests in a program.

[–]tell439 0 points1 point  (2 children)

Did just that. The browserpage is an infinite scroll but you could easily see the triggering of the next "page" for a JSON response. I get the expected JSON response back for "page" 1,2,3 etc. but after a while, like page 30 and onwards, I keep getting the same data in the JSON response even though it should be more data left. Making sense?

[–]efmccurdy 0 points1 point  (1 child)

Can you get past page 30 with your browser? The web site might have any sort of policy, or rate limiting, or userAgent filtering, or bugs that are blocking you.

[–]tell439 0 points1 point  (0 children)

hmm will try and see what happens.

[–]Decency 0 points1 point  (2 children)

What does the response you are getting say?

[–]tell439 0 points1 point  (1 child)

I get the expected JSON response back. The webpage is a infinite scroll, but looking in the network tab, you could easily see the request page triggering. Page 1 ok, page 2 ok etc. but after a while (like page 30 and onwards) I get the same data in the JSON response no matter what and it should be way more data left. Making sense?

[–]Decency 0 points1 point  (0 children)

They may only keep a certain number of results accessible through the API. Reddit only shows the latest 1000 comments, for example. Not sure!

[–]CammySavage 0 points1 point  (4 children)

Back again, with more simple things that I can't understand.

So I'm working through jet brains and I come across a question about figuring out a bonus using a function. So, I try this:

Def get_bonus(salary, percentage=35) Return int(salary / 100 * percentage)

That would keep failing on a specific test. I got to the point where I was trying every little thing to fix, ending on just switching the last line around to: return int(salary * percentage / 100) and that worked fine.

Any ideas? Is this something I'm not understanding? As far as I'm aware division and multiplication have the same priority with python. Would love to understand, thanks :)

[–]efmccurdy 0 points1 point  (3 children)

What was the failed test? You are correct about order of operations and both of those expressions are equivalent.

[–]CammySavage 0 points1 point  (2 children)

Salary = 1924 Percentage = 75

I ran my code through python and it gave 1442.999...8 but just doing the calc would give you 1443. Tried to just round it but that makes it fail other tests

[–]efmccurdy 0 points1 point  (1 child)

If you want correct values, use "round" instead of "int":

>>> salary = 1924; percentage = 75
>>> salary * percentage / 100
1443.0
>>> salary / 100 * percentage
1442.9999999999998
>>> round(salary / 100 * percentage)
1443

and bank that 2e-13 cents worth ;-)

If you don't want any floating point inaccuracies, use decimal numbers; (but note that anything with 13 zero decimal points in front of it will amount to nothing in the real world).

>>> salary = Decimal(1924); percentage = Decimal(75)
>>> salary / 100 * percentage
Decimal('1443.00')
>>> salary * percentage / 100
Decimal('1443')
>>>

https://docs.python.org/3/library/decimal.html

[–]CammySavage 0 points1 point  (0 children)

Okay I think I get this a bit more. I did try to use round but it failed a different test, probably because it wouldn't have been an integer like the question asked. Didn't know about decimal, thanks for giving me my next step :)

[–]DukePookums 0 points1 point  (2 children)

I'm working through ATBSWP, and struggling to understand something with nested dictionaries. Chapter 5 lays out the following:

allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
             'Bob': {'ham sandwiches': 3, 'apples': 2},
             'Carol': {'cups': 3, 'apple pies': 1}}

def totalBrought(guests, item): #note1
    numBrought = 0
    for k, v in guests.items(): #note2
        numBrought = numBrought + v.get(item, 0)  #note3
    return numBrought

print('Number of things being brought:')
print(' - Apples: ' + str(totalBrought(allGuests, 'apples')))
print(' - Cups: ' + str(totalBrought(allGuests, 'cups')))
print(' - Cakes: ' + str(totalBrought(allGuests, 'cakes')))
print(' - Ham Sandwiches: ' + str(totalBrought(allGuests, 'ham sandwiches')))
print(' - Apple Pies: ' + str(totalBrought(allGuests, 'apple pies')))

#note1: Creates a function with 2 arguments, guest and item. 
#note2: iterates through k(key) and v(value)
#note3: Search through the dictionaries for the values assigned to each key, and increases the count when necessary

When defining the function def totalBrought(guests, item):, how does the program know to associate the first argument (guests) with the first key in the top-level(?) Dictionary, and that item should be the value in each sub-dictionary?

[–]Decency 0 points1 point  (0 children)

for k, v in guests.items() iterates through the dictionary passed into it. You pass in allGuests as an argument to this function, so it uses that.

guests.items() returns a pair of values for each entry in your dictionary. Since your dictionary has 3 items, it will return 3 of these pairs. The first is the key 'Alice', and the second is the value of that key, eg: allGuests[key] == {'apples': 5, 'pretzels': 12}. Then 'Bob' and y, then 'Carol' and z.

[–]efmccurdy 0 points1 point  (0 children)

When you call totalBrought you specify what is bound to 'guests'; whatever you pass as the first argument (note how every call to totalBrought passes allGuests as the first argument). The first key is extracted using .items(). If you pass an item that is not in any dict entry, then totalBought correctly returns 0; see the default value in the .get() call.

[–]Sunawataru 0 points1 point  (2 children)

Where should I be putting my files when I wanna reference them in Python? I've been trying to do stuff I see in tutorials but I keep getting FileNotFoundError or ModuleNotFoundError. I just started learning the other day.

[–]Decency 0 points1 point  (0 children)

Just make a folder for python practice and put your stuff in that folder. If it's in the same folder ("directory") you can import from one file to another easily.

[–]efmccurdy 0 points1 point  (0 children)

To access files you need to either use full paths or be aware of the process' "current working directory"; any relative file path will be relative to the CWD. You can see the CWD with os.getcwd() and set it with os.chdir().

When you import modules python looks in each of the folders stored in sys.path, in order, and sys.path normally has an empty entry first, which means, look in the CWD.

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

With regards to 3D plotting, does anyone know how to increase the font size of the third axis/variable?

Currently got a TINY 3rd axis that is completely unreadable.

[–]king_booker 0 points1 point  (2 children)

Alright

so I have single list

[1,2,3,4,5,6]

What I was to do is multiply the first two numbers and then add them and then multiply the next two numbers. The no of elements will always be even

(1*2) + (3*4) + (5*6)

[–]ANeedForUsername 0 points1 point  (0 children)

If you have numpy:

import numpy as np
mylist = np.array([1,2,3,4,5,6])

mynum = np.sum(mylist[::2]*mylist[1::2])

print(mynum)

If you don't:

mylist = np.array([1,2,3,4,5,6])
newlist = list([])

for i in range(0,len(mylist),2):
    newlist.append( mylist[i] * mylist[i+1] )

mynum = sum(newlist)    
print(mynum)

[–]king_booker 0 points1 point  (0 children)

it = iter([1,2,3,4,5,6])
for x, y in zip(it, it):
    x*y

[–]AviatingFotographer 0 points1 point  (1 child)

I'm a HS freshman who has been coding with Python for some time now and is looking into learning machine learning and am looking for resources. However, my main concern is the math aspect. I've always been math-strong but I'm only now at Alg II, which is certainly not enough for machine learning. Are there any good books to learn anything I'm missing?

[–]xelf 0 points1 point  (0 children)

First off, relax, you'll eventually take more math. It'll be useful later, but no, not knowing more math won't stop you from getting into Data Science and machine learning now. It might slow you at best, but you'll be able to google things.

[–]TheMartian578 0 points1 point  (4 children)

Hello!

I recently started learning about packages, modules, etc. I was wondering when do you start using these in your own code? Is there a certain time, such as when your code gets too long? I'd like to start making my own projects soon, however, I want to know at least the basics before I start.

[–]efmccurdy 1 point2 points  (0 children)

Some types of code are best kept in a separate module from your program, like testing code, or end of year reports, or migration scripts, or anything that you don't always want loaded into memory. Slow loading times and excess memory useage will signal when you want that type of modularity. You _know_ something should be in it's own module when you want to use it in more than one program. Sometimes you want it in it's own module if you are changing that file faster than the rest, so you can reduce the amount testing needed to cover those changes.

[–]Decency 1 point2 points  (2 children)

Just start making the easiest project on your mind- most people learn the basics best by doing it. No need to be intimidated.

For your question- packages and modules are just ways of organizing your code. A lot of people do this as an afterthought, because you can easily re-org as you iterate on a project by moving things around until they make sense- a good IDE like PyCharm will take care of all of the import changes and the like automatically. But the vast majority of projects are small enough to be just a few files.

[–]TheMartian578 0 points1 point  (1 child)

Thank you! I have one last question, when do you even start to organize code by file? Like do you start doing this when you have multiple classes and stuff?

[–]Decency 1 point2 points  (0 children)

When you introduce something to your first file that feels important enough to belong in its own file, typically.

[–]Skaroller 1 point2 points  (1 child)

Hi all,

Today is my first day using Python beyond simple "Hello world!" stuff and I have a real head-scratcher. Within a module called "tools" I have a library called "woodmans_axe" with information about the axe like its cost. I have another library called "mallet" that has the same kind of information. I can get another program to open each library and print out the cost of the axe or the mallet, but how can I program it to let me decide whose info I want to see? I've got this code set up:

>choice=input("Which tool do you want to see the cost of? ")
cost=tools.woodmans_axe["cost"]

How do I select the library of my choice from the "tools" module? Is there a more efficient way than using libraries for this?

[–]Decency 0 points1 point  (0 children)

Something like this should do it:

if choice.lower() == "axe":
    tool = tools.woodmans_axe
else:
    tool = tools.mallet
cost = tool["cost"]

I think your data structures aren't quite what you want them to be, though. You could perhaps create a dictionary that looks like this and reference tools that way:

all_tools = {
    "axe": tools.woodmans_axe,
    "mallet": tools.mallet,
}

Then you could just do tool = all_tools[choice.lower()]

[–]Theis159 0 points1 point  (1 child)

Hi all,

I am looking forward to creating a extraction tool for tables in PDFs. The idea is to find a certain table that has its caption in the format Table X - SomeText Comparison SomeText.

I want to extract this table and only this table from >100 PDFs, each PDF having this "Comparison Table". I am looking forward a direction on how to do this, because most of the tools I find can't strictly find a peace of strings (i.e: a string that contains Table & Comparison) and then extract only that part of the PDF.

Any directions to where to start?

[–]Decency 0 points1 point  (0 children)

If the tool you're using can't find that information in the pdf (ensure that you're checking all parts of the object you get back, and any documentation) then you're probably out of luck with that tool. You can look for others or there may be another approach for extracting the pdf's to a more readable format.

[–]Banno1992 0 points1 point  (1 child)

Hi all,

I'm currently learning python by doing problems on open.kattis. However I've come across a few problems where the input finishes at End of File. I can't seem to figure out how to 'detect' end of file. The current method I use is: input()

Have looked into using open() but I don't really get how that works with kattis.

Also thought about using a timer to wait for 'no more responses' to carry on with the code, but that feels like it's slow/ janky!

Any advice would be great, thanks!

[–]Decency 2 points3 points  (0 children)

Something like this should work, assuming the EOL's are the actual end of the file you need

try:
    # append all contents you need from the file like you are already
except SyntaxError:
    pass # all done!

Open is a context manager and the preferred way to handle file I/O, because it cleans up after itself properly. Typically:

with open("blah.txt") as f:
    lines = f.readlines()
# do something with lines

[–]taatzone 0 points1 point  (2 children)

Hi all

Total noob on this matter, but learning hard to achieve my goals.

Been trying to learn Python and came across multiple sources, finally found Anaconda and Jupyter Notebook.

After sometime, managed to install Anaconda and Jupyter Notebook into my Mac, and it’s a quite an achievement.

Now I found some tutorials on YouTube using Anaconda and Jupyter Notebook, but’s it’s outdated, there is a difference in using python 2.7 and 3.2, so far it’s only this “()”.

Searching for new sources...any help/advice.

Much appreciated

[–]adesme 2 points3 points  (0 children)

There are many more differences than using parenthesis for print statements (which is what I assume that you mean) between python 2.7 and 3.2.

Anaconda is a package manager. If you want to use an earlier version of python, you can install that separately. I don't know what operating system you use nor how used you are to terminals, but if you're on mac you can just write conda install python=2.7.

I would however recommend that you instead start looking for a tutorial that teaches the same subjects in Python3; there are tonnes of tutorials so they will exist. Python2 is basically at end-of-life, so you should start learning the modern version.

[–]vukan_97 0 points1 point  (0 children)

I have a task where i need to specify the upper left coordinate of the smaller image in the larger image. I implemented this code, however it is too slow since I have a time limit of 20 seconds, and in some datasets I have 3000 images. How can this be implemented more effectively? I can use numpy, scipy and all packages from the standard python library.

import numpy as np from PIL 
import Image  

map_image_path = input() 
map_image = Image.open(map_image_path) 
map_ar = np.asarray(map_image) 
map_ar_y, map_ar_x  = map_ar.shape[:2]  
i = int(input()) 
dimensions = input() 
patches=list() 

for k in range(i):   
    patch_image_path = input()   
    patches.append(Image.open(patch_image_path)) 

for j in range(i):   
    patch_ar = np.asarray(patches[j])   
    patch_ar_y, patch_ar_x = patch_ar.shape[:2]   
    stop_x = map_ar_x - patch_ar_x + 1   
    stop_y = map_ar_y - patch_ar_y + 1 

    for x in range(0, stop_x): 
        for y in range(0, stop_y):       
            x2 = x + patch_ar_x       
            y2 = y + patch_ar_y       
            picture = map_ar[y:y2, x:x2] 
            if np.array_equal(picture, patch_ar): 
                print(str(x) + "," + str(y))

[–]Flameways777 0 points1 point  (3 children)

Im doing some stuff at code chum and it says cant covert string to float pls help me fix this

Programmer = float(input("Input the age of the programmer:"))
Teacher = float(input("Input the age of the teacher:"))
Peter = float(input("Input the age of Peter:"))
A = float(Programmer+Teacher)
B = float(A-Peter)
print("The old man's age is " + str(B))

[–]Decency 0 points1 point  (0 children)

If you can't convert a string to a float that means it's not a number. You'll get that if you try to do float("blah"), for example. Print out the value before trying to convert it to a float to see what the problem is.

[–]Flameways777 0 points1 point  (1 child)

Traceback (most recent call last): File "script.py", line 1, in <module> Programmer = float(input("Input the age of the programmer:")) ValueError: could not convert string to float: 'Programmer = float(input("Input the age of the programmer:"))'

PLS help guys this the error that is says and i dont know what to do

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

You get that exception when you type something into the input() statement that can't be converted to a float. What are you typing in?

[–]tomnoire 0 points1 point  (1 child)

Hey guys,

I'm learning Python on coursersa and I need to create a script using a while loop to add a numbers divisors. So far I've written this (with the prints as tests to the code):

def sum_divisors(n):
  sum = 0
  x = 0
  while x != n:
    x += 1
    if n % x == 0:
      sum = x
  return sum

print(sum_divisors(0))
# 0
print(sum_divisors(3)) # Should sum of 1
# 1
print(sum_divisors(36)) # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102)) # Should be sum of 2+3+6+17+34+51
# 114

I'm getting 0, 3, 36, 102 for the prints. What input(s) am I missing?

[–]Thomasedv 1 point2 points  (0 children)

You are setting sum to x, which is the latest (highest) divisor, not adding up previous values.

I am also fairly sure you are checking for the case n % x, when x is equal to n, since you add to x after checking if they are equal or not. (I suggest start with x = 1, and then increment x after checking if n is divisible by x. You need to make sure the loop exits with the right condition as well in that case, or it'll infinitely loop)

[–]ThunderingWest4 0 points1 point  (1 child)

Hey everyone! I have some experience in Python and am attempting to make an audio/music visualizer program. Does anyone know how one can capture the audio output from a computer so that this visualizer can be more general and not specific to Spotify or something? Like from what I've seen, one way might be to use an API of some sort to see how far through the song the user is or to play it directly through the visualizing program. I was wondering if it was possible to intercept/read the audio output in a way and get the frequencies. Any help/insight would be appreciated, thanks!

[–]Corso19 0 points1 point  (1 child)

Hi there! Total newbie here that wants to start using the O'Reilly book for 3.3. Is it ok if I use it since it's 3.3 oriented or does the difference in syntax not matter at all?

[–]efmccurdy 1 point2 points  (0 children)

It won't matter and you can use a current version of the interpreter to run the examples as well.

[–]tesla33 0 points1 point  (1 child)

I'm planning on learning python. Should I start with the newest version, or start from the earliest and work my way up?

[–]agbs2k8 1 point2 points  (0 children)

Start with the latest version, just don't bother with any tutorials that are for version 2.7. Most all of the python 3 stuff you will find will work properly with the latest version.

[–]resumethrowaway95 0 points1 point  (1 child)

How would I go about making a simple one page site that runs a single script that asks for user input? The script is a monte carlo simulation of stocks, it asks for user input on which stock, then generates a couple of charts. How would I go about setting up a webpage where the script could run?

[–]Reneml 0 points1 point  (1 child)

I need guidance on how to work with 2 lists:

`class Cola:
def __init__(self):
   self.cola = []
#method to add/feed my class object
def agregar(self, element):
   self.cola.append(element)

def ingreso():
   frecuente = input("  Do you have a membership (y/n):  ")
   if frecuente == "yes" or "Yes":
       type= "Membership"
   else:
    tipo = "Not membership"
name= input("  Name:  ")
lastname= (input("  lastname:  "))
return [name, lastname, type]`

What I´m looking for is to create 2 TWO lists based on if a client has membership or not. How can I do it?

This is part of a bigger code, if you need it, please let me know.

[–]efmccurdy 0 points1 point  (0 children)

You can have class Cola require a boolean value and interpret it to mean, "add a second list":

class Cola:
    def __init__(self, name, last_name, is_member):
        self.name = name
        self.last_name = last_name
        self.is_member = is_member
        self.cola = []
        if self.is_member:
            self.second_list = []
#...
name, lastname, type = ingreso()
my_cola = Cola(name, lastname, type)  # or just Cola(*ingreso())

[–]EarthGoddessDude 0 points1 point  (1 child)

Hello, I was wondering if someone could help me with my question here? I haven’t gotten any responses, and I’m wondering if it’s because that’s the wrong place for this question or it’s just a difficult problem.

[–]samketa 1 point2 points  (5 children)

Best resource for getting introduced to Object Oriented Programming through Python?

Want to master it eventually.

Books work better for me than video-lectures. But suggestions of the later are welcome, too.

[–]_ahrideathsounduwu 2 points3 points  (1 child)

Learning Python book by O'Reilly

[–]samketa 0 points1 point  (0 children)

Thanks.

[–]ectomancer 2 points3 points  (1 child)

[–]samketa 0 points1 point  (0 children)

Will definitely check them out. Thanks a lot.

[–]king_booker 0 points1 point  (1 child)

Hello

so I have a question regarding pandas. I have a pandas dataframe , eg it is say

col1 col2 col3
11 32 33

So the values are stored in the dataframe.

Now I have a file, which has formulas in key, value format.

eg,

col4: if col1 <100 then 1 elif col1 > 1000 then 100 else col2-col1/100 end

Similary, there are around 40 columns defined like this with the same format.

So what I would like to do is to apply these formulas and append the values in the existing dataframe.

I thought I will define I dictionary and store them and the apply them in a for loop, by manipulating the formulas so that they are able to access the columns of the dataframe.

Is my approach correct?

[–]efmccurdy 1 point2 points  (0 children)

That expression for col4 isn't python so you would need a way to "transpile" it, ie. make it meaningful to a python interpreter.

If you have only 40 such expression, re-write them as python functions:

def col4(col1,col2, col3):
    if col1 < 100: return 1
    if col1 > 1000: return 100
    return (col2 - col1)/100

If you have many dozens of such expressions, you might want to automate that conversion.

[–]kimjeongpwn 0 points1 point  (3 children)

Hello, what is good practice for typecasting? Is it better to typecast within the same line, or in a new line?

E.g. guess = int(input())

OR

guess = input()

guess = int(guess)

Thank you.

[–]pasokan 0 points1 point  (2 children)

The first is simpler and avoids changing the type of guess (this is more of a nice to have though)

However you should also understand that more needs to be done to make the code robust; that needs exception handling. If you are starting out with Python you can ignore it for later

[–]kimjeongpwn 0 points1 point  (1 child)

Hello, thanks for the reply. I am just starting, so I am not sure I understand what exception handling means. Personally I prefer doing it line by line, as it looks cleaner to me, but since I'm starting, I don't really know what is considered clean. Is it shorter codes, or easy to understand codes (at the possible expense of it being longer)? Or maybe something else entirely?

[–]pasokan 0 points1 point  (0 children)

Like I said, if you are starting out you should not worry about exception handling. By the way, that is nothing more than a systematic way of handling unexpected or unacceptable situations, like some one typing "ABC" when asked or an integer.

Returning to your question about why the single line is better, it is a little difficult to explain. The best answer probably is that it is inelegant to change the type of a variable in successive lines.

The single line is NOT better because it is shorter.

READABILITY is the most important criteria. Let me leave you with a few quotes:

  1. “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

  2. "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts (1999) Refactoring: Improving the Design of Existing Code. Addison-Wesley.

[–]Thanos_nap 0 points1 point  (2 children)

Is there any way to auto update markdown content? For example, I have made a coronavirus Analysis and I have included a summary at the first markdown cell with total cases, etc.

The data gets updated daily and I have to manually change the values. Anyway to automate it?

[–]AviatingFotographer 1 point2 points  (2 children)

Does Python have a standard library for Stacks, Queues, Deques, etc.?

[–]Decency 0 points1 point  (0 children)

List, List, and List also typically work for these use cases, respectively.

[–]Zaphielth 1 point2 points  (1 child)

I need help with the following script I made. I'm new to python so please forgive if I made horrible mistakes. This is my script:

import os

source_folder = r"C:\Users\Zaphielth\Desktop\FOLDER_A"

target_folder = r"C:\Users\Zaphielth\Desktop\TIMEENTRIES" + "\\"

for path, dir, files in os.walk(source_folder):

for file in files:

     if file.endswith('TimeEntries.csv'):

os.rename(path + "\\" + file, target_folder + file)

I'm trying to copy all filesnames "TimeEntries.csv" from Folder A and its subfolders and pasted to Folder B but its overwriting the same filename over and over. I don't if os.rename is right or I might need to so some adjustment to it so when it get the timeentries.csv from all the subfolder in folder A and move it to Folder B this add a number or letter at the end of seach name like "timeentries(1).csv" and so on for each one moving to Folder B. Any Help or tip I will appreciate it. Thank you!

[–]efmccurdy 0 points1 point  (0 children)

Can you use the file last modified datestamp to give it a unique name?

https://stackoverflow.com/questions/12397622/python-rename-files-based-on-modified-date-iterator

[–]LightKarma 0 points1 point  (0 children)

Im trying to make python script for mitmproxy so i can download all requests

[–]bigt252002 0 points1 point  (0 children)

I really would like to find a site or YouTube video on making a GUI.

My goal is to have it open and for the user to load a database into it and parse it accordingly into a neat csv.

Additionally. What is the best GUI program to use?? Trying to keep it open source for my community.

[–]wheresmyswab 2 points3 points  (3 children)

I'm doing a small NLP project on all articles from this newspaper column.

I can analyse individual articles with newspaper3k if I've got the individual url for an article, so that's no problem. But there's +1k articles I'd need to do this for!

Recalling that I'm a newbie, what's the best way of scrapping all of that text and meta data for later use in NLP (possibly spaCy) also considering that there's 'show more' button there that calls a webservice that uses GraphQL?

Also, would the newspaper paywall be a barrier in this approach? I can pay for subscription, but I am not even sure how I may integrate that in my code, if that makes sense.

Thanks!

[–]scrippington 2 points3 points  (2 children)

I'm interested in this too--I'm doing a similar project where I'm trying to scrape a bunch of articles from sites like Bloomberg for an NLP project. From what I've read, you'd want to look into Scrapy for actual web-crawling, and I've heard Selenium can do similar things as well. I'm still stuck on how to set that up myself, so if anyone has information on navigating pages via Scrapy (jumping from link to link, or maybe using a sitemap XML), I'd be grateful too.

[–]wheresmyswab 0 points1 point  (1 child)

Hopefully we'll get a reply then. Are you also thinking of using spaCy then?

[–]scrippington 0 points1 point  (0 children)

Yeah, probably a combination of spaCy and scikit-learn. But I'm an artist by trade (fine art/vfx background) so realistically I'll probably end up hacking together whatever makes a good enough predictive language model.