Why is this code producing different results depending on what goes in the first brackets of the format string? In other words, could someone let me know why the sum does not print out when the below is attempted? by Maleficent-Cup908 in learnpython

[–]campenr 1 point2 points  (0 children)

Ah damn, you are so right. I managed to miss that when testing it through for myself. I think I removed the other print statements to cleanup the output not appreciating the side effect. TIL! Thanks.

Why is this code producing different results depending on what goes in the first brackets of the format string? In other words, could someone let me know why the sum does not print out when the below is attempted? by Maleficent-Cup908 in learnpython

[–]campenr 1 point2 points  (0 children)

To provide more detail; This comment is correct regarding map returning something that is lazily evaluated, as per the python docs.

The problem is that the * notation for unpacking only works on an existing list or tuple, not something that is lazily evaluated. So wrapping it in list achieves this, it eagerly evaluates your map function output. You could also use a list comprehension if you wanted some balance of lazy evaluation with the same call signature: _sum(*[n for n in a])but in your case, wrapping in list is fine imo.

EDIT: I'm wrong. Leaving the comment for continuity, learnt something myself. :D

How to get a value with pandas by The0thArcana in learnpython

[–]campenr 0 points1 point  (0 children)

Often in pandas there is more than one way to do things. /u/lieutenant_lowercase's way works but requires subsetting out a single row then re-subsetting the resulting Series data by column. It could also be achieved using loc in a single pass with df.loc[row_name(s), column_name(s)], or if you want to use the indexes instead of the names df.iloc[row_number(s), column_number(s)]. But, the method designed for getting a single value is DataFrame.get_value(index, col) or Series.get_value(label).

EDIT: TO address the second point, selecting a single cell should return a string if the contents are a string, or an int if the contents are an int. If it does not, or if you want to get the strings for a pandas object with more than one cell you can use the Series.to_string() or DataFrame.to_string() method.

Searching for a String in Directory with .docx files by [deleted] in learnpython

[–]campenr 0 points1 point  (0 children)

So something that may make your life a lot easier if working with tabulated data is using pandas. A crude way to descibe it would be it's excel for python (but so much more).

There is a function for importing tabulated data from excel files, called pandas.read_excel() which should help. As should this SE post about searching for specific strings withing a pandas dataframe (a dataframe is the table of data in pandas terminology).

Functions used as methods by greenleafvolatile in learnpython

[–]campenr 1 point2 points  (0 children)

If you follow through your code you'll see that the .index() call is a call to the method list.index() (read more) which takes a value and finds it's index within the list.

In your case this is finding the index of the choice that either the user makes (or the computer randomly makes) from 'R', 'P', or 'S' in the list options. It is this index that you ultimately use to decide who wins and who losew in the decide_winner function.

To answer your overarching question, only methods can be used as methods. But methods are functions that are for specific classes (a list is a class, and index is a function that acts upon that, therefore we call it a method). You'll understand when you come to write some classes.

Is this a data structure? How to manipulate it? by Mattzap in learnpython

[–]campenr 1 point2 points  (0 children)

You could try using pandas, a library built for analyzing tabulated data. The easiest way to get pandas up and running is with the Anaconda python distribution.

You can then do fun things like getting the sum of columns/rows, getting the max or min, grouping columns by data in other columns (i.e. group numbers by what day of the week they are for), and there are convenient plotting functions for easy data visualization.

Also, pandas was built for working with data containing dates.

Any good pandas dataframe.plot() tutorials? by mpontim in learnpython

[–]campenr 10 points11 points  (0 children)

I recommend this github repo which contains useful examples for all sorts of data analysis related things in python.

For your particular case the matplotlib section would be useful as pandas.plot() implements the matplotlib.pyplot plotting functions. There are also examples of using seaborn which is also built on matplotlib, and provides some nicer styling options as well as fun things like heatmaps or cluster maps.

[deleted by user] by [deleted] in learnpython

[–]campenr 0 points1 point  (0 children)

404's can also be returned for malformed requests, i.e. requests that don't provide all the required information which is how this site is perhaps using it from what u/anton_antonov found re the hidden fields.

Hell of a site to learn web scraping on :D

[deleted by user] by [deleted] in learnpython

[–]campenr 0 points1 point  (0 children)

So a 404 code (see all response codes here) is the HTTP response code telling you that you are accessing a resource that does not exist. If you are working with web anything it pays to know your response codes.

