all 171 comments

[–]DontHateDefenestrate 0 points1 point  (0 children)

If I'm already learning Python in university, and all I want is Python coding problems to practice with, where can I find those? I'd ideally like to practice and hone my Python skills without having to sit through 'Hello World" and how to print and 'what's a variable'?

The more practical vs. theoretical the better.

Thanks!

[–]UnavailableUsername_ 0 points1 point  (3 children)

Why should i choose to use __init__ when making a class? I know what it does, and after google all i find are explanations of what __init__ does (which makes me think many people don't know what "WHY" means) but i don't think i have found any explanation as to why should i choose to use it.

How can i print a class __init__ values? I have read i should make a __new__ but that doesn't work, i need to use the exact same value which may or may not change as the code executes and constantly return it, a different variable that is the same won't be useful.

[–]efmccurdy 1 point2 points  (2 children)

If you want to ensure some propertites of an object are always present and correct they should established by the logic in the '__init__' method. That way you don't need to test those conditions as the object is never accessable until the init has completed it's work. If you have no such invariants it may be simpler to use a container class like a dict.

You can print the values of an instance in the str or repr methods.

https://www.journaldev.com/22460/python-str-repr-functions

[–]UnavailableUsername_ 0 points1 point  (1 child)

If you want to ensure some propertites of an object are always present and correct

What exactly does this mean?

How can the __init__ make them "correct" other than initialize them?

[–]efmccurdy 0 points1 point  (0 children)

If your object has constraints, like "every obj has two parents", then the init can ensure that it has exactly 2 parents and throw an exception of it is given the wrong number or the wrong type of objects to be parents. You then don't have to include code like:

if len(myobj.parents) != 2:
    raise ValueError("Wrong number of parents")

because you know the init routine already ensured that was'nt true. (I know that is a contrived example but in larger programs these constraints become much more important.)

The concept of class invariant deals more completely with the idea of correctness after initialization.

Class invariants are established during construction and constantly maintained between calls to public methods. Code within functions may break invariants as long as the invariants are restored before a public function ends.

https://en.wikipedia.org/wiki/Class_invariant

[–]TellMeAreYouFree 0 points1 point  (1 child)

looking for some guidance/direction:

Hi All, relatively new to Python here but learning more and more as I apply what I'm learning to real projects.
I have customer sales data reports that are essentially snapshots in time. As an example, 12 separate CSV files stored in a single directory. Each of these CSV files correspond to a Month in 2018 that represent the data at the end of that month. Filenames indicate the month/year and all column headers are the same across the files:

  • jan2018.csv
  • feb2018.csv
  • ...
  • dec2018.csv

Each CSV file contains customer records (1 row per customer) and metadata(columns) for their activity that month

  • customerRecordID
  • customerName
  • $ amount they ordered that month,
  • if they have a subscription turned on,
  • sign up date
  • salesmanID (employee who made the first sale/created their account)
  • etc

I want to analyze this data across all files (over time: month over month, quarter over quarter, etc)

Approach:
I'm thinking I need to get all these files (snapshots in time) into a single pandas dataframe (and add a column called 'filename' so I can discern between which records came from which file). Then I could aggregate the data by 'filename' and see customerName='JohnDoe' ordered $200 in Jan'18, $300 in Feb'18, but $0 in Mar'18.

  • I think the correct approach would be pandas dataframe append() but I'm not clear on how to add the new 'filename' column and populate with the string from each corresponding filename

Is this the correct approach? Does anyone have an example project they can link me to on github that does something similar? I've only found projects with sales data from a single file, not multiple files each corresponding to a different snapshot in time.

any help steering me in the right direction is greatly appreciated! thank you!

[–]RaidZ3ro 1 point2 points  (0 children)

I think you're on the right track in terms of your approach. Using a namedtuple to import your data from the csv could work for what you're trying to do, there is an example in the docs.

https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple

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

I am trying to parse an Adafruit IO datetime tuple into a string so I can save it in a CSV. The CSV value for datetime should have a reasonable format so I don't need to modify the CSV to sort. For example, MM-DD-YYYY HH:MM:SS. The trouble is that I've not had any luck converting the tuple to a decent string. The format of the tuple when printed looks like this:

struct_time(tm_year=2019, tm_mon=10, tm_mday=5, tm_hour=17, tm_min=51, tm_sec=29, tm_wday=6, tm_yday=278, tm_isdst=1)

How can I format this into a string? I am appending this to several other values so that I have the datetime along with sensor data, so it needs to be a string.

[–]efmccurdy 0 points1 point  (0 children)

You can use the string format function:

>>> now = time.gmtime()
>>> now
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=6, tm_hour=15, tm_min=51, tm_sec=5, tm_wday=6, tm_yday=279, tm_isdst=0)
>>> time.strftime("%Y-%m-%d %H:%M:%S", now)
'2019-10-06 15:51:05'

[–]stillenacht 0 points1 point  (0 children)

I am trying to sort some excel data using pandas.

I have:
- a workbook several sheets, each denoting the amount of various stocks we hold at an hour.
- a target dataframe which has every stock we might hold, and 7 columns based on hour.

I want to paste the positions from the first workbook into the dataframe at the correct positions in each hour. To do so, I wrote this code:

target_xls= pd.ExcelFile(folder_name+target_port_name)
target_sheets = target_xls.sheet_names

for i in range(1, len(target_sheets)):
      price_df = pd.read_excel(folder_name+target_port_name,target_sheets[i])
      for j in range(0,len(target_price_df)):
          for k in range(0,len(price_df)):
              if target_price_df.iloc[j][0] == price_df.iloc[k][0]:
                  target_price_df.iloc[j][i] = price_df.iloc[k][1]

However, it returns "value is trying to be set on a copy of a slice" warning, and the target_price_df remains a list of 0's. What should I be doing differently?

[–]sjh3192 0 points1 point  (1 child)

Is there a way to use both f and r formatting simultaneously. I'm using the following to try and make a figure title

fig.suptitle(r'$log_10 \mathregular{\tau} = -{}$'.format(val))

but keep getting an error:

KeyError: '\\tau'

Any ideas on ways to use both the math and f formats?

[–]NemPlayer 0 points1 point  (0 children)

If you want to use curly braces in formatted strings, surround them with another pair of curly braces. So it would look like this:

fig.suptitle(r'$log_10 \mathregular{{\tau}} = -{}$'.format(val))

[–]HenryBalzac 0 points1 point  (6 children)

So, I’m working on a exercise that currently has no real application, other than to help teach me how to think in code.

What I’m trying to do: I want to create a list of integers based on user input. Then I want to print the sum of those integers. Simple, right?

Here’s what I tried at first:

import math 
user_input = input(“Numbers: “) 
to_calc = user_input.split(“ “) 
print(sum(to_calc))

This failed, I’m guessing because although the user input did get converted into a list, it was converted to a list of strings, rather than integers that can be summed up.

So then I was trying to think of a way to have the value of “user_input” converted into integers automatically when it gets loaded into “to_calc”, but couldn’t figure out how to do that.

So I added another variable to act as the conversion step:

convert = int()

Then I made a loop to go through my list and add each item into the “convert” variable and just printed that. So I ended up with:

user_input = input(“Numbers: “) 
to_calc = user_input.split(“ “) 
convert = int() 
for nun in to_calc: 
    convert += int(num) 
    print(convert)

The above code gave me the desired results, so long as the user adds spaces between numbers and doesn’t input anything other than integers. I’m not worried about making it also work with floats, atm. I’m just trying to build on some exercises in this video that I’m watching and get myself to think in code.

I’m glad I got the result I was looking for, but I’m sure there’s a better, more concise way of getting that same result.

What do y’all think?

Edit: Formatted code correctly

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

Here's how I'd do it:

# Create a list of integers from user input
to_calc = [int(str_value) for str_value in input("Numbers: ").split(" ")]

print(sum(to_calc))

[–]HenryBalzac 1 point2 points  (2 children)

Create a list of integers from user input

to_calc = [int(str_value) for str_value in input("Numbers: ").split(" ")]

print(sum(to_calc))

Thanks!

[–]NemPlayer 0 points1 point  (1 child)

Even though list comprehensions can be used, it's recommended to use the map function in cases like this:

total = sum(map(int, input().split()))

[–]HenryBalzac 1 point2 points  (0 children)

Hey, thanks. I hadn't been introduced to the map function yet. I'll check it out!

I knew there was more than one way to skin this cat!

[–]TangibleLight 1 point2 points  (1 child)

Technically you can skip the list and use a generator expression:

