r/pythontips • • Feb 25 '19

Play a sound when a function has finished running

26 Upvotes

Some times it can be useful to have a sound play after a function has finished running (e.g. time consuming data science tasks). Here is a short decorator you can use on your functions to achieve this (win only... i think?)

import time
import winsound


def play_warning(func):
    def wrapper(*args, **kwargs):
        func(*args, **kwargs)
        winsound.MessageBeep()
    return wrapper


@play_warning
def testfunc(secs):
    for _ in range(secs):
        time.sleep(1)
        print("Slept" + "."*_)

testfunc(5)

r/pythontips • • Jan 16 '18

Python's title() method for strings treats spaces, numerics and punctuation characters equivalently.

26 Upvotes
# Good old whitespace
>>> 'probablY a gOod ExampLe'.title()
'Probably A Good Example'

# Numbers work
>>> 'probablY7a88gOod99ExampLe'.title()
'Probably7A88Good99Example'

# Numerics mixed with decimals work too
>>> 'probablY7.8a88gOod99ExampLe'.title()
'Probably7.8A88Good99Example'

 # Now just going random with this
>>> 'probablY7._8a88gOod99ExampLe'.title()
'Probably7._8A88Good99Example'

>>> 'probablY__a88gOod99ExampLe'.title()
'Probably__A88Good99Example'

>>> 'probablY__a88gOod??exampLe'.title()
'Probably__A88Good??Example'

r/pythontips • • Sep 11 '16

Syntax Python chained assignment (a=b=0)

26 Upvotes

When assigning variables to the same number for some kind of initialization, rather than doing

a = 0
b = 0

or using tuple unpacking

a,b = 0,0

you can actually do

a = b = 0

Note that this can be used with any object on the RHS, and that:

a = b = an_obj()

is equivalent to

temp = an_obj()
a = temp
b = temp
del temp

Note that the chain length for assignment can be longer than just two variables, so a = b = c = d = 0 is valid (though it probably gets a bit ugly if you go too far). Specifically, what happens is that the far RHS is evaluated and then from left to right assignments of the variables are made to it. This is different to C.

Obviously, you should be careful if you use this when assigning to mutable objects (such as lists), because of the typical object reference trip-ups in python that can be seen here; it means that

a = b = []
a.append(5)
print(b)
> [5]

a is b
> True

Still, I think it looks a bit neater in some cases, is very nice for number assignment and is a cool little tip :)

The specific python documentation is here, and you can read more about them on stack overflow here and here


r/pythontips • • Feb 26 '23

Meta Is there anywhere I could quickly revise my python knowledge?

25 Upvotes

I've taken an almost 6 month break, and need to quickly revise. I've forgotten quite a bit but noticed that most of it comes back once I read up a bit. Is there a source of beginner to upper-intermediate python tutorials in one place where I could quickly revise before continuing where I left off?


r/pythontips • • Sep 19 '22

Module Use all cores when Multiprocessing?

25 Upvotes

I’m working on a quad-core Jetson Nano, and I want to split the tasks in a pipeline into processes. Can I safely use four processes, or should one core be left to run the main Python file that manages the processes?


r/pythontips • • Jul 22 '22

Algorithms How do I create a calendar that follows a pattern (for divorced parents)?

25 Upvotes

I’m creating a calendar for divorced parents. The pattern goes: Week 1 - D,M,M,M,M,M,M Week 2 - D,M,M,M,D,D,D I want to be able to create a calendar and input that on the calendar. I want to make all the days for the dad blue and all the days for the mum red (for example).

How do I do this?


r/pythontips • • Dec 10 '21

Python3_Specific Complete guide on Python set operations

25 Upvotes

Learn everything about most common operations in set theory using Python:

https://pyshark.com/python-set-operations/


r/pythontips • • Sep 01 '21

Long_video A tutorial to create your own free neural search engine from scratch using open source framework, Jina

25 Upvotes

This video walks you through the fundamentals of how to create your own search engine with neural networks using open source software, Jina.

https://www.youtube.com/watch?v=zQqbXFY0Nco&t=358s&ab_channel=TheAIEpiphany


r/pythontips • • Feb 28 '21

Python3_Specific Automatic junk File Organizer using Python

25 Upvotes

r/pythontips • • Oct 23 '20

Python3_Specific Python script to create a Keylogger.

25 Upvotes

r/pythontips • • Oct 21 '20

Module Tooling for Python Package Development

25 Upvotes

r/pythontips • • Sep 24 '20

Short_Video Things you need to know about Python strings| Part 2

25 Upvotes

r/pythontips • • Sep 23 '20

Python2_Specific What are the uses of mutable and immutable objects:

25 Upvotes

Mutable objects can be used where you want to allow updates. Suppose you have a list of employee names in your organizations, and that needs to be updated every time a new member is hired. You can create a mutable list, and it can be updated easily. Immutability offers a lot of useful applications to different sensitive tasks we do in a network centred environment where we allow for parallel processing. By creating immutable objects, you seal the values and ensure that no threads can invoke overwrite/update to your data. This is also useful in situations where you would like to write a piece of code that cannot be modified. For example, a debug code that attempts to find the value of an immutable object.


r/pythontips • • Aug 03 '20

Long_video Getting Started With Python | Tuples, Lists, & Dictionaries | Tech Talks with Varun | OdinSchool

25 Upvotes

r/pythontips • • Jun 22 '20

Python3_Specific How to Create python API

25 Upvotes

How can i create a Rest API along with layout containing following features:

  1. Token based authentication for accessing the API

  2. Create New Router Details with unique loopback and hostname values.

  3. Maintain a database for the router details based on IP

  4. Get List all the routers based on type(AG1/CSS) using Sap ID

  5. Get the list of routers as per the given IP range values

  6. Delete record from database based on IP


r/pythontips • • Jun 10 '20

Short_Video Web Scraping with Python and Beautiful Soup - CraigsList

25 Upvotes

Hopefully, you will not ban me for sharing this content šŸ™‚ I hope i can help someone with this tutorial šŸ™‚Ā https://youtu.be/4ghsSOMgR8s


r/pythontips • • Dec 09 '19

I made a Classless Python State Machine for beginners

25 Upvotes

Using design patterns can be the best way to create maintainable and flexible code. However for beginners it is daunting task. Especially when it comes to python and OOP.

A Finite State Machine (FSM) is usually the first design pattern most people learn. In Python it is a pain in the butt to understand as you have to first get how classes work. What makes it even harder is the lack of switch/case statements in Python.

So I designed a Simple FSM for everyone to use as I had a tough time finding a State Machine that doesnt use OOP principals.

Yes, I know it's not perfect but it is a start.

Check it out on HERE

Have a nice day!