• Home
  • About
    • About The Coder
    • Language List
  • The Rules
    • Rule #1: Clarity Trumps Everything
    • Rule #2: Source Code is for Humans
    • Rule #3: Don’t Ignore Warnings
    • Rule #4: Comment As You Go!
    • Rule #5: Always Use Parentheses
  • CS-101
  • Python-101
  • Guestbook

The Hard-Core Coder

~ I can't stop writing code!

The Hard-Core Coder

Tag Archives: Python code

Python String Translate

13 Thursday Oct 2022

Posted by Wyrd Smythe in Python

≈ 1 Comment

Tags

Python 101, Python code

Python str instances have many useful methods. I use strip, split, startswith, and others, quite a lot, for instance. One method I haven’t had reason to use so far is translate. It takes a dictionary argument and uses it to map the existing string to a new string.

It’s flexible and useful, so it’s worth knowing how to use.

Continue reading →

Loving the Lambda

15 Saturday Jan 2022

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

computer code, program, Python 101, Python code, Python lambda

One part of Python I especially appreciate is lambda functions. While I’ve never pursued functional programming, I do like many things about it, particularly the notion of functions as native data objects. Programming with functional objects opens new vistas. Most languages handle it one way or another, but languages make it natural.

Python’s lambda is such a facility, and I use it often. This week I finally got around to writing a lambda function I’ve been meaning to for a long time, and it’s my new favorite. I thought I’d share it along with some of my other “one-liners”…

Continue reading →

Calculating Entropy (in Python)

21 Tuesday Dec 2021

Posted by Wyrd Smythe in Python

≈ 2 Comments

Tags

entropy, Python code, Shannon entropy

I’ve been playing with calculating the entropy of a toy system used to illustrate the connection between “disorder” and entropy. (See Entropy 101 and Entropy 102.) I needed to calculate the minimum number of moves required to sort a disordered collection, but that turns out to be an NP problem (no doubt related to Traveling Salesman).

The illustration bridges Boltzmann and Shannon entropy, which got me playing around with the latter, and that led to some practical results. This article describes the code I used mainly as an excuse to try embedding my own colorized source code blocks.

Continue reading →

Building a Turing Machine

10 Friday Dec 2021

Posted by Wyrd Smythe in Fun, Python

≈ 1 Comment

Tags

modeling, Python code, simulation, Turing Machine

I’m not sure how I got to thinking about Turing Machines (TMs). I was going through some files recently and spent some time looking at busy-beaver.c (from 2017 according to the file date). It contains an implementation of a TM. But something else got me speculating about my own implementation; I just don’t recall what.

I decided to actually write it when it occurred to me that I could use a Python generator to implement the Turing Machine tape. Sadly, I didn’t think of it in time for the trilogy I just published about Python generators (part 1, part 2, part 3).

Continue reading →

Python Tokenize

03 Friday Dec 2021

Posted by Wyrd Smythe in Python

≈ 1 Comment

Tags

HTML, Python 101, Python code

Recently I thought to myself, “Hey self,… Although I’ve never looked into them, I know Python has tools for parsing Python code. I wonder if they might make generating syntax-highlighted HTML pages of Python scripts pretty easy?”

I found the help page for the tokenize module, and the introduction says it’s “useful for implementing ‘pretty-printers,’ including colorizers for on-screen displays.” That sounds like a strong yes.

Continue reading →

Python Generators, part 3

24 Wednesday Nov 2021

Posted by Wyrd Smythe in Python

≈ 3 Comments

Tags

Python 101, Python code, Python generator

In the last two posts I’ve explored Python generator functions. The first post went over the basics and showed how they return an iterable object usable in for-loops and list constructors. The second post explored how generator functions work in more detail.

This time I’ll wrap it up with some examples that are a bit more involved. If Python generators were a mystery before these three posts, I hope you feel more comfortable with them after!

Continue reading →

Python Generators, part 2

23 Tuesday Nov 2021

Posted by Wyrd Smythe in Python

≈ 4 Comments

Tags

Collatz conjecture, Python 101, Python code, Python generator

Last time I began exploring Python generator functions. I mostly covered their basic function as iterables, and why they’re good in for-loops. They’re also good for infinite lists. Much of their value lies in how they defer processing an item until it is actually asked for.

This time I’ll dig into the send method, which allows sending messages to a running generator. I’ll also introduce the yield from statement, which allows us to wrap a generator with another function that also acts like a generator.

Continue reading →

Python Generators, part 1

22 Monday Nov 2021

Posted by Wyrd Smythe in Python

≈ 5 Comments

Tags

Python 101, Python code, Python generator

Python has a feature called a generator — it’s a way of writing a function that allows that function to trade control back and forth with a calling function. It’s not something one needs often, but it can very useful in certain situations.

Create a Python generator, firstly, in using yield instead of return and, secondly, by designing the function a little differently from one that generates and then returns a list of items.

Continue reading →

A Simple DL Parser

10 Wednesday Nov 2021

Posted by Wyrd Smythe in Fun, Python

≈ 4 Comments

Tags

computer code, language design, little programming language, Python code, recursive descent parser

Last time I introduced a general Definition Language (DL) I created for defining structured information. The end goal was an extension of DL, called Data Definition Language (DDL), intended for defining memory and file formats. It was intended for tools that examine that data, allowing them more knowledgeable output than a raw hex dump.

I mentioned that DL has been on my mind lately, and as it turns out I spent the day yesterday writing a DL parser in Python.

Continue reading →

Always Implement toString

02 Tuesday Nov 2021

Posted by Wyrd Smythe in CS101

≈ 2 Comments

Tags

code clarity, computer code, computer languages, computer programming, Java, Python code, readable code, toString, __str__

Although I’m categorizing this one as really good advice, rather than as a rule, I think it should be viewed as basically a rule. I think it should be a rule in any object-oriented language that supports it natively (Java and Python, for example).

The advice (rule of thumb, say) is to always create a useful implementation of toString when you create a class. It makes your development and maintenance life ever so much better.

Continue reading →

← Older posts
Follow The Hard-Core Coder on WordPress.com

The Posts

  • Python String Translate
  • The Last Bug
  • Regular Expressions
  • Bits Bytes Chips Clocks
  • Loving the Lambda
  • Failure Tales
  • Naming Things (redux)
  • Calculating Entropy (in Python)
  • Building a Turing Machine
  • Python Tokenize

The Topics

  • Blog (4)
  • CS101 (35)
  • Fun (27)
  • Interesting (1)
  • Opinion (12)
  • Python (24)
  • Stories (15)

The Month

March 2023
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Oct    

The Past

Posts

RSS Feed

Comments

RSS Feed

Enter your email address to follow The Hard-Core Coder and get email when there is a new post.

The Main Blog

Visit Logos con carne

Lots of wyrds... with meat!

The Author

Wyrd Smythe

Wyrd Smythe

The canonical fool on the hill watching the sunset and the rotation of the planet and thinking what he imagines are large thoughts.

View Full Profile →

Create a website or blog at WordPress.com

  • Follow Following
    • The Hard-Core Coder
    • Already have a WordPress.com account? Log in now.
    • The Hard-Core Coder
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar