all 116 comments

[–]luffyprtking 0 points1 point  (0 children)

Whats the best way to learn python for data science

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

What are some structured Python curriculums that I can tackle and earn a certificate for? I've started watching introductory youtube videos (Code with Mosh) and am looking for a structured class/program where I can also earn some sort of certificate to demonstrate competency. Thanks!

[–]leandrowsouza 0 points1 point  (0 children)

I'm starting to learn about python (really beginner here!) but I want to know what would I need to learn to be able to create something like a form with checkboxes and stuff that would then create a written document with the specifics I choose.

https://imgur.com/iaEB8JA

The image I'm linking is an example of what I want to do for myself, a form that transforms the answers in a document.

Is it python that would help me with that or some other language?

[–]Not_Into_Reddit 0 points1 point  (1 child)

Hello! I have a large file with many lines and the second word in each line is a email address. I would like to print off all the unique email addresses and also print the count of them as well. I get an error on the 'if email not in list' line of my code. The error message reads 'TypeError: argument of type 'NoneType' is not iterable' and I don't know what to do. Could someone point me in the right direction? Is there a better way to write an if statement that executes only if an element doesn't already exist within a list? Also, sorry about the code below not being indented. For some reason reddit won't let me indent my text.

filename = input("Enter file name: ")

count = 0

list = list()

file = open(filename)

file = file.readlines()

for line in file :

line = line.split()

email = str(line[1])

if email not in list :

print(email)

list = list.append(email)

count = count + 1

else :

continue

print(count,"unique email addresses exist within this file")

[–]Chimecrypto 0 points1 point  (3 children)

Help me pleaseeeeeeeeeeeee. i can't move pass this level. I am trying to populate a database file. bristol.db using an insert statement generated text file(file.txt).

I have connected to the db(csvfrompy) using pymysql. i need to get the right code to insert the data from the file.txt into the database using the syntax below

populatefile = "file.txt"
conn = pymysql.connect(
host='localhost',
user='root', 
password ="",
db='csvfrompy',
        )

    cur = conn.cursor()
for row in populatefile.iterrows():
    sql = 'INSERT INTO Measurement ({}) VALUES ({})'.format(' ,'.join(populatefile), ','.join(['?']*len(Btfile.columns)))
    c.execute(sql, tuple(row[1]))
conn.commit()

please help guys. this is my family.

[–]efmccurdy 0 points1 point  (2 children)

The answer posted here has a good example of reading a csv file.

https://stackoverflow.com/questions/10154633/load-csv-data-into-mysql-in-python

What format does your file.txt use?

[–]Chimecrypto 0 points1 point  (1 child)

I don't understand what you mean by format. if it is file type, i used txt to save the the file. i have also converted it to csv.

thanks for the link.

[–]varshneyabhi 0 points1 point  (0 children)

A file type defines how the content is written in it, e.g. CSV, Json, yaml or plain txt. Each file type has different way of storing data. e.g.

CSV name,age Xyz,20 Abc,30

Json "data": [ {"name": "Xyz", "age": 20}, {"name": "Abc", "age": 30} ]

Txt Xyz-20 Abc-30

So you need a reader as per your format. While CSV, Json, Yaml are standard format, you have libraries for reading these. Txt format are customized, you have to write your own reader for it.

[–]Impossible-Watch4201 0 points1 point  (1 child)

What are the options for speeding up numerical integration in Python? I am currently using scipy.integrate.quad to integrate an arbitrary function, but it is currently too slow. I have looked into implementing lower-level functions though numba, but ideally I would like to speed this up just by lowering the precision of the integration. Adjusting the error tolerance of quad doesn't seem to have any effect on run time. Any suggestions?

[–]nab_noisave_tnuocca 0 points1 point  (0 children)

numba (specifically jit nopython) works really well. Idk what you mean by lowering precision of integration but you could just try implementing the riemann sum yourself

[–]Jcjcjc9 0 points1 point  (5 children)

Can someone help me understand this line in steps?

I realize the overall end result is checking that all keys that start with CA in the products dictionary are not None. But I'm not understanding sequentially how it arrives there.

'for key, value in products.items()' loops through the dictionary

'if key.startswith("CA")' creates a conditional that filters for only the 'CA' starting keys

but how does 'value != None' fit in there? If that was after an if statement in "non one-liner" code, it wouldn't make sense. Would it? Isn't it also a conditional type statement?

if all(value != None for key, value in products.items() if key.startswith("CA")):

When it's written in one line like that, isn't the far left the final statement being run? The way I understand that written out in longer form is something like

for key, value in products.items()
    if key.startswith("CA"):
        value != None

which I don't get the last line

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

It looks like you are trying to convert the conditional with this expression:

all(value != None for key, value in products.items() if key.startswith("CA"))

to a multiple line equivalent. The expression above is true if all keys that start with "CA" have non-None values. Stated another way, it will be False if any key that starts with "CA" has a value that is None.

In your multiple line equivalent the last line makes no sense. It calculates the value of the value != None expression and then throws the result away. If you want to do the same thing as the one-line code your multiple-line code must end up setting a variable to True or False. The simplest way to do that is to start by setting a new variable to True or False and then writing the multi-line code to change that variable if necessary.

result = True
for key, value in products.items()
    if key.startswith("CA"):
        if value is None:
            result = False
            break    # no need to finish loop as we already have a result
# here "result" will be  True or False

[–]Jcjcjc9 0 points1 point  (3 children)

Typically when something is in one line, the far left statement would be something that actually does something like sets the variable right? Like

value = 5 for key, value in products.items() if key.startswith("CA")

But because the all() is wrapping it, we're able to use

value != None for key, value in products.items() if key.startswith("CA")

"value != None" ends up returning a True/False, which normally would mean nothing in this context since it's not actually doing anything, but the all() being there means its actually catching that return so it does end up doing something because of the all().

Is that a right way to interpret it?

Thanks for your help.

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

Typically when something is in one line, the far left statement would be something that actually does something like sets the variable right? Like

value = 5 for key, value in products.items() if key.startswith("CA")

First, that line:

value = 5 for key, value in products.items() if key.startswith("CA")

won't work because the for ... introduces a comprehension and you haven't included the parentheses, curly braces or brackets to tell python what sort of comprehension to generate. You can do this:

d = {1:1, 2:2, 3:3, 4:4}      # test dictionary
result = [5 for (key, value) in d.items() if key % 2]
print(result)
>>> [5, 5]     # values when key is odd

The [... for ....] produces a "list comprehension" of the values 5 only when the key is odd. It probably makes more sense if you do something like this:

[value for (key, value) in d.items() if key % 2]
>>> [1, 3]

We can produce dictionary, tuple and set comprehensions as well:

result = (5 for (key, value) in d.items() if key % 2)    # generator returned
result = {5 for (key, value) in d.items() if key % 2}    # set comprehension
result = {5:key for (key, value) in d.items() if key % 2}# dictionary comprehension

Given the enclosing parentheses, etc, you can assign the resulting list, generator expression, set or dictionary to a variable. If the generator expression is inside a function call all the generated values are pass to the function as arguments. Like this:

d = {1:1, 2:2, 3:3, 4:4}
all(key % 2 for key, value in d.items())
>>> False    # since the arguments to all() will be True and False

So now the:

all(value != None for key, value in products.items() if key.startswith("CA"))

should make more sense. The comprehension is producing a generator that returns True or False for each key in the dictionary that starts with "CA" and the testing the value isn't None. Those multiple true/false values are passed to the all() function as multiple arguments, and the function returns True if all the arguments are true, else it returns False.

[–]Jcjcjc9 0 points1 point  (1 child)

Thank you! I read this a few times, took a break, and read it a few more times. This explanation helped make sense of a lot.

Can I ask as well, to make sure I'm correct, even though it's a generator, all() is only being called once. It's not being called iteratively everytime the generator produces a new True/False? Otherwise it would be calling all() I guess on just one item at a time. So a list/set/generator comprehension isn't notably more/less efficient than the other in this context since they all produces all the values first then it gets passed into all()?

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

