RSSAmplifier

Blog

Infinitely Abstract

Artful Programming, Clean Design

blog.erezsh.comRSS feed ↗10 posts

Latest posts

My class is bigger than your class

Python classes are great. But they can be better. Take a look at this class, let's call it Foo: Yes, try as it might, this puny class doesn't even know if it's bigger than 0 or not! It would not help to define a __gt__ method for it, because those only run for instances, not... View Article The post My class is bigger than your class appeared first on Infinitely Abstract .

5 Lark features you probably didn’t know about

If you've ever used Lark, you probably already know that it's rather featureful compared to other parsing libraries. Even when it was first released, five years ago, it already had two parsing algorithms, several lexers, automatic AST construction, automatic line counting, and the list goes on and on. What more could anyone want from a... View Article The post 5 Lark features you probably didn t…

Create a stand-alone LALR(1) parser in Python

Over the years, I noticed that many developers are reluctant to use parsing libraries, especially if the language they need to parse is relatively small. The reason is that they wish to avoid adding external dependencies to their project. Although pip (setuptools) can automatically fetch whatever is required, external dependencies makes zip releases harder, and... View Article The post Create a…

How to write a DSL (in Python with Lark)

The first time I used Logo, it felt like magic. I could type a short sequence of simple commands, and draw beautifully complex shapes on the screen. In this tutorial, I will show you how to parse and interpret a Logo-like language in just 70 lines of code, and use this example to make broader... View Article The post How to write a DSL (in Python with Lark) appeared first on Infinitely Abstract .

How To Write A Calculator in 70 Python Lines, By Writing a Recursive-Descent Parser

Three months ago, I wrote a post detailing the process of writing a calculator using a parsing library. The popular response, however, was that readers are far more curious about seeing a calculator written from scratch, with the batteries included but nothing else. I figured, why not? Writing a calculator is simple, if you use hacks... View Article The post How To Write A Calculator in 70 Python…

How To Write A Calculator in 50 Python Lines (Without Eval)

Introduction In this post I will demonstrate how to parse and calculate an arithmetic expression a general-purpose parser. When we're done, we'll be able to handle expressions such as 1 + 2 * -(-3+2)/5.6 + 3, and hopefully you'll have gained the tools to handle much more. My motivation is to provide a simple and... View Article The post How To Write A Calculator in 50 Python Lines (Without Eval)…

Contracts and protocols as a substitute to types and interfaces

I am a big fan of assertions. Whenever I reach a point in my code where I say "that pointer can't possibly be null", I immediately write - assert( p != NULL ); - and whenever I say "this list can't possibly be longer than 256" I write assert len(l) = 256. If you wonder why... View Article The post Contracts and protocols as a substitute to types and interfaces appeared first on Infinitely Abstract…

Baker – Expose Python to the Shell

It's been a long time since my last post, and it would be appropriate that I post about whatever it is that I've been working on. But I won't. I'm writing this post only to tell you about an interesting new python library I stumbled upon. Baker, in their own words, "lets you easily add... View Article The post Baker Expose Python to the Shell appeared first on Infinitely Abstract .

Lazier Copy-On-Write

Copy-on-write (COW) is a popular mechanism of lazy evaluation, that helps improve running speed and reduce memory requirements by transparently delaying the copying of data. Essentially, this is how it works: When you try to copy an object, you are instead given a fake object. Attempts to read that new object will instead read from... View Article The post Lazier Copy-On-Write appeared first on…

PySnippets – improving code reuse

For a long time now, I've been hindered by the issue of utilities, or snippets. These are convenience functions and classes that are too small or too incomplete to justify a library, yet are useful enough to be used. I've posted a few on my blog: Namespaces, X and now FileDict. Others I didn't post,... View Article The post PySnippets improving code reuse appeared first on Infinitely Abstract .