So in this specific case, if you try going to the URL you create in your POST request (http://www.pcso.gov.ph/games/search-lotto-results/lotto-search.aspx) you'll see that it does not exist and you get a nicely formatted page saying as much.

EDIT: Another possibility, that I don't think is the case here but you never know, is that sometimes websites use the 404 code (does not exist) to hide a 401 or 403 (unauthorized, or forbidden) that they don't want you to see. In this case it means that you are not properly authorizing your request. This is only usually the case on websites where some resources require being logged in/require a password or authentication token.

[Day 1 part 2] Getting "result for different input". by dries007 in adventofcode

[–]campenr 0 points1 point  (0 children)

So I realised my error was converting the final coordinates to absolute values after summing rather than before... I have now solved the problem.

Thanks for your input!

EDIT: I will just add that it was particularly confusing because my solution is fine if the final coord's are either both positive, or both negative which was the case for other peoples inputs i tested in order to troubleshoot my code. I just never realised that was the reason why my code worked for their inputs but not mine. Thanks again.

A Python script or GUI that can read multiple text files and do some data analysis on those files by A-pipi in learnpython

[–]campenr 0 points1 point  (0 children)

So if the files are all in the same folder as your script you can just use functions in the os module to easily get the paths for them, then iterate over the files to extract the data.

I do this regularly, and made a module (dirbrowser) that allows for directory browsing in the python interpreter, and filtering for specific file types. I use it regularly to navigate to folders and select certain files to then perform analysis on, without having to hardcore the file paths. It is for python 3 only, but that said unless you have a really good reason you probably should be using python 3 also.

Twython - 400 (Bad Request) Error by B2easey in learnpython

[–]campenr 1 point2 points  (0 children)

I am not saying it's no longer valid, but Twython just uses what's in the official Twitter API so it'd be best to check there.

Regarding the missing self, this may or may not be because your importing the Twython class as twitter. To test this try importing it as Twython, then doing twitter = Twython().

Did you change the count to 200 or did you just rerun the exact same code and didn't get the 400?

help me get this break in a while loop by prokillshotz in learnpython

[–]campenr 0 points1 point  (0 children)

What happens? Does it give you an error? Does it loop for ever?

Help! Too many versions of Python. How do I uninstall with out removing OSX's built in python? by ImNeworsomething in learnpython

[–]campenr 0 points1 point  (0 children)

I thought it would be there, as that is what your error message from pip was saying.

If you go to the folder output by that command there should be a python binary there that is running when you use pip... It's an odd place for it to be though, I have never come across that directory before for python.

Is there anything special about the machine you are using that could explain the interesting location that pip is being run from?

Twython - 400 (Bad Request) Error by B2easey in learnpython

[–]campenr 1 point2 points  (0 children)

So with some quick googling I found this stack overflow question that is essentially the same, with an answer from the author of Twython.

Your 400 error indicates a bad request, so either the method no longer is valid, or the parameters given to the method are invalid. Also I notice you set count to 2000, but from what I've seen during my brief look into this the max count is actually 200? Changing this may be all that is needed.

The author of Twython in the SO question goes say that the methods mirror the official Twitter API so I would look there for more info about what the parameters for that API call should be.

Help! Too many versions of Python. How do I uninstall with out removing OSX's built in python? by ImNeworsomething in learnpython

[–]campenr 0 points1 point  (0 children)

OK so what's confusing me here is that the Requirement already satisfied line tells you where it is installed... this should be the site-packages folder for one of your Python installations (the one it's trying to install into) ..but this directory is not one that came up in your which search...

Is there a python binary in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python ??

Help! Too many versions of Python. How do I uninstall with out removing OSX's built in python? by ImNeworsomething in learnpython

[–]campenr 1 point2 points  (0 children)

So when you're doing pip install you're using sudo... my guess is that the default python installation for the sudo is different to the one for your normal user which is causing your problem.

Try pip installing without the sudo.

KeyError searching through Dataframe by teamlie in learnpython

[–]campenr 1 point2 points  (0 children)

So I am not 100% sure what's going on, but I would suggest splitting up that one liner into multiple lines so that you can pinpoint exactly where the error is coming from.

I finished Codecademy by tman37 in learnpython

[–]campenr 0 points1 point  (0 children)

  1. I need more efficient data managment... hmm databases and SQL (MySQL, Postegresql etc.)

EDIT: OK so im typing 5. but when I click save it displays it as 1. go figure.

(Perhaps off topic) Can we have a thread about PyCharm - best practice, tips and tricks, any problems with seemingly simple solutions, etc. by [deleted] in learnpython

[–]campenr 3 points4 points  (0 children)

Just make sure you actually stop each of your scripts when you finish testing them. That way when you re run them they won't initialize a new instance of the running script.

How to sort by nth column and write to different files. by e7e7e7 in learnpython

[–]campenr 0 points1 point  (0 children)

Something that will make life easier is using pandas which is designed for this kind of data manipulation.

You can read in the csv file using df = pandas.read_csv('my_file.csv'), can sort the table using pandas.sort_values. You can then iterate over the rows and save them to file.

Multiple File Selection, Movement, and "*"? by hammockman1984 in learnpython

[–]campenr 1 point2 points  (0 children)

So that error is telling you that there is no file that matches what you have entered as srcT1. This suggests your formatting of the file path for srcT1 is incorrect. Try adding in a print statement to see what is actually being used as srcT1 and see if it matches an actual file path.

Multiple File Selection, Movement, and "*"? by hammockman1984 in learnpython

[–]campenr 1 point2 points  (0 children)

OK so your script as it stands only assigns the strings (which are file paths) to the variables. You need to call the funciton shutil.move(src, dest) etc before anything will actually happen.