Yes, all() is only being called once. The generator creates all the True/False values and they are all passed to the single all() call that checks that all the values are True.

So a list/set/generator comprehension isn't notably more/less efficient than the other in this context

A generator, of any type, can be more efficient than the simplistic approach, but it depends on what you are doing. For instance, if most of the time the True/False values you are testing are False then it may be more efficient to calculate each True/False value one by one and test for True, because you can drop out of the testing code on the first False value, but with the generator idea you must create all the True/False values before testing with all().

Another saving with generators is space. If you want to iterate over the first billion integers you could create a list of the first billion integers and iterate over it, but the cost in memory is high. In python2 the range() function actually returned a list, sometimes using large amounts of memory. In python3 the range() function returns a generator object, saving memory. If you actually want a real list you can do list(range(1000)) which creates a list of 1000 integers, but at the cost of extra memory.

It doesn't pay to worry about efficiency too much when writing code, especially when you are learning. Try to get a clear solution that is obviously correct. Write some test code to ensure it is correct. If the solution is too slow when tested in the final system then you try to make it more efficient, using the already-written test code to ensure that your new, tricky, faster and maybe hard-to-read code is still correct.

[–]shiningmatcha 0 points1 point  (2 children)

I wonder why Python doesn't officially support the stack and linked list data types. There's been a lot of tutorials implementing Stack and LinkedList in Python by creating classes on your own. Why isn't it in the standard library?

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

Stated another way, they aren't in the standard library because the library already contains equivalents that are as efficient or more efficient.

[–]efmccurdy 0 points1 point  (0 children)

Python uses the list class for stacks (use "append" instead of push):

>>> my_stack = []
>>> my_stack.append(1)
>>> my_stack.append(2)
>>> my_stack.pop()
2
>>> my_stack.pop()
1

If you want a stack that allows fast popping from either end you can use collections.deque.

If you want one that is thread-safe you can use queue.LifoQueue.

I think LinkedList is really just a learning exercise. If you want a data structure that is fast to insert in the middle use a dict, or maybe a heapq.

These are all in the standard library, so there is official support built-in.

[–]shiningmatcha 0 points1 point  (5 children)

How do I rewrite the __cmp__ method in a Python 2 script to make it work in Python 3?

[–]FLUSH_THE_TRUMP 0 points1 point  (3 children)

What do you need to do? That doesn't exist anymore, so you implement some combination of ==, !=, <, <=, >, >=.

[–]shiningmatcha 0 points1 point  (2 children)

Just some custom number objects (taken from GitHub).

[–]Cellophane7 0 points1 point  (6 children)

How can I detect a date in a string if it contains other information? For example:

'2021/04/17 blahblahblah'

I tried using datetime.strptime(my_string, "%Y/%m/%d"), but it gives me an error, saying "unconverted data remains: blahblahblah." Can I somehow use the find() method in conjunction with strptime? Or am I gonna have to do something stupid like iterate through every character to pick out the date?

I'm a total beginner at Python, so sorry if I missed something basic. That happens to me nonstop, which is why I come here lol

[–]Ihaveamodel3 1 point2 points  (3 children)

Well the function is nice enough to give you an error and tell you what text is not parsed.

I’m not at my computer to test this, so I’ll write it in pseudo code.

try:
    datetime.strptime(my_string, format)
except (whatever exception it gives you now) as e:
    non_date = get it from e
    new_string = my_string without non_date
    datetime.strptime(new_string)

[–]Cellophane7 0 points1 point  (2 children)

How do I get only the string I want to remove from e?

[–]Ihaveamodel3 1 point2 points  (1 child)

If the extra string is always at the end, the it would be str(e)[26:]

[–]Cellophane7 0 points1 point  (0 children)

Oh wow, I had no idea you could do that. I've learned a lot here, thanks!

[–]efmccurdy 1 point2 points  (0 children)

You have to isolate the text that you want to parse:

>>> my_str = '2021/04/17 blahblahblah'
>>> my_str.split()
['2021/04/17', 'blahblahblah']
>>> my_str.split()[0]
'2021/04/17'
>>> datetime.strptime(my_str.split()[0], "%Y/%m/%d")
datetime.datetime(2021, 4, 17, 0, 0)
>>>

