onsdag 5 april 2023

"Deep Blue Swag" by "Eminem" (Chat GPT 4)

Chat GPT is very useful indeed. It can do a lot of things, both useful and funny.

I asked Chat GPT to write a song by "Eminem" about the Blue Whale.

I thought it was quite impressive.

Here goes:


"Deep Blue Swag" by "Eminem" (Chat GPT 4)


[Verse 1]

Yo, I'm cruisin' through the ocean, feelin' that sway,

Ridin' waves like a gangsta, lookin' for a way.

See the biggest homie, got me trippin', no lie,

Blue whale so huge, touchin' the sky.


Floatin' smooth like a lowrider, no tension,

Its size and power, man, it's street comprehension.

A hundred feet long, like a block in the hood,

200 tons of weight, ain't nothin' that could.


[Chorus]

Deep blue swag, you own the sea,

Livin' large, the illest life, you're the MVP.

Rhyme and swim, we're tight, like family,

I'm vibin' with the realest, the blue whale, you and me.


[Verse 2]

Eatin' krill like it's fast food, they ain't playin',

Four tons a day, man, they ain't delayin'.

Biggest homies in the sea, but gentle, no doubt,

Chillin' in the deep blue, ain't no sellout.


Faced tough times, hunted, nearly wiped out,

But they bounced back, yo, they stood their ground.

Now they're risin', a symbol of hope,

Their strength and hustle, man, it's straight-up dope.


[Chorus]

Deep blue swag, you own the sea,

Livin' large, the illest life, you're the MVP.

Rhyme and swim, we're tight, like family,

I'm vibin' with the realest, the blue whale, you and me.


[Bridge]

The ocean's depth like the streets I've known,

These whales and my rhymes, connected, we've grown.

Together we'll rise, conquer this world,

The story of the blue whale, let it be heard.


[Verse 3]

Watchin' 'em swim, I'm feelin' mad respect,

Their power, their story, makes my mind reflect.

Inspired by their vibe, their hustle, their game,

A livin' example, don't give up, no shame.


[Chorus]

Deep blue swag, you own the sea,

Livin' large, the illest life, you're the MVP.

Rhyme and swim, we're tight, like family,

I'm vibin' with the realest, the blue whale, you and me.


[Outro]

So here's to the blue whale, the boss of the deep,

Their story's a treasure, one we gotta keep.

I'll carry their vibe in the rhymes I spit,

A shout-out to the blue whale, the ocean's legit. 

onsdag 28 december 2022

Multiplication of sequences like lists, tuples and str | Python

The multiplication operator on a ordered collection (sequence), like strings, lists, tuples, byte sequences or byte arrays means that the items/objects inside are repeated. It does not mean creation of copies (shallow or deep ones) of the items/objects.

This can be tested with the help of the id() function which returns the memory address of an object. Two objects with overlapping lifetime can never have the same id.

An exception to this is small intergers and other immutables, which Python keeps a small cache of hence giving them the same ID.


Example 1:

```

a = [1]

b = [1]

print(id(a), id(b))

```

Output: 2 different IDs because no cashing. 


Example 1.1:

```

a = 1

b = 1

print(id(a), id(b))

```

Output: Same ID for a and b, because of cashing.


Example 1.2 Same ID because of repeat:

```

a = [1]

# Multiply by 2 to get [1, 1]

a = a * 2 

for num in a:

    print(id(a))

```

Output: (Same ID x 2):

140719374866624

140719374866624

onsdag 9 november 2022

[Python] len() and time complexity

Some say "if you care about speed you shouldn't use Python". Others say "since Python isn't fast, you have to optimize it to make it viable".


The behavior of len() depends on the class definition.

Calling len() is always taking constant time for iterable data structures(string, list, tuple, etc.). That's because they have __len__() defined that way. They use a counter which is altered when the iterable in question is altered. len() is therefore just returning a counter in this scenario.

Without defining __len__() you can't use len() on it. Since it's up to the creator of the class to define what this dunder method should do and return, you can have any time complexion. You could for example traverse something every time the method is called. 

måndag 7 november 2022

Pygame for Python 3.11

As of writing this, Python 3.11 is a new release and in order to use it with Pygame you have to install Pygame in slightly different way:

pip install pygame --pre

fredag 4 november 2022

How to learn & three great Python books

 I recently found an interesting article about learning:

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5780548

From this paper:

Six strategies for effective learning:

1. Spaced practice - Instead of studying it all within a short time.

2. Interleaving - Switching between topics while studying.

3. Retrieval practice - Bringing learned information to mind from long-term memory.

4. Elaboration - Asking and explaining why and how things work.

5. Concrete examples - illustrating abstract concepts with specific examples.

6. Dual coding - Combining words with visuals.


For me, learning by doing works great. It makes it more fun and engaging.

For this I recommend the Python Cookbook: Recipes for Mastering Python 3 by Brian K. Jones and David M. Beazley.


Other great intermediate level books are Fluent Python and Effective Python: 90 Specific Ways to Write Better Python by Brett Slatkin.






måndag 17 oktober 2022

How to convert a .py file into .exe

1. Run pip install pyinstaller globally and also in your venv.

2. While using venv, type pyinstaller --onefile -w 'filename.py' in your terminal while being in the same directory as your filename.py.

3. Your .exe etc are now created!


The above worked for me. I read long guides on how to do this but this seems to be enough.

torsdag 22 september 2022

Python | Getting steering wheel to work in Arcade

Steering wheels in pygame and arcade go under the name "joysticks". I was able to get my Logitech G27 Racing Wheel to work with pygame but not with Arcade. I made a project using Arcade and used the joystick handling from pygame. 


General idea of the code, important parts:

joysticks = None

def init():
    pygame.init()
    pygame.joystick.init()

    global joysticks
    joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]


class MyGame(arcade.Window):

    def __init__(self):
    ...

    '''
    Use Logitech G27 Racing Wheel if found, else the first connected, if
    any.
    '''

    self.joy_in_use = None

    if joysticks:  
            for joystick in joysticks:  
                if "Logitech G27 Racing Wheel USB" in joystick.get_name():
                    self.joy_in_use = joystick  
               
            if not self.joy_in_use:
                self.joy_in_use = joysticks[0]
       
        if self.joy_in_use:
            self.joy_in_use.init()


    def arcade_joystick_events(self):
        for event in pygame.event.get(): # User did something.
               
            if event.type == JOYBUTTONDOWN:
                input = event.button
                if (input == 0 or input == 6):
                    # code for player jump
           
            if event.type == pygame.JOYAXISMOTION:
                if joysticks:
                    if (axis_val := self.joy_in_use.get_axis(0)) > 0.2:
                        # code for player moving right
                        # axis_val can be used to affect moving speed.
                        
                    elif axis_val < - 0.2:
                        # code for player moving left
                    else:
                        # code for player standing still
       
        pygame.time.wait(10)


    def on_update(self, delta_time):
        self.arcade_joystick_events()
        ...