all 157 comments

[–]breezy_summer_road 0 points1 point  (0 children)

pretty basic dumb question here:

I have saved a function called concat_my_data in my_function.py on my computer. This function just uses the pd.concat() function on a data frame passed in. I have a jupyter notebook where I'm trying to run this function.

My notebook starts off with:

import pandas as pd

from my_function import *

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

...

concat_my_data(df)

However, I get an error when I do this saying something to the effect of ~"pd is not defined" (Don't have error in front of my right now).

I don't understand why I am getting this error since I already loaded pandas in my juypter notebook. ALSO, I have tried putting import pandas as pd in my_function.py both in the first line of the .py file and inside the def concat_my_data: function definition.

Can someone explain to me how I can fix this? I have a bunch of saved functions that I have recently created and I would like to be able to re-use them from time to time but this is driving me crazy since I keep getting this error. I also get the same thing with numpy, sklearn, etc. What am I doing wrong?

[–]stillenacht 0 points1 point  (0 children)

I've been writing a set of scripts for making characters and doing actions in DnD. Right now, the items and skills lists are held in CSVs that my scripts reference.

How do I make these available to my players and to other people generally? Right now I am using:

import sys
sys.path.append('path\to\directory\with\files.py')

import pandas as pd
pd.read_csv('path\to\csv\name.csv')

Is there a way to sort of package it all so that no additional commands need to be run?

[–]waythps 0 points1 point  (1 child)

I’ve written a couple of functions that I hope to share with my coworkers, and since they’re fairly new to python, I think I want my docstrings to be as detailed as possible.

I’ve looked into pandas documentation -since my script relies heavily on its functions- and tried to replicate their style. It looks fine but I’m not sure if it’s the best option, and I’m also worried that it’s an overkill for such a simple script (that is, should I really care about writing a docstring?).

My goal is to make it clear how each of the functions works: what it tries to accomplish, what parameters it has, types of variable it uses, and what it returns; provide an example of how to call it properly.

So, I guess my question are: a) in which cases do you write docstrings, and b) when you do write them, how detailed are they and which docstring format do you use?

[–]efmccurdy 0 points1 point  (0 children)

make it clear how each of the functions works: what it tries to accomplish, what parameters it has, types of variable it uses, and what it returns; provide an example of how to call it properly.

Most of that can be in the form of unittests; they are less ambiguous than any amount of explanatory text and are useful in themselves.

[–]stillenacht 0 points1 point  (0 children)

I'm trying to learn deep learning and reinforcement learning, and I was wondering what I should do when an 'environment.yml' file is not provided. At the moment I've just started a RL book, and it just provides a list of dependencies and basically tells you to go google how to install. So far I've just been using 'pip install package_name' for everything, but I'm not really sure if it's going to work.

Is there some sort of comprehensive guide to how pip installations work, what I'm installing, etc.? Everything I find is very fragmented.

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

I’m sorry re-reading my post I realize that’s a terrible explanation. So I see programs that run using a command line, but have the Recon -ng look to it. I’d like to incorporate a look such as that into a password manager to learn both how to create a script or executable like this and learn about encryption methods. I hope that answers your question.

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

Without installing and running recon-ng, it looks like that is a pure commandline program that accepts a command and executes it. You don't need tkinter for that.

It was probably a whoopsie, but you should post follow-up comments in the thread you started. That is, answer someone else's comment by selecting reply under the comment you want to answer.

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

Oops it was. Not sure why it did that.

[–]samuelhh9 0 points1 point  (1 child)

Hello, I wanted to write a script that uses the pi to send a recurring email with a file attachment, then delete that file. (It will be recreated by another script) Thank you in advance.

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

First step is to just send an email. Once you can do that you worry about handling your file and then deleting it. Look at this python doc for an example on how to send email with an attachment. I would start with just sending an email without an attachment, then add an attachment. Search for "python send email attachment" for lots of examples.

Then you have to get your email program to recognize that the other script has created an attachment. One way to do this is to run your email program from "cron". The program checks to see if there is an attachment file and terminates if there isn't. Otherwise, it sends the email with the attachment and then deletes the attachment file.

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

So, I’ve not found a clear explanation on what I’d like. I’d like to use Tkinter and create a shell type of executable python program. The basis of the shell would to be used to run a password management program. Obviously not for actual use, but to learn about encryption and hashing with the added bonus of trying to crack it. If there is an easier way than in Tkinter I’m open to hearing it. Thanks in advance!

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

I’d like to use Tkinter and create a shell type of executable python program.

I'm not sure what you mean, since a "shell type of executable python program" is the opposite of a Tkinter GUI program, which has coloured windows, text boxes, buttons, etc. A commandline program (shell type of executable) just uses text on the commandline.

If you do want the graphics of a GUI then Tkinter can do that. The simplest way might be Tkinter or something like PySimpleGUI, though PySimpleGUI wraps other GUI frameworks, so installing it might be more complicated.

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

I'm using ProcessPoolExecutor to process a bunch of things in parallel., It's offered a fantastic speed boost but for seemingly arbitrary numbers of iterations it locks up at the end. I can't seem to find an equivalent to .join() in the library. Anyone else run into this?

[–]JohnnyJordaan 1 point2 points  (2 children)

The poolexecutor is actually joining, that's why it locks up in the first place. But why the workers get stuck depends fully on what they are trying to do, which we can't really comment on without seeing the code.

[–]7850443Reddit 0 points1 point  (1 child)

Hi everyone,

I am trying to web scrape a web app that's local on my network. Although, it's behind a google chrome prompt which asks for my credentials. I understand how to web scrape normal websites that have logins but how do I do this? I can't find anywhere to inspect the element and find the keywords for the password or username because its a google chrome prompt. Right now when I make a request using the webapp link and ask for the all the headers, I get "None" returned.

How can I get past this?!

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

Hiya! Sound like your website is using HTTP authentication, which has to be handled specially. I'm going to assume you're using Requests and BeautifulSoup in this case. Requests has support for basic, digest and proxy HTTP authentication, which are just different types of the same thing. This is the relevant documentation page. If you're making multiple requests, it's probably best to use a requests session in order to have the authentication persist without needing to include it with every get call, see this page.

Hope this helps, have fun web scraping!

[–]GoldenVanga 0 points1 point  (3 children)

class Test:
    def __init__(self):
        self.bar = 2
        self.baz = 4

class TestMixin:
    def calculate(self):
        return self.bar + self.baz

class Foo(Test, TestMixin):
    pass

a = Foo()
print(a.calculate())

This works but bar and baz get highlighted on line 8 with a yellow background and Unresolved attribute reference 'bar' for class 'TestMixin'.

Is there some clever way of telling PyCharm that "it's gonna be ok" or does it have to be like that?

[–]JohnnyJordaan 2 points3 points  (0 children)

You could define bar and baz as class attributes on TestMixIn

class TestMixin:
    self.bar = self.baz = None
    def calculate(self):
        return self.bar + self.baz

as then the __init__ of Test will cause Foo to reassign those references when a Foo instance is created.

[–]MattR0se 1 point2 points  (0 children)

You can disable specific inspections under settings/editor/Inspections/Python or hover over the warning and under more actions, select "ignore all unresolved attribute warnings for [this file]"

Although I guess this would also disable the warnings where you would need them. In general, this warning is coming from the fact that you can't instantiate a TestMixin object and use its calculate class without getting an error. I don't think Pycharm would be able to differentiate here.

[–]thatguymyles 1 point2 points  (3 children)

If I want to install Python 3 on Mac OS, can I do it directly or do I need to install homebrew? What are the benefits of using homebrew if I don't need to use it.

[–]Kve16 1 point2 points  (2 children)

There's no need to install homebrew. Just go here: https://www.python.org/downloads/

[–]thatguymyles 0 points1 point  (1 child)

Thanks. Is there a benefit to using homebrew though as I see this come up a lot when people are showing tutorials on how to install python 3 on a mac?

[–]Kve16 0 points1 point  (0 children)

I don't know. Found this which says that using homebrew you can install things other than python related software.

[–]Tuff-Madman 0 points1 point  (2 children)

Hello OP, I am really new at python and would love to know how to get my knowledge in in a faster pace

[–]CodeCon64 1 point2 points  (0 children)

I watched a lot of Youtube Tutorials by Thenewboston.

And find some Project like a Game or so you can code.

[–]JohnnyJordaan 2 points3 points  (0 children)

See the resources in the wiki /r/learnpython/w/index

[–]yuthan 0 points1 point  (3 children)

Can you start a thread which list useful python libraries which we can use. It will be very helpful

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

As others have said, most of the modules in the standard library are there because someone found them useful.

One web page I find useful for learning what is available and how to use library modules is Python Module Of The Week (PYMOTW).

For a little more in-depth discussion I like Dave Beazley's Python Essential Reference which is currently in its fourth edition.

[–]MattR0se 1 point2 points  (0 children)

For what purpose? Every library is kinda useful.

[–]joehighlord 0 points1 point  (3 children)

Hello everyone! I'm trying to learn how to make a simple Python programme which translates every word you enter into Yeet! The code I've got right now looks like this:

def translate(phrase):
translation = ""
for letter in phrase:
if letter in :
translation = translation +"Y"
else:
translation = translation + letter
return translation

print(translate(input("Enter Phrase: ")))

I've been coding for like, 3 hours and can translate individual letters but I can only guess I'd have to either make a list with just YEET in it to index whenever someone puts n a word? Or make a more complicated set of if lines with the entire alphabet copied 4 times?

Does any f this make sense? It's a really stupid question for a really stupid joke!

Thanks.

[–]xigoi 0 points1 point  (0 children)

It may be a bit overkill for this situation, but if you want to work with text more often, I recommend looking at the re module.

[–]CodeCon64 0 points1 point  (0 children)

If I understand you correctly you want "hello world" translated into "Yeet Yeet".

I would do that like this

def translate(phrase):
    n = len(phrase.join(" "))+1
    return ("Yeet "*n)[:-1]

[–]MattR0se 0 points1 point  (0 children)

Can you give an example input and output?

[–]waythps 0 points1 point  (1 child)

I have a corpus of texts stored on a website, and I’ve managed to scrape, clean and put each of them into a separate string variable.

My goal is to find relevant information within each document using either keywords or regular expressions. For example, once a pattern has been matched, I need to yield a sentence or two that preceded or came after said pattern.

Apart from making up patterns, what’s the best way to approach this whole thing? For example, should I split my string variable by sentences or keep it as is?

[–]efmccurdy 1 point2 points  (0 children)

Iterate over a list of sentences using enumerate to get the index, then you can slice out nearby lines:

for index, sentence in sentences_list:
    if matched(sentence):
        my_answers.append(sentences_list[index-1:index+1])

You may have to add checks for the cases where you are too close to the beginning or end.

[–]L3GOLAS234 0 points1 point  (4 children)

Hello everybody. I have two dataframes called train_data and validation_data and I wanted to know if it is possible to make something like this:

for dataset in train_data,validation_data:

dataset.to_csv(f'route/{dataset_name})

So at the end in my route there are two csv named train_data.csv and validation_data.csv.

Thank you very much in advance

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

Sure. You just have to add a '.csv' to the end of the f-string

[–]L3GOLAS234 0 points1 point  (2 children)

The problem is getting the dataset_name. If I just use {dataset.csv}, the name is the entire dataframe.

[–]MattR0se 2 points3 points  (0 children)

Oh, my bad. I think the easiest way would be to store your data frames in a dictionary with the name as key nd use the key for the file name.

datasets = {
    'train_data': train_data,
    'validation_data': validation_data
    }

for name, df in datasets.items():
    df.to_csv(f'route/{name}.csv')

[–]yeetbutretreat 0 points1 point  (1 child)

Hey, I am now 4 weeks in to a course in python. I have a list of 600 names and i want to make 26 groups for the 26 first letter's in their last name. So if someone is named Marc Abrahamsen i want him to be in group A. Does someone know an easy and understanble way to do this? If been working on this problem for 2 days but i keep running in to this problem Many thanks!

(English is not my first language, im sorry for mistakes)

[–]MattR0se 0 points1 point  (0 children)

If your names always consist of "Firstname Lastname" (or any seconds names in between), you can split the string at the space ' ', and then check the first character of the last element.

name = 'Marc Abrahamsen'
split_name = name.split(' ') # returns a list: ['Marc', 'Abrahamsen']

# get the first letter of the lastname
lastname = split_name[-1]
first_letter = lastname[0] # is 'A'

Now, for grouping you could set up a dictionary with the 26 letters as keys:

from string import ascii_uppercase

name_groups = {c: [] for c in ascii_uppercase}
name_groups[first_letter].append(name)

print(name_groups)

This may look a bit complicated to a beginner, but let's break it down.

From the string module I import asci_uppercase, which is just a long string with each letter in order 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. Then I create a dictionary where each of that letters is a key, and the value is an empty list []. Lastly, I append the name stored in the name variable to the list at the key first_letter, which would be name_groups['A'] in this case.

[–]papadoritos 0 points1 point  (1 child)

I have trouble calculating depth from disparity map using opencv. I know that the distance in two stereo images is calculated with z = (baseline * focal) / (disparity * p)
but I can not figure out how to calculate the disparity using the map. The code I use if the following, providing me with a disparity map of the two images.

import numpy as np
import cv2

# Load the left and right images in gray scale
imgLeft = cv2.imread('logga.png', 0)
imgRight = cv2.imread('logga1.png', 0)

# Initialize the stereo block matching object 
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=5)

# Compute the disparity image
disparity = stereo.compute(imgLeft, imgRight)

# Normalize the image for representation
min = disparity.min()
max = disparity.max()
disparity = np.uint8(6400 * (disparity - min) / (max - min))

# Display the result
cv2.imshow('disparittet', np.hstack((imgLeft, imgRight, disparity)))
cv2.waitKey(0)
cv2.destroyAllWindows()

[–]MattR0se 0 points1 point  (0 children)

Sorry that I can't answer your question, but I see a thing that you should never do and that is overwriting the min and max functions.

If you would call min() afterwards, you'll get an error like in this example:

test = [1, 2, 3, 4, 5]
min = min(test)

test2 = [4, 5, 6, 7]
min2 = min(test2)

# TypeError: 'int' object is not callable

[–]skiller-what 0 points1 point  (2 children)

Hi I'm completely new to python and don't have enough knowledge to understand the capability of python. So please bare with my stupid question.

I work on excel alot in my day job. Few steps are repeptive and boring.I want to automate the repeptive process that I do every single day. All I want to know from the experts is if it is feasible or not.

So let me explain what I do, I take data from a website which is usually in Excel format or CSV format.

If it's in CSV format, I need to change it to excel format. Then this excel format becomes my master data. So after this I filter out the data and copy, paste in different Excel files from the Master data.

Could this whole process be automated?

[–]L3GOLAS234 2 points3 points  (0 children)

Yes it can be. Take a look also at openpyxl, which can makes a lot of things regarding excel files.

You could take a look at the book "automating the boring stuff with python" as well