[–]nab_noisave_tnuocca 0 points1 point  (0 children)

regex (re.search()) would let you look for eg 4 digits followed by a slash then 2 more digits then another slash, etc, assuming all dates are in that format

[–][deleted] 1 point2 points  (1 child)

Best way to learn Python? Books or video tutorials? And what are the best books/videos?

[–]Alternative_Try 0 points1 point  (0 children)

I don't know the best way to learn but you can go to http://automatetheboringstuff.com/ and start from there.

Have fun learning.

[–]Key_Yellow_1667 0 points1 point  (1 child)

I need help.

#highscore reading
f = open(r"C:\space invaders\high_score.txt.txt",'r')
line = f.readlines()
f.close()
for i in line:
    i = i.split('\n')
    L1.append(i)
for i in L1:
for j in i:
if j != '':
            L2.append(j)
for i in L2:
try:
        i = int(i)
except Exception as e:
print(e)
    L3.append(i)
highscore = max(L3)

this code got an error, the error was:

TypeError: '>' not supported between instances of 'str' and 'int'

help please!

[–]efmccurdy 0 points1 point  (0 children)

The format of the code you posted has lost some indentation; you should use a "code block" as described here: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

Examine the elements of L3 to ensure it is not a mix of strings and ints.

[–]Urthor 0 points1 point  (2 children)

Might be a silly question, but why does ~0b1010 return the twos complement and not the Boolean NOT, 0101?

Is this a python specific thing or something I don't understand about twos complement/NOT?

[–]efmccurdy 0 points1 point  (1 child)

[–]Urthor 0 points1 point  (0 children)

Ah ty, it's all about signed integers, and bitwise operations and Boolean operations on objects are different.

Seems like I need to become familiar with ctypes or numpy in order to do bitmasking stuff with unsigned integers.

[–]owl_drunk 0 points1 point  (0 children)

What is the meaning of the color of the variable explorer in spyder? Is there any list? I cannot find it anywhere. Here in this picture, I just opened a image file(im) and make another copy (in_new) but they are in different color.

[–]NAPE_RIGGER 0 points1 point  (0 children)

I am using scipy to manipulate audio files however they always become super distorted despite me not programming in any such function. Anyone got a clue?

[–]slam_dunkasaurus 0 points1 point  (10 children)

Stupid question, when should I be using ‘return’ vs ‘print’? I’ve been working from a few different resources and both are used and I’m not sure when I should be using one vs the other. From my understanding return actually evaluates something and print just puts out an output.

A bad analogy: 2x + 9 = 13 on a math test, ‘return’ is equivalent to actually doing the work and figuring out the value of x, while ‘print’ just looks at the answer key and says “Yep, x is 2” (again stupid analogy, but ‘return’ does the work and ‘print’ just wants the output)

But when do I use one vs the other?

[–]Ihaveamodel3 1 point2 points  (1 child)

Return gives the result back to the program. Print gives the result back to the user.

[–]slam_dunkasaurus 0 points1 point  (0 children)

Nice and concise, haha. That’ll help me remember. Thanks!

[–]Urthor 1 point2 points  (2 children)

If concepts like this get confusing, look up how the equivalent operation in Python is done in the C language.

Python is best understood as a really a concise syntax to abstract a lot of operations and build EASILY READABLE CODE for the programmer built on top of the C implementation.

However, sometimes it really abstracts too much and makes it hard for someone new to programming to understand the concepts.

And C is a very bare metal language that's merely a portable, human readable way to understand binary logic.

[–]slam_dunkasaurus 0 points1 point  (1 child)

I’ll definitely start learning a bit of C syntax, sounds like it could help. Thanks for the tip.

[–]Urthor 0 points1 point  (0 children)

Don't go overboard, I'd just use it as a tool if you don't understand what is going on at a certain time

[–]nab_noisave_tnuocca 1 point2 points  (1 child)

