6/15/18

Python: Picking random events for a game: developing the algrothm





This video walks though developing code to pick random events where the
events have different probabilities.



This could be used in games,
simulations, or picking random visual elements.



It will show two functional programming approaches and one Object
Oriented approach



6/11/18

Web Scrape YouTube channel for video info and a table of contents using Python 3.6 and Beautiful Soup (V1.1)


THIS TECHNIQUE NO LONGER WORKS!
I will post an update in the future with code that using the Goggle API.

I have a YouTube channel for my videos to teach programming and needed to create a nice table of contents for a web page.

YouTube always list the videos and playlists with a lot of graphics and thumbnail images which look nice, but it does not fit much on a page for reference.

I tried to use write a program using their API, but decided it was better to try 'screen scraping' or 'web scraping'. This is a technique that is used to get information out of web pages.

I ended up writing a python program. Since many sites do not like scrapping, I checked the User Terms Contract for YouTube, and it is OK as long as it is as slow as a human.

I walk through version 1.0 of the code at my YouTube video @ https://youtu.be/NvxAhUkVgHU

Also please subscribe to my YouTube channel

I wanted a program that would output a CSV file to import to a spreadsheet or DB and a HTML file providing a table of contents to all the videos on the channel that I could copy to my blog.

I decided to pull the following data for the CSV file:

  • channel name
  • section name 
  • playlist name 
  • video title
  • video link
  • video time length
  • number of views
  • publication date
  • number of likes
  • number of dislikes
  • video description


I have embedded a gist here that you can see and get the code from:








You need python 3.6 or greater and just need to change the channel name near the top of the program.
Then run it to create the CSV and HTML file in the current run directory.

If you only need the HTML, json, or CSV file, see the if __name__ == '__main__' section and write an driver py program with imports like run_json.py example.

3/15/18

One Link to find FREE images though Google Search


HOW TO FIND FREE PICTURES AND IMAGES

  Click on this image to search:

 Click on this image
              (you can bookmark it of course)


Just use this image to link to google search that already has the advanced settings for finding free images.

Once there:
  1. type in a search phrase for image
  2. click on image that you what
  3. you can then select to download one you see
  4. or click on visit to go to the original page which may have other sizes

The images you see in searh are under the following copyright license or equivalent:

    CC0 License

    ✓ Free for personal and commercial use
    ✓ No attribution required

So you are free to use it for anything including commercial use, without attribution to the original author, including modifying it.

3/13/18

Strava Global Heatmap lands Strava in Hot Water:

Strava Global Heatmap lands Strava in Hot Water:


Strava developed a online map of the world showing activity tracker of people's paths that wear the Strava devices.

It was released in November 2017.

(Strava Heatmap Site)

The map shows activity of billions of activities around the world. Unfortunately it also showed activity at sensitive locations like areas 51, the pentagon, and arms services bases overseas. This was pointed out by Nathan Ruser on Twitter (Twitter Reference)

File this one under 'Unintended Consequences of New Tech'

3/5/18

Python Class: ElapsedTime (works like a stopwatch)

 If you are learning python, short examples are always helpful. Here is one.

Class in Python: ElapsedTime:

Use:

e1 = ElapsedTime()  # starts time e1

x = e1()  # get elapsed time as float in seconds from timer

... do something more

x = e1()  # get elapsed time since start

The code shows a couple neat things in python:
  • using a object reference as a methods by defining the __call__ standard method
  • using a Unicode char in output (this is easier in python 3)

The Code:

 
"""Small elapsed time class"""
# YouTube subscribe link: https://goo.gl/ZgZrZ5
# Gerry Jenkins

import time

class ElapsedTime():
    """elapsed since creation objects"""

    def __init__(self):
                self.start = time.time()  # store only start time
        
    def __call__(self):  # used to use object name as call
        """return elapsed time in float seconds"""
        return time.time() - self.start
        
        
if __name__ == "__main__":
    # check it out, follow two elapsed timers
    
    e1 = ElapsedTime()
    for _ in range(5):  # use e1
        print(f'e1: {e1()*1000000:0.1f} µsecs') # unicode micro
        
    e2 = ElapsedTime()
    for _ in range(5):  # use both e1 and e2
        print(f'e1: {e1()*1000000:0.1f} | e2: {e2()*1000000:0.1f}')

        

OH, and please subscribe to my YouTube Channel at: youtube.com/gjenkinslbcc

