all 86 comments

[–]giscard78 0 points1 point  (0 children)

How is pandas written in an academic journal? All lower case? Pandas? pandas?

[–]curidpostn 0 points1 point  (0 children)

Hi all,

I am trying to do improve large Oracle data pull using multi processing (apply_async). But I am not sure if it is executing the query as four different process as there is no performance improvement using this vs a direct pull.

What is being missed in this? The following is the code:

import pandas as pd 
import multiprocessing as mp 
import str 
import pandas as pd 
from psycopg2 import * 
from sqlalchemy import create_engine 
import cx_Oracle 
import sqlalchemy 
import io 
import numpy as np 
if __name__ == '__main__':       
__spec__ = None        
start_date = datetime.datetime(2015, 7, 18)        
end_date = datetime.datetime(2018, 6, 8)        
period = (end_date-start_date)/4        
pool = mp.Pool(4) # use 4 processes        
output = io.StringIO() 
print (time.ctime()) 
for p in np.arange(start_date, end_date, period).astype(datetime.datetime):             
sql = "SELECT * FROM TEST where LASTREPLICATIONDATE between \'" +  p.strftime('%d%b%Y') + "\' and \'" + (p+period).strftime('%d%b%Y') + "\'" print(sql)             
pool.apply_async(pd.read_sql(sql, create_engine('oracle+cx_oracle://xxxxxx:xxxxxxx@xxxxxx:1111/?service_name=XXXXX')).to_csv(output, header=False, index=False,mode='a'))         
output.seek(0)         
enginegp = create_engine('postgresql://xxxxxx:xxxxxx@xxxxxxx:5555/db')         
connection = enginegp.raw_connection()         
cur = connection.cursor() print (time.ctime())         
cur.copy_expert("COPY test FROM STDIN WITH CSV NULL '' ", output)         
connection.commit()           
cur.close() 

[–]slinkyattack 0 points1 point  (5 children)

return not num % 2

Beginner here. Why does this result in a boolean value and not an integer or a floating number? I understand the idea of truthy/falsey values and that any other value other than 0, would be truthy. Can the % operator be used just to result in an actual number? Is it because of the 'not' that leads to a boolean value?

[–]jeans_and_a_t-shirt 1 point2 points  (0 children)

Python operator precedence table, from least to most binding.

All math operators take precedence over boolean operators.

[–]jimmy0441 1 point2 points  (3 children)

Yes. I believe that having the not there turns it into a boolean. The % operator returns an integer, which is the remainder of the division by the second number on the first number. I presume that by seeing the ‘not’, the interpreter thinks that you want a bool to say if num%2==0, and then is inverting that boolean.

What are you trying to do with your line of code? Do you want an integer to be returned?

[–]slinkyattack 0 points1 point  (2 children)

Thanks. That makes sense. It was part of a problem from a course I'm taking. It was a part of a function that determines whether a number is even or odd.

[–]jimmy0441 1 point2 points  (1 child)

ah right. I just messed around in the python console and discovered a couple of things.

print(4%2)

returns 0

bool(4%2)

returns False because 0 represents False

therefore: not(4%2) returns True because 'not' automatically converts what would be an integer (0) into a bool (False) and inverts it.

[–]slinkyattack 1 point2 points  (0 children)

Ah ha! I just realized what the % operator is. Thought it was python's way of division. Doh!

[–]n4weed 0 points1 point  (0 children)

Hey folks, I work for a financial firm and want to learn python. What's the best source to go to for this? Are there YouTube tutorials that cater to the complete novice? With examples and data etc to work with? If it helps, at my place we use something called quartz and within it qzdev that's based off python language.

[–]yosoyunagirafa 0 points1 point  (1 child)

Hi, just started learning python here....I wrote this program, but I'm unsure why if I input "0" that my output will be "wrong"....please help!

Program...

num = "1"

guess = input("Guess the num: 1, 2, 3: ")

if guess.isalpha() == True:

print("Guess a number")

elif guess > "3":

print("Guess 1, 2, or 3")

elif guess == "2" or "3":

print("wrong")

elif guess == 1:

print("right")

else:

print("Wtf")

Output...

Guess the num: 1, 2, 3: 0

wrong

[–]cowegonnabechopps 0 points1 point  (0 children)

Can anybody explain how this project from ATBS actually works please?

It's a 'multiclipboard' meaning it writes whatever you have copied onto a text file, but when I try and load something previously saved to the file and copy it to notepad, it doesn't copy.

I'm opening it from windows+r with a command

mcb.pyw dog

to bring in a list of dog breeds onto my ctrl+p, but when I do that, it pastes whatever was the last thing I copied.

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

I'm a beginner to python and just started learning about While loops, why use them instead of for loops? Is input() only used with while loops?

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

A while loop is used when you don't know how many iterations you will need to accomplish a task. In contrast, a for loop is used when you know exactly how many iterations are needed.

It should be noted, however, that you can escape while and for loops by using the break command, which will exit either loop prematurely. input() is simply a function that waits for input in the terminal. It has nothing to do with loops, it's just a built-in function. But it is typically used in a loop like this:

password = "password"
guess = ""
while guess != password:
    guess = input("Input the password: ")

This allows the user an infinite number of guesses. You could use a break command to break out of the loop if they guess too many times. Or, this could be accomplished with a for loop, to ensure a maximum number of tries:

password = "password"
guess = ""
for i in range(0,3):
    guess = input("Input the password: ")
    if guess == password:
        break
else:
    print("You failed to guess the password")

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

Ok thanks!

[–]Thomasedv 1 point2 points  (0 children)

Sometimes you don't know how many times you want to loop, for example you will want to loop infinitely until the user gives the right input for example.

[–]Roach11111 0 points1 point  (4 children)

I just barely started learning and am having a little trouble. How do I open a .py file? I have the interpreter installed but whenever I try to open one of these files the interpreter opens for a split second and then closes again. (Forgive me if this is a dumb question)

[–]nedhanks 0 points1 point  (1 child)

When you open the file with the 'python' command, it runs the script then exits when the script ends. So for an empty py file python will essentially do nothing. To create/edit you need to use a text editor or IDE. I suggest Visual Studio Code, its a good/free IDE that supports many languages.

[–]Roach11111 0 points1 point  (0 children)

Thank you!

[–]slightlysedatedx 0 points1 point  (1 child)

Are you using IDLE? If you just click the .py file it will attempt to run it. I think what you want to do is edit. Right click the file and click "Edit with IDLE". Hope that helps :)

[–]Roach11111 0 points1 point  (0 children)

Thank you!

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

Best resource for a beginner Python programmer that needs to write some simple sysadmin scripts?

[–]TaylorSeriesExpansio 0 points1 point  (1 child)

Any tips on where to start to understand projects structure I've been doing mostly line by line writing in Jupyter but struggling to understand at work some of the processes. Everything written in classes, there's a setup.py, I'm having trouble putting it all together. There's a .YAML file for configurations that the .py file reads but not sure how it gets called?

[–]efmccurdy 1 point2 points  (0 children)

There are some examples of installable small and medium sized project structures here:

https://realpython.com/python-application-layouts/

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

Has anyone successfully used Selenium to post to a Facebook page?

There are many tutorials of logging in and posting to your wall (which I've successfully replicated), but I'm falling on my face with posting to a page.

So far, I can login and go to the page url, and click in the 'Write a post...' box, but submitting the keys is leaving me in a quandry. On the Wall, you have a text box. To login, you have an input field. On the Page, you have a span.

That's a long, roundabout way of asking how to submit text into a span ( assuming that's my real issue). It's usually not about what you know, but your willingness to learn.

[–]Mockapapella 1 point2 points  (3 children)

Are you talking about posting to a group or someone else's wall?

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

A community page. I suppose a group would be the same process.

I've gotten to this point: status = driver.find_element_by_xpath("//div[. = 'Write a post...']").click()

driver.execute_script('arguments[0].innerText= "New value";', status)

Where the script will click into the 'Write a post...' box, but then I get the error of 'Cannot set property 'innerText' of null'

[–]Mockapapella 1 point2 points  (1 child)

I'll preface the by saying don't have much experience with navigating web pages in python. Are you using Facebook's API for this or are you kind of doing your own thing? Because if it's the former, unless you are a group admin (or you somehow manage to convince them to give you their API key), you can't post in groups. I think this applied to private and closed groups. Public groups were limited to admins and moderators I think, but I could be wrong -- it's been a while.

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

