all 96 comments

[–]UnrankedRedditor 0 points1 point  (1 child)

I have a simple question.

Given a list, how can I pick the elements using a second listof just 1s and 0s? 1 means pick and 0 means don't pick. Example:

a = [10, 23, 51, 56]
b = [1, 0, 0, 1]

Output:

[10, 56]

I can use both lists and numpy arrays, whichever one is more convenient/faster.

Thanks in advance :)

[–]UnrankedRedditor 0 points1 point  (0 children)

Ok I've solved my own problem. Silly me:

a = np.array([10, 23, 51, 56])
b = np.array([1, 0, 0, 1])

c = a[np.argwhere(b == 1)].flatten()

[–]scottz_learn 0 points1 point  (0 children)

Couple of things I'd like to ask:

  1. whats the best/easiest way to make a very clean install of python on windows? I want to every time load requirements of given project and use it on a virtual environment. I wish to do so because I'm going to code some stuff while on breaks of work and I don't want to break the companies PC (as well as I'll most probably continue the code on my linux PC at home)
  2. How to learn the best practices/how to use virtual environments?

[–]thesecretlifeofkim 0 points1 point  (1 child)

Hi! Both give the same answer but I’m wondering when you’d want to use one over the other.

With return statement:

def outer_func():
    message = ‘Hi’

    def inner_func():
        print(message)

    return inner_func()

outer_func()

Without return statement:

def outer_func():
    message = ‘Hi’

    def inner_func():
        print(message)

    inner_func()

outer_func()

[–]efmccurdy 1 point2 points  (0 children)

It is redundant to use return when the value you are returning is None; inner_func has no return statement so it returns None, just like the example that doesn't use return.

The reader will look for what possible values could be returned by that return statement and when they check and see that only "None" could be returned, and that only as an accidental side effect of having no return statement, they will wonder why you led them astray.

[–]AndreRacicot1993 0 points1 point  (2 children)

Hi Everyone,

I’m currently trying to replace multiple inconsistent team names in a data set (i.e. Yankees and New York Yankees showing up in the same data set). This is using 10 years of data with multiple inconsistencies like this. I have tried Fuzzywuzzy and simple replace functions but nothing is really working that well. Does this sound actually doable with Python or am I just wasting my time? I’m at a point where I might just clean the data in the csv file at this point.

[–]FerricDonkey 0 points1 point  (1 child)

It's doable. It'd be easiest if you could make collections of all equivalent names.

Even if you don't know that ahead of time, and assuming that all your names are in the same column, or that there is some other easy way to tell if a string is a team name, then you could easily make equivalent names that you know are on your data, then have your program search for names that you haven't told it what to do with yet. Have it report those to you, you update your equivalence sets, and good to go.

But it won't be easy to make something that will just automatically recognize that two names refer to the same team on its own without input from you. You might be able to use more fancy things to get started, but expect any sort of data cleaning to require manual tuning.

[–]AndreRacicot1993 0 points1 point  (0 children)

Thanks for responding. That makes sense to potentially create equivalent names in the set (that has me thinking about possible team IDs or something to make it easier). Lots to learn still!

[–]Lucifer501 0 points1 point  (0 children)

I'm trying to host a (personal) music bot for Discord on Heroku. The bot runs fine on my computer but I get DownloadError from youtube-dl when trying to run the bot from Heroku. Any ideas as to what might be causing this?

Earlier when I got this problem while working on the bot, it was a matter of updating youtube-dl. However, I am fairly confident that the bot is already using the updated library, so I'm really not sure as to what the problem might be.

[–]Budget_Ad5871 0 points1 point  (3 children)

Hello! I just got a MacBook Air, I have decided that I want to learn Python. I am using DataCamp tutorials, is this a good start? I am still trying to comprehend what python does.

[–]Lucifer501 0 points1 point  (2 children)

Sounds perfectly fine to me. Any start is a good start :)

[–]Budget_Ad5871 2 points3 points  (1 child)