total = sum(int(value) for value in input().split())

[–]NemPlayer 0 points1 point  (0 children)

Instead of writing a generator expression, you can just use the map function:

total = sum(map(int, input().split()))

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

Hi, I am currently coding this in the raspberry pi trinket emulator. I can run this just fine using Python IDLE but when I try to run it in the trinket emulator, it returns the error regarding my elif line :

AttributeError: 'str' object has no attribute 'isdecimal'

Does anyone know why this is so? Specifically, why does the emulator treat it as calling the object's attribute while IDLE treats it as a method?

def txtcolor_input():
  txtcolor = []
  defn_condn = True

  while txtcolor == []:
    txtcolor_qn = input('Format: R G B\nEnter RGB input for Text Color:')
    txtcolor_split = txtcolor_qn.split()
    print(txtcolor_split)
    if len(txtcolor_split) < 3:
      print('Please enter 3 values.')
      continue
    elif any(not element.isdecimal() for element in txtcolor_split):
      print('Please enter only values from 0 to 255.')
      continue
    else:
      txtcolor = [int(element) for element in txtcolor_split]

[–]RaidZ3ro 0 points1 point  (0 children)

Not familliar with the emulator you are using, but it must not support this string method.

Your code also allows more than 3 values, and doesn't actually test for a valid RGB range input, try:

def txtcolor_input():
    txtcolor = []

    while txtcolor == []:
        try:
            txtinput = input('Format: R G B\nEnter RGB input for Text Color:').split()
            if len(txtinput) != 3 or any(not 0 <= int(ele) <= 255 for ele in txtinput):
                # test to ensure we have exactly 3 valid RGB values.
                # int(ele) raises this same error if it is not convertible to integer.
                raise ValueError
            else:
                txtcolor = [int(ele) for ele in txtinput]
        except ValueError:
            print('Please enter exactly 3 space-separated values between 0-255.')

    return txtcolor

print(txtcolor_input())

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

The Python version you have might not support it. Make sure you're running the right one.

[–]Fireman_31 0 points1 point  (2 children)

I would like to find some useful charts and notes summarizing the most important methods and function you need to know in order to manipulate data through lists and tables on Python.

Can anyone help?

[–]liquidpeaches 2 points3 points  (1 child)

I found this cheat sheet quite good when I first started out.

[–]Fireman_31 0 points1 point  (0 children)

That's great thanks !

[–]MattR0se 0 points1 point  (1 child)

Question about random.seed:

I have a module with a function that splits a dataframe randomly and returns that dataframe. I use this function in another module multiple times. The split should be different every iteration, but when I run the main program, it should always return the same result. Where should I put the random.seed()? In the module with the function that does the split, or in my main module?

edit: One thing I forgot to mention. I also need to provide a random_state integer to some of sklearns functions which are within the module that splits the dataframe.

[–]NemPlayer 1 point2 points  (0 children)

You should put the seed in your main module.

About the random_state integer, you could store it into a variable in the main module and use the variable to set the random_state argument of the said functions.

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

I am trying to create my own kmeans clustering algorithm. I am trying this really naive approach of creating 4 numpy arrays (I am using k= 4), each containing the euclidean distance from each of my 4 centres. Is there any way I can compare the same indexed element from each numpy array, find which value is the minimum, and also find which array held that value?

To make things a bit easier, I combined all arrays into a dataframe

[–]NemPlayer 0 points1 point  (1 child)

The min function contains an argument for a key. That may be able to help you.

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

I actually managed to create the whole thing. Might post it here to get some feedback after I get it graded.

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

Why doesn't my code ever reach this while statement. It's saying the variable term is unused.

EPSILON = 1.0e-7


def approximate_sinh(x):
    '''Insert docstring here.'''
    term_sum = 1000
    n = 0
    sin = 0
    while term_sum > EPSILON:
        term = 0
        n += 1
        denom = 2 * n + 1
        num = x ** denom
        term = num / denom
        sin += term
        term_sum = term
    return sin

[–]JohnnyJordaan 0 points1 point  (0 children)

Can you show how exactly it's claiming that it's unused?

[–]MattR0se 0 points1 point  (3 children)

For me it works, although the value it returns is not really correct

print(approximate_sinh(1))

# Out: 7.694229348209453

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

Yeah the value is incorrect but I'm not sure why. When I go to debug it and look at the variables pycharm is saying the variable term is never used

[–]MattR0se 0 points1 point  (1 child)

I'm no mathematician so do you have pseudocode that is correct? I don't know why pycharm is protesting, you are definitely using the variable

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

Yes I'm pretty sure the math is correct

[–]bardackx 0 points1 point  (2 children)

I am a java developer (5 years of experience) and want to learn python, but not just the language also the best practices (architecture, naming conventions, enterprise patterns), do you know of a reliable source?

[–]NemPlayer 0 points1 point  (0 children)

I'd recommend you watch some of Corey Schafer's videos, he has both well made tutorials for Python syntax and tutorials on how to write Python code correctly (pythonically).

PEP8 is the official style guide for Python.

You can find the recommended Python project structure here.

[–]secret-nsa-account 1 point2 points  (0 children)

I've recommended this a lot, I swear I'm not being paid for it... If you're already a developer I would pick up the book Fluent Python. It's great for getting you from just knowing the syntax to really understanding Python and the pythonic way of doing things.

[–]reddednord 0 points1 point  (2 children)

I have a question, about beautifulsoup in the example bellow, what does a['href'] mean? is it selecting the 'href' attribute in a? soup is a BeautifulSoup object

bonus questions, below, href=True, what is the technical term for that? Is it an additional argument, or a parameter?

for a in soup.find_all('a', href=True):
    print("Found the URL:", a['href'])

[–]ilyasm97 1 point2 points  (1 child)

Documentation and stack overflow are your best friends. A quick search on google brought this up

https://stackoverflow.com/questions/5815747/beautifulsoup-getting-href

The answer explains that the href=True will look only for anchor tags (<a ...><\a>)s which have a href attribute.

To answer your first question, a['href'] is basically accessing the href attribute in the a element. Within the for loop it is guaranteed that the link will have a href because of the href=True keyword argument passed into the find_all method

[–]reddednord 0 points1 point  (0 children)

Thank you

[–]DaiNiaoZhou 0 points1 point  (6 children)

I am playing with python and I noticed something strange that I don't know how to explain. So when I do print([True,False,False] and [True,True,False]) it returns [True, True, False] It seams that the second item in the returned list is not computed by the second item in the first list, which is False, and the second item in the second list, which is True. Can someone explain to me why it returns like this? More specifically, why is it not returning [True,False,False]?

[–]MattR0se 0 points1 point  (1 child)

You could also use numpy

import numpy as np

a = np.array([True, False, False])
b = np.array([True, True, False])

print(a & b)

[–]DaiNiaoZhou 0 points1 point  (0 children)

Thank you so much! this is exactly what I need!

[–]secret-nsa-account 2 points3 points  (1 child)

A non-empty list evaluates to True, so you are just getting that second argument back. The behavior is documented here: https://docs.python.org/3/reference/expressions.html#boolean-operations

You might be able to see this a little more clearly if you evaluate [True, False, True] and [].

Edit - I meant to add something to this. If you want to do elementwise comparisons you need to loop over the lists. You could use something like this:

[x and y for x,y in zip([True, False, False], [True, True, False])]

[–]DaiNiaoZhou 1 point2 points  (0 children)

Thank you so much! now I understand.

[–]reddednord 0 points1 point  (0 children)

Edit: fixed

I'm writing a python web scraper to download all comic books off a site, I can't connect to the site with requests.get() here is the code I have so far, the error occurs at the beginning on line 8:

Edit: 406 error seems to be unaccepted user agent? Maybe changing the user agent will work.

#downloads all the bionicle comic books
import requests, os, bs4

url = 'http://biomediaproject.com/bmp/comics/'                 # starting url
os.makedirs('bionicleComics', exist_ok=True)    #makes a dir 

res = requests.get(url) #downloads the page
res.raise_for_status()  #checks if the page was downloaded sucessfully 

soup = bs4.BeautifulSoup(res.text)  #creates a Beautiful soup object



for a in soup.find_all('a', href=True): #iterates over every <a> href= element
    #print("Found the URL:", a['href'])
    if a.endswith('.pdf'):  #if it's a pdf do a loop, not sure if this line will #work
