DEV Community

Cover image for Periodic Table toy in Python
Ben Sinclair
Ben Sinclair

Posted on

Periodic Table toy in Python

Wat.

It's a thing that lets you assemble words using only the symbols on the Periodic Table. It's a console app and it's purty.

Python 3

I've never committed to Python 3 before. I moved away from using Python in my day job a long time ago. But since 2 got deprecated, it's time to dive in. And you know what? It's not a big leap, and it's a nice warm pool.

I got the bug back from attempting Advent Of Code in Python this year.

Decisions, decisions

I started out with the data as a JSON file, which I imported like this:

with open('data/elements.json') as data_file:
    self.elements = json.load(data_file)

with open('data/layout.txt') as layout_file:
    self.layout = layout_file.read()
Enter fullscreen mode Exit fullscreen mode

but I'm still not sure how that works with distributing a package. So I converted it to a python module (which given that Python dictionaries are basically JSON was trivial)

Learning

I learned a few things:

  • f.strings
  • argparse groupings

Still to learn

  • Proper packaging and distribution. I can't believe it but I've never done this in Python, despite having written really quite a lot of code over the years.

Issues

  • No tests.
  • Not thread-safe.
  • The recursing solver function stores its stack in the object scope. This was done to save having to pass too many clunky variables around and tbh when I tried it I was off-by-one so I immediately gave up.

Prety Pictuars

This has been my first venture into Github projects with a README that contains images, and I'm not totally happy with it. I have to:

  1. keep the images in the main repo branch even though they're not part of the package
  2. host the images elsewhere, which is a fragile dependency
  3. use a separate branch and GitHub pages and link off to it, which is duplication

I went with option 1 because it's simplest. I don't think I like any of them, though.

Rationale

I used to have a periodic-table shower curtain and I'd read it while in the bathroom. I'd try to come up with as long a word or phrase as possible.

I write essentially the same program in whatever language I'm learning. I've done it in C, PHP, Javascript, Perl and Haskell (though I've lost most of the code as the years passed)

Where is this beast?

On GitHub as moopet/pyriodic

tl;di (too long, didn't install)

You can see roughly what it does from an online version moopet.net/periodic

This isn't running the same code, or even in Python. It's PHP if you must know.

Chocolate Bacon Counterespionage

The words,

Top comments (1)

Collapse
 
osde8info profile image
Clive Da

fantastic thanks ! i will add a link to this post on my edutainment blog