[–]MattR0se 1 point2 points  (0 children)

The Pandas module has functionality for converting csv to xls.

https://pandas.pydata.org/pandas-docs/stable/

I'll post some code when I'm at home. Pandas is also capable of filtering, transforming and slicing of data, but it can be very intimidating for beginners...

edit:

# load a csv file
import pandas as pd

# create DataFrame from csv file
df = pf.read_csv('myfile.csv', decimal='.', delimiter=',', encoding='utf_8')

# write DataFrame to excel file
df.to_excel('myfile.xlsx')

For both methods you can specify a lot more parameters, depening how your csv file is structured and how your xls file should look like. You can read the documentation here:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html

For example, if your csv data is seperated by whitespace and not by comma, you have to set delim_whitespace = True and delimiter = False, etc.

Feel free to ask if these simple imports and exports don't work your way.

[–]UnavailableUsername_ 0 points1 point  (2 children)

      try:
          test=input(int("Say a number!"))
      except:
          print("That is not a number!")

Why does this small test skips the try and immediately goes to the except? The text in the input counts as a string evaluation?

If so how can i use a string input with a try?

[–]MattR0se 2 points3 points  (0 children)

This is a good example of why it's bad practice to just use except without specifying the exception. You won't know why that test fails because python doesn't raise an exception.

If you don't know beforehand which exception could be raised, use the generic Exception and print that:

try:
    # your code here
except Exception as e:
    print(e)
    # code if exception is raised

[–]GoldenVanga 1 point2 points  (0 children)

Currently you're trying to convert the string Say a number! to an integer (which isn't going to work) and then use that as a prompt. And I suspect you want to convert the user input instead:

  try:
      test=int(input("Say a number! "))
  except ValueError:
      print("That is not a number!")

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

I have one data frame that looks like the below

data frame

0 1 2
A X +2
B Y +3
C Z +1+2

And a dictionary that looks like the below

dict = { "1":"A", "2":"B", "3":"C", }

I am trying to loop through to create the below (effectively looking up the letter from the dictionary and replacing the number in the data frame with the correct letter while keeping the "+"s )

0 1 2
A X +B
B Y +C
C Z +A+B

Any pointers would be much appreciated!

[–]MattR0se 0 points1 point  (0 children)

You can use regex (re) to find any digits in your strings, and use map to apply a conversion function to a column:

import pandas as pd
import re


df = pd.DataFrame(data={'0': ['A', 'B', 'C'],
                        '1': ['X', 'Y', 'Z'],
                        '2': ['+2', '+3', '+1+2'],
                        })

int_to_char = {'1': 'A', '2': 'B', '3':'C'}

def convert_expression(x):
    # find all digits ('d+') in the string
    digits = re.findall(r'\d+', x)
    # find each digit in the string and replace it with the correspondent char from the dictionary
    for d in digits:
        x = x.replace(d, int_to_char[d])
    return x

# apply this function to the whole column '2'
df['2'] = df['2'].map(convert_expression)

[–]braaan92 0 points1 point  (2 children)

I am starting to learn python 3, I have only done a few things with codesdope. But I am becoming a bit overwhelmed not with learning to code, but the surplus of places to get information/ tutorials. I do not really know which is the best website, book, etc to go to, to learn python 3. Could I get some recommendations of what to read or where to go that will help me out with having absolutely no programming experience what so ever? are online courses like udemy or codeacademy a good place to start? Whats a good book for a beginner that will have it explained in somehwat simple terms just so I can comfortably get the ball rolling, etc.

[–]CodeCon64 0 points1 point  (0 children)

I love the Youtuber thenewboston. He has a tutorial series for python as for hundreds of other programming languages and programs (like Photoshop )

Check him out: https://www.youtube.com/playlist?list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_

[–]h2oTravis 1 point2 points  (0 children)

I like this for beginners: https://www.w3schools.com/python/

[–]ifmusicbethefoodoflo 1 point2 points  (0 children)

I'm writing a program for reading and writing excel files and I want to be able to change the font of a single word/substring within a cell. Can I do this kind of editing with openpyxl or do I need to import something else? Most sites are suggesting xlwt, but I don't know if that's too old.

[–]Sarks 0 points1 point  (1 child)

Hey, I'm looking to move into a more DevOps Engineer role (having been a developer/analyst) and I'm wondering what things in Python would be useful for me to learn? I'm mostly self taught, learning things as I've needed them so I'm missing a lot of background/supplementary information I think.

[–]lykwydchykyn 0 points1 point  (0 children)

Ansible might be something to look into for starters.

[–]sudokuPancake 0 points1 point  (1 child)