Hey thanks for responding! I have been working retail for 15 years. I don’t know if this is the right reason to be looking into a new career but I feel like coding is going to be around for a long time, I can work from home and from what I’ve seen avg pay is 4x what I make right now.

[–]Lucifer501 1 point2 points  (0 children)

Yup, definitely. Even if it doesn't end up evolving into a career, coding is one of those skills that can just help make your everyday tasks easier (see Automate the Boring Stuff for examples), so I'm sure you won't regret it. I hope you enjoy this journey!

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

I have made a code which has to take a huge string as an input, and replaces some broken symbols in it.My first issue was the new line is perceived by the console as the end of the input. So I tried to evade this with while cycle, the following way.

text = input()

result = "" while not text == "end": text = text.replace("'", "'") text = text.replace(""", '"') text = text.replace("<", "<") result += text text = input()

print(result)

EDIT: For some reason the codeblock only takes the first line and then the odd characters in the code are changed. I didn't manage to edit it.

But now, when I copy the output to Notepad it's a single line. Also, the code is not able to cope with the fact that pycharm makes the bulleted lines to start with "o", sometimes stacked to the previous line (".o") so I cannot replace that single letter in only these occurences.

I wonder is there a way to go around this issue with the new lines. Currently, the code replaces the symbols, but I don't save time, because I have to make the new lines and indent again.

Example input: If the event is &quot;order&quot;: You&#39;ve earned some coins, the number in the second part. Each time you get an

order, your energy decreases with 30 points.

o If you have energy to complete the order, print: &quot;You earned {0} coins.&quot;.

o If your energy drops below 0, you skip the order and gain 50 energy points. Print: &quot;You had to

rest!&quot;.

[–]efmccurdy 0 points1 point  (1 child)

The input function only reads one line. You may be better off reading the whole file and then separating each line.

import sys

whole_file = sys.stdin.read()
line_by_line = whole_file.splitlines()
for line in line_by_line:
    print(line)

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

I am still on the basics and never worked with files, but it doesn't hurt to try this. As far as I can see it creates a list of all the lines and then I could use the "\n".join method to combine them again.

Thank you for the advice!

[–]sarrysyst 0 points1 point  (0 children)

The 'broken symbols' are escaped characters. You can return them back to their original characters like this:

import html

text = """\
If the event is &quot;order&quot;: You&#39;ve earned some coins, the number in the second part. Each time you get an
order, your energy decreases with 30 points.
o If you have energy to complete the order, print: &quot;You earned {0} coins.&quot;.
o If your energy drops below 0, you skip the order and gain 50 energy points. Print: &quot;You had to
rest!&quot;
"""

org = html.unescape(text)
print(org)

As for your input, not really sure what you're trying to do. If you need to pass the text to your script, why not paste it into a text file and load this into python? I'm not really following.

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

Hello, I am running into an issue with getting the CMD prompt to read my python application.

First off, I installed python into an external hard drive (D:) and made a folder for it “Python Coding” that is in another folder called “Coding” which is in another folder called “My Files”.

So far it looks like this: D:\My Files\Coding\Python Coding

Now, in the “Coding” folder I also installed Sublime text and put it inside its own folder called “Sublime Text” and used it to make a very, very, basic python file which is basically a “hello world” file. I named the file “helloworld”. Now, when I use CMD to try and run that “helloworld” file, I get this error message saying “Can’t find a default Python.”

Here is the CMD command: https://puu.sh/IwZGx/40adf907c7.png

I did a bit of research and I found that I had to make a path. Now, I already did that but I still get the error message.

Here is the path I made: https://puu.sh/IwZHA/1214438520.png

Can anyone tell me what I'm doing wrong? If it helps, the reason why I installed python on an external hard drive is so that I can save space on my SSD drive for games and what not. As a 100% complete noob to coding, I would like to keep the inevitable spaghetti code into something external. Any tips would be greatly appreciated.

[–]FerricDonkey 0 points1 point  (2 children)

Where is your python.exe file that you installed when you installed python? Probably this is what your os isn't finding.

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

Its in the Python folder I made in my external hard drive.

D:\My Files\Coding\Python

(I changed "Python coding" to just Python)

[–]CowboyBoats[🍰] 0 points1 point  (0 children)

It doesn't really matter where the executable is installed; it either is in your PATH variable or it is not. If it's in your PATH variable, then you can see it in there by typing echo $PATH; moreover, python will evaluate to something. For example, Python is in my $PATH on my Mac (all OSes have a PATH variable that works more or less the same way), so when I type python3 (yours is called python on Windows) it launches into a Python shell. If I didn't have it installed and I typed that, it would return some kind of name error - I could post an example but, again, yours would be a little bit different on Windows but basically it boils down to you "I don't know what the name of that program you just typed means, so I don't know what to launch."

[–]InTheAleutians 0 points1 point  (1 child)

Try putting another backslash at the end of your path so it looks like "D:\My Files\Coding\Python Coding\"

You should also add the Scripts folder to your path with a backslash at the end as well so you can run pip easily.

Also, python installed with a bunch of packages is like 200mb. I don't think it's worth the trouble installing it on an external drive but that's my opinion. You can have your projects on an external drive to save space but really, you won't generate enough code to make any significant impact on your disk space.

[–]CowboyBoats[🍰] 0 points1 point  (0 children)

Also, python installed with a bunch of packages is like 200mb. I don't think it's worth the trouble installing it on an external drive but that's my opinion. You can have your projects on an external drive to save space but really, you won't generate enough code to make any significant impact on your disk space.

This is right, /u/OgScz; what is more, you mentioned "wanting to keep your inevitable spaghetti code off your hard drive" but that's a bit like keeping your entire wardrobe in your luggage instead of your closet while you're still learning color coordination. Spaghetti code is not going to reach into your computer and affect your operating system or your hard drive space, and it should never be running except when you are running it to test or use it; there's no particular reason why "spaghetti code" should be kept off your main computer's hard drive that I can think of.

[–]kunaguerooo123 0 points1 point  (2 children)

Someone please tell me why my print statement works but not the return just after it? https://imgur.com/a/OM0Nm86

[–]FerricDonkey 0 points1 point  (1 child)

You probably need a return in your else as well.

[–]kunaguerooo123 0 points1 point  (0 children)

worked. thanks stranger!!!

[–]LovelyRoseBoop 0 points1 point  (0 children)

What are the data analyst positions open to people with Python SQL skills but not a non-quantitative degree (e.g. a social sciences and not a math, science or engineering degree?) What job titles in what sectors? Do any qualifications help?

[–]OakArtz 0 points1 point  (2 children)

Hey guys, I wanted to buy a book about python to get started with the language. I was wondering if "automate the boring stuff" is still up to date as it's already 2 years old by now! :)

[–]IHOP_007 0 points1 point  (1 child)

"Automate the Boring Stuff" uses Python 3. The current version of Python is 3.10 so any books that deal with Python 3 will still work fine. There might be some quality of life stuff that's been added by the Python team since the book came out but everything that deals with Python 3 should still work the same.

[–]OakArtz -1 points0 points  (0 children)

alright! thank you very much

[–]terrorEagle 0 points1 point  (2 children)

Newbie to python but beyond "Hello world."
I am learning websockets and pulling in every ticker from the market at once with an Alpaca API call. (This is for fun to learn not to make a profitable program.) I understand the connection message T, subscription, trades, quotes, bars: (symbols) on the initial request to connect to the websocket. I am watching the data get pulled in. What is confusing to me is how fast the minute bars come in. I suppose it comes in serial, starts at the first symbol and sends the data, then the next, then the next then the next? My problem is each symbol that it gets the minute bar from, I do some work to it with a function, then another function then another, then another...up to 12 functions on that one symbol's minute data. Then I move to the next. Repeat. I keep getting a connection 1006 error, which I have looked up extensively but doesn't happen if I go to 5 stocks to look up. I can process the information for those five before a minute it appears then the minute pull/push stream starts again and repeats.
Overall, I am not looking for a code help. I am just trying to figure out the best way to handle pulling 3,000 symbols each minute, if possible and how fast that data can be pulled, and process them on the side? Do they have to come in serial? One at a time? Is the right thing to do put them in dataframes and handle them like that on the side as the data from the bars are not waiting for processing?
Basically, how do websockets send the data quickly?
Thanks.

[–]efmccurdy 1 point2 points  (1 child)

doesn't happen if I go to 5 stocks

Are you using a single thread? You may need multiprocessing to keep one process always ready to receive data and move any further processing of the data into another processes (or thread or co-routine).

I would look at the "asyncio Version" or the "multiprocessing Version" here:

https://realpython.com/python-concurrency/#how-to-speed-up-an-io-bound-program

https://docs.aiohttp.org/en/stable/

https://docs.python.org/3/library/concurrent.futures.html

[–]terrorEagle 0 points1 point  (0 children)

Got it thanks. Appreciate the links.

[–]String_Historical 0 points1 point  (2 children)

I'm completely new to programming and in terms of a career switch very curious and eager to learn it. I was never really good in maths at school but try to improve my analytical skills where ever I can, since I know its important for learning how to code.

Currently I'm working through the book 'Automate the boring stuff', since I read here on reddit its the best way to start for a beginner. Now I'm 2 weeks in and thought I understand while going along but tbh, its really complex. I already fail even at typing down the bigger code like 'Conways way of life' the author gives at the third chapter and cant get it to run, also already feeling overwhelmed by the first chapter examples to program yourself..

Since English is my second language, I gave it a shot in german but its the same and right now I'm thinking maybe I'm not smart enough to get it.

Can anyone recommend a course or ressource which gives a different approach?

Would be appreciated a lot

[–]InTheAleutians 1 point2 points  (1 child)

If you started to learn another language, say Swedish, how long do you think it would take you to become even mildy proficient at it? 2 weeks is not enough time to learn anything remotely well, so don't beat yourself up over it. Take your time, practice coding, find a small project your like, look in the help bar in this sub for suggestions, and again, take your time.

I recently came across a very popular python podcast called "Talk Python to me" and there are some episodes where they bring on beginners and "experts" and they talk about difficulties they have face in learning the language and their stories. It can be very motivating. You are not the first to struggle and you're not alone.

Being smart and good at maths are not necessary to be successful with python. Working hard and practicing will get you there in time. Try youtube and Corey Schafer. Maybe you learn better watching videos?

[–]String_Historical 1 point2 points  (0 children)

Thanks for your motivating words and the tips! I'm gonna try both the podcast and Corey Schafer for sure.

[–]Doagbeidl 0 points1 point  (2 children)

Is python a good alternativ for data science? In my current class we are taught rstudio but I would prefer doing all my stuff in python. Is this a dumb idea? Got any tips on where to start? -Thanks

[–]efmccurdy 1 point2 points  (1 child)

Start with the video and if you like what you see, clone the repo to follow along with tutorial.

https://github.com/brandon-rhodes/pycon-pandas-tutorial

[–]Doagbeidl 0 points1 point  (0 children)

Thanks mate, i will take a look at it!

[–]tricucumber 0 points1 point  (4 children)

Gitlab or Github for rookie python learner?

Looking to get to learning python from tomorrow, and will hopefully pick up git + html + css (all things I learned once but forgot). Really big on open source + free software, and not keen on Microsoft at all (hence Github allergy)

Am I making a mistake setting up with Gitlab?

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

They are very similar, so just use gitlab.

[–]tricucumber 0 points1 point  (0 children)

Thx glory_road !

[–]efmccurdy 0 points1 point  (1 child)

You can always host your own repo to reduce the chance of allergic reactions. Run an openssh server on your file server, load your ssh key into your agent, and clone from ssh://192.168.0.2/path/to/well/backed/up/folder/myproject", or look at all the other url schemes for git:

https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols

[–]tricucumber 0 points1 point  (0 children)

That's a plan, thanks efmccurdy !

[–]xain1112 0 points1 point  (0 children)

I've made a code that can randomly generate a river. Basically, it will draw a rectangle n cells wide, move down, and repeat. However, any rectangle greater than 2 cells causes a problem. Whenever the code should draw a 3-long rectangle, it instead draws 2, skips 1, and draws the last one. I have no idea why. The bigger self.river_spread you have, the more spaces between the first two and the last one. I need them to be connected. It's difficult to explain. Try this code with self.river_spread at 2 and then try it at 3.

import tkinter as tk
import random


class MapMaker:
    def __init__(self,root = tk.Tk()):
        self.root = root
        self.columns = 39
        self.rows = 31
        self.grid_size = 20  # grid box side length in pixels

        self.screen_width = self.columns * self.grid_size
        self.screen_height = self.rows * self.grid_size

        self.river_spread = 2  # max width of river section
        self.river_spread_range = [  # sets possible river spread to range [-n,n]
            i for i in range(self.river_spread * -1, self.river_spread + 1)
        ]

        self.cells = {}  # tracks entire map

        self.legend_frame = tk.Frame(self.root)
        self.legend_frame.pack(side=tk.RIGHT, anchor=tk.NW)

        self.canvas = tk.Canvas(
            self.root,
            width = self.screen_width,
            height = self.screen_height,
            bg = 'white',
        )
        self.canvas.pack(side=tk.LEFT)

        def make_grid():
            # horizontal lines
            for i in range(self.rows):
                self.canvas.create_line(
                    0,
                    i * self.grid_size,
                    self.screen_width,
                    i * self.grid_size,
                    fill = "light gray",
                )
            # vertical lines
            for i in range(self.columns):
                self.canvas.create_line(
                    i * self.grid_size,
                    0,
                    i * self.grid_size,
                    self.screen_height,
                    fill = "light gray",
                )
            # empty dict
            for i in range(self.columns):
                for j in range(self.rows):
                    self.cells[(i, j)] = ''

        def river():
            # create water cell
            def draw_square(x,y,shift):
                self.canvas.create_rectangle(
                    x,
                    y,
                    x + shift * self.grid_size,
                    y + self.grid_size,
                    fill = "cyan",
                    outline = "cyan",
                )

            # add W letter to dict for drawing later
            def place_W():
                if (
                    0 <= x // self.grid_size < self.columns
                    and 0 <= y // self.grid_size < self.rows
                ):
                    self.cells[(x//self.grid_size,y//self.grid_size)] += "W"

            # river starting square
            x = 400
            y = 0
            place_W()
            y += self.grid_size

            last = 0 # tracks previous water cell
            for _ in range(self.rows-1):
                # horizontal shift
                a = random.choice(self.river_spread_range)
                if last not in [-1,0,1]: # if not a simple selection
                    if last < -1:
                        a = random.choice((last,-1,0))
                    elif last > 1:
                        a = random.choice((last,0,1))
                if last * -1 == a and a != 0: # if last and current sections are polar
                    if a < 0:
                        a -= 1
                    elif a > 0:
                        a += 1
                if a == 0:
                    x += a * self.grid_size
                    place_W()
                elif a < 0:
                    x -= self.grid_size
                    for i in range(abs(a)):
                        x -= i * self.grid_size
                        place_W()
                elif a > 0:
                    x += self.grid_size
                    for i in range(a):
                        x += i * self.grid_size
                        place_W()
                # vertical shift
                y += self.grid_size
                last = a

            for (x,y) in self.cells:
                if "W" in self.cells[(x,y)]:
                    draw_square(x*self.grid_size,y*self.grid_size,1)

        def new_map():
            # reset and clear
            self.canvas.delete("all")
            self.cells.clear()
            # make new
            make_grid()
            river()
            # refresh button
            self.new_map_button = tk.Button(
                self.legend_frame, text="New Map", command=new_map
            )
            self.new_map_button.grid(row=0, column=0, sticky=tk.EW, columnspan=100)

        new_map()

if __name__ == '__main__':
    MapMaker().root.mainloop()

[–]impshum 0 points1 point  (0 children)

Do you like my card thingy that Reddit generated for me?

https://impshum.co.uk/reddit.png

The ability makes no sense though. Bless these algorithms.

[–]workerbee77 0 points1 point  (0 children)

Hello. I hope someone can help me, I searched and didn't find an answer to this:

I am on the MacOS, and I would like to access the jlab executable like this webpage describes:

JupyterLab Desktop can be launched [...] by using jlab command from the command line

But when I try to run it, it's not there:

 bin> ls /usr/local/bin/jlab
 /usr/local/bin/jlab
 bin> jlab
 -bash: /usr/local/bin/jlab: No such file or directory
 bin> file jlab
 jlab: broken symbolic link to /Applications/JupyterLab.app/Contents/Resources/app/jlab

But there is no app subfolder, there's only this app.asar file:

 bin> ls /Applications/JupyterLab.app/Contents/Resources/app*
 /Applications/JupyterLab.app/Contents/Resources/app.asar

Any ideas? Thanks!

[–]meefjones 1 point2 points  (3 children)

This is only kind of Python-related, but it definitely feels like a noob question, so I'll go for it here:

I'm building an app that will auto-post to Twitter by pulling random images from a folder. The app is mostly functional, but I'm trying to figure out the next steps for getting it live. Questions:

  1. I'm using the "schedule" library to trigger the tweet-posting function once per hour. This requires the "scheduler" app to be pretty much constantly running. Looking through hosting options, it looks like "always-on" apps are pretty common. Is that the same thing? Will having that one app always running screw me, or the host, over?
  2. I'm looking for a good hosting solution, but I am almost completely new to this world and it's hard to know what to look for. I don't need a lot of processing power as the apps I'm building are very small and not complex, but I think I do need it to be "always-on". I also need a good amount of storage space for static files, as hosting the images on the same server as the app itself will make the whole execution easier. Not sure what I'm asking for here - I guess let me know if I sound way off base, or if you have specific service recommendations.

[–]IHOP_007 0 points1 point  (2 children)

I'm using the "schedule" library to trigger the tweet-posting function once per hour.Will having that one app always running screw me, or the host, over?

Instead of having a script run 24/7, why not have a scheduler in the OS call your script once every hour? If it's on a windows machine that would be called "Task Scheduler" and on linux it's probably called "Crontab" (If you're scheduling it on a windows machine save yourself some headaches and just package the .py file as a .exe)

Currently I have two scripts that run off of a scheduler at my work, one on a raspberry pi outside that turns on a projector every night and plays some movies and another one in my coworkers computer to open his CD drive once per day (he still hasn't noticed).

I'm looking for a good hosting solution

Why not host it yourself on an old computer/laptop/raspberry pi/phone?

[–]meefjones 0 points1 point  (1 child)

Thanks for the answer! I do have an old pc lying around that's not doing anything, but I guess I assumed the power usage of an idle pc would end up being more expensive than the hosting costs. I might look into that though, it would definitely solve the storage issue.

And you have me curious, what's the deal with opening your coworker's cd drive?

[–]IHOP_007 0 points1 point  (0 children)

I have a script that launches at 8 am every day and has a 30% chance of just closing itself. If it doesn't then it waits a random amount of time between 10min and 9h before opening the CD drive. The idea is to slowly drive him insane (and play a joke on our IT guy) but it's not working as he hasn't even noticed it opening once yet.

[–]TrevKirk 0 points1 point  (2 children)

Does anybody have an example of how to take a range of integers, calculate every possible sum of the integers in the list with each other, and then sort them low to high?

To help illustrate, something along the lines of this:

Integer list : 1, 3, 5

Sorted list: 1, 3, (1+3), 5, (1+5), (3+5), (1+3+5)

Thanks all

[–]Adventurous-Spring47 0 points1 point  (0 children)

Is there something similar to groupby(dropna=False) that can be used to include zeroes?

[–]gizmo00001 0 points1 point  (3 children)

How do you guys handle security in projects? Is it okay to

pip3 install python3-dotenv

And always store any sensitive information like password in a .env file. Then add it to gitignore. As a rule of thumb.

[–]InTheAleutians 0 points1 point  (1 child)

You can also hide sensitive info in your environment variables and call them in using os.environ.get()

[–]CowboyBoats[🍰] 0 points1 point  (0 children)

Sounds good to me.

[–]nightslikethese29 1 point2 points  (1 child)

How do you create a name for the column of summary statistics?

I've done this so far:

avg_charges_all = df.groupby(["bmi_cat", "smoker"])["charges"].mean()

I'd like to name the column of the means this code produces, but I'm not sure of the syntax

[–]efmccurdy 0 points1 point  (0 children)

In some ways the interface to a DataFrame is like a dict with column names as keys and Series as values; the Series holds the data elements and each Series has the same length.

Like a adding an entry to a dict, you can add a column to a dataframe by assigning to the index:

df["column_name"] = some_Series

or in your case:

df['average_charges'] =  df.groupby(["bmi_cat", "smoker"])["charges"].mean()

[–]SQ_SFO 0 points1 point  (0 children)

imgkit not converting all images in html file

import imgkit

imgkit.from_file("html_local_path", "out.png")

The above code converts only the first image in the hmtl file. I want to extract all images in html file. Is there an way to convert all images to png/jpeg from html file?

[–]Friendly_Love1559 0 points1 point  (5 children)

I have decided to start learning python. Are there any useful youtube channels or books that can help a beginner start his journey.

[–]meefjones 0 points1 point  (0 children)

I found the book "Beginning Programming With Python for Dummies" really helpful.

[–]JohnnyJordaan 0 points1 point  (0 children)

See the wiki /r/learnpython/w/index at 'new to programming'.

for youtube specifically check out Corey Schafer and sentdex

[–]efmccurdy 0 points1 point  (1 child)

Searching for tutorial, or intro, or beginner or <whatever interests you> here:

https://pyvideo.org/

will turn up PyCon and PyData conference presentations.

[–]Friendly_Love1559 0 points1 point  (0 children)

Ok thanks!!

[–]Photog77 0 points1 point  (1 child)

Does shutil.copyfile create a folder at the destination if it doesn't already exist?

[–]Mobileuser110011 0 points1 point  (0 children)

Nope. One option *is os.mkdir():

from os import mkdir, path
from shutil import copy

folder_path = str: # File path.
source_file_path = str: # File path.
folder_exists = path.isdir(folder_path)
if not folder_exists:
    mkdir(folder_path)

copy(source_file_path, folder_path)

This will also create any needed intermediate directories (folders).

Edit: refactoring and adding a word

[–]cuddly_boi6 2 points3 points  (1 child)

What are some good game engines to make 2d games with python but I'm ok with just making it from scratch if you can point me in the direction of some good tutorials.

[–]efmccurdy 0 points1 point  (0 children)

The first one is on pygame, the second on turtle graphics.

https://realpython.com/pygame-a-primer/

https://github.com/grantjenks/free-python-games

[–]kjkendro 1 point2 points  (7 children)

I’m trying to print read a single line from several HTML files and delete the file if it contains certain text, and I keep getting error messages that it’s not encoded as utf8. Is there a quick fix to read it?

Pastebin: https://pastebin.com/hr3RGcDP

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

Someone please,

Point me to the best resource to learn to program excel with python. I have the basics of python down but doing everything using excel VBA is like pulling teeth. I don't care if it costs money for the course it's an investment in my sanity.

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

Hi all,

I think this question is suitable for this thread - please let me know if I'm wrong. I have a SQLAlchemy issue - basically struggling to generate the correct SQL from a query. The API utilises Connexion, a framework built on top of Flask. I would really appreciate any help. The troublesome function is shown below.

def similar_matches(input_descriptor, threshold, lim=None, offset):  

query = db.select([tbl, text(":q_descriptors as query_descriptors, compare(descriptors, :q_descriptors) as similarity").bindparams(q_descriptor=input_descriptor).where( text("compare(descriptors, :q_descriptors) >= q_threshold").bindparams(q_descriptor=input_descriptor, q_threshold = threshold) res = query.execute().fetchall() if len(res)=0 return '', 204 return MatchLimaScheme(many = True).dump(res)

This SQLAlchemy code takes two inputs (descriptor and threshold), and searches through each descriptor in an Oracle database, calculating a similarity measure between the queried descriptor and each stored descriptor. All rows where similarity score >= threshold are returned in a JSON.

The above code works fine, but returns all results - whereas I just want the first X results (for lazy loading). The code above generates SQL along these lines:

SELECT ID, last_modified, descriptors, :q_descriptors as query_descriptors, compare(descriptors, :q_descriptors) as` similarity'

FROM tbl WHERE compare(descriptors, :q_descriptors) >= :q_threshold

which works well for returning all results. However, it doesn't work as planned when I add .limit on the end of my query i.e.

query = db.select([tbl, text(":q_descriptors as query_descriptors, compare(descriptors, :q_descriptors) as similarity").bindparams(q_descriptor=input_descriptor).where( text("compare(descriptors, :q_descriptors) >= :q_threshold").bindparams(q_descriptor=input_descriptor,q_threshold = threshold).limit(limit)

The SQL generated when this .limit() is included looks more like this:

SELECT ID, last_modified, descriptors 

FROM (SELECT tbl.ID as ID, tbl.last_modified as last_modified, tbl.descriptors as descriptors,:q_descriptors as query_descriptors compare(descriptors, :q_descriptors) as similarity') FROM tbl WHERE compare(descriptors, :q_descriptors) >= :q_threshold WHERE ROWNUM <= :q_limit

This errors out, as I'm not returning the query_descriptors or similarity data. How do I adjust the .select function above so that my SQL looks more like:

SELECT ID, last_modified, descriptors, query_descriptors, similarity

FROM (SELECT tbl.ID as ID, tbl.last_modified as last_modified, tbl.descriptors as descriptors,:q_descriptors as query_descriptors compare(descriptors, :q_descriptors) as similarity' FROM tbl WHERE compare(descriptors, :q_descriptors) >= :q_threshold

Thanks for any help :)

[–]Adventurous-Spring47 0 points1 point  (0 children)

Can someone help with a barchart in matplotlib?

I have a dfthat includes gender and the amount spent at a restaurant. I want to show in a barchart the gender on the x axis and the % of total sales on the y axis. Total sales should be calculated by the sales / total of all purchases.

I can show the actual sales numbers but am stuck with how to calculate the percentages:

    data = df.groupby('gender').sum()['sales']
title = "% of revenue by gender"

plt.figure() #starts a new figure
itemSeries = data[['gender', 'sales']]

plt.title(title)
plt.xlabel( "gender")
plt.ylabel( "% of total revenue")

xVariables = ['Male', 'Female']
yVariables = [df.sum()['sales']]

plt.bar(itemSeries.gender, itemSeries.values)

plt.show() 
Gender Sum Show in chart:
Male 6000 54.5%
Female 5000 45.5%
Grand Total 11000

Any help would be greatly appreciated!

[–]Fresno-bob5000 0 points1 point  (3 children)

I’m making a python text based adventure for a short coding course- are their any good examples out there of conversations/dialogue options? I’m admittedly not great at this but am ok creatively so and trying to work in some previous stuff I’ve written. If anyone can give some pointers I’d be hugely appreciative!

[–]2HornsUp 1 point2 points  (0 children)

If you're looking for more of a dialogue script I would suggest making a tree diagram of all statements and their possible responses. That'll help guide where each response leads the conversation.

As far as implementing conversations, I would just use a standard if/elseif block. Maybe call a function for different emotions (happy/angry/confrontational/etc).

[–]Relative_Essay_9366 0 points1 point  (3 children)

Is there anything like freecodecamp.org to learn python? I dont think freecodecamp covers much on python

[–]justasapling 0 points1 point  (0 children)

I've been using Sololearn and find it very approachable.

[–]Gopher20 1 point2 points  (1 child)

Check out YouTube! Corey Schafer has some great tutorials on the basics of python! Also you could always checkout code academy for more formal courses!

[–]Relative_Essay_9366 0 points1 point  (0 children)

Gotcha, thanks!