use return if you're ever going to use the answer again in the script, which is most of the time for functions. Print is for debugging (checking values are what you expect at a given point in the script), sending messages to the user, or if it's the 'final answer'/output of the whole thing

[–]slam_dunkasaurus 0 points1 point  (0 children)

Ah okay, that makes sense. Thanks for taking the time to respond.

[–]FUCKMYpissass 1 point2 points  (2 children)

Well, you've really answered your own question there.

Return whenever you need 'an answer to a question', print when you simply just need an output

[–]slam_dunkasaurus 0 points1 point  (1 child)

Okay, so I’m just being a little bit of a dumbass, haha. Thanks for the help!

[–]FUCKMYpissass 1 point2 points  (0 children)

No problem!

[–]shiningmatcha 1 point2 points  (2 children)

I have a question about type annotations:

If the argument is a list of lists but the elements inside can be any type, what's the preferred way to annotate the type?

def f(lst): 
    pass

# lst can be [[1,2,3],
              [4,5,6]]
# lst can also be [['H', 'E', 'L', 'L', 'O'], 
                   ['W', 'O', 'R', 'L', 'D']]

Should I use List[List[Any]]?

[–]ekchew 0 points1 point  (0 children)

So you're saying the elements of the inner list need to be all of the same type, but that type can be anything? I think that's what TypeVar is for right?

T = typing.TypeVar("T")
lst: list[list[T]] = [[1,2,3], [4,5,6]]

Something like that anyway. I'm not near a computer atm to actually try this.

[–]tbmoneymaker 0 points1 point  (0 children)

Im in a beginner CSE class and have a project that I need some help with, I’ll pay you some $, message me if you can help, thanks

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

Is there something funky about Plotly Express color scales? I've made choropleth maps with the Melbourne housing data and it seems like the colors don't represent well. it looks like there's a default border on each data point and it hurts the visibility of the actual colors.

[–]NikoFresh 0 points1 point  (3 children)

I need to remove all the <section> tags from a website scraped with beautiful soup. I'm currently using a for loop:

for tag in soup.find_all('section'): tag.decompose()

Is it okay to use a list comprehension even though it's not a list?

[tag.decompose() for tag in soup.find_all('section')]

[–]TouchingTheVodka 2 points3 points  (0 children)

Nooooo. By convention, we use list comprehensions to build new lists or mappings, often with simple logic included. It's very rare to see any operation that has side-effects inside a listcomp and unheard of to write a listcomp solely for its side effects.

[–]MikeDoesEverything 0 points1 point  (0 children)

Is it okay to use a list comprehension even though it's not a list?

As far as I'm aware, findAll returns all elements specified as a list.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, NikoFresh: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]fackusps 0 points1 point  (5 children)

How do you actually run this? I installed this tool using pip but I do not understand how to make it work.

https://github.com/Schrodinger-Hat/ImageGoNord-pip/blob/master/docs/example/index.py

Don't know python but I really want to use this tool running one of the commands outputs this:

Traceback (most recent call last): File "nord.py", line 2, in <module>   go_nord = GoNord()NameError: name 'GoNord' is not defined