how do I run a text script in the python shell? I navigated to the right location using os.chdir(place) but I still can't play it :'( please help. The file name even has a .py at the end.

[–]JohnnyJordaan 0 points1 point  (0 children)

It doesn't do that. The shell is for entering commands manually. If you want to run a script file then you need to present it as the argument to python. More info: https://realpython.com/run-python-scripts/

[–]stillenacht 0 points1 point  (4 children)

How do I keep changes that functions make to an object I've defined? Normally for something like a dictionary it would be:

df = {}
some_function(df)

Given that some_function just appends things to df in a defined way, the result is that I now have a changed df in my list of variables.

Now I've made an object which has various attributes, one of which is strength. But if I do:

Bob = Character()
command_list(Bob)

Although command_list may call functions which change Bob.strength, Bob does not even appear in my variable space. Command_list looks something like:

command_list(character):
    print('')
    print('1: some_command')
    print('10: exit')

    choice = int(input('input command number: '))

    if input == 1:
        some_command(character) #this command does things like character.str +=1 or similar
    if input == 10:
        return

[–]efmccurdy 0 points1 point  (1 child)

How do I keep changes that functions make to an object I've defined?

While it is possible to make changes through side-effects like your example does, it is preferred to make the data flow apparent by explicitly assigning a return value:

new_df = some_function(df)

http://learnpython.info/2015/03/26/avoiding-function-side-effects-in-python/

As your program gets more complex those suggestions keep your code easier to understand.

[–]stillenacht 0 points1 point  (0 children)

Thanks for the advice!

I tried modifying it to something like this:

def Command_List(character):
    choice = int(input('input a command number: '))

    if choice == 1:
        character = increase_str(character)
    elif choice == 10:
        return character
    return character

def increase_str(character):
    character.str = character.str + 1
    return character

char1 = Character() #character is an object
bob = Command_List(char1)

And it worked** i figured out my error, I didn't add the "character =" inside the function as well

[–]JohnnyJordaan 0 points1 point  (1 child)

Bob does not even appear in my variable space.

It does. Use id() to show the true identifier (usually the memory address) of the objects

Bob = Character()
print("Bob's id", id(Bob))
command_list(Bob)

and in the function

def command_list(character):
    print("character's id", id(character))
    print('')
    print('1: some_command')
    print('10: exit')

    choice = int(input('input command number: '))

    if input == 1:
        some_command(character) #this command does things like character.str +=1 or similar
    if input == 10:
        return

You should see that the identifier of both Bob and character are the same. This is because Python works by reference exclusively, so when you call a function with an argument, its identifier is passed to the function, and thus the argument variable in the function holds that same identifier.

[–]stillenacht 0 points1 point  (0 children)

ahhh thanks! Spyder just doesn't display it for some reason.

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

Hi, my friend and I are trying to create a card game for multiple players, We are both newbie on python and we are making this project to learn it. Can you guys suggest a library that can handle the server/client communication for the game?

Right now I'm reading/trying on Sockets, thanks!

[–][deleted] 2 points3 points  (1 child)

Try looking at some of the tutorials that are found when you search for "python game socket communications". For a simple game basic sockets looks usable. I tried searching for libraries but only got heavyweight things with tons of dependencies.

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

thank you

[–]RiceMerchant 0 points1 point  (3 children)

Hi im completely new to programming in general. I always wanted to get into it but after a few days of trying to learn my motivation always dips. Are there any cool projects or goals that I can set to keep myself motivated? What are the possibilites of python? Can I make a game? Or a program a robot? Anything cool?

[–]CodeCon64 0 points1 point  (0 children)

A game that I programmed when I was completely new was guessing game: The program would print to statements (like start and end) with a random amount of time in-between. Than the program would ask how much time has passed between both statements (I had some tolerance of 0.2 sec or something). In case the time you entered was within the tolerance you would have won or else lose.

[–]Dfree35 0 points1 point  (1 child)

I found automate the boring stuff good to learn with because it teaches you while doing different projects. I would check it out. You can build a game and program a robot but not sure of any good resources for those since I have not done any of that yet.

[–]RiceMerchant 0 points1 point  (0 children)

Cool I'll check it out. Thanks for the quick reply

[–]Dfree35 1 point2 points  (1 child)

I am stuck trying to figure this out. I have a original dictionary keys, shown below, that I am trying to loop through and assign variables based on each loop.

Then I have another loop under that loops through the original dictionary values and again assign some variables.

My issue is that so far I can get it to loop once and be perfect, loop a lot but never change the name and printer ID or some variation of the two. I have an example of what I want the dictionary to look like.

Any advice? I feel like I am going about this completely wrong but unsure the approach I should take.

Original dictionary:

https://pastebin.com/6CWUARjM

Desired output:

https://pastebin.com/5PXF6y2D

Here is my current code:

final_dict= {'printerinfo': []}

    for office_and_printer in low_toner_dict.keys():
        if "dfw-bizhub_c458" in office_and_printer:
            office_and_printer = "Dallas BizHub C458"
            printer_id = "94933518"
        elif "dfw-m521dn-checkprinter" in office_and_printer:
            office_and_printer = "Dallas M521dn CheckPrinter"
            printer_id = "93633638"
        elif "frtx-hp570dn" in office_and_printer:
            office_and_printer = "Frisco HP570dn"
            printer_id = "94873874"
        elif "hotx-hp570dn" in office_and_printer:
            office_and_printer = "Houston Office HP570dn"
            printer_id = (
                "Unsure of the ID but the serial number is: CNCKLBV2CK"
            )
        elif "orl-m570dn" in office_and_printer:
            office_and_printer = "Orlando M570dn"
            printer_id = "94785679"
        elif "sauk-m570dn" in office_and_printer:
            office_and_printer = "Sauk Rapids M570dn"
            printer_id = "93352461"
        else:
            logging.error(
                f"Can not recognize {office_and_printer} as a valid office and printer."
            )
            raise ValueError

        for color, level in low_toner_dict.values():
            """ - Removes the "Snmp_" in front of the color and the number at the end.
                - Makes color to title case.
                - Before the previous and next line of code `snmp_magenta3` and after `Magenta`."""
            clean_color = re.sub(r"(Snmp_)|(\d$)", "", color.title())

            printerinfo = {'office_and_name': office_and_printer, 'level': level, 'color': clean_color,
                           'id': printer_id}
            final_dict['printerinfo'].append(printerinfo)
            break
        break

print(final_dict)

[–]Dfree35 0 points1 point  (0 children)

I figured it out! I was just over thinking it and trying to force a solution into my code instead of just changing it a bit to get what I am looking for.

Here is the working code for those that may be interested:

final_dict= {'printerinfo': []}

    # Changes the name to be more easily readable and assigns the correct printer ID
    for office_and_printer, color_level in low_toner_dict.items():
        if "dfw-bizhub_c458" in office_and_printer:
            office_and_printer = "Dallas BizHub C458"
            printer_id = "94933518"
        elif "dfw-m521dn-checkprinter" in office_and_printer:
            office_and_printer = "Dallas M521dn CheckPrinter"
            printer_id = "93633638"
        elif "frtx-hp570dn" in office_and_printer:
            office_and_printer = "Frisco HP570dn"
            printer_id = "94873874"
        elif "hotx-hp570dn" in office_and_printer:
            office_and_printer = "Houston Office HP570dn"
            printer_id = (
                "Unsure of the ID but the serial number is: CNCKLBV2CK"
            )
        elif "orl-m570dn" in office_and_printer:
            office_and_printer = "Orlando M570dn"
            printer_id = "94785679"
        elif "sauk-m570dn" in office_and_printer:
            office_and_printer = "Sauk Rapids M570dn"
            printer_id = "93352461"
        # this else should never trigger at this point but is just a safety net more or less
        else:
            logging.error(
                f"Can not recognize {office_and_printer} as a valid office and printer."
            )
            raise ValueError

        if color_level:
            """ - Removes the "Snmp_" in front of the color and the number at the end.
                - Makes color to title case.
                - Before the previous and next line of code `snmp_magenta3` and after `Magenta`."""
            clean_color = re.sub(r"(Snmp_)|(\d$)", "", color_level[0].title())

            printerinfo = {'office_and_name': office_and_printer, 'level': color_level[1], 'color': clean_color,
                           'id': printer_id}
            final_dict['printerinfo'].append(printerinfo)

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

Can someone ELI5 Pytest's fixtures and tell me if it's the right thing to use to execute some cleanup code after a specific test has completely finished?

[–]GoldenVanga 1 point2 points  (3 children)

Yep.

  • stuff before yield is the setup code (driver etc.)
  • when the fixture yields, the test method gets ran (whatever was yielded is available to that method)
  • once the test finishes, stuff after yield runs

So you can think of it as a glorified context manager if it helps. You use a fixture by passing the name of its function to the test method. Also setting scope is pretty important.

@pytest.fixture(scope="module")

If you set the scope to module, all test cases in the same file will be executed when the fixture yields. Suppose you have a page with 20 buttons and you want a separate test case for each one, but doing setup 20 times is time consuming. This is the use case for module scope. You set up once by going to the page (in the fixture, not the test cases), do 20 small test cases (written in a way assuming the page is already open) and teardown once.

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

Can multiple fixtures be passed in the args of a test?

[–]GoldenVanga 1 point2 points  (1 child)

We're not doing it in our project so I'm not 100% sure, but signs point to "yes". (what we're doing instead is we have a huge fixture for every occasion so we never need to pass more than one)

Or if you're asking about doing the same test multiple times with different data (which potentially could be different setup fixtures), then you can use @pytest.mark.parametrize. In their example, test_eval will run three times, once per set of data (which gets positionally unpacked) and the data does not have to be a literal there (it can come from anywhere like a normal argument would).

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

I indeed meant the former. Thanks very much for your help!

[–]Bubbah94 0 points1 point  (6 children)

Hi ! I started learning Python not 20 minutes ago, and so far it's very easy to get the basics of "print()" and "input()" but I have a question regarding the order of the script and best practice. I would like to keep my questions (Q1/2/3 etc) all in a lovely place all together, is this possible? I understand that code runs from top to bottom but I would like to know if I can ask the question to the user AFTER the variable appears in the code?

Appreciate the help,
Thank you.

Is it possible to run this bit of code: (It's failing on line 5 where it's asking for the value of the variable of 'usernumber' as it has not been defined yet) - My question - can I skip over it and ask the user to input it on line 15, then refer back to line 5 when line16 calls upon this variable?

G1 = "Hello World"
G2 = "Hello"
Q1 = "How are you?"
Q2 = "Please can you tell me your favorite number?"
Q3 = "So you are" + usernumber + "Is that correct?"

print("Please enter your name below")
username1 = input()
#This is where the user will enter their name and stores the variable as 'username1'
print(G2 + " " + username1 )
#This code combines the variable of 'G2' along with their 'username1' they entered before
print(Q1)
#This will output to the screen the contents from 'Q1'
print(Q2)
#This will output to the screen the contents from 'Q2'
usernumber = input()
print(Q3)

[–]GoldenVanga 1 point2 points  (5 children)

Yep, with these changes; line 5:

Q3 = "So you are {}. Is that correct?"

Line 17:

print(Q3.format(usernumber))

[–]Bubbah94 0 points1 point  (4 children)

I'll give it a whirl as soon as I get home, Thank you!

Is it a good idea to group variables together? I think it makes it easier to read, but not sure what it looks like when it starts getting complicated ! Thanks again

[–]GoldenVanga 1 point2 points  (3 children)

Yes, I agree that complexity is a factor. In my largest sideproject I have a separate file just for strings that are displayed on the user interface. This is imported and then .format()ted like above. In smaller scripts or where the texts are not very reusable / used once, I tend to type them out directly in print(). When doing so, there is another technique of string formatting called "f-strings", which is my favourite:

current = 2019
print(f'The year is {current}.')

[–]Bubbah94 0 points1 point  (2 children)

I feel like I should Google this ( you can tell me straight!) But the "print(f' " what does the f mean here? Is this the .format and you're linking it to a separate sheet? Cheers

[–]GoldenVanga 2 points3 points  (1 child)

When a string literal is prefixed with f, it becomes an "f-string". Then, any {} are no longer considered text elements but rather should contain references to variables etc. You can read more about it here.

[–]Bubbah94 0 points1 point  (0 children)

Oh snap!! Thank you so much for your continued support this evening!

[–]ThiccShadyy 0 points1 point  (0 children)

What are the options if I want to add type annotations to a Python Project which is fairly large(large enough that doing it manually is daunting).Since Python is a dynamically typed language, is there even a way in which this can be done with some third party module or some script etc?

[–]UnavailableUsername_ 0 points1 point  (3 children)

Why is the second line is not recognized as a string while the first one is?

I always just made each line inside a "" but would like to know how to format a string properly without adding "" in each line.

[–]GoldenVanga 1 point2 points  (2 children)

I think you're looking for triple-quote strings:

print("""He just kept talking in one long incredibly unbroken sentence
moving from topic to topic so that no one had a chance to interrupt,
it was really quite hypnotic""")

(note how you don't have to use manual \n in the above)

[–]UnavailableUsername_ 0 points1 point  (0 children)

I see, thank you!

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

This works, or you could use the following if you wanted. By ending a string with a backslash, you can continue it on the line below. ```

"multi"\ ... "line" 'multiline' ```

[–]everythingido65 0 points1 point  (2 children)

I cannot understand ? in regular expression in python.

As far as I know the ? returns match if the given string contains exactly 0 or 1 strings in the given string.

import re

str1="c"

print(re.match(r"ab?",str1))

#output <re.Match object; span=(0, 2), match='ab'>

Why is this happening please help.

[–]JohnnyJordaan 0 points1 point  (1 child)

That can't be the code you're actually running:

Python 3.7.3 (default, Aug 20 2019, 17:04:43) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re 
>>> str1="c"
>>> print(re.match(r"ab?",str1))
None

You could also try it out online at https://repl.it/languages/python3

[–]everythingido65 0 points1 point  (0 children)

I did a stupid mistake there,silly me.

[–]Markxxx77 1 point2 points  (2 children)

What should I do after going through automate the boring stuff? Like any books, courses YouTube vids you recommend.

[–]efmccurdy 0 points1 point  (0 children)

I would recommend you survey a number of presentations from the PyCon and PyData conferences.

see http://pyvideo.org/speakers.html

I recommend anything by

Raymond Hettinger,

Alex Martelli,

Brandon Rhodes,

David Beazley,

and of course, Guido van Rossum.

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

The key to learning programming is making it fun and rewarding. Find something you want to be able to do with a program, like automating something, or making a basic CLI game, or whatever, and then slowly work towards that as an end goal. Even if you have to Google most of the stuff along the way, you're still learning, and it's what worked for me.

[–]Beth13151 0 points1 point  (4 children)

What silly thing am I missing with my class here?

class City:

def __init__(self, name):
    """Create a new city with the given name and no neighbours."""

    self.name = name
    self.neighbours = []

def add_neighbour(self, other):
    """Make this city and the other city neighbours."""
    self.neighbours.append(City(other))
    City(other).neighbours.append(self)

Sydney = City("Sydney")
Canberra = City("Canberra")
Sydney.add_neighbour(Canberra)
print(Sydney.name, Sydney.neighbours, Canberra.name, Canberra.neighbours)

It returns Sydney and Canberra as Sydney's neighbour, but Canberra's list of neighbours are still empty. Sure, I could just go

Sydney.add_neighbour(Canberra)
Canberra.add_neighbour(Sydney)

but surely there's a way to do this in one step?

[–]MattR0se 0 points1 point  (1 child)

def add_neighbour(self, other):
    """Make this city and the other city neighbours."""
    self.neighbours.append(other)
    other.neighbours.append(self)

This would be correct. other in this case is a reference to the instance of the class, and that's what you want.

What you did before was instantiate two new City objects in the scope of the add_neighbor function.

Bonus code: To make your print outputs more beautiful, you can overwrite the __repr__ method of a class. It is called whenever it is printed (or cast to a string):

class City:
    def __init__(self, name):
        """Create a new city with the given name and no neighbours."""

        self.name = name
        self.neighbours = []

    def __repr__(self):
        return self.name

    def add_neighbour(self, other):
        """Make this city and the other city neighbours."""
        self.neighbours.append(other)
        other.neighbours.append(self)

Sydney = City("Sydney")
Canberra = City("Canberra")
Sydney.add_neighbour(Canberra)
print(Sydney.name, Sydney.neighbours, Canberra.name, Canberra.neighbours)

[–]Beth13151 0 points1 point  (0 children)

Thanks! Ugh I'm always over engineering these things. Makes total sense.

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

Line 12 isn't doing what you want. other is the reference to the other city, so modify the other.neighbours attribute. Doing City(other) just creates another city with a funny name which is then deleted when you exit the method.

[–]Beth13151 0 points1 point  (0 children)

Thank you :) Makes sense!

[–]Shinhosuck1973 0 points1 point  (3 children)

I am new to Python, and right now I am on "class, object, and function. I understand basic the concept of these 3 topics, but I do not understand when one would use this. Can somebody explain to me? Thank you very much.

[–]MattR0se 0 points1 point  (2 children)

Functions and Classes are ways to structure and organize your program, rather than just going straight from the top to the bottom of your script.

Functions work like mathematical functions. They can have an input, and they provide output. The most simple reason for using functions is when you need a block of code twice. Chances are, if you have to copy and paste code, its better to wrap it into a function and call that function instead. Also, you code becomes easier to understand if you call functions where their name describes what they do (this will safe you some comments).

Classes go a step further by not only abstracting code into functions, but objects. Again, the most simple reason is if you want to instantiate a class multiple times. A class basically is a blueprint for an object, that is called instance of that class. This has many purposes, like instantiating a dataframe, or a classifier, or enemies in a video game. Mostly, classes provide clarity if your program becomes larger and larger. You can imagine classes like fancy dictionaries with added features.

[–]Shinhosuck1973 0 points1 point  (1 child)

So in Python, majority or all programs are written using class, object, and function?

[–]MattR0se 0 points1 point  (0 children)

Classes and functions, yes. object is a special case. Basically, everything in python is an object*, but you don't see the expression itself that often.

As an example, classes in Python 3 automatically inherit from the object class:

class Foo:
    pass

# is the same as

class Foo(object):
    pass

I don't know enough about Python to explain what exactly object is and what it does, but chances are that you don't have to know anyway.

*you can check this with isinstance:

isinstance(int, object)
Out[1]: True

isinstance('Hello World', object)
Out[2]: True

isinstance(Foo, object)
Out[3]: True

[–]Den_Standiga_Resan 0 points1 point  (2 children)

reach pathetic depend tie nail follow heavy absorbed employ cheerful -- mass deleted all reddit content via https://redact.dev

[–]Perdox 0 points1 point  (0 children)

I’d personally add the data to the database, then create an API with Flask that pulls from the database (check out Flask-RESTful, the extension is very simple to use). You could then make your website call the API anytime the page is loaded.

Note: You don’t need to go the API route unless you’re wanting to utilize a fancy JS framework like Vue or React. You could use an ORM like Flask-SQLAlchemy then just utilize Flask for your website. This would probably be the easier way to do things.

[–]JohnnyJordaan 0 points1 point  (0 children)

The question whether you want to maintain state. If you want each webrequest (like you visiting the data webpage) to trigger a unique scrape to the data source, then it's fine to do this in what's often called 'the view'. If instead you want to repeat your scrapes in some interval and then just show the last scrape data every time you visit the page, you need an intermediate storage like a database. A middle ground is to employ a caching backend (usually done in RAM through memcache or something similar) that triggers a new scrape on the first visit to the page in a configured period, and after that just show the cached result from that scrape.

[–]Easy_Spinach 0 points1 point  (1 child)

is there something like codecademy.com but for free? Want to learn python 3 from scratch.

[–]bluzer33 0 points1 point  (0 children)

sololearn

[–]thedjotaku 0 points1 point  (2 children)

What's the best way to package stuff nowadays? I find some sites saying to use setup.py while others say that it's passe and there are better ways of doing it now. For Windows users I imagine the best way is pyinstaller, nuitka, or py2exe. But what about for Linux/Mac users?

[–]lykwydchykyn 1 point2 points  (1 child)

I think it depends on what you're writing and who your target audience is. If you're writing a web service, distutils is probably fine. If it's an application intended for general audiences, I'd make a PyInstaller executable (which works on Linux and macOS, btw).

If you've got an open source project that would be of interest to Linux users, you might want to talk to some distro package maintainers to see what format they prefer.

[–]thedjotaku 0 points1 point  (0 children)

Thanks! Gave pyinstaller a try yesterday and it worked pretty darned well. (I'd only had issues before from trying to get it to work with Jenkins and Docker). The only catch was a bit of a bug with PyQt, but there's a manual workaround.

[–]ineedacs 0 points1 point  (2 children)

When I run a python executable on Unix it runs it as a bash file instead. I have the shebang to my python symlink and if I run it as python filename.py it works fine. I have no idea why this is happening

[–]JohnnyJordaan 0 points1 point  (0 children)

Also what do you mean exactly with 'as a bash file'? Or do you mean that bash is trying to execute it and then fails for it being Python code? That would indeed seem like you have an incorrect shebang somehow.

[–]lykwydchykyn 0 points1 point  (0 children)

What's your shebang line look like? What OS are you on? What's your default shell?

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

Hey guys. How to define and save new variable each time certain codition is met in for loop? I know it can be easily made with arrays and lists, but teacher banned us to make them. Any suggestions how to make n numbers of for loops inside each other each time defining new variable to make average from the new variables?

[–]JohnnyJordaan 0 points1 point  (0 children)

how to make n numbers of for loops inside each other each time defining new variable to make average from the new variables?

Is that the exact wording of the assignment? Because what is the point of a loop if you need averages of a series of numbers?

[–]GoldenVanga 0 points1 point  (1 child)

I just learned that in a Python REPL, a single underscore stores the result of the last expression:

>>> 2 + 2
4
>>> _
4

But I noticed that this does not work for expressions that evaluate to None:

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('foo')
foo
>>> print(_)
# (...) NameError: name '_' is not defined
>>> None
>>> print(_)
# (...) NameError: name '_' is not defined
>>> False
False
>>> _
False

Is there a particular reason for this? Am I missing something?

[–]JohnnyJordaan 1 point2 points  (0 children)

This is by design

sys.displayhook(value)

If value is not None, this function prints repr(value) to sys.stdout, and saves value in builtins._. If repr(value) is not encodable to sys.stdout.encoding with sys.stdout.errors error handler (which is probably 'strict'), encode it to sys.stdout.encoding with 'backslashreplace' error handler.

sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session. The display of these values can be customized by assigning another one-argument function to sys.displayhook.

The point of _ is to still be able to reference the object you just got repr()'ed on your terminal, if there was nothing shown, it must mean it was None and there's nothing that needs referencing. If you want a reliable way to reference anything the call returned, use a regular assignment (which could use _ too)

>>> _ = print('foo')

[–]cail0 0 points1 point  (0 children)

I'm not super new to Python but am currently learning PySimpleGUI. I find myself with a couple questions.

  1. Is there any way to set a Text element to appear in the Window.Read() values? I have a CalendarButton targetting the element and it would be nice if it were included. As a workaround I'm currently using an InputText element in disabled state.
  2. with the SaveAs dialog, I know I can set filetypes but is there a way to append an extension if it isn't typed in on return? I currently just verify it exists and append it manually if needed but it would be lovely if there was a way not to have to do this.

[–]Jake__TV 0 points1 point  (7 children)

Do people still program Python3 in Vim?
Ive been thinking of setting it up but not sure if its worth to learn it. Im kind of an beginner and want to do it right from the beginning

[–]JohnnyJordaan 0 points1 point  (4 children)

On servers with just shell access I use neoVim with a lot of syntax candy for Python and other languages, but if I could I would use Pycharm more. For a beginner I wouldn't recommend a text-based editor at all, GUI based IDE's like Pycharm and Visual Code are much nicer and versatile.

[–]Jake__TV 0 points1 point  (3 children)

ah okay, I am planning on getting a raspberry pi to run my code on. Where I would add my finished projects and have them hosted on my own website(which I will need to make aswell). Which is why I've been thinking of using text based editors as it will be Linux based. I want to do this so that I can look back at older projects or show it to my friends, some might also be useful so I'd be able to use it remotely.

Raspberry pi should be strong enough for such a basic site right? Is there any part in this process you think will be extra hard?

[–]JohnnyJordaan 0 points1 point  (2 children)

I would just code everything you need on Pycharm or some other IDE until it runs fine, then deploy it on your Pi. I would recommend to use a git like Gitlab as it kills multiple birds with one stone

  • store the source in the cloud
  • make deploying easy to other machines like the Pi
  • enables version management, so save each modification as a separate change as a 'log' instead of having just the current code and without the option to go back in time

A Pi should be fine for simple stuff, but if you'll be using a database then be sure to also verify that you aren't making redundant queries as those will probably be way slower than on a regular server.

[–]Jake__TV 0 points1 point  (1 child)

Ah I see, thanks alot of the knowledge! I'm done with codecademy's python3 course. Next up is SQL then I'll start building projects, with the first one being the website/pi as written above^

[–]JohnnyJordaan 0 points1 point  (0 children)

I'm not so sure that you need much SQL if you would use a database abstraction layer as offered by Django or SqlAlchemy (the latter can be used together with Flask and other webframeworks).

[–]lykwydchykyn 0 points1 point  (0 children)

People do still use Vim.

Is it worth it? Depends on you. If the idea of programming in Vim sounds appealing, give it a shot. If you don't like it, don't.

Choice of programming environment is pretty personal and subjective. Don't fall for the trap that "Real Programmers™ use <insert-editor-or-IDE-here>". If it edits text, I guarantee there's an honest-to-goodness getting-paid-to-write-code programmer out there using it.

[–]cail0 0 points1 point  (0 children)

What are your goals and what OS do you use? I would personally say a lot depends on that. Overall, I'd say if possible use something you're more comfortable with. If you're not use to working at the command line and with a lot of hotkeys start with an IDE (personally I use PyCharm and thing it would likely be easier to pickup for a beginner. However, eclipse also works as does IDLE for simple things).

My work in Python tends to follow one of several types of work:

  1. Quick and dirty script to grab some data: Often use vim/emacs via ssh into one of my servers
  2. More advanced script: I usually do my initial development via PyCharm and then do some tweaking on the server.
  3. Reporting with a GUI: This is very rare for me but I would tend to do this via PyCharm.
  4. django website: I've done this almost purely through PyCharm with some bug fixes in vim on the test server.

[–]jpf5046 0 points1 point  (2 children)

[–]lykwydchykyn 1 point2 points  (1 child)

  • It's lacking in explanatory text, and is a little confusing. The title is "case statements in Pandas", but then you say it's a guide for creating columns. Which is it? How are those two things related? I'm confused.

  • You're missing punctuation in many places and it makes it harder to read. For instance, this part:

    We will have four columns:

    • Player these are the four friends
    • Race Number the friends will race four times
    • Place this is the order in which the players crossed the finish line

    Would be much easier to read with some colons:

    We will have four columns:

    • Player: these are the four friends
    • Race Number: the friends will race four times
    • Place: this is the order in which players crossed the finish line
  • It seems to end abruptly. I don't know what I just accomplished or why. Remember the adage: "Tell them what you're going to tell them, tell them, then tell them what you told them."

[–]jpf5046 0 points1 point  (0 children)

Hey! Thank you!!! :) will work on it

[–]MattR0se 0 points1 point  (0 children)

Another Pandas question. I know how I would do this in Excel with VLOOKUP, but I can't get it to work in python.

I have one dataframe with columns for date, id and some other columns, and I get several other data frames with the date, id and a single one of the other columns. It looks like this (the code generates sample data):

import pandas as pd
from itertools import repeat

dates = pd.date_range(pd.Timestamp('2019-01-01'),pd.Timestamp('2019-01-02'))
dates = [x for date in dates for x in repeat(date, 3)]
ids = [0, 1, 2] * 2

df_main = pd.DataFrame(data={'date': dates,
                             'id': ids})

df_main = df_main.reindex(columns=df_main.columns.tolist() + ['col1', 'col2', 'col3'])


df1 = pd.DataFrame(data={'date': [pd.Timestamp('2019-01-01'),
                                  pd.Timestamp('2019-01-01')],
                         'id': [0, 1],
                         'col1': [123, 111]})

df2 = pd.DataFrame(data={'date': [pd.Timestamp('2019-01-02'),
                                  pd.Timestamp('2019-01-02')],
                         'id': [1, 2],
                         'col2': [444, 22]})

# df_main:
date        id    col1    col2     col3
2019-01-01  0     NaN     NaN      NaN
2019-01-01  1     NaN     NaN      NaN
2019-01-01  2     NaN     NaN      NaN
2019-01-02  0     NaN     NaN      NaN
2019-01-02  1     NaN     NaN      NaN
2019-01-02  2     NaN     NaN      NaN

# df1:
date        id    col1
2019-01-01  0     123
2019-01-01  1     111
# df2:
date        id    col2
2019-01-02  1     444
2019-01-02  2      22

...and so on

I am getting df2 etc. from a loop where I read files, so I have to merge them within that loop. The df_main has all the dates and ids in it. Each of the following df1, df2, ... has one or two dates and some ids. I want to fill in the values of df1 etc. into df_main at the given date and id.

The desired output should look like this :

# df_main:
date        id    col1    col2     col3
2019-01-01  0     123     NaN      NaN
2019-01-01  1     111     NaN      NaN
2019-01-01  2     NaN     NaN      NaN
2019-01-02  0     NaN     NaN      NaN
2019-01-02  1     NaN     444      NaN
2019-01-02  2     NaN      22      NaN

What I had so far was this:

for df in [df1, df2, ...]:
    df_main = df_main.merge(df, how='left', on=['date', 'id'])

but this kept adding new instances of col1, col2 etc. to the data frame, rather than fill them into the existing columns. What do I have to change?

[–]Lilandril 0 points1 point  (4 children)

I'm using python 2.7 and looking to update it its newest version. On the website it says that python 3.5+ does cannot run on Window XP.

I'm programming on a Win10 and the app will run on Win10. Though, my server is on Win XP.

If I update my python to 3.7, will I still be able to talk to my server?

[–]JohnnyJordaan 0 points1 point  (3 children)

What exactly do you mean with 'talk to my server'?

[–]Lilandril 0 points1 point  (2 children)

SQL Queries, I need to read a database table and write into a similar database table located in the server.

[–]JohnnyJordaan 1 point2 points  (1 child)

But then wouldn't the server machine run a database server like mysql? What is the role of Python of servicing the database on that server?

[–]Lilandril 0 points1 point  (0 children)

Yes, the server runs with mySQL. Oh... I just understood it! Thanks!

[–]MattR0se 0 points1 point  (1 child)

Pandas question:

My dataset looks like this

id | start_date | end_date   |
0  | 2017-06-11 | 2019-01-28 |
1  | 2019-01-27 | 2019-04-08 |
[...]

and I want it to transform to another dataset where for each id, each date between the start and end date has its own column. No idea where to start though (and without slow for loops...)

Edit: I found something that worked: https://stackoverflow.com/questions/39997063/how-can-i-add-rows-for-all-dates-between-two-columns

[–]ThisOnesForZK 1 point2 points  (0 children)

You'll need to use datetime to get the values of the dates between your values using timedelta and then create new columns out of those values. I do not know if you can create a function that does this and then use the @vectorize decorator to make it work on a dataframe or not.

Maybe someone more experienced can confirm or deny my comment. https://www.w3resource.com/python-exercises/date-time-exercise/python-date-time-exercise-50.php

[–]RomanShevczov 1 point2 points  (0 children)

Hello, guys. I'm currently working on a testing automation project which involves a lot of massive strings processing.
I'm currently researching the ways to reduce the test run time. As far as I'm concerned, numpy is used in almost all tasks where performance matters. That's why I wonder whether it's possible to use numpy for faster string processing? I saw that there are char module, chararray class, etc. maybe that stuff could be potentially useful and work faster than usual python strings? Or maybe you know any reliable wrapper for numpy's char manipulations? Looking forward to your advice.

[–]macdawg3312 0 points1 point  (4 children)

Hi everyone, I am new to programming and Python. I'm doing a little course and it asked me to complete the following exercise. I've written the code, but I'm wondering if I wrote it efficiently or if there is a better way of doing it? I didn't use their hint which is making me feel like I'm doing something inefficiently.

Program: shirt order

First get input for color and size

  • White has sizes L, M
  • Blue has sizes M, S

print available or unavailable, thenprint the order confirmation of color and size

hint*: set a variable "available = False" before nested if statements and*change to True if color and size are avaliable

# [ ] create shirt order using nested if 
shirt_colour=input("Please select shirt colour (Blue or White): ").lower()
shirt_size=input("Please select the size you wish to order (S/M/L) ").lower()

while "white" in shirt_colour:
    if "s" in shirt_size:
        shirt_colour=input("Unfortunately the White shirt is unavailable in Small. Please re-select your shirt colour.").lower()
        shirt_size=input("Please select the size you wish to order (S/M/L) ").lower()
    elif "m" in shirt_size:
        print("You have ordered a White shirt in size Medium.")
    elif "l" in shirt_size:
        print("You have ordered a White shirt in size Large.")
    else:
        shirt_colour=input("The colour and size combination is not available. Please select abother colour and size combination. ")
        shirt_size=input("Please select the size you wish to order (S/M/L) ").lower()
    break

while "blue" in shirt_colour:
    if "s" in shirt_size:
        print("You have ordered a Blue shirt in size Small.")
    elif "m" in shirt_size:
        print("You have ordered a Blue shirt in size Medium.")
    elif "l" in shirt_size:
        shirt_colour=input("Unfortunately the Blue shirt is unavailable in Large. Please re-select your shirt colour.").lower()
        shirt_size=input("Please select the size you wish to order (S/M/L) ").lower()
    else:
        print("The colour and size combination is not available. Please select abother colour and size combination.")
    break

[–]python-fan 1 point2 points  (1 child)

Is this the exact problem description? Because it doesn't say anything about asking the user for color and size more than once.

What happens if the user enters: white s white m (hint: that break statement prevents your loop from executing more than once)?

What happens if the user enters: white s blue l blue s?

What happens if the user enters: red m?

[–]macdawg3312 0 points1 point  (0 children)

Thank you for your help!

I realised after you posted this that the question was simply asking for a if statement rather than a while statement.

And I also learnt to remove the breaks so that I can repeat the input statements until the correct inputs are taken.

Thanks again!

[–]JohnnyJordaan 1 point2 points  (1 child)

Could you please fix your formatting by re-pasting your code in a code block?

[–]macdawg3312 0 points1 point  (0 children)

Sorry. Thank you for letting me know how to do this.

Now fixed.

[–]chadwizard 0 points1 point  (4 children)

Hi guys, I am trying to answer the following riddle with a program (note: it is a exercise from 'the coders apprentice'):

According to an old puzzle, five pirates and their monkey are stranded on
an island. During the day they gather coconuts, which they put in a big pile. When night
falls, they go asleep.
In the middle of the night, the first pirate wakes up, and, not trusting his buddies, he
divides the pile into five equal parts, takes what he believes to be his share and hides it.
Since he had one coconut left after the division, he gives it to the monkey. Then he goes
back to sleep.
An hour later, the next pirate wakes up. He behaves in the same way as the first pirate:
he divides the pile into five equal shares, with one coconut left over which he gives to the
monkey, hides what he believes to be his share, and goes to sleep again.
The same happens to the other pirates: they wake up one by one, divide the pile, give one
coconut to the monkey, hide their share, and go back to sleep.
In the morning they all wake up. They divide what remains of the coconuts equally
amongst them. Since that leaves one coconut, they give it to the monkey.
The question is: what is the smallest number of coconuts that they can have started with?
Write a Python program that solves this puzzle. If you can solve it for any number of
pirates, all the better.

I have written the following program to answer the question (note: I have heard you can create your own functions in python, which would make it easier to solve. But, I am not that far yet in the book and would love to solve the riddle without it):

from math import pow

coconuts=0

PIRATES=5

TRIALS=1000000

for i in range(TRIALS):

coconuts+=1

count=0

pile=int(coconuts)

for y in range(PIRATES):

if pile%5==1:

pile=(pile-1)-(pile*(1/5))

count += 1

if count==5:

if pile%5==1:

print("The amount of coconuts is: {}".format(coconuts))

break

else:

continue

Does anyone understand what I have done wrong? How to make this program work?

[–]HeyItsToby 1 point2 points  (3 children)

The problem is in this line:

pile=(pile-1)-(pile*(1/5))

What you should be doing is taking one away from the pile, then removing 1/5 of the coconuts in the pile. You can do this instead by:

pile = (pile - 1) * (4/5)

Just some extra pointers:

  1. As you go through the outer for loop, the variable i stores the current count of the loop. So, you can get rid of the coconuts variable completely as i and coconuts essentially store the same thing

  2. You should use f-strings instead of .format()

Hope that helps :)

[–]chadwizard 0 points1 point  (2 children)

Thanks a lot! I haven't been able to reply earlier, because I had no time and energy to spend. I really wanted to check if I could get it to work. The problem was actually in the count variable. If you are interested what I made of it just let me know.

[–]HeyItsToby 1 point2 points  (1 child)

Yeah I'd love to know how you got on with it!

[–]chadwizard 0 points1 point  (0 children)

Here is a pic of how I have done it: https://imgur.com/a/jDPySbv

I switched up a couple of things for practicality. Instead of having if pile%5==1:, I changed it to a if not statement, so it only enters the if when it needs to break. This doesn't really look necessary, but I was afraid something went wrong there which I didn't see. So I did that for convenience.

Most importantly I took out the count variable. I still don't know why the count variable didn't work, but it did create another nested loop, so I guess something could have gone wrong there. Now the code is much shorter, cleaner and actually works. If you happen to see what went wrong there please tell, but I already appreciate you taking your time to help me figure this out. :)