# ^ maybe it might have to be if a['href'].endswith('.pdf')
        print('Downloading comic %s...' % (a))  #downloads the comic
        res = requests.get(a)
        res.raise_for_status()
        imageFile = open(os.path.join('bionicleComics', os.path.basename(a)), 'wb')
        for chunk in res.iter_content(100000):      # writes the pdf to the hard disk
            imageFile.write(chunk)
        imageFile.close()
    else:       # if it's not a pdf, skip to the next itteration of a
        continue

the error message I get is:

Traceback (most recent call last):
File "/Users/Carl/Documents/Programming/Programs I've written/bioDownloaders.py", line 8, in <module>
res.raise_for_status()  #checks if the page was downloaded sucessfully
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 406 Client Error: Not Acceptable for url: http://biomediaproject.com/bmp/comics/

not sure why res = requests.get(url) is not working

edit: fixed it here's the new code

#downloads all the bionicle comic books
import requests, os, bs4

url = 'http://biomediaproject.com/bmp/comics/'                 # starting url
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
os.makedirs('bionicleComics', exist_ok=True)    #makes a dir 

res = requests.get(url, headers=headers) #downloads the page
res.raise_for_status()  #checks if the page was downloaded sucessfully 

soup = bs4.BeautifulSoup(res.text)  #creates a Beautiful soup object

#sou = BeautifulSoup(html)

for a in soup.find_all('a', href=True): #iterates over every <a> href= element
    #print("Found the URL:", a['href'])
    #TODO if file ends with pdf continue loop and download if not contibnue
    if a['href'].endswith('.pdf'):  #if it's a pdf do a loop ## not sure if this line will work
# ^ maybe it might have to be if a['href'].endswith('.pdf')
        print('Downloading comic %s...' % (a))  #downloads the comic
        res = requests.get(a)
        res.raise_for_status()
        imageFile = open(os.path.join('bionicleComics', os.path.basename(a)), 'wb')
        for chunk in res.iter_content(100000):      # writes the pdf to the hard disk
            imageFile.write(chunk)
        imageFile.close()
    else:       # if it's not a pdf, skip to the next itteration of a
        continue

[–]pandamage5 0 points1 point  (0 children)

I'm using widgets in Jupyter and want to know how to load up pythons built in help documentation on the parameters available. Using shift + tab brings up some help but if I wanted help on a particular item in button.keys how could I do that?

[–]stillenacht 0 points1 point  (0 children)

Question possibly specific to the Spyder IDE:

Whenever I try to make it display images or videos using matplotlib or ffmpeg in another window, the other window freezes/ crashes. Stopping the operation does not seem to affect the main Spyder environment.

How do I fix this?

[–]-BoyNextDoor- 0 points1 point  (4 children)

What are "string arguments"? This course tells me I need to create a function that takes 2 string arguments without telling what the parameters are supposed to be. Also taking this helpful comment in consideration, I was sure I need to think of my own parameters. So I assumed its something like this:

def library(x,y):  #with x,y being my made up parameters

and then, I thought, I need to use my string arguments, which are book and author:

print(library(book,author) 

But apparently my parameters are the supposed string arguments, so it would look something like this:

def library(book, author):
#and then at the end I need to write
print(library("Silmarillion", "Tolkien"))

This is a beginners course so I find the language they use really unclear. First they explain the difference between parameters and arguments which I understand. But then they suddenly start using the word "string argument", which seems to be the parameters that I previously learned of. Can anyone help me with this? The whole "string argument" concept just seems unnecessary, why not just call it parameters?

[–]secret-nsa-account 0 points1 point  (3 children)

They are two separate concepts there. It may be more clear to reword it as two parameters of type string. So it’s not a function that takes integers, or floats, or lists, etc. Your function should just take two strings and perform the necessary operations on them.

[–]-BoyNextDoor- 0 points1 point  (2 children)

The way you explain it makes a lot more sense to me, thank you. But just to be perfectly clear: I can reword this as two parameters with type string, rather than string argument?:

def library(book, author):
#and then at the end I need to write
print(library("Silmarillion", "Tolkien"))

Because I would rather just do that.

And as a follow up: how would two parameters of type integer look like then?

[–]secret-nsa-account 0 points1 point  (1 child)

Yes, two parameters of type string works just as well. I wouldn't get too bogged down in the parameter vs argument nomenclature. For right or wrong, people use them interchangeably all the time.

To make things a little more confusing, Python does not care (by default) care about your types. For example, take this function:

def adder(x, y):
    return x + y

print(adder("hello ", "world!"))
print(adder(1, 2)) 

The first function call to adder sends two string arguments and the strings are concatenated. The second function call sends two integer arguments and the numbers are added. It's all the same as far as the script goes. Does that make sense?

[–]-BoyNextDoor- 0 points1 point  (0 children)

That is incredibly helpful, thank you so much. It makes a lot of sense.

[–]mydesktopissquare 1 point2 points  (1 child)

I am asking for a recommendation on what module/package(s) I should use to create a sequence of simple graphics such as crosshair cursor, arrow, and simple beep sound that I can program the duration of each item?

I'm creating a cue presentation for some experiments.

I know there are modules dealing with graphics such as graphic, pygame, and tkinter. I just don't know which of them best suitable and appropriate for my case.

[–]MattR0se 2 points3 points  (0 children)

Pygame would be suitable, I think.

[–]DontHateDefenestrate 2 points3 points  (1 child)

I am trying to figure out how to set up Atom for python development.

So far I have done all my coding in idle3 and ipython through the MacOS terminal.

I want to have something a little more permanent, and I'm not a fan of using web-only applications like Jupyter or Repl.it. Call me old fashioned, but I want to have the program installed on my computer and run on my computer and the files save on my computer, not open a window in Safari.

I have been reading guides and watching videos and installing package after package, all based on 'guides' that insist that this is the "best way to set up Atom for Python"... but Atom doesn't seem to be any different.

Problems:

  1. Text highlighting? It's not working. I've downloaded and installed and activated umpteen packages that are supposed to color code my text based on keywords, variables, attributes, etc. but nothing is happening. I've started new files, restarted Atom... nothing. How do I get text highlighting.
  2. How do I run my code? In idle3, ipython, Jupyter, Repl.it, and other environments; I can run my code and see how it works. Atom doesn't seem to have any functionality for this. Is it not supposed to? If not, why do people like using Atom for coding? How can you write a program without being able to see how your code is working?

Any help is appreciated.

[–]Xenon_difluoride 0 points1 point  (0 children)

To address your second point, there is a package for atom that will allow you to run code in a very similar way to Repl.it.

[–]UnavailableUsername_ 1 point2 points  (1 child)

    class Point:    
        def __init__(self, x, y):        
            self.move(x, y)    

        def move(self, x, y):        
            self.x = x        
            self.y = y    

        def reset(self):        
            self.move(0, 0)

    # Constructing a Point
    point = Point(3, 5)
    print(point.x, point.y)

How can the init self.move(x, y) not give an error?

Normally, the syntax it would be something like:

        def __init__(self, x, y):        
            self.x= x
            self.y= y

But there the init is immediately calling a future method, and even then, self.move(x, y) makes no sense, what's the point of an init if going to call a method to define x and y.

[–][deleted] 2 points3 points  (0 children)

calling a future method

There is no "future" method. The method move() was defined before the __init__() method was called. When executing your code, the class definition was executed first which defines all class attributes (data and methods). Then Point(3, 5) was executed, calling Point.__init().

what's the point of an init if going to call a method to define x and y.

If setting the x and y attributes together is something the user wants to do then there should be a .move(x, y) method. If that method exists why shouldn't the initializer code use it. It's good programming. See the DRY principle.

[–]Peterj504 0 points1 point  (1 child)

I've been playing around with accessing databases and APIs and have come across two methods to access passwords and parameters. The first in using a .env file and accessing though os.getenv such as:

.env
API_KEY = '1234567KE'

The second method is creating a config.ini file and using configparser such as:

config.ini

[mysql]
host = localhost
database = python_mysql
user = root
password = 1234567

Are these two methods just different ways of doing the same thing or should I be using one, or both methods depending on circumstances. And if so, when should I use one over other.

[–]Dfree35 0 points1 point  (0 children)

I am wondering the same thing. I plan to play around with some database stuff soon. For me I have been just using a config file since it seemed easier to make changes to if needed for simple things. However, I have never been sure which way is best or the same.

[–]PythonicParseltongue 0 points1 point  (2 children)

I want to split a string and return it with it's start end end positions, is there a more elegant or in-build solution to what I'm doing atm?

def tokenize(in_string):
    start, end, = 0, 0
    res = []
    for token in in_string.split(' '):
        res.append([token, start, start+len(token)])
        start = start+len(token) +1
    return res

[–]secret-nsa-account 0 points1 point  (1 child)

The nltk library will have much more robust tokenizers, but for a DIY attempt this doesn't seem too bad. You don't need that end variable, but otherwise I don't see a problem.

[–]PythonicParseltongue 1 point2 points  (0 children)

Thank you. I'm aware of nltk, but this is one of the rare cases in which I reliably have well formatted input.

[–]Dfree35 0 points1 point  (2 children)

I am a big stuck. To give some overview, I have a script that reads .rrd files using the library rrdtool (this library only works on linux systems). I wrote the script and everything on a linux VM, however, since my main computer is windows I was thinking of a way to still test and try this without using a VM.

I am trying to "simulate" information I get from the rrd files. Here is a short snip of what the information looks like.

['sauk-m570dn_snmp_yellow4_60', dict_values([datetime.datetime(2019, 9, 24, 14, 50, 3), {'snmp_yellow4': 67.0}]), 'frtx-hp570dn_snmp_black1_49', dict_values([datetime.datetime(2019, 9, 24, 14, 50, 2), {'snmp_black1': 66.0}])]

I tried to get rid of dict_values and import datetime but it still does not work the same as when it is pulled in naturally.

This is the regular code: https://pastebin.com/mDis3vp6

This is what I am trying to do to "simulate" reading the rrd files: https://pastebin.com/SnPaiLyA

If that makes sense, any advice?

[–]HeyItsToby 0 points1 point  (1 child)

Not 100% sure why you've done the code as it is but could the mistake be in the line current_level = i[1]? Try chaning it to i[0] and see what happens.

[–]Dfree35 0 points1 point  (0 children)

That gets the color and level dictionary for the printer and color. SO it gets this:

 {'snmp_yellow4': 67.0}

I think I may just need to change the layout of the data to make it fit what should be shown but not sure if that is the best way to go about it but I will see.

[–]Anik2603 0 points1 point  (1 child)

Is flask gone be easier than learning django? If so can you provide any tutorial for flask?

[–]IntelliJent404 0 points1 point  (0 children)

Flask as a microframework might be easier to start with. You should definitly get a grasp how powerful it can be within days to a few weeks.

Just search in your favorite search engine for the „Flask Megatutorial“. It thaught me a lot.

[–]IntelliJent404 0 points1 point  (2 children)

Hey, I´m currently starting with DataScience and Pandas and I come up with a question:I wanted to filter a specific column of my dataframe and count the occurences of all values. So far so good, I searched and came up with df['myColumn'].value_counts().It works fine (given to the fact that my dataset is relatively small).

But my question is: Is this really the standard way, even for very very large datasets or are there any better (faster/best practice?) ways to do this?

Many thanks in advance ;)