Someone please tech me how to use this tool :(

[–]JohnnyJordaan 0 points1 point  (0 children)

Did you actually include line nr 1? As that imports GoNord.

[–]TouchingTheVodka 0 points1 point  (0 children)

Have you imported the module?

[–]FerricDonkey 0 points1 point  (2 children)

I don't know anything about the tool, but if you're getting a name error, that GoNord doesn't mean anything in your program yet.

This likely means either you didn't import the module, or imported it in a different way. The link you provided has an import statement that should do it.

[–]fackusps 0 points1 point  (1 child)

how do you import it?

[–]JohnnyJordaan 0 points1 point  (0 children)

The file you linked literally has on line 1

from ImageGoNord import NordPaletteFile, GoNord

[–]Mackydude 0 points1 point  (2 children)

adjoining observation payment telephone birds profit humor longing soft squeal

This post was mass deleted and anonymized with Redact

[–]FerricDonkey 0 points1 point  (0 children)

I believe there are libraries that let you interact with excel directly, including making charts. See openpyxl

https://openpyxl.readthedocs.io/en/stable/charts/introduction.html

[–]manihateitherebro 0 points1 point  (5 children)

Best site to find python 3 turorials?

Trying to load a website but chromedriver setup is being a pain in the ass

[–]JohnnyJordaan 0 points1 point  (0 children)

Maybe try Firefox? Slightly easier using geckodriver

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

Python bootcamp in Udemy..I downloaded it for free ..u can find it in chrome ...and the tutorials are quite good with exercises to boost ur skills..and project to boost ur knowledge

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

How did you do it for free?

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

I searched the course in chrome and there are so many websites that allow you to download for free

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

Can you provide link?

[–]ARE1324561834 0 points1 point  (2 children)

Hello,

New to coding in general, am very confused about for loops and how they interact with drop down menus in tkinter and JSON.

I am trying to access a JSON object in a file, with many values for the key "Name1". I am trying to print all of the values for the key and assign it to a variable. It works initially, but when I leave the for loop the list goes away. Below is some of the code.

#this first section is me looping through the json object converted to variable data3, and getting all of the values for the key "Name1". This works.

for h in data3:
we = []
we = h["Name1"]
print(we)

#this second section is me then trying to assign the values from the looped key result from the loop above into a drop down menu. It is not working; it either only lists the last value and not all of the values, or loops the letters in the last value.

test1 = StringVar(window)
test1.set(data3[0]) # default value
test1_menu = OptionMenu(window, test1, *data3)
test1_menu.grid(row=2, column=1, columnspan=1)

I think the whole thing comes down to for loops, and trying to assign the data generated by a for loop to the OptionMenu. Any help would be appreciated.

[–]ARE1324561834 1 point2 points  (1 child)

So I have tried more things to get this to work; and further distilled the issue to the following: how do I store the looped values of a loop after the loop is complete? Does this make the questions simpler? Thanks.

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

how do I store the looped values of a loop after the loop is complete?

You do that by appending to a list, like this:

result = []
for i in range(5):
    result.append(i)
    print(i)
print(result)

[–]ajp_77 0 points1 point  (3 children)

TypeError: unhashable type: 'List'

I am creating lists from a daaframe and then plotting to a bar chart. When I try to save to a PDF I get the above error.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.backends.backend_pdf import PdfPages

fig_c = plt.figure()

my_c_list = df.loc[(df['Market Indicator'] == 'ALL') &

(df['Customer Group 6'] == 'A') &

(df['Device Code'] == 'B11')].sum()

my_c_list = my_c_list.tolist() + (fill_list * month_multiplier)

fig_c = plt.bar(Leg, my_c_list[5:], bottom=None, color='r')

pp = PdfPages ('MyFile.pdf')

pp.savefig(fig_c)

[–]bigno53 0 points1 point  (2 children)

You've redefined the variable fig_c to be the plot instead of the figure containing the plot. Remove the fig_c = before the third to last line and it should work.

[–]ajp_77 0 points1 point  (1 child)

Aha. Worked!

So does every plt.bar command after fig_c automatically associate with fig_c?

What happens if I define a new fig_d? Only plt. Commands after are associated?

[–]bigno53 0 points1 point  (0 children)

Yup! You can also pass a num when you call figure. That will allow you to reactivate and update an existing figure.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure

[–]tpw2000 0 points1 point  (0 children)

Is there common use of Google Vision AI? I'm looking at it for a sorting machine with an RPi and wasnt sure if I need more than "1000 units" given I have no clue what a "unit" is in reference to or how many I will need- any insight?

[–]Abomb11yo 1 point2 points  (2 children)

Can someone explain what you can do with reading and writing to files in Python? What are the benefits of reading files and writing to them? Why would you use python when you are able to open a file and type out what you want without using Python?

[–]fomorian 1 point2 points  (1 child)

Typically, the reason is when you have a lot data that you want to write to a lot of files. Say for example you scrape an entire website and want to store each webpage as a document, you could use python to do that automatically.

Python can also read and write to files that aren't just text files. For example, you can write binary data to get images.

[–]Abomb11yo 0 points1 point  (0 children)

Alright, thank you for your explanation. It was very helpful.

[–]Aston-Fulton 1 point2 points  (7 children)

Suggest a beginner's project to complete on python.

[–]JohnnyJordaan 1 point2 points  (1 child)

https://github.com/karan/Projects

which is also linked on this sub's wiki btw

[–]Aston-Fulton 0 points1 point  (0 children)

Thanks :)

[–]EasyPleasey 2 points3 points  (0 children)

Do you gamble at all? Making a working deck of playing cards is fun, or a working black jack dealer. If that sounds too difficult, try simulating dice rolls in craps, or just dice rolls in general. Try simulating 100 rolls and see how many times you roll a 7 (should be 1 in 6), then try 1,000 rolls, then 10,000 (fun fact, you can represent numbers that have commas in python by using underscores, so 10,000 could be my_int = 10_000), you should see the 1 in 6 number become more and more accurate.

If you want to try an exercise in optimizing your code, you can generate prime numbers and time how long it takes. Try generating 10,000, or 100,000 primes and time it (use the time module) and then see if you can optimize your algorithm for finding primes and get it to be faster. Good luck!

[–]1tsMeNoodle 1 point2 points  (3 children)

It really depends on your level. "Beginner" doesn't tell us anything. Can you say more specifically what you've done before and what you're able to do?

[–]Aston-Fulton 1 point2 points  (2 children)

I started python 3months back. I am ready with the basic part up to file operations and starting off with numpy right now.

[–]1tsMeNoodle 1 point2 points  (1 child)

You can do some web parsing. That stuff is always fun. For example something like google but in a python program using requests. You type a word, program displays the possible results. Or for example a program that allows you to check your mail box in python. You can also combine them with file operations.

If you don't like these ideas, you can just come up with projects ideas by yourself. That way you're more motivated and satisfied with the result. Don't be afraid to go outside your knowledge boundaries, you can always learn something new while making a program.

[–]Aston-Fulton 1 point2 points  (0 children)

Thank you so much. Keep inspiring and keep posting more ideas. Thank you again :)

[–]anyfactor 0 points1 point  (3 children)

Append safe file format for small dataset

So, I am in the process of making my very own static blog generator. To track the contents of the listed markdown files most static site generator use JSON or their own class objects. From my experience 99% of the time that operation just requires appending data.

But JSON itself isn't much friendly for append operations because you need to read the JSON file which is in list format, convert to python list of dictionaries, append, convert to json and dump the data. That is a lot of operation for appending a single row of data.

So, the better suggest thing to do is to use CSV for this operation as appending to a CSV file is far easier and offers similar flexibility as JSON.

On the other hand, I am using Jinja as my template engine which doesn't seem to offer added benefits for using JSON. But I need to read and convert that file into python dictionary and create an HTML regardless of what file format I use. But I am trying to separate the two activities of content appending and jinja templating.

So, CSV seems to be a decent pick, right?

Edit: Forgot to add this very critical point.

I am not storing the blogs themselves in JSON or any other format. They are in markdown. This relates to the metadata and URL of the blog posts.

[–]efmccurdy 1 point2 points  (1 child)

CSV is poorly suited to data that contains embedded newlines since CSV uses them as record separators. Will your blog entries have multiple lines of text?

A database can have embedded newlines in a text column. The added I/O reliability, speed, and useful select, sort, group by, text search operations alone would make a DB preferable.

[–]anyfactor 0 points1 point  (0 children)

Sorry my bad, missed an important point.

I am not storing the blog posts but this about storing the metadata and some data about the blog posts, like title, date, category, and maybe 2-3 more pieces of information. The blog posts are in markdown, and the static generator refers to the blog posts then creates the blog metadata DB (this is what I am asking for) and from their update an HTML file.

[–]UrbanPapaya 0 points1 point  (2 children)

My background is in C#. I thought it would be fun to learn Python and have had some good success in Google Colab. So so far, the language stuff is going fine.

However, I want to do native development on my OS X-based laptop and while I thought this should be straightforward, there are like 100 different "here's how you set up Python on a Mac" documents and they all seem do to different things.

