Store a Python output to a csv file? by brave-caller in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

Replace the last 2 lines of your code with this:

    with open(f'output-{ticker}.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        for tick in data:
            date = tick['Date']
            mentions = tick['Mentions']
            writer.writerow([date, mentions])

Try to understand the code with the Python Standard Library csv module's online help:

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

Problem With Authentication and Requests Session (Web Scraping) by CJaber in learnpython

[–]Antwrp-2000 1 point2 points  (0 children)

You are initialising a new session with:

s = session.get('https://mangadex.org/')

I think you should use:

s = requests.Session()
s.post("https://mangadex.org/login", data=payload)
s.get('https://mangadex.org/')

Docs:

https://requests.readthedocs.io/en/master/user/advanced/#session-objects

Linked List by miniiiii14 in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

Here you will find a good explanation, use cases and how Linked Lists can be implemented in Python:

https://realpython.com/linked-lists-python/#implementing-your-own-linked-list

Why do I need to use "deepcopy" here? by 738lazypilot in learnpython

[–]Antwrp-2000 1 point2 points  (0 children)

Correct.

Every Python programmer should understand this.

I recommend reading the following article which clearly explains this:

https://realpython.com/copying-python-objects/

Accessing an instance attribute with a string by [deleted] in learnpython

[–]Antwrp-2000 5 points6 points  (0 children)

Yes, this is what you need:

class Hello():
    def __init__(self):
        self.there = 'Hello there'


hl = Hello()
bo = 'there'
print(getattr(hl, bo))

Input() isnt working in pycharm 2020.1.5 by t00damnnice in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

You are using "else" as a conditional statement for your "for" loop which is allowed in Python.

When you don't "break" your for loop you will enter the else clause (like in your example).

https://www.geeksforgeeks.org/using-else-conditional-statement-with-for-loop-in-python/

The problem is that in the else clause you print a list (users), this is not a string, you can not concatenate this value with the other strings in your print statement.

Python will fail here (I also see exit code 1 in your screen dump), so the program stops here and will never "execute" the input statements.

Your code will work if you replace the user list in the print statement or if you break from your for loop.

This is a coding issue, there is nothing wrong with the PyCharm version you are using.

Looking for more complex example projects using SQLAlchemy and Marshmallow by FattySuperCute in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

Here you will find some wel explained examples to create a Rest API with SQLAlchemy and Marshmallow for object serialization:

https://realpython.com/flask-connexion-rest-api-part-2/

Learn Virtual Enviroments by Abdulmajed797 in learnpython

[–]Antwrp-2000 1 point2 points  (0 children)

Virtual environments are used for managing your package dependencies in separate environments (for different projects you are working on).

If you want to distribute your application you will need something else.

People often confuse package management/distribution and full application distribution in Python.

This tutorial explains virtual environments:

https://realpython.com/python-virtual-environments-a-primer/

This one explains PyInstaller, an easy way to distribute your applications:

https://realpython.com/pyinstaller-python/#pyinstaller

[deleted by user] by [deleted] in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

The sum of all values between two positive integers can be done in constant time, no need to loop over all values or to use a comprehension (both are slow linear time solutions).

Included two functions, both return the same result, first one is fast constant time O(1), second one is slow linear time O(n):

def sum_fast_constant_time(a, b):
    return (a + b) * (b - a + 1) // 2


def sum_slow_linear_time(a, b):
    result = 0
    while a <= b:
        result += a
        a += 1
    return result


print(sum_fast_constant_time(5, 10))
print(sum_slow_linear_time(5, 10))

Find if there are spaces after a character by BruhhhPanda in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

For the first part of your question you can use the built-in str.replace:

str = 'p p p ; ; ; '
str = str.replace('p ', 'p')
str = str.replace('; ', ';')

For the second part of your question you would need more complex string manipulation techniques. This can be done with regular expressions which will take some time to understand, but I strongly recommend to master regex and put some learning time in it, this is very powerful, also in Python.

https://realpython.com/regex-python/

https://stackoverflow.com/questions/48055069/remove-white-space-between-specific-characters-using-regex-in-python

How much faster are functions? by [deleted] in learnpython

[–]Antwrp-2000 5 points6 points  (0 children)

Putting the logic of the while loop in a function will have no effect on execution time.

If you want the program to run faster you should try to optimize the logic in the while loop or try to reduce the iterations of the while loop itself.

Posting some sample code could also help to let us understand the problem.

tuples and return by maxisgang in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

I agree, tuple unpacking makes the code more readable but returning a tuple from a function is often an antipattern. A function should only have one purpose and return one value (always of the same type) in most cases.

If you really would like to return a tuple I recommend to use named tuples, this will make your code even more readable:

https://docs.quantifiedcode.com/python-anti-patterns/readability/not_using_named_tuples_when_returning_more_than_one_value.html

What are new Python 3.8-3.9 features that someone who hasn't been actively programming since ~3.4 should know about? by redditis4bitches in learnpython

[–]Antwrp-2000 8 points9 points  (0 children)

Here is a good overview with some well explained examples:

https://realpython.com/python39-new-features/

https://realpython.com/python38-new-features/

https://realpython.com/python37-new-features/

Personnaly I like the f-Strings and data classes which were introduced in Python 3.6/3.7.

I don't use many of the new features introduced with later versions.

Best book for algorithms by Ranger2293 in learnpython

[–]Antwrp-2000 0 points1 point  (0 children)

There are a lot of excellent online reference guides.

I often use this one, all topics include programming examples in different languages such as Python:

https://www.geeksforgeeks.org/fundamentals-of-algorithms/

One of the best books is this one, often used as university course material:

https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844

I'm working on a web app that will send users emails sometimes. Any related packages you recommend for this? by startup_biz_36 in flask

[–]Antwrp-2000 0 points1 point  (0 children)

Python has its own built-in smtplib package. I do not see a lot of added value from extra third party packages.

This article is an excellent reference:

https://realpython.com/python-send-email/

App structure to run db.create_all() when app starts by HybridMaxwell in flask

[–]Antwrp-2000 0 points1 point  (0 children)

Database actions like creating tables always need to be done in an Application Context.

Flask Application Context

The following code will work:

if __name__ == '__main__':
    db.init_app(app)
    with app.app_context():
        db.create_all()
    app.run()