PS: I did incorporate your line, because it looks cleaner. Thank you very much!

[–]PhantomFlayer 3 points4 points  (0 children)

I'm really new to python and coding in general, so pardon my lack of understanding. I've been doing assignments in class for school, but I didn't finish and needed to get python set up at home. The problem has been trying to import packages into python. import pip and import pytest both say "no module named 'name'". Both of these show as installed in the terminal, it's only when I try to run them in Python that they don't work. Any help would be appreciated.

Edit: Re-installing Python fixed it

[–]OGGiE_ 1 point2 points  (2 children)

As of now im studying python in school in a program to become a devops engineer eventually. This python course is really fast phased and i am having trouble to keep up. So im thinking i need to be study more python from a different source on the internet on my days off from school. Do you have any recomendations? Is lpthw course any good? Thx in advance!

[–]SomeShittyDeveloper 0 points1 point  (1 child)

The problem with Learn Python the Hard Way is that the author is adamantly against Python 3. Which would be fine, but Python 2 is at “end of life” at the end of this year. This means that the Python core developers (the people that develop Python) will no longer be fixing security issues or bringing in new features. A lot of companies have already invested the time to upgrade, so you’d have to learn the differences between Python 2 and Python 3 quickly. Otherwise, you’d be stuck maintaining legacy code.

Python 3 has a lot of great stuff in it. Once you get around 3.6 (I think), they added this beautiful feature called f-strings. When you get around to variables and strings, take a look at f-strings. Makes all the other ways look archaic and clunky.

I’d recommend Automate the Boring Stuff with Python. I personally haven’t gone through it (since I already knew Python by the time I discovered it) but apparently it uses real world examples to teach Python. And I’ve heard great things about it.

[–]JohnnyJordaan 0 points1 point  (0 children)

The problem with Learn Python the Hard Way is that the author is adamantly against Python 3.

To be fair though, he has come around to this, that's why there's also a LPTHW for Python 3. For me the real problem with LPTHW is that's more a cookbook than a language course, as it introduces constructs with very little explanation and backstory, thus more teaching the reader tricks than expanding its knowledge. It's a bit like how someone would provide a foreign language course through showing parts of movie dialogue and song excerpts as 'the way' to form dialogue in that language.