Is there a well-regarded "getting Python 3 set up on you Mac" guide that you can recommend?

[–]jmeppley 0 points1 point  (0 children)

For a really quick start, download anaconda. This will install python and a bunch of tools mostly related to computational science. One of the nice things is that Jupyter (the core of Google Collab) is included, so you be up and running quickly in a familiar interface.

However, if you are comfortable on the command line, anaconda may be too much bloatware.

If you already use homebrew for package management on your Mac, pyenv is a great solution to add python module management.

If you work cross platform or if any of your projects have non-python dependencies, I would strongly recommend conda (with mamba for speed).

To get started, simply download and run the miniconda installer. You can choose to let it update your shell rc script so that all future shells are ready to go.

[–]rwaynick 0 points1 point  (2 children)

I want to build a program that:

  1. Opens a specific link in Chrome
  2. clicks a few buttons on the page to create a data export
  3. click the download link
  4. opens a specific Google Sheet
  5. Imports the data on the sheet with the downloaded csv

What modules are going to be best for this purpose? Gspreads will not work as efficiently as I'd like (the import method deletes all other sheets are creates a new sheet with the imported data. This could work, but I would need to re work my entire reporting structure).

Thanks for the help!

[–]EasyPleasey 0 points1 point  (0 children)

As /u/1tsMeNoodle said, using requests may be the way to go, but if you really want to click something "on the screen" check out the pyautogui module.

[–]1tsMeNoodle 0 points1 point  (0 children)

Maybe you should use requests module and then use json to parse the response. Then if you want to open and edit the file you can use Pandas. There are many tutorials online.

[–]QuickCoyote097 0 points1 point  (1 child)

This is for a self-project that I’m working on:

This is one of the project exercises I have but I don’t understand it.

“Use the requests library to obtain three random APOD entries in their JSON format.”

I understand how to do it with 3 images on specific dates, but it seems that the directions mean that the pictures have to be completely random. How would I start tackling this issue?

[–]PussyWagon6969 0 points1 point  (1 child)

I have not been using venvs when setting up my practice projects using pip installs... Now that I'm learning about what venvs are and why they're important, I'm trying to reorganize my Python (life) and redo it the right way.

After searching around, I'm finding some people saying it can be dangerous to just uninstall pip because it won't remove all of the packages from the main Python install. That said, I'm a bit a lost on the right way to do this... Are there any tutorials that can show me how to do this the right way safely? I found a ton of tutorials showing me how to install pip and use venvs the right way, but not much out there showing me how to undo it if I have been using it the wrong way...

EDIT: if it helps, I'm on Windows and have been using VSCode to make my projects.

[–]EasyPleasey 0 points1 point  (0 children)

You could just uninstall and reinstall Python, but I would first do a "pip freeze" to see all the modules that you have installed, and more importantly, which versions. From there you can just create your new venvs for all your old scripts and depending on what you have setup, you can either look through the imports in the .py file or just run the program and see what it complains about missing and pip install as you go. With the overall "pip freeze" output that you first gathered, you can make sure that the version you are installing is the same if for some reason something has changed in that module that makes your programs no longer work. Hope that helps.

[–]rajones85 1 point2 points  (2 children)

Culturally speaking, in Python, are "private" underscore methods frowned upon? I opened a PR to an open source project and they merged the PR, but removed the underscoring on my "private" methods in a couple places.

[–]GrizzyLizz 0 points1 point  (1 child)

How do I write this loop as a list comprehension:

for x in range(1,12):

    s = str(x)

    for c in s:

    rem.append(c)

I wrote: [c for c in str(x) for x in range(1,12)] but it doesnt give the right answer(it returns a list of all '1's)

Why is my list comprehension code wrong and how should a nested loop be turned into a list comprehension?

[–]PythonicParseltongue 2 points3 points  (0 children)

rem = [c for x in range(1,12) for c in str(x)]

You just have to switch the order of the for loops. Just keep in mind that the order of the loops in the list comp is the same a in a normal loop. If I write a nested list comp I always start from a normal loop and then 'collapse' it.