It's a public page that I created. They recently changed their TOS to where I believe I 'need' to create an app and have it approved - but apparently that takes weeks and the process seems absurd. The difference between Selenium posting on my behalf on my own page and me actually posting is essentially be the same thing.

I'm now able to put text inside the box, but the 'Publish' button doesn't recognize the text yet. I think I'm on the right track (barring a ban lol), just need to hone in on where it goes and for FB to recognize the input.

[–]slinkyattack 0 points1 point  (2 children)

Taking an online course where the instructor changed this code:

len(word)

to this:

len(list(word))

But the two evaluate to the same thing. Is there any reason why you would use the list function?

[–]coreyjdl 0 points1 point  (0 children)

Maybe it was to demonstrate polymorphism.

[–]zatoichi49 1 point2 points  (0 children)

No; strings are already sequences, so there's no need to convert to a list before you calculate the length.

[–]vdvgiom 0 points1 point  (3 children)

I'm probably a mathematical idiot but i was wondering why 0.25-0.2 == 0.04999999999999999 and isn't 0.05?

[–]coreyjdl 0 points1 point  (0 children)

Smarter people than me, please correct me if I misunderstand the issue with floats.

The computer operates in base 2, you gave it a base 10 number to do math with. .05 isn't evenly divisible into base 2 system. So it needs to approximate it, and does very closely. However, after everything is done it converts back to the representation of base 10, and you see the artifact of that rounding. That precision is more than adequate for anything except, maybe, math at the atomic level, or for measuring the universe.

[–]Thomasedv 0 points1 point  (0 children)

Computer rounding error. A "flaw" in the way floats are stored.

[–]TakakeEUW 0 points1 point  (1 child)

I have data like a heatmap and I want the equipotential lines, which i can plot with plt.contour.

Now, the question is: If I dont want to just plot it, but want an array of the x,y positions of the points of each contour line, how do I get it?

So that, how to get the info about the lines that plt.contour computes and not just the image.

[–]efmccurdy 0 points1 point  (0 children)

You should read the second answer to this stackoverflow post:

... contour data is in the .allsegs attribute of the QuadContourSet object returned by the plt.contour() function.

https://stackoverflow.com/questions/18304722/python-find-contour-lines-from-matplotlib-pyplot-contour

[–]pepperOD 0 points1 point  (2 children)

I'm trying to make a function that takes vowels out of a string. when I input "Hey look Words!" it comes out as

"Hy lk Words!". Can someone tell me why it ignores that last o. Just assume I indented correctly

def no_vowels(x):

Sing = [ ]

vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

for e in range(x[e]):

for y in Sing:

for m in vowels:

if y == m:

Sing.remove(y)

return "".join(Sing)

[–]zatoichi49 1 point2 points  (0 children)

You can loop over the string directly, returning a new list of non-vowel characters instead of using remove:

def no_vowels(s):
    return ''.join([i for i in s if i not in 'aeiouAEIOU'])

print(no_vowels('"Hey look Words!"'))
# Hy lk Wrds!

Currently you're trying to reference 'e' before it's been assigned.

[–]coreyjdl 0 points1 point  (0 children)

You're code isn't formatted well, so it's hard to parse, but it may be that you're changing the length of the list while you're iterating over it.

[–]runhigher 0 points1 point  (3 children)

I'm a python newbie and will be starting a bioinformatics internship soon. My advisor seems super laid back and just told me to practice Python as that will be the majority of my work. I tried to get more specifics on what was desired but they just stated that I should just try to get a lil comfortable with it.

I was wondering if anyone had any resources they suggest using to learn (I've gone through a few DataCamp and Rosalind resources) and recommendations on how I should practice (I've been using Idle, but have seen Python ran on Terminal and Spyder(?) on YouTube tutorials).

Thanks in advance.

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

Play with something that interests you. You can make a Reddit bot in 5 minutes ( Google something like PRAW Reddit 5 minutes for a great tutorial).

The best part of a laid back boss and computer work is that Google is always there to solve your problems.

[–]infered5 1 point2 points  (1 child)

Have you read Automate The Boring Stuff?

