Weak notes

by Benny

typically less than weekly things of note

← back 498 words

From PHP to Python and GitHub Actions

Some time ago for a bit of fun I had created Horo Football - a twitter account that auto-tweeted (via a php script and cron job) some randomly generated horoscopes from a football context.

Having dabbled with a bit of python recently, I decided to rewrite this in python.

$string = str_replace( '#zodiac#', $go->getValueFromKey( $zodiac ) , $string );
$randHouses  = $go->getValuesFromArray( $house  , 2);

public function getValueFromKey(&$array) {
 $rand_key = array_rand($array);
return array_pop($array);
public function getValuesFromArray($array, $n) {
 return array_rand(array_flip($array), $n);


for sign in signs :
 dic = {
    "#zodiac#"  : sign,
    "#adjOrb#"  : random.choice(adjOrbs),
    "#suffix#"  : random.choice(suffixes),
    "#gerund#"  : random.choice(gerunds),
    "#house#"   : random.choice(houses),
    "#house2#"  : random.choice(houses),
    "#planet#"  : random.choice(planets),
    "#planet2#" : random.choice(planets)
 output_string = multiple_replace(dic, random.choice(phrasing))

I am amazed at how concise python is, having switched from a 200 line php file with a few methods coded in different classes so around 250 all in, to around 180 in python, but with processing (excluding the arrays of data) to around 25 lines.

You can see outputs published via a GitHub action to a README.md file in this repo

name: Daily Build
# build
    - cron:  '30 7 * * *'

    runs-on: ubuntu-latest
    - name: Check out repo
      uses: actions/[email protected]
    - name: Set up Python
      uses: actions/[email protected]
        python-version: 3.8
    - uses: actions/[email protected]
      name: Configure pip caching
        path: ~/.cache/pip
        key: $-pip-$
        restore-keys: |
    - name: Install Python dependencies
      run: |
        python -m pip install -r requirements.txt
    - name: Update README
      run: |-
        python 'python/build.py'
        cat README.md
    - name: Commit and push if README changed
      run: |-
        git diff
        git config --global user.email "[email protected]"
        git config --global user.name "README-bot"
        git diff --quiet || (git add README.md && git commit -m "Updated README")
        git push

As you can see from the action it is essentially a set of key value pairs in yaml. Installing Python and any dependencies in a serverless environment - managed by GitHub, the Python script updates the README file and this action commits it.

workflow_dispatch means it can be run manually, schedule takes a cron expression. It can also be triggered on a push or pull request.

GitHub actions are stored in your repository in a .github/workflows folder and thats it.