And check out my Python, Data Structures and Algorithms video class

2/11/18

Great article on history of Olympic Icons from Graphic point of view

Great article for Graphic Design on the history of Olympic Games Icon design:

Drawing From The Past – Josh S. Rose – Medium A Deeper Look at PyeongChang’s Olympic Pictograms @ medium.com

2/5/18

Some intresting Articles




30-minute Python Web Scraper – Hacker Noon


Short article on using Python 3.6, PyCharm, geckodriver, Pillow, Selenium with Firefox to web scrape nice images off unsplash.




Biggest list of free online MOOC classes I have found: class-central.com


This is a site with thousands of free online Massive Open Online Courses (MOOCs). Many of these courses are on technical subjects.  There are over 700 courses in Computer Science alone



Docker training, better than virtual images, great for running little linux CLI machines



Better than sliced toast!   If you like linux, and running it from the command line. This is the best way to do small linux machines running each in their own 'virtual environment'. And this is the core of the new 'container' craze for cloud apps.




Great story of setting up a $25 computer to run windows .net framework in a linux environment.




The Best Monitor for Programming: A Cheap 40″ 4K TV



Setting up a $300 4K TV to get a 40 inch 3840×2160 pixels as computer monitor.





Using better CLIs


For those that use the command line, things to go to the next level.

1/2/18

Python dynamic program for minimum change with arbitrary currency values

In my videos:

Recursion 9 | Min Coins - Dynamic Programming 1 (12:01)
Recursion 10 | Min Coin - Dynamic Programming 2 (10:17)

that work along with the Miller ebook: 
Problem Solving with Algorithms and Data Structures using Python


code is presented to solve the problem of choosing the minimum number of coins to to make change for an amount utilizing dynamic programming. These videos walk you through how to solve this problem in different ways including handling adding a new 21 cent coin to the normal US mix of pennies, nickels, dimes,  and quarters.

A comment from a youtube viewer asked if the code could solve for coin values that sometimes did not have a solution.

I have modified the original code and here is that solution in python for those interested. The dynamic programming technique builds a list all solutions up to the one asked for in the dpMakeChange method and stores those solutions in the lists coinCount and coinsUsed.

Here is the new code:





# modification to dynamic programming Miller solution:
#   http://interactivepython.org/runestone/static/pythonds/Recursion/DynamicProgramming.html
# in the case of arbitrary coin currency amounts that there is not always a solution

def dpMakeChange(coinValueList, change, minCoins, coinsUsed):
    smallestCoin = coinValueList[0]
    minCoins[smallestCoin] = 1
    coinsUsed[smallestCoin] = smallestCoin
    for cents in range(smallestCoin + 1, change + 1):
        coinCount = cents + 1  # pick biggest possible to replace with min
        newCoin = smallestCoin
        for j in [c for c in coinValueList if c <= cents]:
            prevSolution = minCoins[cents - j]  # add this coin to prev solution
            # check if exact coin or prevSolution exist and a new minimum was found
            if j == cents or (prevSolution != 0 and prevSolution + 1 < coinCount):
                coinCount = prevSolution + 1
                newCoin = j
        if coinCount < cents + 1:  # found a solution min
            minCoins[cents] = coinCount
            coinsUsed[cents] = newCoin
    return minCoins[change]

def printCoins(coinsUsed, change):
    coin = change
    if coinsUsed[coin] == 0:
        print(f"no solution for {change}")
        return
    while coin < 0:
        thisCoin = coinsUsed[coin]
        print(thisCoin)
        coin = coin - thisCoin


def main():
    amnt = 63
    clist = [1, 5, 10, 21, 25]  # these are the coins to choose from
    coinsUsed = [0] * (amnt + 1)
    coinCount = [0] * (amnt + 1)

    print("Making change for", amnt, "requires")
    print(dpMakeChange(clist, amnt, coinCount, coinsUsed), "coins")
    print("They are:")
    printCoins(coinsUsed, amnt)
    print("The used list is as follows:")
    print(coinsUsed)
    print(coinCount)


main()




Attribution: 

Problem Solving with Algorithms and Data Structures using Python

By Brad Miller and David Ranum, Luther College
The code above is a modification under creative commons protections:
Creative Commons License
"Problem Solving with Algorithms and Data Structures using Python" by Bradley N. Miller, David L. Ranum
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License