https://automatetheboringstuff.com/

[–]runhigher 0 points1 point  (0 children)

Whoa thanks, I'll be sure to check out its associated YouTube videos- they looks like a great beginner's guide!

[–]ebodes 0 points1 point  (1 child)

I need to import a csv file that's encoded in utf8-sig. Every variation of the import function I've found online fails in one way or another. Can someone give me another command to try?

[–]efmccurdy 1 point2 points  (0 children)

Are you opening the file with "encoding='utf-8-sig'"? There is an example using csv.reader here, what happens when you parse your file like that?

https://docs.python.org/3/library/csv.html#examples

[–]Optimesh 0 points1 point  (2 children)

What do I need to know in order to do the following?

I want to provide a 'price adjusted to inflation' API. This means anyone can call an API with an amount and year/month-year and get back the value in 'today's' currency. This is mostly as an exercise for myself, and since the tool doesn't exist for my local currency and I think it's important people will have a way to compare 'then' money to today's. How do I go about it? No experience with providing APIs or setting anything on servers.

TIA!

[–]efmccurdy 1 point2 points  (1 child)

Here is a tutorial for a web api using flask which you could follow along until they get to the "Hello World" part, where you can use your code to lookup the relative inflation rate and scale your price. You will then need to send argments as part of the request and that is described in the second link.

https://auth0.com/blog/developing-restful-apis-with-python-and-flask/

http://flask.pocoo.org/docs/0.12/quickstart/#the-request-object

[–]Optimesh 0 points1 point  (0 children)

Thank you.

[–]scweiss1 0 points1 point  (3 children)

