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()
        ...

torsdag 18 augusti 2022

Python | Avoid IndexError by slicing

"IndexError: list index out of range" can for example be avoided by doing [0:1] instead of [0] 

Let's say you're handling iterators that sometimes are empty and do something like "...if list_ else []". 

You're doing this to avoid trying to access an index that doesn't exist when the list is empty, like this: [][0].


If you don't need the item directly and it's ok to return an iterable, an easier or at least shorter way of returning empty iterable can be to return a sliced version of it. 

[][:1] returns an empty list []

["Test"][:1] returns ["Test"]


Other working examples:[][0:0], [][1:0], [][0:2] [][100:25], [][25:100]

These all return [].




torsdag 4 augusti 2022

Decorators in Python

Decorator is a good name to describe what it does although the concept is a bit confusing at first. 

When a decorator has been made, it can potentially be used on any function you like. For example, you can extend/enhance your function to make it calculate how long it takes to run it.
All you have to do is adding "@your_decorator_name" just above the function you like to extend.
This means that additional code will be run, using your function in whatever way the decorator is defined to run it. And it uses the same arguments.

Example:


import functools

def your_decorator(base_func):
    @functools.wraps(base_func)
    # extended_func will be called instead of base func
    # since it's returned and replaces base func
    def extended_func(input_from_base_func):
        print("Printed inside the extended func")
        # This will call will_be_extended
        base_func(input_from_base_func)
        return "Returned when will_be_extended is called"
    return extended_func

@your_decorator
def will_be_extended(used_by_extension):
    # Do something
    print("Printed from base function")
    # This prints: "Sent to base func from decorator"
    print(used_by_extension)
   
# will_be_extended is called but the extended_func is returned
# automatically instead, including the text argument.
from_extended_func = will_be_extended("Sent to base func from decorator")

# This will print the returning value of extended_func,
# "Returned when will_be_extended is called"
print(from_extended_func)

 

måndag 1 augusti 2022

Python - Journey from intermediate to expert Pythonista

 As with any programming language you learn, you need a roadmap. It might not be obvious how to personalize such a path.

Things to take into consideration is for example prior knowledge and which format that works best (video or text).

The basics is similar between many languages. Most courses starts from the very basics and it can sometimes be hard to skip content in a course or tutorial.

Today there's a ton of readily available both free and paid learning resources online. There's an argument to be made that for motivated, self-learning is enough. Many employers care more about what skills and knowledge you actually have and less about which schools you've graduated from.

I started learning Python a few months back and I would like to share my views on the learning experience so far.


My general opinions regarding learning programming:

Videos

Great for explaining difficult topics.

Articles

Great for a more in-depth understanding about specific subjects.

Books: 

Great as guided tours on laid out paths. Make sure it's the right path before investing your time in a book that might not be for you. 

Podcasts:

Great when doing something else.

Apps (Android/iOS):

Great for studying when you're not at home.

Projects:

Creating projects is incredibly important. This is the way to actually learn what you've consumed and read. Doing projects is what makes you actually learn and memories.

Problem solving exercises like leetcode, codewars or checkio:

Solving engaging challenges and fun tasks is nice a complement to your studies. It also gives a confidence boost and lets you know your skill level.  You can share your code and interact with others. 

Other thoughts:

If possible, finding a mentor gives motivation a boost. Same goes with forums, slack and discord. 

Knowing how to do research and use search engines efficiently is crucial. More or less all questions you might have is already answered.

As with everything, a clear goal with what you're doing and why you learn is essential for motivation. Maybe becoming a data analyst even data scientist? 


Finding a path

As someone with a bachelor in computer science and experience with languages like Java and C++, doing basic Python tutorials wasn't ideal. I have tried many services online. Here are my recommendations:

CS50's Introduction to Programming with Python[Video format] (Free) (harvard.edu/course/cs50s-introduction-programming-python)

I took this course after watching this review by "Python Programmer": 


CS50 will be challenging for someone new to programming. With prior knowledge you can increase the playback speed.

Real Python [Basic to Master] (Paid and Free) realpython.com

I just bought a subscription. This site seems to have it all. Articles, videos, learning paths, community, slack, updates, quizzes, Q&A with experts, books (cost extra) and more.

Example of article: realpython.com/python-time-module


App [Basic to Intermediate] (15 days Free) "Sololearn" (Android/iOS) 