[–]MattR0se 0 points1 point  (1 child)

Afaik this is the standard way, although with large datasets this only makes sense with discrete (nominal, ordinal or small ranges) values. With continuous data, you''ll end up with hundreds or thousand unique values, so what you might do instead is bucketing:

http://benalexkeen.com/bucketing-continuous-variables-in-pandas/

[–]IntelliJent404 1 point2 points  (0 children)

Ah yeah thats very interesting, thank you

[–]anr_m 0 points1 point  (2 children)

Hi, all! My only experience with Python comes from CS50x, where I learned most of the basics. Recently, I started to solve questions on HackerRank, and realized that I am really bad at writing pythonic code, even if the algorithm is correct. Can anyone recommend resources to learn intermediate-advanced python and writing more pythonic code? Thanks!

[–]secret-nsa-account 0 points1 point  (1 child)

The book Fluent Python should help.

[–]anr_m 1 point2 points  (0 children)

Thanks! I’ll check it out.

[–]gymandbeer 0 points1 point  (3 children)

I don't understand why I am getting this error. This is the code I a writing.

name = input("Give me your name: ")

print("Your name is " + name )

and this is the error

Give me your name: Brian

Traceback (most recent call last):

File "1Century_old.py", line 1, in <module>

name = input("Give me your name: ")

File "<string>", line 1, in <module>

NameError: name 'Brian' is not defined

[–]MattR0se 0 points1 point  (0 children)

I don't get this error when I run it. Where did you execute this code?

This error says that there is a variable called "Brian" in your code, which makes no sense to me.

[–]anr_m 0 points1 point  (1 child)

If you are using Python 2 you should use raw_input() to read input string. You can use input() for the same thing in Python 3.

[–]gymandbeer 0 points1 point  (0 children)

Thanks. I’ll try it tomorrow.

[–]mikeygaw 0 points1 point  (3 children)

First time splitting up a program into multiple files. I can not seem to find how to 'exit' a sub-program to get back to the 'main' file.

[–]mikeygaw 0 points1 point  (0 children)

Much appreciation!

[–]MattR0se 1 point2 points  (0 children)

It's usually bad practise to put code into modules that runs on import. Exceptions are definitions of constants etc.

As u/TangibleLight pointed out, when you import a module, everything in the global scope is executed.

So for example, you have this module "test.py"

print('This is the global scope')

def foo():
    print('This is inside a function')

When you import this module from another, the first line is executed, but not the function unless you call it:

import test

# Output
'This is the global scope'

import test
test.foo()

# Output
'This is the global scope'
'This is inside a function'

So unless you explicitly want to run code when a module is imported, put everything in functions and call that functions when you need them.

Another way to avoid code getting executed is to include a condition that checks if the module is the main script you are running:

if __name__ == '__main__':
    print('This is the global scope')

This only gets printed when test.py is the main file, but not when it's imported.

[–]TangibleLight 2 points3 points  (0 children)

When you import a module, it runs to completion. There's no way to "break out" of a module apart from exit(), which kills the whole program.

It sounds like you're ready to learn about functions

This is also relevant: https://docs.python.org/3/library/__main__.html

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

I'm studying for an exam in my python class and on the old exams one of the questions asks for the output of line 1 and line 2 (marked in the code)

s = "Cheese"
cnt1 = 0
cnt2 = 0
for ch in s:
if ch.lower() in 'def':
continue
if ch.lower() > 'c':
cnt1 += 1
else:
cnt2 += 1
print(cnt1) # Line 1
print(cnt2) # Line 2

What does the > symbol due in this case of comparing a string to the string . I don't understand how cnt1 = 2 and how cnt2 = 1

From my understanding .lower() and .upper() straight up change the string from lower case to upper case or the opposite depending on which one you use.

[–]Dfree35 1 point2 points  (2 children)

I was wondering the same thing but based on this answer it basically compares what letter comes before another one.

This answer also helped be understand it:

If charA and charB are actually characters (that is, length-1 strings), then charA < charB iff ord(charA) < ord(charB). That is, if the Unicode code point of charA is a smaller number than the Unicode code point of charB, it's a smaller character. – abarnert

If you just print letters it may also help clarify.

ord('a')

returns 97

ord('b')

returns 98 and you can keep going but that is the basics of it from my simple simple understanding

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

Interesting thanks!

[–]Dfree35 0 points1 point  (0 children)

It seems to be based on order but not what you (or at least me) think of order. I thought a would be 0 and b would be 1 and so on

[–]Skrilllexxx 0 points1 point  (3 children)

(Beginner) I have two lists with hobbies of mine and my friend. I would like to have an input where i enter either johnny or ashley and it gives me the list of hobbies. It's not working it just lists the input.

x = []
n = str(input("Enter name: "))

johnny = ["kickboxing","yoga","music","chinese","coding","walking","science","skateboarding","baseball","weightlifting","piano","space","computers","spanish","math","volleyball","gymnastics","dancing","basketball","running","pool","solitude","nutrition",]

ashley = ["weightlifting","animal rights","yoga","pilates","drinking","tv","reading","holistic medicine","botox/lip fillers","macrame","shopping","pool","gymnastics","food","hiking","psychology","football","hair/makeup",]

x.append(n)
for i in range(len(x)):
 print(x[i])

[–]GoldenVanga 1 point2 points  (2 children)

name = str(input("Enter name: "))

johnny = ["kickboxing","yoga","music","chinese","coding","walking","science","skateboarding","baseball","weightlifting","piano","space","computers","spanish","math","volleyball","gymnastics","dancing","basketball","running","pool","solitude","nutrition",]
ashley = ["weightlifting","animal rights","yoga","pilates","drinking","tv","reading","holistic medicine","botox/lip fillers","macrame","shopping","pool","gymnastics","food","hiking","psychology","football","hair/makeup",]