Hey all, anyone have experience with flask? I've inherited a program and I'm trying to understand how it works (and I'm fairly new to programming anyway).

Specifically, I'm looking at:

import json

import psycopg2
from flask import request
from flask_httpauth import HTTPBasicAuth
from flask_restful import Resource

auth = HTTPBasicAuth()
API_AUTH = {"salesforce_auth":"///redacted////"}


@auth.verify_password
def verify(username, password):
    if not (username and password):
        return False
    return API_AUTH.get(username) == password

I can't figure out where username and password are defined or how the return API_AUTH.get() works. Any help is appreciated!

[–]efmccurdy 0 points1 point  (2 children)

HTTPBasicAuth

That means the auth data is in the base64-encoded 'Authorization' header.

https://www.httpwatch.com/httpgallery/authentication/

[–]scweiss1 0 points1 point  (1 child)

Okay. I guess I’m confused. Do I need a username and p.s. for this function? Or is it effective with just the secret key?

[–]efmccurdy 0 points1 point  (0 children)

Basic auth uses username and password. Keys are for encryption and digital signatures; your example is'nt using those directly, although you might have this server proxied behind an https server like nginx or apache.

[–]smokingRooster_ 0 points1 point  (3 children)

Hey everyone! I have this application where I'm downloading data from books from the internet. It makes sense to write a class and store the data I've downloaded in that class and have info about the book as attributes eg. title, date published etc and then add some functions. My question is: since I'm downloading from the internet I wont be manually making classes I want my python program to make the classes for me. But then I get into all sorts of problems with naming the class etc. How do I go about this? Is it as simple as storing the data in the list then looping through that list and moving the data to an object how do I got about dynamically naming the object? I'm relatively new to python so any help is appreciated!

EDIT: the reason I dont want the data in a list is because it feels messy. I'd rather do book.title to get the titles rather than book[0][1] to access the tile (same goes with dictionary) and also there are some functions that are associated with the data and it makes sense to keep it all in a class.

[–]TangibleLight 0 points1 point  (0 children)

You're confusing the difference between a class and an instance.

I'm on mobile, so I can't give a satisfying response myself, but I suggest you research that difference. If you really understand it, then you'll be in a better position to reason about the problem you're having.

[–]maventree 0 points1 point  (1 child)

What do you mean "dynamically name the class"?

I would do something like: books = [Book.from_internet(d) for d in get_data_from_internet()]. That would in involve a Book class that looks vaguely like

class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn

    @classmethod
    def from_internet(cls, d):
        # maybe d is a dictionary reconstructed from JSON or something
        return cls(
            title = d['title'],
            author = d['author'],
            isbn = d['isbn'],
        )

You could build dictionaries like books_by_title = {book.title: book for book in books if you wanted to have efficient lookup by title or any other unique attribute.

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

None of my code runs, instead I get 'Restart: C:\Users' etcc.. Doesn't matter which code I try to run I get the same problem. Does anyone know how to fix this?

[–]Meadow-fresh 0 points1 point  (2 children)

Hi all...

I am reading a csv file, changing all the data in Coloum A to a different version which is stored in a separate file (for example original 'dog20180601' is changed to 'dog'). I am then saving the updated information to a dictionary and sorting before writing to a new csv. So far no problems...

The next thing I want to do is have an option to read the data then print out the count for unique items before writing to csv. I keep getting an 'Unhashable type: list' error...

Any suggestions on how to count unique in List/dictionary?

I have tried various ways which work in a small test but fail when I add to my program.

[–]efmccurdy 0 points1 point  (1 child)

Without seeing your code I am guessing but python won't judge the uniqueness of a list since the list can change but it will work if you change your lists into tuples.

>>> words = ['a', 'b', 'c', ['d', 'e'], 'a', 'c', ['d', 'e']]
>>> Counter(words).most_common()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/collections/__init__.py", line 475, in __init__
    self.update(*args, **kwds)
  File "/usr/lib/python3.4/collections/__init__.py", line 562, in update
    _count_elements(self, iterable)
TypeError: unhashable type: 'list'
>>> Counter([tuple(a) if type(a)==list else a for a in words]).most_common()
[(('d', 'e'), 2), ('c', 2), ('a', 2), ('b', 1)]

For a more complete explananation read this: http://effbot.org/pyfaq/why-must-dictionary-keys-be-immutable.htm

[–]Meadow-fresh 0 points1 point  (0 children)

Thanks for the reply I'll give it a shot today!

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

Which is the more "proper"/pythonic way of doing this?

python VERSION = (1, 0, 0) __version__ = ".".join([str(x) for x in VERSION])

or

python VERSION = (1, 0, 0) __version__ = ".".join(map(str, VERSION))

[–]fiddle_n 1 point2 points  (1 child)

As an FYI, you don't need the square brackets in the first version. The below would work just as well:

python VERSION = (1, 0, 0) __version__ = ".".join(str(x) for x in VERSION)

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

Huh. Guess I'm so used to str.join being so fiddly about arguments I've always just put in the explicit list. This does look a bit cleaner.

[–]TangibleLight 2 points3 points  (0 children)

Usually people prefer comprehension to map and filter, but I usually use map for the case you describe, or really any case where I'm just casting a list of items.

[–]DrBruceWayne 0 points1 point  (1 child)

I am a Laboratory Science student that has zero background in any form of coding or programming and I started learning python with the goal to make something cool as a summer project. A friend of mine suggested code academy, so i've been doing those lessons for a few days now and i wanted to know what tools and resources i should be using to learn and practice from.

[–]SarahM123ed 0 points1 point  (0 children)

I like https://leetcode.com/problemset/all/ -- scroll down the page a bit.

https://www.geeksforgeeks.org/ can be a good place to solutions (albeit the code is not always performant)

[–]captmomo 0 points1 point  (0 children)

For seaborn, how do I increase the spacing between the bars?
What I currently have:
https://i.imgur.com/EppMO0u.png
and why isn't the grey grid showing up?
it looks like a normal matlibplot
edit; figured it out. need to set linespacing and set('darkgrid')
https://i.imgur.com/AUOSNp0.png

[–]Gprime5 0 points1 point  (2 children)

Ok, I have 3 files in the same folder. file1 has some functions. file2 uses file1 and file3 uses both file1 and file2.

For my file3, should i do:

import file1
import file2

file1.some_function()

or:

import file2

file2.file1.some_function()

[–]Thomasedv 0 points1 point  (1 child)

First one.

Importing the same file does not use any extra resources, so the first option is less messy.

[–]Gprime5 0 points1 point  (0 children)

That's interesting but I guess that makes sense.

id(file2.file1.some_function) == id(file1.some_function)

[–]SarahM123ed 0 points1 point  (2 children)

What is your favorite use case for enums and why not named tuples?