It is a little hard to type on the phone but the material was incredible and was made into a fun game-like experience with leaderboards and certificates.

Try to complete these courses: Python Data Structures, Intermediate Python and Python for Data Science. The SQL course is also really good. 


Books

So far my favorite books are (intermediate level) "Fluent Python" and "Python tricks the book".


Podcast

There are many but one that I've listen to a lot is realpython.com/podcasts/rpp/


Some Youtube recommendations

Corey Schafer youtube.com/c/Coreyms

Fireship youtube.com/c/Fireship

Python Engineer youtube.com/c/PythonEngineer/

Coding Tech youtube.com/c/CodingTech

Python Programmer youtube.com/c/FlickThrough

freeCodeCamp.org youtube.com/c/Freecodecamp 

fredag 22 juli 2022

From a list, count how many numbers are within the standard deviation from the mean. (Python)

heights = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190]

height_sum = sum(heights)
mean = height_sum / len(heights)

# **2 makes negatives (heights less than the mean) become positive.
diff_list = [(x - mean) ** 2 for x in heights]
variance = sum(diff_list) / len(heights)

# We get the square root in order to get back the original units.
standard_deviation = variance **0.5

# Now we just need to find out how many heights are within the
# standard deviation from the mean.
count = 0
upper_lim = mean + standard_deviation
lower_lim = mean - standard_deviation

# Count how many heights are within the
# standard deviation from the mean.
for height in heights:
    if lower_lim < height < upper_lim:
        count += 1
       
print(count)






''' Or as a class, initiated with the heights list: '''

class Std():
   
    def __init__(self, data: list):
        self.data = data
        self.mean = self._get_mean()
        self.var = self._get_variance()
        self.std = self._get_std_from_var()
       
        self.upper_lim = self.mean + self.std
        self.lower_lim = self.mean - self.std
   
        self.in_range = lambda x: self.lower_lim < x < self.upper_lim
        self.count_within = len(list(filter(self.in_range, self.data)))

    def _get_mean(self):
        return sum(self.data) / len(self.data)

    def _get_variance(self):
        diff_list = [(x - self.mean) ** 2 for x in self.data]
        return sum(diff_list) / len(diff_list)

    def _get_std_from_var(self):
        return self.var **0.5

std = Std(heights)
print(std.count_within)

måndag 11 juli 2022

OpenArtViewer

I've made an art viewer program in Unity. I'm now working on database update automatization in Python. That is because the files with art information provided are updated frequently.

OpenArtViewer uses the National Gallery of Art Open Data API and data from Metropolitan Museum of Art. The included SQLite database has stored info about the artworks in public domain (100K+) including image links to the artworks on the NGA and MM servers. 

From the GUI you can search type of art and by title, year, artist. To the left there's a scroll bar with the results as thumbnails. These can be clicked to get more information and a higher resolution version of the artwork in the big panel to the right. A high resolution image can then be downloaded. 

Download this program (Windows) https://wartem.se/files/OpenArtViewer.zip




National Gallery of Art

Metropolitan Museum of Art

lördag 9 juli 2022

Flask - Absolute basic client input and render

The following example shows how you can handle input with Flask and update pages, in this case with the user input sent back to the client. 

I'm making this as short as possible. 

Requirements: pip install Flask 

Implementation: 
1. In your project root dir create: A file named app.py 
2. In your project root dir create: A folder named "templates" and inside a file named "index.html" 


index.html 
<html lang="en"> 
  <head> 
  <title>Flask Basic Example</title>
  </head>  
  <body>    
  <h1>Flask Post: {{ text_to_show }}</h1>    
  <form action="/" method="POST">      
  <input type="text" name="content" />     
  <input type="submit" />    
  </form>  
  </body></html>

app.py
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        user_input = request.form['content']
        return render_template("index.html", text_to_show=user_input)
    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True, port=5000)

How it's used: Run app.py and go to http://127.0.0.1:5000 in your browser. Type some input, press send and the server will update the HTML with your input in a HTML paragraph. The Internet is full of extensive guides and tutorials. Sometimes you just want a quick example.

Python Virtual Environment Setup

Working in a virtual environment is easy to setup and makes life easier thanks to the project isolation and dependency management.
Run once:
py -m pip install --user virtualenv
Run in project folder:
py -m venv env
Run in project folder:
.\env\Scripts\activate
Done! Happy coding.