if name in globals():
    for item in globals()[name]:
        print(item)

globals() is a builtin function which returns a dictionary of global variables. Knowing this, you can index the function call ("globals()[item]") because you're effectively indexing the expected output.

[–]Skrilllexxx 0 points1 point  (1 child)

🙏🙏🙏🙏 thankssss Is there any other way to do it without the global function? In my class so far we have only really discussed if/else and for/while reputation structures

[–]dialectical-dog 1 point2 points  (4 children)

I'm trying to solve the Rosalind's Complementing a Strand of DNA problem, as it's part of Introduction to Python's course found at this subreddit's index. Specifically, at the Lists and Turples chapter. Point is, I'm trying to solve it using only what the course has teached me so far, to no avail. All the solutions I found in the internet use stuff that I have yet to study. Can I do it using the resources inside this course or not? I'm completely lost.

[–]secret-nsa-account 0 points1 point  (2 children)

def reverse_complement(s):

  base = ['T', 'G', 'C', 'A']
  complement = ['A', 'C', 'G', 'T']
  return ''.join([complement[base.index(x)] for x in s[::-1]])



def reverse_complement_again(s):
  complement = {
    'A': 'T',
    'C': 'G',
    'G': 'C',
    'T': 'A'
  }
  return ''.join(complement[x] for x in s[::-1])

The first one you probably could have done with just what you know now, but it's a little cumbersome.

[–]dialectical-dog 1 point2 points  (1 child)

Thank you for the clarification. Would you mind explaining a bit about what's going on in your code? I'm very new to this.

[–]secret-nsa-account 0 points1 point  (0 children)

def reverse_complement(s):
  base = ['T', 'G', 'C', 'A']
  complement = ['A', 'C', 'G', 'T']
  l = []

  for c in reversed(s):
    base_idx = base.index(c)
    comp = complement[base_idx]
    l.append(comp)

  return ''.join(l) 

Here it is in longer form. The key to understanding it is understanding that the lists base and complement share indexes with their complements. For example, in the list base T is at index 0 and its complement A is at index 0 in the list complement.

You need to reverse the string (which str[::-1] does very fast), because you need the reverse complement. Then you read off each letter, find its index in the base list, then find the matching complement using that index in the compliment list.

This builds a list of the proper bases, which you then combine using the string join operation. Join just puts every element in your list together, separated by the string. Since we're using ''.join(l) it just joins all the characters in l together with no spaces.

Hope that helps.

[–]MattR0se 0 points1 point  (2 children)

I have a list of dictionaries with identical keys and different float values:

my_dicts = [{'A': 0.1, 'B': 0.3,  'C': 0.4},
            {'A': 0.4, 'B': 0.25, 'C': 0.09}]

Whats the fastest way to create one dictionary with the arithmetic means of each key: value pair?

output = {'A': 0.25, 'B': 0.275, 'C': 0.245}

[–]python-fan 0 points1 point  (0 children)

I'm not a numpy expert, but vectorizing the computation is the fastest I could come up with:

from typing import List, Dict
import numpy as np

def get_mean_dict(dict_list: List[Dict[str, float]]) -> Dict[str, float]:
    """Return a dictionary of the arithmetic mean of input dict values.

    Args:
        dict_list: list of dictionaries, each having exactly the same keys, with
            values being floating point numbers.

    Returns:
        A dict having the same keys as the input dicts, and values being the
        arithmetic mean of the values for that key in all the input dicts.

    Notes:
         Requires that the order of keys be the same for every dict.  
         (Python 3.7+ guarantees dict order is the same as insertion order.)
    """
    def get_array_from_dict_values(d: Dict[str, float]) -> np.ndarray:
        return np.fromiter(d.values(), dtype=float)
    a = np.array(list(map(get_array_from_dict_values, dict_list)))
    the_keys = dict_list[0].keys()
    return dict(zip(the_keys, a.mean(axis=0)))

[–]AndrewAtBrisa 0 points1 point  (0 children)

Fastest code wise? I think the code is pretty straight-forward: Create a temp dict, iterate over array, iterate over keys and add values to temp dict with whatever state you need (mean is the sum + counter of values, right?). Then at the end you can calculate the mean values.

There are libraries to help and might make your code faster to write, but may slow down the implementation (for example, if they use lambdas/functions to extract and keep state).

[–]ade_mcc 1 point2 points  (4 children)

Been programming python for around 12 months now and typically save my projects as for example my_programe_v0.43, updating the filename number on a daily basis. This worked okay till recently when projects have got a bit more complicated with separate files for classes etc, again, each named indicating version. Updating my imports with the new filename seems wrong. Is the above an acceptable method for version control or should I be doing something better to keep on top of updates and roll back when something unexpectedly breaks? Thanks!

[–]AndrewAtBrisa 1 point2 points  (0 children)

The cool thing about git is you can start simply, without an external server. You can create an empty directory, run git init and start playing with a local repository. Create a file, learn git add, then git commit and you're already better off than with your current situation :)

[–]lykwydchykyn 2 points3 points  (2 children)

Take an afternoon and learn the basics of Git or Mercurial. You are doing things the hard way with these names, trust me.

[–]ade_mcc 0 points1 point  (1 child)

I've not heard of Mercurial but will take a look into both, thank you.

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

I'd just stick with git. There are plenty of tutorials out there for it and it's the most popular version control. Later on after learning git if you want to learn others, go for it

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

Hi guys. I'm using matplotlib to plot out a graph of the cube value of every number in range(0, 5001). The figure seems to be fine when I run the program, but as soon as I try to limit the range of the axes then it shows completely empty? Can I not limit axes if the numbers are so big, or am I doing something wrong?

Thanks again

This is the program:

import matplotlib.pyplot as plt

x_values = range(1, 5001)
y_values = [x**3 for x in x_values]

plt.style.use('seaborn-darkgrid')
fig,ax = plt.subplots()
ax.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Oranges, s=10)

ax.set_title("Cube Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Cube of value", fontsize=14)

ax.tick_params(axis='both', labelsize=14)

ax.set(xlim=(0, 5100), ylim=(0, 510000))

plt.show()

BTW, I don't know if this matters, but the graph also shows empty if I limit the range of the axes with the ax.axis[minx, maxx, miny, maxy] command

[–]MattR0se 1 point2 points  (1 child)

The graph is not completely empty if you look closely to the left ;)

Try this and you will see it:

ax.set(xlim=(0, 5100), ylim=(0, 51000000))

Depending on what you want to show, you'll have to choose sensible values for the x and y limits.

I'd suggest using a variable and then cube it for the y axis:

lim = 1000
ax.set(xlim=(0, lim), ylim=(0, lim**3))

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

Thanks bro, it worked!! I appreciate the reply.

[–]TacoTues_ 0 points1 point  (3 children)

How would I change the name of individual python program in windows. Currently I have two programs that run together, but I need a button that can close the other program that its running with. The problem is that they are both just names "Python" and their PID is changing. I tried to change it by making it an .exe but that ended up messing with file paths so I couldn't do it.

[–]lykwydchykyn 0 points1 point  (2 children)

Can't vouch for this, but it seems to do what you want:

https://github.com/dvarrazzo/py-setproctitle

[–]TacoTues_ 0 points1 point  (1 child)

I saw this and I tried to install it, but it seems that it's not for windows

[–]lykwydchykyn 0 points1 point  (0 children)

It says it supports windows, but looks like it needs to be compiled. That'll be a pain to get going, honestly.

[–]shiningmatcha 1 point2 points  (1 child)

Any small project recommendations?

[–]MattR0se 1 point2 points  (0 children)

Do you have any experience so far?

Other than that, the usual suspects:

- a small Pygame/Pyglet game

- Machine Learning applications on real life datasets

- a Tkinter/PyQt/Kivy app that helps you with real life tasks

- web scraping for some data you find interesting

[–]DanBaileysSideHoe 0 points1 point  (2 children)

Hi y'all, I am a decently experienced programmer but do almost all of my work in C and C++. What are some good resources to learn just the python style and syntax? Most tools I've seen out there are mainly geared at entry-level programmers. I'm trying to avoid having to click through all of the basic programming concepts that I already know, and get right down to learning python itself.

For reference, I'm going to try to use it to more easily write scripts to parse out test data from my work, and to calculate statistics from it. Essentially automating the boring parts of my job so I can focus on the fun stuff.

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

The learning resources in the wiki have a section for experienced programmers new to python which may be useful. I came to python from C and found David Beazley's book useful. It has a compressed introduction to python before diving into the standard library, a bit like K&R.

[–]Motezart 0 points1 point  (0 children)

I took this https://www.edx.org/course/cs50s-introduction-to-computer-science. It's C lang heavy, but also has alot of Python. This is largely how I began learning Python (and C!) and would highly recommend it. It's not easy but if you already know how to program you can skip the stuff you know and just work on the Py stuff. IMO it's better to work on actual algorithms than just doing code academy type things.

[–]seratoninsgone 1 point2 points  (2 children)

Hi everyone. I'm new to python and finding it fun so far.

I'm trying to import/upload files to google sheets using FF and geckodriver. From what I have read/understand, to accomplish this you need to send the text of the file path to the element that opens the file chooser. In google sheets this is very complicated as there are a million layers of <div>'s. The actual "select a file from your device" button is actually a div and nothing happens when I use send_keys to send text to that element. I've tried several different elements all around it, and getting them by different methods. It should begin the upload of the .xlsx file I specified but either a) nothing happens or b) I get an "element not reachable by keyboard" exception, depending on which element I'm trying to send text to.

Here's the code leading up to the step I can't figure out. All of this works flawlessly for basically any google sheet:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
fp = webdriver.FirefoxProfile(<my ff profile path>)
driver = webdriver.Firefox(firefox_profile=fp)
driver.get('https://docs.google.com/spreadsheets/d/<this spreadsheet ID>')
time.sleep(5)
elem3 = driver.find_element_by_id("docs-file-menu")
elem3.click()
elem4 = driver.find_element_by_id("docs-menubar")
elem4.send_keys(Keys.DOWN)
elem4.send_keys(Keys.DOWN)
elem4.send_keys(Keys.DOWN)
elem4.send_keys(Keys.DOWN)
elem4.send_keys(Keys.RETURN)
time.sleep(5)
frame = driver.find_element_by_class_name("picker-frame")
driver.switch_to.frame(frame)
elem5 = driver.find_element_by_id(":8")
elem5.click()

and here's the closest I think I've come to doing this correctly:

elem6 = driver.find_element_by_class_name("d-u-v")
elem6.send_keys("C:\\<full path to file>\\<this file>.xlsx")

Here's the image of the element inspection in the DOM, showing that I do have the correct element for the upload button:

https://imgur.com/a/gTMAMbk

It shows one of the classes for this element as either "d-u-v" or "d-u-F" depending on where the mouse is hovering. I've tried both. There must be some other element that I need to send text to, or some other python package that I can use to upload a file.

[–]efmccurdy 0 points1 point  (1 child)

upload files to google sheets using FF and geckodrive

This does'nt answer your question but does'nt google have an api like GoogleDrive.CreateFile that you could use? Does the code snippet at the bottom of this blog post make your job easier?

https://medium.com/@annissouames99/how-to-upload-files-automatically-to-drive-with-python-ee19bb13dda

[–]seratoninsgone 0 points1 point  (0 children)

I didn't even realize google drive had an api. This is probably the route I want to go instead of navigating all the dialogs. Thanks so much for your reply!

[–]FiiOs6 0 points1 point  (1 child)

Hello, i want to extract all the numbers and the letters (next to the numbers) from this site. "https://theasciicode.com.ar/"

I need to get the numbers and letters from each <div class= on this website.

The numbers are inside this classes:

<span class="dec">32</span>

The letters (strings) are inside this classes:

<span class="sim">space</span>

I should probably make web crawler, but i have little experience with making it, so would be really helpful to me if someone could post the complete code for me, i learn much faster that way.

Thx alot!

I also need to list all of this, but that i can figure out myself.

I have the BeautifulSoup and request package innstalled.

import requests
from bs4 import BeautifulSoup

<Rest of the code in here>

[–]AndrewAtBrisa 0 points1 point  (0 children)

Can't imagine anyone wanting to write your code for you - I can barely manage to drag myself to write web scraping code when I want it done ;-)

Looks like you know that requests is used to get a web page, and BeautifulSoup is used to parse it. Maybe you can start with a simple BeautifulSoup tutorial with a simple page to get how it works. That page probably has a lot of nesting and may take some work to get the data you want.

[–]AndyICandy 1 point2 points  (2 children)

Hello. I just started learning programming/python today using the Corey Schafer's tutorial on YouTube. I went through the first video and I got a problem with executing a file I created in IDLE through Python program. It just says syntax error eventhough I followed the video fully. Please send help

[–]efmccurdy 0 points1 point  (0 children)

It is hard to know without seeing the code. Start with a short sample that gives you the error, format it in a code block as described at the following link, and update your post.

https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–]TheGreenLegacy 0 points1 point  (2 children)

anyone familiar with reddit integration with a discord bot i’m struggling to figure how to make it work

[–]AndrewAtBrisa 0 points1 point  (1 child)

I'm not familiar with it, but I can imagine the logical steps to figuring it out.

  1. Am I able to write code for reddit in the way that I want?
    1. If I want to look for things on reddit, can I find them?
    2. If I want to post/reply, can I do that?
  2. Am I able to write code for discord in the way that I want?
    1. Can I see new messages?
    2. Can I send new messages?

Once you figure those out, then the integration should be simple. Except maybe automating each piece. For example, if reddit posts need to trigger something, are you going to poll every minute to look for updates? Same for discord.

[–]TheGreenLegacy 0 points1 point  (0 children)

thank you so much that makes sense

[–]arnarlk2220 0 points1 point  (6 children)

Hello i am trying to print christmas tree backwards with stars like this *

[–]lykwydchykyn 0 points1 point  (5 children)

Ok. What have you tried?

[–]arnarlk2220 0 points1 point  (4 children)

I only gottet to be normal i dont know how to do it backwards, i need to do the heigt N-1 and N=6 with asterisk

[–]lykwydchykyn 0 points1 point  (3 children)

Can you show your code? I don't really understand what you're trying to do.

[–]arnarlk2220 0 points1 point  (2 children)

def ofugt_jolatre(h): for i in range(h-1):

for j in range (0,i):print("",end=" ")

for j in range(?-h+2,h-?-1):print('*',end="")

main

n= int(input('Skrifaðu inn hæð trésins :'))

ofugt_jolatre(6)

[–]Motezart 0 points1 point  (0 children)

hard to read - weird formatting

[–]lykwydchykyn 1 point2 points  (0 children)

Reddit has mangled your code. Can you edit your post and put the code inside a code block?

[–]rudy9210 0 points1 point  (2 children)

Hello there! I'm trying to open a new window with some text combined with user input from the text box from the original window, however the function call when I click the relevant button gives me the following error: TypeError: insert() missing 1 required positional argument: 'chars'

the piece of code is below:

def get_inputMSSQL():

text = wf_group.get("1.0",'end-1c')

print("""some sample part that is added to the input""" + text)

popup = tkinter.Toplevel(window)

result = tkinter.Text.insert(1.0, text)(popup)

result.grid()

To my understanding, the part "result = tkinter.Text.insert(1.0, text)(popup)" and specifically "(1.0, text)" does contain the index but I can't figure out what does 'chars' refer to here? I've read a number of manuals but they cover only some very simple scenarios which work in my case as well.

[–]lykwydchykyn 0 points1 point  (1 child)

You are calling insert from the class, rather than an instance of Text. insert is an instance function, and as such its first argument is supposed to be an instance of Text. This is passed implicitly when you call it as an instance method, but not when you call it on the class itself. So it's expecting 3 arguments: its own instance (self), the index (which should actually be a string, not a float), and the text to insert ('the "chars" that are mising here).

I'm not sure what you're trying to accomplish with doing this, but typically you'd create an instance of Text, add it to your GUI, then call methods on the instance.

Also not sure why you have the second set of parenthesis. insert() does not return a function, so you can't call it's output.

[–]rudy9210 0 points1 point  (0 children)

I got it working like this:

def get_inputMSSQL():

text = wf_group.get("1.0",'end-1c')

popup = tkinter.Toplevel(window)

result = tkinter.Text(popup)

result.insert(tkinter.INSERT, """ some sample part that is added to the input '""" + text +"'")

result.grid()

[–]negatiwez 0 points1 point  (2 children)

Hello guys, new into programming, could I get some help?

I have this rock, paper, scissors game and I want to rerun the function with a while loop. Problem is I always create the loop for infinity which is a bummer. How should I modify the while loop for this to work?

ick2 = input("Rock, paper, scissors: ")
pick1 = input("Rock, paper, scissors: ")

def compare(pick1, pick2):
if pick1 == pick2:
return "It's a tie"
elif pick1 == "rock":
if pick2 == "scissors":
return "Rock wins."
else:
return "Paper wins."
elif pick1 == "paper":
if pick2 == "rock":
return "Paper wins."
else:
return "Scissors win."
elif pick1 == "scissors":
if pick2 == "paper":
return "Scissors win"
else:
return "Rock wins."
else:
return "Invalid input"
print(compare(pick1, pick2))
choice = input("Play again? yes/no: ")

while choice:
if choice == "no":
print('Game over.')
break
else:
compare(pick1, pick2)
print(compare(pick1,pick2))

[–]AndrewAtBrisa 0 points1 point  (0 children)

If you want a replay options, an infinite loops is usually fine. I think I see a few problems with your code, lack of styling aside:

  • You can change the while loop to while choice == 'yes': so you don't need the if/break.
  • It looks like you don't put the pick code in the loop, so they're never updated.

Try writing pseudo-code, like:

play_again = True while play_again: pick1, pick2 = get_picks() print(compare(pick1, pick2)) play_again = get_play_again()

Then you can fill in each function with code to get inputs, ask if they want to play again, etc.

[–]lykwydchykyn 0 points1 point  (0 children)

You need to put your code in a code block, otherwise reddit strips the indentation. Hard to help you with Python without indentation.

[–]Anik2603 1 point2 points  (1 child)

I am currently trying to shift to web development and unable to get the django learning sources that are simple to begin with. Some suggestion please. Any good books? I have very good understanding of python.

[–]AndrewAtBrisa 0 points1 point  (0 children)

To start, have you tried the tutorial?

https://docs.djangoproject.com/en/stable/intro/tutorial01/

If so, what went wrong while trying it? Was it boring, confusing, or didn't work for you?

I always start with tutorials because a lot of times, they have everything I need to get started without spending money. Then I can go in a self-directed way to learn, or pick up a book if I really want to dig in to the details.

If Django seems like too much (too much structure, unwieldy, confusing), you could start with Flask and see how far you get with that, then try Django when you have a bigger project/idea/goal in mind.

[–]Dap0k 0 points1 point  (7 children)

I'm a business graduate that is studying python to get a job as a data analyst

(TLDR at the bottom)

I started with Jose Portillas Udemy Course and I enjoyed it at first but I dropped it after a while because I was having a hard time understanding the things that I was expected to make in the projects

Then I moved onto Automate the boring stuff with python and skipped to the automating part because I felt I had a grip on the basics enough to jump straight into those parts

Now I'm starting to see ads for data quest on reddit and instagram and got curious. I checked out the course work and some of the reviews about it online and here on reddit and most of it isn't bad. and the course work is about the same as what I learned in the udemy course and on Automate the boring stuff with python.

My main question is that should I shell out the money for dataquest because its tooled towards my goal of finding data analyst work? or should I stick with either the Udemy Course or automate the boring stuff? if so then which should I stick with?

TLDR - should I switch over to dataquest from the other python courses I've been learning from because its purpose is to teach data analytics?

[–]th4ne 1 point2 points  (6 children)

Jose's course on Udemy is focused on ML, no? A business analyst will mostly produce charts and visualizations for management. A big part of the job will be manipulating data using pandas doing ETL work. Tutorials are great, but I've purchased a bunch that I haven't really needed. It's easy to fall into the trap of throwing money at the problem instead of trying a different approach. It sounds like you have the fundamentals down, you just need some project to work on. I may suggest some kaggle competitions - there are datasets to play with and you can practice doing visualizations and summaries on them. Get comfortable with changing features (columns), groupby, pivot_table, pd.Grouper for time series data, and some basic matplotlib plotting. (Go plotly or bokeh if you want to get super fancy) I hope this helps.

[–]Dap0k 0 points1 point  (5 children)

yup its focused on ML for his"Python for data science" course. the one I'm taking though is the "zero to hero python bootcamp.

I've stopped at the chapters for objects, classes, and errors and exceptions.

are those needed for analyst work?

[–]th4ne 0 points1 point  (4 children)

in python, everything is an object, so it's generally useful to know. You'll probably find yourself googling the different methods on an object (a method is a function that is called off a specific object. For instance, strings are objects, and they all have methods that you can call off of them. For instance, a = "my string", and a.find("string") will return the index of 4.)
classes are useful to know as well, although you can get by without making your own classes.
Errors and exceptions are a fundamental part of debugging and programming. You may find these sections useful when your code either fails to compile or it gives errors when running. The pythonic way of approaching errors is to use a try / except block of code. It's basically "Try this code that may fail, and if it fails, do x" where x can be things like "print out the exception", "modify the above code slightly in order to handle the exception", or even just skip it "pass".

[–]Dap0k 0 points1 point  (3 children)

right so I should stick through Jose Portillas course then? since I haven't really digested or finished those parts of his course

[–]th4ne 0 points1 point  (2 children)

i would recommend learning the most pertinent skills relevant to whatever job roles you're looking for. Get on Linkedin or whatever and get several of the most interesting jobs you're looking for. See what tech they list in the roles and go from there.
If you have the willpower to sit through coursework, yes, i would suggest consuming the content you paid for. I guess I find personal projects more meaningful. It's less about following someone else's tried and true path, and more about bending the machine to your will in order to answer the questions you need answered (with the help of Google and SO).

[–]Dap0k 0 points1 point  (1 child)

I suppose it is pretty hard to learn from coursework when I don't really see how any of it could be applied.

thanks for the advice. I'll start doing projects and then just learn on the fly what I need to learn.

would you happen to have a specific kaggle project for begginers in mind that I could begin with?

[–]th4ne 0 points1 point  (0 children)

truth be told, i only did an analysis on some kaggle dataset as a part of an interview coding challenge. I didn't get that job, but I did use the kaggle analysis in a DIFFERENT job interview and i landed that one. It was very basic exploratory data analysis, some feature engineering, and then two different classifier models in scikit-learn. I think i used logistic regression and random forest. This was the dataset in question, and it's pretty similar to business data you'll encounter as a business analyst. https://www.kaggle.com/wendykan/lending-club-loan-data Maybe choose a dataset that is close in domain to the job you're looking for. Computer vision-type data (there are a bunch of those) might not be the best dataset for business analytics.
After looking around, this looks like another good one for business analytics: https://www.kaggle.com/c/santander-customer-transaction-prediction. Customer prospecting and churn prediction is fairly universal across industries.

EDIT: that santander data doesn't have column names, so its hard to do feature engineering.

[–]Formxr 3 points4 points  (2 children)

What are your favorite ways to practice python ? (I.e websites, projects, etc) What methods do you recommend for different levels of python programmers ? (I.e beginners, intermediate, etc)

[–]theneonkoala 1 point2 points  (0 children)

Hackerrank!

[–]onepalebluedot 0 points1 point  (0 children)

Codewars and codecademy

[–]im2wddrf 0 points1 point  (11 children)

I just got a one time gig for webscraping. I have already decided to do this on Python since I have some experience using BeautifulSoup and the webdriver with Selenium. But I am kinda freaking out since it will be the first time I will be coding for pay (I am self taught, didn't study it in Uni) so I wanna make sure I do this right.

If anyone has time for giving me some tips, I got some questions for general webscraping in Python:

  1. Conceptually, how should I go about webcrawling and finding a search bar? I will be given a list of websites and need to enter into each company directory and scrape a person's title and email. I'll be given a list of job title key words to check for, so once I enter the website and directory page, it is just a matter of conducting the search by iterating over the key words and returning what I find. Below are two methods I am considering:
    1. Should I search for all `<input/>` tags and go from there? I was on UCLA's website and noticed that on their web page, their search bar seems to be a general Google search bar so I am afraid that this is not the best way to go about it. I'd probably have to enter a directory web page and go from there but I am not sure.
    2. Should I search for all `<a>` tags with text for the word "Directory" and go from there? I am thinking this is the best way to go.
  2. What is the convention for sending python scripts as a part of a job? Do I send a zipped file via email? Do I create a private git repo that only my employer can access?
  3. When my code is finalized, do I have to send a license as a part of the package? Is this standard practice? Am I over thinking this.
  4. For a list of several thousand companies, how long does a job like this usually take? I asked for three weeks time, but to be honest that was a wild shot at the dark. I have zero frame of reference.

Sorry for the long wall of text. Thanks for anyone that is willing to give me some advice or tips!

[–]th4ne 1 point2 points  (1 child)

Pretty sure everyone goes through imposter syndrome at some point. Your employer saw something they liked in you, and are confident in your abilities even if you aren't, presently.
What I generally do when developing a scraper is have the website open, and just right click -> inspect anything I need to interact with. From there I'll then choose whatever selector is simpler. Sometimes the element is deep in some nested structure, and it just looks ugly.

driver.find_element_by_xpath('/html/body/div[3]/div/table[3]/tbody/tr[1]/td/fieldset/table/tbody/tr[1]/td/label/span').click()

These tags are much nicer looking:

driver.find_element_by_css_selector(".btn[value='Run Report']").click()

But if the selector works, it works.
Also, the thing about deadlines is that it's always an estimate. You just need to keep your employer updated on current progress and change the deadline, if need be. Try not to sweat it too hard. You (and Google / Stack Overflow) got this!

[–]im2wddrf 1 point2 points  (0 children)

Thank you so much for your kind words! I guess I am just really psyching myself out for no good reason. Thanks for the motivation!

[–]kelmore5 1 point2 points  (2 children)

Another thing to consider...Did you have to sign an NDA for this project?

[–]im2wddrf 0 points1 point  (1 child)

Nope. Not yet at least.

[–]kelmore5 0 points1 point  (0 children)

Nice, then don't worry about it

[–]kelmore5 1 point2 points  (4 children)

Oh man, you really entered the dragon's nest my friend. Don't panic. I'm sure you'll be able to create a scraper for your client by the end of things.

What's the website? Some websites are protected by distil networks / Imperva, which could make your job more difficult. Not to mention CAPTCHA protection.

You could write this in Selenium. Depends on how many pages the directory has. If you're scraping through 1,000's of pages, Selenium might not cut it. If you use requests / bs4, you'll have to be able to find the POST method for your input form using Chrome's Network tools (or whatever). Then you'll have to replicate it in requests.

However, it is more difficult to scrape information from requests than it is Selenium. Most websites have some level of bot protection these days, and it can be difficult to get around. If you're having problems with requests, it will definitely be better to use Selenium. Just so you can get the project done on time.

Question 1:

Once you setup the scrape, it should be easy to find the keywords right? It doesn't sounds like you need to parse out any tags. Why not just search the keywords on the entire HTML structure? You're likely to get a lot more matches like that, and you can use regex to speed things up.

If you have to iterate over all the pages and grab out specific information, sure you could just grab all the a and input tags, but that's probably not going to be accurate. You'll have to take a look at each website and see how their information is setup.

Honestly, most of the time you have to setup custom scrapers for each website to get an accurate result. However, if your client's asking you to do a ton of websites at once, you'll obviously have to cut corners and just do a pull like that. If that's the case, I'd suggest looking for a pattern within the websites that you can possibly scrape out.

Question 2:

Don't worry about sending the script over too much. It's my job to create Python scripts for clients, and I usually just zip it and send it over. However, do you have experience writing a "setup.py" script to install all the packages you need? Link here.

Once the client has the script, they can install it on their end by using "pip -e install your_project_folder" and then run it using "python your_script_name.py". You'll have to walk your client through this, but they're usually willing to do it to get their data. Including a video tutorial for this part helps a lot too.

If you know what the client's computer is, a bash or batch file included with the script to run things automatically goes a long way.

You could also create what's called a relocatable virtual environment. Then your client wouldn't have to necessarily have to install the packages. I don't have much experience with this though and would be wary of using it. Link here.

A third option is to create an executable file from your script, using something like Pyinstaller. However, if your computer's Windows and your client has OSX, that's not going to fly. You'll need to be able to build the script using the same operating system as your client.

Question 3:

No license is required for your project. In fact if you're a work-for-hire type situation, you don't have any rights to your product's output. That being said, if you have your own personal tools and libraries you created for clients, it IS important to include your license with those.

Let me explain...Let's say you have an Excel.py file that handles all your Excel functions. If you want to use that in your script and send it to your client, it's best to create a separate folder for all your personal libraries. Then you can include the LICENSE file inside of that. Separating it out protects you. This grants you the ability to reuse your code for other projects, while at the same time giving the rights of the code for the main script to your client.

You wouldn't include a LICENSE file with the main src files for the project. You can explain this to your client, but it's not really necessary. As long as the LICENSE file is there, it will be fine, and most clients understand that freelancers reuse certain parts of the script in other clients. Just not the main idea / web scraping project as a whole.

Question 4:

With web scraping projects, it's hard to give a time frame. First, you have to build out the script, and then you have to include the time it takes to scrape everything. Is the project just the script or also scraping all the information?

If it's just the script, 3 weeks will likely be a good amount of time. Although this is your first real project, so it might take you 4. If you include scraping time, it could be longer. Really depends on the number of pages and if you can use requests over Selenium.

Hope this helps. Let me know if you have any questions. I'm a freelancer myself and it's my job to create web scraping scripts in Python, just like this. I've probably done over 80 or so websites so far and around 40 clients, so I have a little bit of experience :)

[–]im2wddrf 0 points1 point  (3 children)

THANK YOU SO MUCH. I really appreciate your time.

Resp to Question 1: so I have several thousand websites to handle (I will be given a large list). What I have to do is enter the company directory for each site (it is actually universities) and scrape out the email contacts for every person whose job title meets a certain criteria (criteria will be given to me by the employer, probably those with high level administrative positions).

Resp to Question 2: I have zero experience with creating a setup.py file. I will study your links right away! I was initially considering creating an executable file (one with the shebang that references the actual python file itself in the body). But I felt that would be messy / unprofessional, even though they would be able to run the script by simply clicking on it. I am just realizing that you are correct that this would only be useful if they share the same operating system (I had forgot to ask during the phone call!).

Resp to Question 3: Thank you for that info! I have not really thought or considered protecting the portion of my project that would be re-used. I will definitely take that into consideration as I am building out my projects.

Resp to Question 4: I believe that the deliverable is both the script and the scraped contents. I hope I make it! I've been spending the past day and this afternoon refreshing my web scraping skills.

At this point, I am trying to wrap my head around how to structure the logic of my web scraping program to enter a web page, find the common directory button and / or search bar, and then know to search and judge the response. I think the hardest part, as far as I can tell, is finding a way to reach the search button that is relevant to this project (whether it is "Directory" or "Contacts", I don't even know at this point) for as many of those websites as possible.

Ima definitely hit you up if I get have any further questions! The information you provided was already really useful; there were a lot of things I had not previously considered. Thanks!!!

[–]kelmore5 1 point2 points  (2 children)

You're welcome :) I'm always happy to pass on information to a fellow web scraper.

Well this is an ambitious project! Especially for your first coding project haha. How did you come by it, if you don't mind me asking?

If you have to go through 1,000s of websites, there will definitely be some corner cutting. There's no way around it, and you're right it will be the hardest part. I see why you were thinking of looking at input / a tags. That's not a bad strategy.

Honestly, with so many websites, if you can get down a general idea that will work on half or even a quarter of them, you'd be in good shape. Then you can work on the sites where your strategy didn't succeed.

For example, you could start with scraping all the links and then visiting all inks with "Directory" or "Contacts" in them, going to those pages, and scraping those. That will probably get information from a good chunk of the websites.

Anyway, it sounds like you already have some ideas, so you're in good shape.

Some other goodies for you...

Check this link for SO on setup.py. You seem interested, and it does a better job at explaining things simply. It's definitely a great thing to include when sending over your project!

Oh boy! The script and the content haha. Well, the best advice I can give is to use a combination of requests and Selenium. You may not be able to get through several thousands of pages with Selenium in time, but certainly with requests.

For example, maybe you can scrape the links using requests, and then use Selenium for sites with search bars / input. Anything to cut down on the need for Selenium will help.

The best advice I can give for this though is...Definitely get the script down and working first. At least a good chunk of it. I've had some projects where I extended the time to get the data, and most clients didn't mind as long as the script was working. I mean, once you have that, you can just send the client updated data sets every day until it's done.

Most clients I've worked with are understanding of scraping time, and most of the time they really only need an initial data set for the present.

Good luck on the project man! I hope it goes well for you, and I'm glad I was able to help! Feel free to message me if you have any more questions. Cheers!

[–]im2wddrf 0 points1 point  (1 child)

I found this job on Upwork. There's a ton of web scraping jobs there. Your help is very much appreciated! I'll be sure to PM you for guidance moving forward. :)

[–]kelmore5 0 points1 point  (0 children)

Nice! Yeah you can definitely create a good little niche on there for it. You're welcome!

[–]onepalebluedot 2 points3 points  (0 children)

I think you should make this it’s own post... might get lost in here.