<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samuel Baafi Boakye on Medium]]></title>
        <description><![CDATA[Stories by Samuel Baafi Boakye on Medium]]></description>
        <link>https://medium.com/@samuelbaafi?source=rss-162d1d4b6ec1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Bo-WCRC8Dx2wnJBp.</url>
            <title>Stories by Samuel Baafi Boakye on Medium</title>
            <link>https://medium.com/@samuelbaafi?source=rss-162d1d4b6ec1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 11:20:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samuelbaafi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improving Analytics Engineering at mPharma — PART 1]]></title>
            <link>https://medium.com/mpharma-tech-blog/improving-analytics-engineering-at-mpharma-part-1-4b84b050f8a0?source=rss-162d1d4b6ec1------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b84b050f8a0</guid>
            <category><![CDATA[dbt]]></category>
            <category><![CDATA[analytics-engineering]]></category>
            <category><![CDATA[data-engineering]]></category>
            <dc:creator><![CDATA[Samuel Baafi Boakye]]></dc:creator>
            <pubDate>Thu, 20 May 2021 12:02:26 GMT</pubDate>
            <atom:updated>2021-06-28T09:31:41.531Z</atom:updated>
            <content:encoded><![CDATA[<h3>Improving Analytics Engineering at mPharma — PART 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*inpgOWU_2XuG5VrYeW1o_g.jpeg" /><figcaption>Photo Courtesy of <a href="https://unsplash.com/photos/D4LDw5eXhgg">Adam Nowakowski</a></figcaption></figure><p>If you have ever worked on a data science-related project, you might be familiar with the process of data cleaning and feature engineering. The cleaning process usually involves removing null values, renaming fields, correcting spelling mistakes, and dealing with missing data while the feature engineering process usually involves generating new fields for the dataset, merging dataset with other datasets, and generating even more fields. Data cleaning and feature engineering could be summed up into one process called analytics engineering.</p><p><em>In a data team, Analytics engineering can be defined as what an engineer does to bridge the gap between a data engineer and a data analyst.</em></p><p>In a modern data team, you would probably have an analytics engineer to “<a href="https://www.getdbt.com/what-is-analytics-engineering/">provide clean data sets to end users, modelling data in a way that empowers end users to answer their own questions</a>”. But traditional data teams do not have an analytics engineer. A traditional data team consists of data engineers and data analysts.</p><p>This article would cover what traditional data teams are lacking and how to bridge the gap between the data engineer and the data analyst.</p><h3>The Role of a Data Engineer</h3><p>A data engineer’s sole responsibility is to move data from one point to another. The engineer ensures that data is transported in the safest way possible from point A to point B and disseminated to the people who need it in a timely manner.</p><p>Ever wondered what happens when the data gets to its destination? This is where the data analyst plays a role.</p><h3>The Role of a Data Analyst</h3><p>A data analyst generates business insights from data that is provided by the Data Engineer. Once the analyst receives the data, the individual needs to understand it, transform the data (if necessary) and extract insights from it.</p><p>Hence in a traditional data team, the process entails a data engineer moving data from point A to point B, then the data analyst uses the data at point B to generate insight. This is generally the workflow in a traditional data team.</p><p>With the workflow in a traditional data team in mind and the definition of analytics engineering, you might be wondering if a gap really exists between a data engineer and a data analyst. The crux of the matter is, both roles can co-exist but it leads to some serious implications.</p><h3>Implications of the Traditional Data Team’s Workflow</h3><p>A traditional data team’s workflow suggests that when the data engineer moves data from its original source (Point A) to a destination (Point B), his job is done. But why is this a problem?</p><ul><li>Data in its original source is in its rawest form — when data is moved through a pipeline to a data warehouse or data lake, it is still pretty much in its raw form although probably not as raw as it was in the source. This is problematic for the data analyst because the analyst will have to generate insight from the raw data. To illustrate, this could be likened to someone inviting you to a buffet, but you are served uncooked food and asked to use your discretion to prepare your own food. This leads us to the next problem.</li><li>Since the analyst has been served uncooked food, the analyst will have to take the pains and effort to prepare his own food; that is a bit misleading. Because of the structure of the traditional data team, you will find analysts writing long lines of SQL code just to create materialized views (MVs) or reports for a particular department in the business. Although the analysts do their best to create MVs that end up acting as dependencies for other MVs and reports, it can clearly be observed that the process of preparing their own food could have been avoided. How so?</li></ul><h3>The Gap</h3><p>As can clearly be observed, a gap exists between the role of a data engineer and the analyst for traditional data teams. The gap is created because neither the data engineer nor the data analyst is a cook. The former provides raw materials like vegetables, a bag of rice, oil, and the likes while the latter needs finished products like sauce or stew, cooked rice or cooked meat to taste in order to make sense of what has been prepared, and finds ways to combine several finished products to suit others.</p><blockquote>The gap is the cook which is rightly named Analytics Engineering.</blockquote><p>With Analytics Engineering, when the data gets to Point B, the data undergoes a process where the data is cleaned and transformed to prepare it for the data analyst. A number of processes are involved in analytics engineering which are:</p><ul><li>Raw data is transformed into meaningful data. Instead of the transformation happening in the reports given to the business, it is done at this stage. This makes it easier for the analyst to generate insight and create reports at a later stage.</li><li>Raw data is decoupled into various data marts to serve various business functions.</li><li>Raw data and processed data are properly tested for discrepancies based on predefined metrics or expectations at regular time intervals.</li></ul><p>At mPharma, the data team’s structure falls within the structure of a traditional data team and a modern data team. This means we do a little bit of analytics engineering but this function is not properly defined. But unlike the modern data team which would have an analytics engineer, we plan to have every member in the data team function as an analytics engineer as well. This means data engineers and data analysts are all going to learn to cook.</p><p>There are a number of ways to approach Analytics Engineering. I would like to summarize them into two main ways — <strong>the hard way</strong> and <strong>the easy way</strong>.</p><h3>The Hard Way</h3><p>Currently, we approach analytics engineering the hard way.</p><blockquote>The hard way is when the data analyst combines a set of tools which are partially automated to handle a particular process under analytics engineering.</blockquote><p>At mPharma we have a platform to automate our scripts for analytics engineering. We version control our scripts using GitLab and run tests separately. We manually document our scripts and database tables using GitLab wikis. The point I am making here is that each part of the analytics engineering workflow seems to be decoupled from each other which makes it difficult to maintain and it does not help analytics engineering scale due to the following reasons:</p><ul><li>Tests are only run when data is being cleaned and transformed for the first time. This means that in the subsequent runs of our custom scripts, there will be no testing. This leads to a series of problems — What if bad data has entered a materialized view? What if the count of rows in a materialized view has reduced when it should be increasing? What if the refresh rate of a particular table is off? What if vital metrics are off? We would never know unless we manually check or run tests. The solution would be to run automated tests as part of the analytics engineering workflow.</li><li>Another issue with <strong>the hard way</strong> is that it is not easy to create dynamic and custom queries with a templating engine. With the current workflow we have, queries are structured for a particular materialized view or report. If in any case, you have two scripts that have a similar code structure, you will not be able to apply the DRY (Don’t Repeat Yourself) software development principle — you will have to deploy both scripts independently. The workaround is to create a materialized view with the part of the code that is similar and both scripts will use that materialized view.</li><li>Another point worth mentioning is that there are libraries and packages to make analytics engineering a little easier. Libraries like <a href="https://greatexpectations.io/">Great expectations</a> can seriously improve our workflow. There are some packages that are tuned to suit some data warehouses like AWS Redshift which could significantly improve analytics engineering at mPharma. Unfortunately, <strong>the hard way</strong> makes it difficult to use some of these libraries and packages.</li><li>The final point is that when we are done creating scripts and tables, we have to manually create documentation for each of our tables or views. Would it not be nice to have this generated automatically and also embedded in the workflow?</li></ul><h3>The Easy Way</h3><blockquote>The easy way is to manage the analytics engineering workflow with a tool like <a href="https://www.getdbt.com/">dbt</a>.</blockquote><p>Why do I call it the easy way? Well, it caters to all the problems mentioned in <strong>the hard way</strong>. For instance, using <a href="https://www.getdbt.com/pricing/">dbt cloud</a> gives you a beautiful UI to manage your SQL scripts with version control integrated. You can create jobs to automate your scripts and automate your tests using the same tool without having to decouple your workflow. You can also install packages in <a href="https://www.getdbt.com/">dbt</a> and fortunately for us, there is a dbt package for <a href="https://greatexpectations.io/">Great expectations</a> and a package for Amazon Redshift. Since we use Redshift as our data warehouse it is a huge plus. You can also create dynamic and custom queries which makes it easy to apply the DRY principle and also use macros. dbt gives you some simple test cases to apply to your data and allows you to create custom test cases as well. When you create your materialized views or tables using dbt, you can generate documentation and get a graph-like view of how your tables are connected which is good for auditing and debugging purposes.</p><p>The beautiful thing about dbt is that your analytics engineering workflow happens in the same place with the same tool.</p><h3>Conclusion</h3><p>I hope I have given enough reasons why we want to improve our analytics engineering workflow and why we chose dbt to help us do this? Ultimately, I believe you cannot build an efficient and effective data team without considering the role analytics engineering plays. At mPharma, we always strive to be the best at what we do. We are a data-driven organization leveraging the right tool for the right job to build a modern data team. And we chose dbt.</p><p>Kindly look forward to the next part of this series as we talk about our learnings with the use of dbt.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b84b050f8a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mpharma-tech-blog/improving-analytics-engineering-at-mpharma-part-1-4b84b050f8a0">Improving Analytics Engineering at mPharma — PART 1</a> was originally published in <a href="https://medium.com/mpharma-tech-blog">mPharma Product &amp; Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the Blockchain with Python]]></title>
            <link>https://medium.com/@samuelbaafi/understanding-the-blockchain-with-python-3ea67e71d23?source=rss-162d1d4b6ec1------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ea67e71d23</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Samuel Baafi Boakye]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 09:45:18 GMT</pubDate>
            <atom:updated>2018-03-21T09:45:18.670Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Simple Implementation of the Blockchain with the Python Programming Language</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ookr0Cj2DIl2adHmEsF-rA.jpeg" /><figcaption>Brick &amp; Mortar via <a href="https://www.georgelines.co.uk/building-materials-2/choose-right-brick-for-your-build/">George Lines</a></figcaption></figure><p>If you are a layman like me, by the end of this piece you would come to know how very simple the concept of the blockchain is and how you can implement it with a beginner’s understanding of the Python programming language. What then is a blockchain? A chain of blocks. Do not think too much into it. I bet you have witnessed the construction of a building before. Lets say you have three blocks stacked on top of each other. You know the other thing to do to prevent them from detaching is to add your mortar between each connection and there you go. That right there is a chain of blocks. The logic behind this very traditional method of constructing a building is to prevent the blocks from detaching and falling apart. Hence, making the structure of a building strong.</p><p>From the perspective of the digital world, each building block refers to a list of transactions. So one block could be made up of so many transactions. Each block also has a timestamp as to when it was created, an id which is commonly known as the hash and a previous id also a prev hash which refers to the hash of the previous block in the chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*Ou3Li8yClnu0h4yEt575HQ.png" /><figcaption>A blockchain. Fig 1.</figcaption></figure><p>In the real world mortar is used to connect the blocks to each other. How then can we connect our blocks in the digital world. This is where the hash property of the blocks become very important. So with the same analogy, if we have five blocks, block number 2 will have its id (i.e hash) and also have a previous id (prev hash) which is the hash of block number 1. This connects block number 2 to block number 1. Block number 3 also has its hash property and the prev hash property which is the same as the hash property of block number 2. Now that’s a block chain in the digital world. Lets code.</p><p>We would need just three python classes for our coding exercise. These are Transaction, Block and Blockchain objects or classes for a more accurate term. Let’s begin by importing the required libraries and creating an object to handle transactions of our blocks.</p><pre>import hashlib<br>import json<br>from datetime import datetime</pre><pre>class Transaction:<br>    <br>    def __init__(self, sender, receiver, amount):<br>        self.sender = sender<br>        self.receiver = receiver<br>        self.amount = amount<br>    <br>    def __str__(self):<br>        return f&#39;A transaction of an amount of {self.amount} from    {self.sender} to {self.receiver}&#39;</pre><p>Those import at the beginning are not needed yet but it’s always good practice to have your imports ready before you begin to code.</p><p>The Transaction class has a sender, a receiver and an amount property. So something like Samuel (sender) sends an amount of 20,000(amount) Ghana cedis to Phyllis(receiver) is recorded as one transaction. I would later explain why I added the __str__() method (also referred to as dunder method, i.e double underscore method) to the Transaction class. Now we can create our Block object.</p><pre>class Block:<br>    <br>    def __init__(self, transactions, prev_hash = &#39;&#39;):<br>        self.transactions = transactions<br>        self.prev_hash = prev_hash<br>        self.timestamp = self.calculate_datetime()<br>        self.hash = self.calculate_hash()</pre><p>Earlier on, I mentioned that a block in the digital world has a set of properties.Block object has a transactions property that contains a list of all the transactions associated with it. The prev_hash property refers to the hash of the previous block. It can be seen that the timestamp and hash properties have methods assigned to them. Let’s go ahead and create those methods.</p><pre>def calculate_datetime(self):<br>    return datetime.now().strftime(&quot;%A, %d. %B %Y %I:%M%p&quot;)</pre><p>Here we apply our datetime import to give us the current time in which an instance of the Block class was created. This will return a datetime output in the form Monday, 12. March 2018 10:47PM .</p><pre>def calculate_hash(self):<br>    data = &#39;,&#39;.join(str(trans) for trans in self.transactions)<br>    h = hashlib.sha256()<br>    h.update(b&quot;%s %s %s %d&quot; % (<br>                               data.encode(),       <br>                               self.prev_hash.encode(),<br>                               self.timestamp.encode(),<br>                               )<br>    return h.hexdigest()</pre><p>The hash code is calculated with the hashlib library provided by python. The Hash computed is calculated using the transactions, prevHash and timestamp properties of the Block and sha256 algorithm is chosen to generate the hash code. Note that the hashlib library only accepts byte strings so the properties had to be encoded. The transactions property is a list of Transaction objects so I had to loop through and join each Transaction into a string so it can be easily encoded and accepted by hashlib library and that is the reason why I added the __str__() method to the Transaction class.</p><p>So for now our Block class should look like this:</p><pre>class Block:<br>    <br>    def __init__(self, transactions, prevHash = &#39;&#39;):<br>        self.transactions = transactions<br>        self.pre_hash = prev_hash<br>        self.nonce = 0<br>        self.timestamp = self.calculate_datetime()<br>        self.hash = self.calculate_hash()<br>    <br>    def calculate_datetime(self):<br>        return datetime.now().strftime(&quot;%A, %d. %B %Y %I:%M%p&quot;)</pre><pre>    def calculate_hash(self):<br>        data = &#39;,&#39;.join(str(trans) for trans in self.transactions)<br>        h = hashlib.sha256()<br>        h.update(b&quot;%s %s %s %d&quot; % (<br>                               data.encode(),       <br>                               self.prevHash.encode(),<br>                               self.timestamp.encode(),<br>                               )<br>        return h.hexdigest()</pre><p>Next will be to create a BlockChain class.</p><pre>class BlockChain:<br>    <br>    def __init__(self):<br>        self.chain = [self.createGenesisBlock()]</pre><p>The BlockChain class for now has only one property called chain which holds a list of Block objects. It can seen that the chain list is not empty. It contains a block by default which is commonly known as the genesis block . That acts as the first block in the chain. This ensures that the second block in the chain will have a previous hash equal to the first block so there will be a link.</p><p>Lets go ahead and create the method to add a genesis block to our chain anytime a blockchain is created.</p><pre># note the information provided in the transaction object is not<br># that relevant but for teaching purposes and to give our genesis<br># block some data</pre><pre>def create_genesis_block(self):<br>    return Block(transactions=[Transaction(&quot;Phyllis&quot;, &quot;Boatemaah&quot;, 10)], prevHash=&quot;0&quot;)</pre><p>create_genesis_block method simply creates a new Block object called the Genesis Block. Since this will be the first block in a chain, it has a prevHash property of ‘0’. We know that one block can’t form a chain. So let’s create a method that adds more blocks to create a blockchain.</p><pre>def add_block(self, new_block):<br>    new_block.prev_hash = self.get_latest_block().hash<br>    self.chain.append(new_block)</pre><p>This methods takes in one parameter called new_block which requires a Block object as it’s argument. It then assigns the previous block’s (the latest block in the chain) hash code to the prev_hash property of the new_block object in order to link the new_block object to the last block on the chain (NOTE: in fig 1. above, the last block or latest block will be block 5). After that it will append/add new_block to the chain list.</p><p>So in order to get the hash of the last/latest block we would create a new method to help us.</p><pre>def get_latest_block(self):<br>    return self.chain[len(self.chain) - 1]</pre><p>This simply finds the last item, in this case the latest block in the chain list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*t8cXKo88NKxJ5wkxAijdnA.png" /><figcaption>A broken chain. Fig 2.</figcaption></figure><p>Lastly, we could add a method that will check the validity of our blockchain. This is what I mean by validity. Let’s say you have a chain of five blocks (fig 2.). First block is linked to the second, second is linked to the third, third is linked to the fourth and so on. This is a valid blockchain. Lets see another scenario. The same chain of five blocks, this time the first is linked to the second, second to third but the third is not linked to the fourth. This means the hash of the third block is not the same as the prev_hash of the fourth. The chain is broken. So we need a method to check and make sure that the second scenario’s occurrence can be detected.</p><pre>def is_chain_valid(self):<br>    for i in range(1, len(self.chain)):<br>        current_block = self.chain[i]<br>        prev_block = self.chain[i - 1]<br>            <br>        if current_block.hash != current_block.calculate_hash():<br>            return False<br>            <br>        if current_block.prev_hash != prev_block.hash:<br>            return False<br>        <br>    return True</pre><p>This method selects a block starting from the second block, since the first will always be okay by default, and checks it against the previous block (i.e. the first block) to see if the hash of the first is the same as the prev_hash of the second block. It does the same for the rest of the blocks in the chain. So it will select the third and check it against the second, selects the fourth and checks it against the third and so on. That’s it we have our very simple blockchain. Now your lines of code should look like this:</p><pre>import hashlib<br>import json<br>from datetime import datetime</pre><pre>class Transaction:<br>    <br>    def __init__(self, sender, receiver, amount):<br>        self.sender = sender<br>        self.receiver = receiver<br>        self.amount = amount<br>    <br>    def __str__(self):<br>        return f&#39;A transaction of an amount of {self.amount} from    {self.sender} to {self.receiver}&#39;<br></pre><pre>class Block:<br>    <br>    def __init__(self, transactions, prevHash = &#39;&#39;):<br>        self.transactions = transactions<br>        self.prev_hash = prev_hash<br>        self.nonce = 0<br>        self.timestamp = self.calculate_datetime()<br>        self.hash = self.calculate_hash()<br>    <br>    def calculate_datetime(self):<br>        return datetime.now().strftime(&quot;%A, %d. %B %Y %I:%M%p&quot;)</pre><pre>    def calculate_hash(self):<br>        data = &#39;,&#39;.join(str(trans) for trans in self.transactions)<br>        h = hashlib.sha256()<br>        h.update(b&quot;%s %s %s %d&quot; % (<br>                               data.encode(),       <br>                               self.prev_hash.encode(),<br>                               self.timestamp.encode(),<br>                               )<br>        return h.hexdigest()</pre><pre><br>class BlockChain:<br>    <br>    def __init__(self):<br>        self.chain = [self.create_genesis_block()]</pre><pre>    def createGenesisBlock(self):<br>        return Block(transactions=[Transaction(&quot;Phyllis&quot;, &quot;Boatemaah&quot;, 10)], prevHash=&quot;0&quot;)</pre><pre>    def get_latest_block(self):<br>        return self.chain[len(self.chain) - 1]</pre><pre>    def add_block(self, new_block):<br>        new_block.prev_hash = self.get_latest_block().hash<br>        self.chain.append(new_block)</pre><pre>    def is_chain_valid(self):<br>        for i in range(1, len(self.chain)):<br>            current_block = self.chain[i]<br>            prev_block = self.chain[i - 1]<br>            <br>            if current_block.hash != current_block.calculate_hash():<br>                return False<br>            <br>            if current_block.prev_hash != prev_block.hash:<br>                return False<br>        <br>        return True</pre><p>This exercise was inspired by <a href="https://www.youtube.com/channel/UCnxrdFPXJMeHru_b4Q_vTPQ">Simply Explained</a>. You can check their implementation of the blockchain in Javascript <a href="https://www.youtube.com/watch?v=zVqczFZr124">here</a>.</p><p>This is not the full implementation of the blockchain. There are other areas I did not cover like the Proof of work which deals with the security issues of a simple blockchain like what we just did calling into play terms like mining, miners, what miners do and how these miners are rewarded. You can check out my Githuh <a href="https://github.com/Sambeth/Understanding-The-BlockChain-with-Python">repo</a> for full implementation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ea67e71d23" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>